Orgfield.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. MWF.xDesktop.requireApp("process.Xform", "Personfield", null, false);
  2. MWF.require("MWF.widget.O2Identity", null,false);
  3. MWF.xDesktop.requireApp("Selector", "package", null, false);
  4. MWF.xApplication.process.Xform.Orgfield = MWF.APPOrgfield = new Class({
  5. Extends: MWF.APPPersonfield,
  6. iconStyle: "orgfieldIcon",
  7. loadDescription: function(){
  8. if (this.readonly || this.json.isReadonly)return;
  9. var v = this._getBusinessData();
  10. if (!v || !v.length){
  11. if (this.json.description){
  12. var size = this.node.getFirst().getSize();
  13. var w = size.x-3;
  14. if (COMMON.Browser.safari) w = w-20;
  15. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  16. this.descriptionNode.setStyles({
  17. "width": ""+w+"px",
  18. "height": ""+size.y+"px",
  19. "line-height": ""+size.y+"px"
  20. });
  21. this.setDescriptionEvent();
  22. }
  23. }
  24. },
  25. setDescriptionEvent: function(){
  26. if (this.descriptionNode){
  27. this.descriptionNode.addEvents({
  28. "mousedown": function( ev ){
  29. this.descriptionNode.setStyle("display", "none");
  30. this.clickSelect( ev );
  31. }.bind(this)
  32. });
  33. }
  34. },
  35. _loadUserInterface: function(){
  36. this.field = true;
  37. this._loadNode();
  38. if (this.json.compute == "show"){
  39. this._setValue(this._computeValue());
  40. }else{
  41. this._loadValue();
  42. }
  43. },
  44. _loadNode: function(){
  45. if (this.readonly || this.json.isReadonly){
  46. this._loadNodeRead();
  47. }else{
  48. this._getOrgOptions();
  49. if (this.json.isInput){
  50. this._loadNodeInputEdit();
  51. }else{
  52. this._loadNodeEdit();
  53. }
  54. }
  55. },
  56. _getOrgOptions: function(){
  57. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  58. if( selectType.contains("unit") || selectType.contains("identity")){
  59. this.selectUnits = this.getSelectRange();
  60. if (this.json.range!=="all"){
  61. if (!this.selectUnits.length){
  62. this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
  63. return false;
  64. }
  65. }
  66. }else{
  67. this.selectUnits = [];
  68. }
  69. },
  70. _loadNodeRead: function(){
  71. this.node.empty();
  72. this.node.setStyle("overflow" , "hidden");
  73. var node = new Element("div").inject(this.node);
  74. },
  75. _searchConfirmPerson: function(item){
  76. var inforNode = item.inforNode || new Element("div");
  77. if (item.data){
  78. var text = "";
  79. var flag = item.data.distinguishedName.substr(item.data.distinguishedName.length-2, 2);
  80. switch (flag.toLowerCase()){
  81. case "@i":
  82. text = item.data.name+"("+item.data.unitName+")";
  83. break;
  84. case "@p":
  85. text = item.data.name+ (item.data.employee ? "("+item.data.employee+")" : "");
  86. break;
  87. case "@u":
  88. text = item.data.levelName;
  89. break;
  90. case "@g":
  91. text = item.data.name;
  92. break;
  93. default:
  94. text = item.data.name;
  95. }
  96. inforNode.set({
  97. "styles": {"font-size": "14px", "color": ""},
  98. "text": text
  99. });
  100. }else{
  101. inforNode.set({
  102. "styles": {"font-size": "14px", "color": "#bd0000"},
  103. "text": MWF.xApplication.process.Xform.LP.noOrgObject
  104. });
  105. }
  106. if (!item.inforNode){
  107. new mBox.Tooltip({
  108. content: inforNode,
  109. setStyles: {content: {padding: 15, lineHeight: 20}},
  110. attach: item.node,
  111. transition: 'flyin'
  112. });
  113. item.inforNode = inforNode;
  114. }
  115. },
  116. _searchOptions: function(value, callback){
  117. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  118. var options = {
  119. "type" : "",
  120. "types": selectType,
  121. "units": this.selectUnits, //范围
  122. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
  123. };
  124. if (!this.comboxFilter) this.comboxFilter = new MWF.O2SelectorFilter(value, options);
  125. this.comboxFilter.filter(value, function(data){
  126. data.map(function(d){
  127. var value = Object.clone(d);
  128. d.value = value;
  129. var flag = d.distinguishedName.substr(d.distinguishedName.length-2, 2);
  130. switch (flag.toLowerCase()){
  131. case "@i":
  132. d.text = d.name+"("+d.unitName+")";
  133. break;
  134. case "@p":
  135. d.text = d.name+(d.employee ? "("+d.employee+")" : "");
  136. break;
  137. case "@u":
  138. d.text = d.name;
  139. break;
  140. case "@g":
  141. d.text = d.name;
  142. break;
  143. default:
  144. d.text = d.name;
  145. }
  146. });
  147. if (callback) callback(data);
  148. });
  149. },
  150. _loadNodeInputEdit: function(){
  151. var input=null;
  152. MWF.require("MWF.widget.Combox", function(){
  153. this.combox = input = new MWF.widget.Combox({
  154. "count": this.json.count || 0,
  155. "splitShow": this.json.splitShow || ", ",
  156. "onCommitInput": function(item){
  157. this._searchConfirmPerson(item);
  158. //this.fireEvent("change");
  159. }.bind(this),
  160. "onChange": function(){
  161. this._setBusinessData(this.getInputData());
  162. this.fireEvent("change");
  163. }.bind(this),
  164. "optionsMethod": this._searchOptions.bind(this)
  165. });
  166. }.bind(this), false);
  167. input.setStyles({
  168. "background": "transparent",
  169. "border": "0px"
  170. });
  171. input.set(this.json.properties);
  172. var node = new Element("div", {"styles": {
  173. "overflow": "hidden",
  174. //"position": "relative",
  175. "margin-right": "20px"
  176. }}).inject(this.node, "after");
  177. input.inject(node);
  178. //this.combox = input;
  179. this.node.destroy();
  180. this.node = node;
  181. this.node.set({
  182. "id": this.json.id,
  183. "MWFType": this.json.type
  184. });
  185. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  186. this.iconNode = new Element("div", {
  187. "styles": this.form.css[this.iconStyle],
  188. "events": {
  189. "click": function (ev) {
  190. this.clickSelect( ev );
  191. }.bind(this)
  192. //this.clickSelect.bind(this)
  193. }
  194. }).inject(this.node, "before");
  195. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  196. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  197. }
  198. this.combox.addEvent("change", function(){
  199. this.validationMode();
  200. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  201. }.bind(this));
  202. },
  203. _loadNodeEdit : function(){
  204. var input = this.input = new Element("div", {
  205. "styles": {
  206. "background": "transparent",
  207. "border": "0px",
  208. "min-height": "20px"
  209. }
  210. });
  211. input.set(this.json.properties);
  212. var node = new Element("div", {"styles": {
  213. "overflow": "hidden",
  214. "position": "relative",
  215. "min-height" : "20px",
  216. "margin-right": "20px"
  217. }}).inject(this.node, "after");
  218. input.inject(node);
  219. this.node.destroy();
  220. this.node = node;
  221. this.node.set({
  222. "id": this.json.id,
  223. "MWFType": this.json.type,
  224. "readonly": true
  225. });
  226. if( !this.readonly ) {
  227. this.node.setStyle("cursor" , "pointer");
  228. this.node.addEvents({
  229. "click": function (ev) {
  230. this.clickSelect( ev );
  231. }.bind(this)
  232. //this.clickSelect.bind(this)
  233. });
  234. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  235. this.iconNode = new Element("div", { //this.form.css[this.iconStyle],
  236. "styles": {
  237. "background": "url(" + "/x_component_process_Xform/$Form/default/icon/selectorg.png) center center no-repeat",
  238. "width": "18px",
  239. "height": "18px",
  240. "float": "right"
  241. }
  242. }).inject(this.node, "before");
  243. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  244. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  245. }
  246. if (this.iconNode){
  247. this.iconNode.setStyle("cursor" , "pointer");
  248. this.iconNode.addEvents({
  249. "click": function (ev) {
  250. this.clickSelect( ev );
  251. }.bind(this)
  252. //this.clickSelect.bind(this)
  253. });
  254. }
  255. }
  256. },
  257. getValue: function(){
  258. var value = this._getBusinessData();
  259. if( typeOf( value ) === "array" ){
  260. if( value.length === 0 )value = this._computeValue();
  261. }else if (!value){
  262. value = this._computeValue();
  263. }
  264. return value || "";
  265. },
  266. getData: function(when){
  267. if (this.json.compute == "save") this._setValue(this._computeValue());
  268. return this._getBusinessData();
  269. },
  270. getInputData: function(){
  271. if (this.json.isInput){
  272. if (this.combox) return this.combox.getData();
  273. return this._getBusinessData();
  274. }else{
  275. return this._getBusinessData();
  276. }
  277. },
  278. addData: function(value){
  279. if (!value) return false;
  280. value.each(function(v){
  281. var vtype = typeOf(v);
  282. if (vtype==="string"){
  283. var data;
  284. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), null, v, false);
  285. if (data) this.combox.addNewValue(this.getDataText(data), data);
  286. }
  287. if (vtype==="object"){
  288. this.combox.addNewValue(this.getDataText(v), v);
  289. }
  290. }.bind(this));
  291. },
  292. setData: function(data) {
  293. this._setValue(data);
  294. },
  295. _computeValue: function(){
  296. var values = [];
  297. if (this.json.identityValue) {
  298. this.json.identityValue.each(function(v){ if (v) values.push(v)});
  299. }
  300. if (this.json.unitValue) {
  301. this.json.unitValue.each(function(v){ if (v) values.push(v)});
  302. }
  303. if (this.json.defaultValue && this.json.defaultValue.code){
  304. var fd = this.form.Macro.exec(this.json.defaultValue.code, this);
  305. if (typeOf(fd)!=="array") fd = (fd) ? [fd] : [];
  306. fd.each(function(fdd){
  307. if (fdd){
  308. if (typeOf(fdd)==="string"){
  309. var data;
  310. this.getOrgAction()[this.getValueMethod(fdd)](function(json){ data = json.data }.bind(this), null, fdd, false);
  311. values.push(data);
  312. }else{
  313. values.push(fdd);
  314. }
  315. }
  316. }.bind(this));
  317. }
  318. if (this.json.count>0){
  319. return values.slice(0, this.json.count);
  320. }
  321. return values;
  322. },
  323. _getBusinessData: function(){
  324. if (this.json.section=="yes"){
  325. var data = this._getBusinessSectionData();
  326. }else {
  327. var data = this.form.businessData.data[this.json.id] || "";
  328. }
  329. if (typeOf( data ) != "array"){
  330. if (data) return [data];
  331. return [];
  332. }else{
  333. return data;
  334. }
  335. },
  336. creteShowNode: function(data, islast){
  337. var nodeText = (data.text) ? data.text : data;
  338. if (!islast) nodeText = nodeText + (this.json.splitStr || ", ");
  339. var node = new Element("div", {
  340. "styles": {
  341. "float": "left",
  342. "margin-right": "5px"
  343. },
  344. "text": nodeText
  345. });
  346. var text = "";
  347. if (data.value){
  348. var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-2, 2);
  349. switch (flag.toLowerCase()){
  350. case "@i":
  351. text = data.value.name+"("+data.value.unitName+")";
  352. break;
  353. case "@p":
  354. text = data.value.name+ (data.value.employee ? "("+data.value.employee+")" : "");
  355. break;
  356. case "@u":
  357. text = data.value.levelName;
  358. break;
  359. case "@g":
  360. text = data.value.name;
  361. break;
  362. default:
  363. text = data.value.name;
  364. }
  365. var inforNode = new Element("div").set({
  366. "styles": {"font-size": "14px", "color": ""},
  367. "text": text
  368. });
  369. new mBox.Tooltip({
  370. content: inforNode,
  371. setStyles: {content: {padding: 15, lineHeight: 20}},
  372. attach: node,
  373. transition: 'flyin'
  374. });
  375. }
  376. return node;
  377. },
  378. _setValue: function(value){
  379. if (value.length==1 && !(value[0])) value=[];
  380. var values = [];
  381. var comboxValues = [];
  382. var type = typeOf(value);
  383. if (type==="array"){
  384. value.each(function(v){
  385. var data=null;
  386. var vtype = typeOf(v);
  387. if (vtype==="string"){
  388. var error = (this.json.isInput) ? function(){ comboxValues.push(v); } : null;
  389. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), error, v, false);
  390. }
  391. if (vtype==="object") data = v;
  392. if (data){
  393. values.push(data);
  394. comboxValues.push({"text": this.getDataText(data),"value": data});
  395. }
  396. }.bind(this));
  397. }
  398. if (type==="string"){
  399. var vData;
  400. var error = (this.json.isInput) ? function(){ comboxValues.push(value); } : null;
  401. this.getOrgAction()[this.getValueMethod(value)](function(json){ vData = json.data }.bind(this), error, value, false);
  402. if (vData){
  403. values.push(vData);
  404. comboxValues.push({"text": this.getDataText(vData),"value": vData});
  405. }
  406. }
  407. if (type==="object"){
  408. values.push(value);
  409. comboxValues.push({"text": this.getDataText(value),"value": value});
  410. }
  411. this._setBusinessData(value);
  412. if (this.json.isInput){
  413. if (this.combox){
  414. this.combox.clear();
  415. this.combox.addNewValues(comboxValues);
  416. }else{
  417. var node = this.node.getFirst();
  418. if (node){
  419. comboxValues.each(function(v, i){
  420. this.creteShowNode(v, (i===comboxValues.length-1)).inject(node);
  421. }.bind(this));
  422. }
  423. }
  424. }else {
  425. var input = this.node.getFirst();
  426. if (!input) {
  427. input = this.node;
  428. }
  429. input.empty();
  430. this.loadOrgWidget(values, input);
  431. }
  432. },
  433. loadOrgWidget: function(value, node){
  434. var options = {"style": "xform", "canRemove":false , "onRemove" : this.removeItem};
  435. value.each(function(data){
  436. if( data.distinguishedName ){
  437. var flag = data.distinguishedName.substr(data.distinguishedName.length-2, 2);
  438. switch (flag.toLowerCase()){
  439. case "@i":
  440. var widget = new MWF.widget.O2Identity(data, node, options );
  441. break;
  442. case "@p":
  443. var widget = new MWF.widget.O2Person(data, node, options);
  444. break;
  445. case "@u":
  446. var widget = new MWF.widget.O2Unit(data, node, options);
  447. break;
  448. case "@g":
  449. var widget = new MWF.widget.O2Group(data, node, options);
  450. break;
  451. default:
  452. var widget = new MWF.widget.O2Other(data, node, options);
  453. }
  454. widget.field = this;
  455. if( layout.mobile ){
  456. widget.node.setStyles({
  457. "float" : "none"
  458. })
  459. }
  460. }
  461. }.bind(this));
  462. },
  463. removeItem : function( widget, ev ){
  464. //this 是 MWF.widget.O2Identity 之类的对象
  465. var _self = this.field; //这个才是Readerfield
  466. var dn = this.data.distinguishedName;
  467. var data = _self._getBusinessData();
  468. var index;
  469. data.each( function ( d , i){
  470. if( d.distinguishedName == dn ){
  471. index = i
  472. }
  473. });
  474. data.splice( index, 1 );
  475. _self._setBusinessData( data );
  476. this.node.destroy();
  477. ev.stopPropagation();
  478. },
  479. _loadValue: function(){
  480. this._setValue(this.getValue());
  481. },
  482. clickSelect: function( ev ){
  483. debugger;
  484. this.validationMode();
  485. var count = (this.json.count) ? this.json.count : 0;
  486. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  487. // if( selectType.contains("unit") || selectType.contains("identity")){
  488. // var selectUnits = this.getSelectRange();
  489. // if (this.json.range!=="all"){
  490. // if (!selectUnits.length){
  491. // this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
  492. // return false;
  493. // }
  494. // }
  495. // }else{
  496. // var selectUnits = [];
  497. // }
  498. var selectUnits = this.selectUnits;
  499. if( !selectType[0] ){
  500. this.form.notice(MWF.xApplication.process.Xform.LP.noSelectType, "error", this.node);
  501. return false;
  502. }
  503. var exclude = [];
  504. if( this.json.exclude ){
  505. var v = this.form.Macro.exec(this.json.exclude.code, this);
  506. exclude = typeOf(v)==="array" ? v : [v];
  507. }
  508. var options = {
  509. "type" : "",
  510. "types": selectType,
  511. "values" : (this.json.isInput) ? [] : this._getBusinessData(),
  512. "count": count,
  513. "units": selectUnits, //范围
  514. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
  515. "exclude" : exclude,
  516. "expandSubEnable" : (this.json.expandSubEnable=="no") ? false : true,
  517. //"expand" : false,
  518. "onComplete": function(items, itemsObject){
  519. var values = [];
  520. items.each( function(it){
  521. values.push(MWF.org.parseOrgData(it.data));
  522. });
  523. if (this.json.isInput){
  524. this.addData(values);
  525. }else{
  526. this.setData(values);
  527. }
  528. //this.setData( values );
  529. this.validation();
  530. this.fireEvent("select");
  531. }.bind(this),
  532. "onCancel": function(){
  533. this.validation();
  534. }.bind(this),
  535. "onLoad": function(){
  536. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  537. }.bind(this),
  538. "onClose": function(){
  539. //var v = this.node.getFirst().get("value");
  540. var v = this.getInputData();
  541. if (!v || !v.length) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  542. }.bind(this)
  543. };
  544. if( this.form.json.selectorStyle )options = Object.merge( options, this.form.json.selectorStyle );
  545. if( this.selector && this.selector.loading ) {
  546. }else if( this.selector && this.selector.selector && this.selector.selector.active ){
  547. }else{
  548. this.selector = new MWF.O2Selector(this.form.app.content, options);
  549. }
  550. },
  551. _loadStyles: function(){
  552. if (this.readonly || this.json.isReadonly){
  553. if (this.json.styles) this.node.setStyles(this.json.styles);
  554. }else{
  555. if (this.json.styles) this.node.setStyles(this.json.styles);
  556. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  557. if (this.iconNode){
  558. var size = this.node.getSize();
  559. this.iconNode.setStyle("height", ""+size.y+"px");
  560. }
  561. }
  562. }
  563. });