Orgfield.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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(){
  29. this.descriptionNode.setStyle("display", "none");
  30. this.clickSelect();
  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": this.clickSelect.bind(this)
  190. }
  191. }).inject(this.node, "before");
  192. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  193. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  194. }
  195. this.combox.addEvent("change", function(){
  196. this.validationMode();
  197. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  198. }.bind(this));
  199. },
  200. _loadNodeEdit : function(){
  201. var input = this.input = new Element("div", {
  202. "styles": {
  203. "background": "transparent",
  204. "border": "0px",
  205. "min-height": "20px"
  206. }
  207. });
  208. input.set(this.json.properties);
  209. var node = new Element("div", {"styles": {
  210. "overflow": "hidden",
  211. "position": "relative",
  212. "min-height" : "20px",
  213. "margin-right": "20px"
  214. }}).inject(this.node, "after");
  215. input.inject(node);
  216. this.node.destroy();
  217. this.node = node;
  218. this.node.set({
  219. "id": this.json.id,
  220. "MWFType": this.json.type,
  221. "readonly": true
  222. });
  223. if( !this.readonly ) {
  224. this.node.setStyle("cursor" , "pointer");
  225. this.node.addEvents({
  226. "click": this.clickSelect.bind(this)
  227. });
  228. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  229. this.iconNode = new Element("div", { //this.form.css[this.iconStyle],
  230. "styles": {
  231. "background": "url(" + "/x_component_process_Xform/$Form/default/icon/selectorg.png) center center no-repeat",
  232. "width": "18px",
  233. "height": "18px",
  234. "float": "right"
  235. }
  236. }).inject(this.node, "before");
  237. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  238. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  239. }
  240. if (this.iconNode){
  241. this.iconNode.setStyle("cursor" , "pointer");
  242. this.iconNode.addEvents({
  243. "click": this.clickSelect.bind(this)
  244. });
  245. }
  246. }
  247. },
  248. getValue: function(){
  249. var value = this._getBusinessData();
  250. if( typeOf( value ) === "array" ){
  251. if( value.length === 0 )value = this._computeValue();
  252. }else if (!value){
  253. value = this._computeValue();
  254. }
  255. return value || "";
  256. },
  257. getData: function(when){
  258. if (this.json.compute == "save") this._setValue(this._computeValue());
  259. return this._getBusinessData();
  260. },
  261. getInputData: function(){
  262. if (this.json.isInput){
  263. if (this.combox) return this.combox.getData();
  264. return this._getBusinessData();
  265. }else{
  266. return this._getBusinessData();
  267. }
  268. },
  269. addData: function(value){
  270. if (!value) return false;
  271. value.each(function(v){
  272. var vtype = typeOf(v);
  273. if (vtype==="string"){
  274. var data;
  275. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), null, v, false);
  276. if (data) this.combox.addNewValue(this.getDataText(data), data);
  277. }
  278. if (vtype==="object"){
  279. this.combox.addNewValue(this.getDataText(v), v);
  280. }
  281. }.bind(this));
  282. },
  283. setData: function(data) {
  284. this._setValue(data);
  285. },
  286. _computeValue: function(){
  287. var values = [];
  288. if (this.json.identityValue) {
  289. this.json.identityValue.each(function(v){ if (v) values.push(v)});
  290. }
  291. if (this.json.unitValue) {
  292. this.json.unitValue.each(function(v){ if (v) values.push(v)});
  293. }
  294. if (this.json.defaultValue && this.json.defaultValue.code){
  295. var fd = this.form.Macro.exec(this.json.defaultValue.code, this);
  296. if (typeOf(fd)!=="array") fd = (fd) ? [fd] : [];
  297. fd.each(function(fdd){
  298. if (fdd){
  299. if (typeOf(fdd)==="string"){
  300. var data;
  301. this.getOrgAction()[this.getValueMethod(fdd)](function(json){ data = json.data }.bind(this), null, fdd, false);
  302. values.push(data);
  303. }else{
  304. values.push(fdd);
  305. }
  306. }
  307. }.bind(this));
  308. }
  309. if (this.json.count>0){
  310. return values.slice(0, this.json.count);
  311. }
  312. return values;
  313. },
  314. _getBusinessData: function(){
  315. if (this.json.section=="yes"){
  316. var data = this._getBusinessSectionData();
  317. }else {
  318. var data = this.form.businessData.data[this.json.id] || "";
  319. }
  320. if (typeOf( data ) != "array"){
  321. if (data) return [data];
  322. return [];
  323. }else{
  324. return data;
  325. }
  326. },
  327. creteShowNode: function(data, islast){
  328. var nodeText = (data.text) ? data.text : data;
  329. if (!islast) nodeText = nodeText + (this.json.splitStr || ", ");
  330. var node = new Element("div", {
  331. "styles": {
  332. "float": "left",
  333. "margin-right": "5px"
  334. },
  335. "text": nodeText
  336. });
  337. var text = "";
  338. if (data.value){
  339. var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-2, 2);
  340. switch (flag.toLowerCase()){
  341. case "@i":
  342. text = data.value.name+"("+data.value.unitName+")";
  343. break;
  344. case "@p":
  345. text = data.value.name+ (data.value.employee ? "("+data.value.employee+")" : "");
  346. break;
  347. case "@u":
  348. text = data.value.levelName;
  349. break;
  350. case "@g":
  351. text = data.value.name;
  352. break;
  353. default:
  354. text = data.value.name;
  355. }
  356. var inforNode = new Element("div").set({
  357. "styles": {"font-size": "14px", "color": ""},
  358. "text": text
  359. });
  360. new mBox.Tooltip({
  361. content: inforNode,
  362. setStyles: {content: {padding: 15, lineHeight: 20}},
  363. attach: node,
  364. transition: 'flyin'
  365. });
  366. }
  367. return node;
  368. },
  369. _setValue: function(value){
  370. if (value.length==1 && !(value[0])) value=[];
  371. var values = [];
  372. var comboxValues = [];
  373. var type = typeOf(value);
  374. if (type==="array"){
  375. value.each(function(v){
  376. var data=null;
  377. var vtype = typeOf(v);
  378. if (vtype==="string"){
  379. var error = (this.json.isInput) ? function(){ comboxValues.push(v); } : null;
  380. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), error, v, false);
  381. }
  382. if (vtype==="object") data = v;
  383. if (data){
  384. values.push(data);
  385. comboxValues.push({"text": this.getDataText(data),"value": data});
  386. }
  387. }.bind(this));
  388. }
  389. if (type==="string"){
  390. var vData;
  391. var error = (this.json.isInput) ? function(){ comboxValues.push(value); } : null;
  392. this.getOrgAction()[this.getValueMethod(value)](function(json){ vData = json.data }.bind(this), error, value, false);
  393. if (vData){
  394. values.push(vData);
  395. comboxValues.push({"text": this.getDataText(vData),"value": vData});
  396. }
  397. }
  398. if (type==="object"){
  399. values.push(value);
  400. comboxValues.push({"text": this.getDataText(value),"value": value});
  401. }
  402. this._setBusinessData(value);
  403. if (this.json.isInput){
  404. if (this.combox){
  405. this.combox.clear();
  406. this.combox.addNewValues(comboxValues);
  407. }else{
  408. var node = this.node.getFirst();
  409. if (node){
  410. comboxValues.each(function(v, i){
  411. this.creteShowNode(v, (i===comboxValues.length-1)).inject(node);
  412. }.bind(this));
  413. }
  414. }
  415. }else {
  416. var input = this.node.getFirst();
  417. if (!input) {
  418. input = this.node;
  419. }
  420. input.empty();
  421. this.loadOrgWidget(values, input);
  422. }
  423. },
  424. loadOrgWidget: function(value, node){
  425. var options = {"style": "xform", "canRemove":false , "onRemove" : this.removeItem};
  426. value.each(function(data){
  427. if( data.distinguishedName ){
  428. var flag = data.distinguishedName.substr(data.distinguishedName.length-2, 2);
  429. switch (flag.toLowerCase()){
  430. case "@i":
  431. var widget = new MWF.widget.O2Identity(data, node, options );
  432. break;
  433. case "@p":
  434. var widget = new MWF.widget.O2Person(data, node, options);
  435. break;
  436. case "@u":
  437. var widget = new MWF.widget.O2Unit(data, node, options);
  438. break;
  439. case "@g":
  440. var widget = new MWF.widget.O2Group(data, node, options);
  441. break;
  442. default:
  443. var widget = new MWF.widget.O2Other(data, node, options);
  444. }
  445. widget.field = this;
  446. if( layout.mobile ){
  447. widget.node.setStyles({
  448. "float" : "none"
  449. })
  450. }
  451. }
  452. }.bind(this));
  453. },
  454. removeItem : function( widget, ev ){
  455. //this 是 MWF.widget.O2Identity 之类的对象
  456. var _self = this.field; //这个才是Readerfield
  457. var dn = this.data.distinguishedName;
  458. var data = _self._getBusinessData();
  459. var index;
  460. data.each( function ( d , i){
  461. if( d.distinguishedName == dn ){
  462. index = i
  463. }
  464. });
  465. data.splice( index, 1 );
  466. _self._setBusinessData( data );
  467. this.node.destroy();
  468. ev.stopPropagation();
  469. },
  470. _loadValue: function(){
  471. this._setValue(this.getValue());
  472. },
  473. clickSelect: function(){
  474. this.validationMode();
  475. var count = (this.json.count) ? this.json.count : 0;
  476. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  477. // if( selectType.contains("unit") || selectType.contains("identity")){
  478. // var selectUnits = this.getSelectRange();
  479. // if (this.json.range!=="all"){
  480. // if (!selectUnits.length){
  481. // this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
  482. // return false;
  483. // }
  484. // }
  485. // }else{
  486. // var selectUnits = [];
  487. // }
  488. var selectUnits = this.selectUnits;
  489. if( !selectType[0] ){
  490. this.form.notice(MWF.xApplication.process.Xform.LP.noSelectType, "error", this.node);
  491. return false;
  492. }
  493. var exclude = [];
  494. if( this.json.exclude ){
  495. var v = this.form.Macro.exec(this.json.exclude.code, this);
  496. exclude = typeOf(v)==="array" ? v : [v];
  497. }
  498. var options = {
  499. "type" : "",
  500. "types": selectType,
  501. "values" : (this.json.isInput) ? [] : this._getBusinessData(),
  502. "count": count,
  503. "units": selectUnits, //范围
  504. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
  505. "exclude" : exclude,
  506. "expandSubEnable" : (this.json.expandSubEnable=="no") ? false : true,
  507. //"expand" : false,
  508. "onComplete": function(items, itemsObject){
  509. var values = [];
  510. items.each( function(it){
  511. values.push(MWF.org.parseOrgData(it.data));
  512. });
  513. if (this.json.isInput){
  514. this.addData(values);
  515. }else{
  516. this.setData(values);
  517. }
  518. //this.setData( values );
  519. this.validation();
  520. this.fireEvent("select");
  521. }.bind(this),
  522. "onCancel": function(){
  523. this.validation();
  524. }.bind(this),
  525. "onLoad": function(){
  526. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  527. }.bind(this),
  528. "onClose": function(){
  529. //var v = this.node.getFirst().get("value");
  530. var v = this.getInputData();
  531. if (!v || !v.length) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  532. }.bind(this)
  533. };
  534. if( this.form.json.selectorStyle )options = Object.merge( options, this.form.json.selectorStyle );
  535. var selector = new MWF.O2Selector(this.form.app.content, options);
  536. },
  537. _loadStyles: function(){
  538. if (this.readonly || this.json.isReadonly){
  539. if (this.json.styles) this.node.setStyles(this.json.styles);
  540. }else{
  541. if (this.json.styles) this.node.setStyles(this.json.styles);
  542. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  543. if (this.iconNode){
  544. var size = this.node.getSize();
  545. this.iconNode.setStyle("height", ""+size.y+"px");
  546. }
  547. }
  548. }
  549. });