Orgfield.js 20 KB

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