Elselect.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. o2.xDesktop.requireApp("process.Xform", "$Elinput", null, false);
  2. MWF.xDesktop.requireApp("process.Xform", "$Selector", null, false);
  3. if( !o2.APP$ElSelector ){
  4. o2.xApplication.process.Xform.$ElSelector = o2.APP$ElSelector = new Class({
  5. Implements: [Events],
  6. Extends: MWF.APP$Elinput
  7. });
  8. Object.assign(o2.APP$ElSelector.prototype, o2.APP$Selector.prototype);
  9. }
  10. /** @class Elselect 基于Element UI的选择框组de件。
  11. * @o2cn 选择框
  12. * @example
  13. * //可以在脚本中获取该组件
  14. * //方法1:
  15. * var input = this.form.get("name"); //获取组件
  16. * //方法2
  17. * var input = this.target; //在组件事件脚本中获取
  18. * @extends MWF.xApplication.process.Xform.$Module
  19. * @o2category FormComponents
  20. * @o2range {Process|CMS|Portal}
  21. * @hideconstructor
  22. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|Element UI Select 选择器}
  23. */
  24. MWF.xApplication.process.Xform.Elselect = MWF.APPElselect = new Class(
  25. /** @lends o2.xApplication.process.Xform.Elselect# */
  26. {
  27. Implements: [Events],
  28. Extends: MWF.APP$ElSelector,
  29. options: {
  30. /**
  31. * 组件加载后触发。如果选项加载为异步,则异步处理完成后触发此事件
  32. * @event MWF.xApplication.process.Xform.Elselect#load
  33. */
  34. "moduleEvents": ["load", "queryLoad", "postLoad"],
  35. /**
  36. * 当 input 获得焦点时触发。this.event[0]指向Event
  37. * @event MWF.xApplication.process.Xform.Elselect#focus
  38. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  39. */
  40. /**
  41. * 当 input 失去焦点时触发。this.event[0]指向Event
  42. * @event MWF.xApplication.process.Xform.Elselect#blur
  43. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  44. */
  45. /**
  46. * 选中值发生变化时触发。this.event[0]为组件目前的选中值
  47. * @event MWF.xApplication.process.Xform.Elselect#change
  48. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  49. */
  50. /**
  51. * 下拉框出现/隐藏时触发。this.event[0]的值出现则为 true,隐藏则为 false
  52. * @event MWF.xApplication.process.Xform.Elselect#visible-change
  53. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  54. */
  55. /**
  56. * 多选模式下移除tag时触发。this.event[0]为移除的tag值
  57. * @event MWF.xApplication.process.Xform.Elselect#remove-tag
  58. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  59. */
  60. /**
  61. * 可清空的单选模式下用户点击清空按钮时触发。
  62. * @event MWF.xApplication.process.Xform.Elselect#clear
  63. * @see {@link https://element.eleme.cn/#/zh-CN/component/select|选择器的Select Events章节}
  64. */
  65. "elEvents": ["focus", "blur", "change", "visible-change", "remove-tag", "clear"]
  66. },
  67. // _loadNode: function(){
  68. // if (this.isReadonly()) this.json.disabled = true;
  69. // this._loadNodeEdit();
  70. // },
  71. _loadMergeReadContentNode: function( contentNode, data ){
  72. this._loadOptions();
  73. Promise.resolve( this.json.options || this.moduleSelectAG ).then(function(options){
  74. var values = (o2.typeOf(data.data) !== "array") ? [data.data] : data.data;
  75. var text = this.__getOptionsText(options, values);
  76. contentNode.set("text", text.join(","));
  77. }.bind(this));
  78. },
  79. resetOption: function(){
  80. this.reload();
  81. },
  82. _appendVueData: function(){
  83. // this.form.Macro.environment.data.check(this.json.id);
  84. // this.json[this.json.id] = this._getBusinessData();
  85. if (!this.json.clearable) this.json.clearable = false;
  86. if (!this.json.size) this.json.size = "";
  87. if (!this.json.multiple) this.json.multiple = false;
  88. if (!this.json.collapseTags) this.json.collapseTags = false;
  89. if (!this.json.filterable) this.json.filterable = false;
  90. if (!this.json.allowCreate) this.json.allowCreate = false;
  91. if (!this.json.remote) this.json.remote = false;
  92. if (!this.json.popperClass) this.json.popperClass = "";
  93. if (!this.json.multipleLimit || !this.json.multipleLimit.toInt()) this.json.multipleLimit = 0;
  94. if (!this.json.noMatchText) this.json.noMatchText = "";
  95. if (!this.json.noDataText) this.json.noDataText = "";
  96. if (!this.json.loadingText) this.json.loadingText = "";
  97. if (!this.json.description) this.json.description = "";
  98. if (!this.json.disabled) this.json.disabled = false;
  99. if (!this.json.loading) this.json.loading = false;
  100. if (!this.json.options) this.json.options = [];
  101. this._loadOptions();
  102. if (this.json.multiple===true) if (!this.json[this.json.$id] || !this.json[this.json.$id].length) this.json[this.json.$id] = [];
  103. },
  104. appendVueMethods: function(methods){
  105. if (this.json.filterMethod && this.json.filterMethod.code){
  106. var fn = this.form.Macro.exec(this.json.filterMethod.code, this);
  107. methods.$filterMethod = function(){
  108. fn.apply(this, arguments);
  109. }.bind(this)
  110. }
  111. if (this.json.remoteMethod && this.json.remoteMethod.code){
  112. var fn = this.form.Macro.exec(this.json.remoteMethod.code, this);
  113. methods.$remoteMethod = function(){
  114. fn.apply(this, arguments);
  115. }.bind(this)
  116. }
  117. },
  118. _setOptionsWithCode: function(code){
  119. var v = this.form.Macro.exec(code, this);
  120. if (v.then){
  121. this.moduleSelectAG = v.then(function(o){
  122. if (o2.typeOf(o)==="array"){
  123. this.json.options = o.map(function(item){
  124. if (o2.typeOf(item)!=="object"){
  125. var value = item.toString();
  126. return {"value": value, "label": value};
  127. }
  128. return item;
  129. });
  130. this.json.$options = Array.clone(this.json.options);
  131. return this.json.options;
  132. }
  133. }.bind(this));
  134. }else if (o2.typeOf(v)==="array"){
  135. this.json.options = v.map(function(item){
  136. if (o2.typeOf(item)!=="object"){
  137. var value = item.toString();
  138. return {"value": value, "label": value};
  139. }
  140. return item;
  141. });
  142. this.json.$options = Array.clone(this.json.options);
  143. }
  144. },
  145. _setOptionsWithArray: function( optionItems ){
  146. if (o2.typeOf(optionItems)==="array"){
  147. this.json.options = optionItems.map(function(item){
  148. if (item && o2.typeOf(item)!=="object"){
  149. var value = item.toString();
  150. var arr = value.split("|");
  151. if (arr.length>1){
  152. return {"label": arr[0], "value": arr[1]};
  153. }else{
  154. return {"label": arr[0], "value": arr[0]};
  155. }
  156. }
  157. return item;
  158. });
  159. this.json.$options = Array.clone(this.json.options);
  160. }
  161. },
  162. _loadOptions: function(){
  163. if (this.json.itemsGroup===true){
  164. if (this.json.itemGroupScript && this.json.itemGroupScript.code) this._setOptionsWithCode(this.json.itemGroupScript.code);
  165. }else{
  166. // if (this.json.itemType=="script"){
  167. // if (this.json.itemScript && this.json.itemScript.code) this._setOptionsWithCode(this.json.itemScript.code);
  168. // }else{
  169. // if (this.json.itemValues && (o2.typeOf(this.json.itemValues)==="array")){
  170. // this.json.options = this.json.itemValues.map(function(item){
  171. // if (item){
  172. // var arr = item.split("|");
  173. // if (arr.length>1){
  174. // return {"label": arr[0], "value": arr[1]};
  175. // }else{
  176. // return {"label": arr[0], "value": arr[0]};
  177. // }
  178. // }
  179. // });
  180. // }
  181. // }
  182. var optionItems = this.getOptions();
  183. if( optionItems && typeOf(optionItems.then) === "function" ){
  184. this.moduleSelectAG = Promise.resolve(optionItems).then(function (optItems) {
  185. this._setOptionsWithArray( optItems );
  186. this.moduleSelectAG = false;
  187. return this.json.options;
  188. }.bind(this));
  189. }else{
  190. this._setOptionsWithArray( optionItems );
  191. }
  192. }
  193. },
  194. _createElementHtml: function(){
  195. var html = "<el-select";
  196. html += " v-model=\""+this.json.$id+"\"";
  197. html += " :clearable=\"clearable\"";
  198. html += " :size=\"size\"";
  199. html += " :filterable=\"filterable\"";
  200. html += " :disabled=\"disabled\"";
  201. html += " :placeholder=\"description\"";
  202. html += " :multiple=\"multiple\"";
  203. html += " :collapse-tags=\"collapseTags\"";
  204. html += " :allow-create=\"allowCreate\"";
  205. html += " :remote=\"remote\"";
  206. html += " :popper-class=\"popperClass\"";
  207. html += " :multiple-limit=\"multipleLimit\"";
  208. html += " :no-match-text=\"noMatchText\"";
  209. html += " :no-data-text=\"noDataText\"";
  210. html += " :loading-text=\"loadingText\"";
  211. html += " :loading=\"loading\"";
  212. if (this.json.filterMethod && this.json.filterMethod.code){
  213. html += " :filter-method=\"$filterMethod\"";
  214. }
  215. if (this.json.remoteMethod && this.json.remoteMethod.code){
  216. html += " :remote-method=\"$remoteMethod\"";
  217. }
  218. this.options.elEvents.forEach(function(k){
  219. html += " @"+k+"=\"$loadElEvent_"+k.camelCase()+"\"";
  220. });
  221. if (this.json.elProperties){
  222. Object.keys(this.json.elProperties).forEach(function(k){
  223. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  224. }, this);
  225. }
  226. if (this.json.elStyles) html += " :style=\"elStyles\"";
  227. html += ">";
  228. var itemFor = "item in options";
  229. if (this.json.itemsGroup===true){
  230. html += "<el-option-group v-for=\"group in options\" :key=\"group.label\" :label=\"group.label\" :disabled=\"group.disabled\"";
  231. if (this.json.elGroupProperties){
  232. Object.keys(this.json.elGroupProperties).forEach(function(k){
  233. if (this.json.elGroupProperties[k]) html += " "+k+"=\""+this.json.elGroupProperties[k]+"\"";
  234. }, this);
  235. }
  236. if (this.json.elGroupStyles) html += " :style=\"elGroupStyles\"";
  237. html += ">";
  238. itemFor = "item in group.options";
  239. }
  240. html += "<el-option v-for=\""+itemFor+"\" :key=\"item.value\" :label=\"item.label\" :value=\"item.value\" :disabled=\"item.disabled\"";
  241. if (this.json.elOptionProperties){
  242. Object.keys(this.json.elOptionProperties).forEach(function(k){
  243. if (this.json.elOptionProperties[k]) html += " "+k+"=\""+this.json.elOptionProperties[k]+"\"";
  244. }, this);
  245. }
  246. if (this.json.elOptionStyles) html += " :style=\"elOptionStyles\"";
  247. html += ">";
  248. if (this.json.customTemplete) html += this.json.customTemplete;
  249. html += "</el-option>";
  250. if (this.json.itemsGroup===true){
  251. html += "</el-option-group>"
  252. }
  253. if (this.json.vueSlot) html += this.json.vueSlot;
  254. html += "</el-select>";
  255. return html;
  256. },
  257. // _afterLoaded: function(){
  258. // if (this.isReadonly()){
  259. // this.node.hide();
  260. // window.setTimeout(function(){
  261. // var text = "";
  262. // var nodes = this.node.getElements(".el-select__tags-text");
  263. // if (nodes && nodes.length){
  264. // nodes.forEach(function(n){
  265. // text += ((text) ? ", " : "")+n.get("text");
  266. // });
  267. // }
  268. // var node = new Element("div").inject(this.node, "before");
  269. // this.node.destroy();
  270. // this.node = node;
  271. // this.node.set({
  272. // "nodeId": this.json.id,
  273. // "MWFType": this.json.type
  274. // });
  275. // this._loadDomEvents();
  276. // //this.node.removeEvents("click");
  277. // //this.node.empty();
  278. // this.node.set("text", text);
  279. // //this.node.show();
  280. // }.bind(this), 20);
  281. //
  282. // }
  283. // },
  284. _createEventFunction: function(methods, k){
  285. methods["$loadElEvent_"+k.camelCase()] = function(){
  286. var flag = true;
  287. if (k==="change"){
  288. if (this.validationMode){
  289. this.validationMode();
  290. this._setBusinessData(this.getInputData());
  291. if( !this.validation() ) flag = false;
  292. }
  293. }else if( this.json.filterable && ['visible-change','focus'].contains(k) ){
  294. var input = this.node.getElement('.el-input__inner');
  295. if( input )input.removeAttribute('readonly');
  296. }
  297. if (this.json.events && this.json.events[k] && this.json.events[k].code){
  298. this.form.Macro.fire(this.json.events[k].code, this, arguments);
  299. }
  300. if( flag )this.fireEvent(k, arguments);
  301. }.bind(this);
  302. },
  303. _afterLoaded: function (){
  304. if( this.json.filterable ){
  305. var input = this.node.getElement('.el-input__inner');
  306. if( input )input.removeAttribute('readonly');
  307. }
  308. },
  309. __setReadonly: function(data){
  310. if (this.isReadonly()){
  311. this._loadOptions();
  312. Promise.resolve(this.json.options || this.moduleSelectAG).then(function(options){
  313. var values = (o2.typeOf(data) !== "array") ? [data] : data;
  314. var text = this.__getOptionsText(options, values);
  315. this.node.set("text", text.join(","));
  316. this.fireEvent("load");
  317. this.isLoaded = true;
  318. }.bind(this));
  319. if( this.json.elProperties ){
  320. this.node.set(this.json.elProperties );
  321. }
  322. if (this.json.elStyles){
  323. this.node.setStyles( this._parseStyles(this.json.elStyles) );
  324. }
  325. this.fireEvent("postLoad");
  326. }
  327. },
  328. __getOptionsText: function(options, values){
  329. var text = [];
  330. options.forEach(function(op){
  331. if (op.value){
  332. if (values.indexOf(op.value)!=-1) text.push(op.label || op.value);
  333. }else if (op.options && op.options.length){
  334. text = text.concat(this.__getOptionsText(op.options, values));
  335. }
  336. }.bind(this));
  337. return text;
  338. },
  339. getDataByText: function(text){
  340. var opt = this.json.options;
  341. if( !opt )return "";
  342. if( o2.typeOf(opt.then)==="function" ){
  343. return Promise.resolve(opt).then(function(options){
  344. return this._getDataByText(options, text);
  345. }.bind(this));
  346. }else{
  347. return this._getDataByText(opt, text);
  348. }
  349. },
  350. _getDataByText: function(options, texts){
  351. var value = [];
  352. options.forEach(function(op){
  353. if (op.value){
  354. if (texts.indexOf(op.label || op.value)!=-1) value.push(op.value);
  355. }else if (op.options && op.options.length){
  356. value = value.concat(this._getDataByText(op.options, texts));
  357. }
  358. }.bind(this));
  359. return value;
  360. },
  361. getExcelData: function(){
  362. var data = this.json[this.json.$id];
  363. if( !data )return "";
  364. if( !this.json.options )this._loadOptions();
  365. var text, opt = this.json.options;
  366. if( !opt )return "";
  367. if( o2.typeOf(opt.then)==="function" ){
  368. return Promise.resolve(opt).then(function(options){
  369. text = this.__getOptionsText(options, data);
  370. return typeOf(text) === "array" ? text.join(", ") : (text || "");
  371. }.bind(this));
  372. }else{
  373. text = this.__getOptionsText(opt, data);
  374. return typeOf(text) === "array" ? text.join(", ") : (text || "");
  375. }
  376. },
  377. setExcelData: function(d){
  378. this._loadOptions();
  379. this.moduleExcelAG = Promise.resolve( this.json.options || this.moduleSelectAG ).then(function(options){
  380. var arr = this.stringToArray(d);
  381. this.excelData = arr;
  382. arr = arr.map(function (a) {
  383. return a.contains("/") ? a.split("/") : a;
  384. });
  385. var data = this.getDataByText( arr );
  386. this.setData( this.json.multiple ? data : data[0], true);
  387. this.moduleExcelAG = null;
  388. }.bind(this))
  389. }
  390. });