Orgfield.js 21 KB

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