Orgfield.js 20 KB

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