Orgfield.js 20 KB

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