Orgfield.js 19 KB

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