Orgfield.js 21 KB

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