Orgfield.js 20 KB

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