Orgfield.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 (!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+")";
  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+")";
  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. getData: function(when){
  240. if (this.json.compute == "save") this._setValue(this._computeValue());
  241. return this._getBusinessData();
  242. },
  243. getInputData: function(){
  244. if (this.json.isInput){
  245. if (this.combox) return this.combox.getData();
  246. return this._getBusinessData();
  247. }else{
  248. return this._getBusinessData();
  249. }
  250. },
  251. addData: function(value){
  252. if (!value) return false;
  253. value.each(function(v){
  254. var vtype = typeOf(v);
  255. if (vtype==="string"){
  256. var data;
  257. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), null, v, false);
  258. if (data) this.combox.addNewValue(this.getDataText(data), data);
  259. }
  260. if (vtype==="object"){
  261. this.combox.addNewValue(this.getDataText(v), v);
  262. }
  263. }.bind(this));
  264. },
  265. setData: function(data) {
  266. this._setValue(data);
  267. },
  268. _computeValue: function(){
  269. var values = [];
  270. if (this.json.identityValue) {
  271. this.json.identityValue.each(function(v){ if (v) values.push(v)});
  272. }
  273. if (this.json.unitValue) {
  274. this.json.unitValue.each(function(v){ if (v) values.push(v)});
  275. }
  276. if (this.json.defaultValue && this.json.defaultValue.code){
  277. var fd = this.form.Macro.exec(this.json.defaultValue.code, this);
  278. if (typeOf(fd)!=="array") fd = (fd) ? [fd.toString()] : [];
  279. fd.each(function(fdd){if (fdd) values.push(fdd);});
  280. }
  281. if (this.json.count>0){
  282. return values.slice(0, this.json.count);
  283. }
  284. return values;
  285. },
  286. _getBusinessData: function(){
  287. if (this.json.section=="yes"){
  288. var data = this._getBusinessSectionData();
  289. }else {
  290. var data = this.form.businessData.data[this.json.id] || "";
  291. }
  292. if (typeOf( data ) != "array"){
  293. if (data) return [data];
  294. return [];
  295. }else{
  296. return data;
  297. }
  298. },
  299. creteShowNode: function(data, islast){
  300. var nodeText = (data.text) ? data.text : data;
  301. if (!islast) nodeText = nodeText + (this.json.splitStr || ", ");
  302. var node = new Element("div", {
  303. "styles": {
  304. "float": "left",
  305. "margin-right": "5px"
  306. },
  307. "text": nodeText
  308. });
  309. var text = "";
  310. if (data.value){
  311. var flag = data.value.distinguishedName.substr(data.value.distinguishedName.length-1, 1);
  312. switch (flag.toLowerCase()){
  313. case "i":
  314. text = data.value.name+"("+data.value.unitName+")";
  315. break;
  316. case "p":
  317. text = data.value.name+"("+data.value.employee+")";
  318. break;
  319. case "u":
  320. text = data.value.levelName;
  321. break;
  322. case "g":
  323. text = data.value.name;
  324. break;
  325. default:
  326. text = data.value.name;
  327. }
  328. var inforNode = new Element("div").set({
  329. "styles": {"font-size": "14px", "color": ""},
  330. "text": text
  331. });
  332. new mBox.Tooltip({
  333. content: inforNode,
  334. setStyles: {content: {padding: 15, lineHeight: 20}},
  335. attach: node,
  336. transition: 'flyin'
  337. });
  338. }
  339. return node;
  340. },
  341. _setValue: function(value){
  342. if (value.length==1 && !(value[0])) value=[];
  343. var values = [];
  344. var comboxValues = [];
  345. var type = typeOf(value);
  346. if (type==="array"){
  347. value.each(function(v){
  348. var data=null;
  349. var vtype = typeOf(v);
  350. if (vtype==="string"){
  351. var error = (this.json.isInput) ? function(){ comboxValues.push(v); } : null;
  352. this.getOrgAction()[this.getValueMethod(v)](function(json){ data = json.data }.bind(this), error, v, false);
  353. }
  354. if (vtype==="object") data = v;
  355. if (data){
  356. values.push(data);
  357. comboxValues.push({"text": this.getDataText(data),"value": data});
  358. }
  359. }.bind(this));
  360. }
  361. if (type==="string"){
  362. var vData;
  363. var error = (this.json.isInput) ? function(){ comboxValues.push(value); } : null;
  364. this.getOrgAction()[this.getValueMethod(value)](function(json){ vData = json.data }.bind(this), error, value, false);
  365. if (vData){
  366. values.push(vData);
  367. comboxValues.push({"text": this.getDataText(vData),"value": vData});
  368. }
  369. }
  370. if (type==="object"){
  371. values.push(value);
  372. comboxValues.push({"text": this.getDataText(value),"value": value});
  373. }
  374. this._setBusinessData(value);
  375. if (this.json.isInput){
  376. if (this.combox){
  377. this.combox.addNewValues(comboxValues);
  378. }else{
  379. var node = this.node.getFirst();
  380. if (node){
  381. comboxValues.each(function(v, i){
  382. this.creteShowNode(v, (i===comboxValues.length-1)).inject(node);
  383. }.bind(this));
  384. }
  385. }
  386. }else {
  387. var input = this.node.getFirst();
  388. if (!input) {
  389. input = this.node;
  390. }
  391. input.empty();
  392. this.loadOrgWidget(values, input);
  393. }
  394. },
  395. loadOrgWidget: function(value, node){
  396. var options = {"style": "xform", "canRemove":false , "onRemove" : this.removeItem};
  397. value.each(function(data){
  398. if( data.distinguishedName ){
  399. var flag = data.distinguishedName.substr(data.distinguishedName.length-1, 1);
  400. switch (flag.toLowerCase()){
  401. case "i":
  402. var widget = new MWF.widget.O2Identity(data, node, options );
  403. break;
  404. case "p":
  405. var widget = new MWF.widget.O2Person(data, node, options);
  406. break;
  407. case "u":
  408. var widget = new MWF.widget.O2Unit(data, node, options);
  409. break;
  410. case "g":
  411. var widget = new MWF.widget.O2Group(data, node, options);
  412. break;
  413. default:
  414. var widget = new MWF.widget.O2Other(data, node, options);
  415. }
  416. widget.field = this;
  417. if( layout.mobile ){
  418. widget.node.setStyles({
  419. "float" : "none"
  420. })
  421. }
  422. }
  423. }.bind(this));
  424. },
  425. removeItem : function( widget, ev ){
  426. //this 是 MWF.widget.O2Identity 之类的对象
  427. var _self = this.field; //这个才是Readerfield
  428. var dn = this.data.distinguishedName;
  429. var data = _self._getBusinessData();
  430. var index;
  431. data.each( function ( d , i){
  432. if( d.distinguishedName == dn ){
  433. index = i
  434. }
  435. });
  436. data.splice( index, 1 );
  437. _self._setBusinessData( data );
  438. this.node.destroy();
  439. ev.stopPropagation();
  440. },
  441. _loadValue: function(){
  442. this._setValue(this.getValue());
  443. },
  444. clickSelect: function(){
  445. this.validationMode();
  446. var count = (this.json.count) ? this.json.count : 0;
  447. var selectType = typeOf( this.json.selectType ) == "array" ? this.json.selectType : [this.json.selectType];
  448. // if( selectType.contains("unit") || selectType.contains("identity")){
  449. // var selectUnits = this.getSelectRange();
  450. // if (this.json.range!=="all"){
  451. // if (!selectUnits.length){
  452. // this.form.notice(MWF.xApplication.process.Xform.LP.noSelectRange, "error", this.node);
  453. // return false;
  454. // }
  455. // }
  456. // }else{
  457. // var selectUnits = [];
  458. // }
  459. var selectUnits = this.selectUnits;
  460. if( !selectType[0] ){
  461. this.form.notice(MWF.xApplication.process.Xform.LP.noSelectType, "error", this.node);
  462. return false;
  463. }
  464. var options = {
  465. "type" : "",
  466. "types": selectType,
  467. "values" : (this.json.isInput) ? [] : this._getBusinessData(),
  468. "count": count,
  469. "units": selectUnits, //范围
  470. "unitType": (this.json.selectUnitType==="all") ? "" : this.json.selectUnitType,
  471. //"expand" : false,
  472. "onComplete": function(items, itemsObject){
  473. var values = [];
  474. items.each( function(it){
  475. values.push(MWF.org.parseOrgData(it.data));
  476. });
  477. if (this.json.isInput){
  478. this.addData(values);
  479. }else{
  480. this.setData(values);
  481. }
  482. //this.setData( values );
  483. this.validation()
  484. }.bind(this),
  485. "onCancel": function(){
  486. this.validation();
  487. }.bind(this),
  488. "onLoad": function(){
  489. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  490. }.bind(this),
  491. "onClose": function(){
  492. //var v = this.node.getFirst().get("value");
  493. var v = this.getInputData();
  494. if (!v || !v.length) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  495. }.bind(this)
  496. };
  497. var selector = new MWF.O2Selector(this.form.app.content, options);
  498. },
  499. _loadStyles: function(){
  500. if (this.readonly || this.json.isReadonly){
  501. if (this.json.styles) this.node.setStyles(this.json.styles);
  502. }else{
  503. if (this.json.styles) this.node.setStyles(this.json.styles);
  504. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  505. if (this.iconNode){
  506. var size = this.node.getSize();
  507. this.iconNode.setStyle("height", ""+size.y+"px");
  508. }
  509. }
  510. }
  511. });