Orgfield.js 21 KB

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