Elcascader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. o2.xDesktop.requireApp("process.Xform", "$Elinput", null, false);
  2. /** @class Elcascader 基于Element UI的级联选择框组件。
  3. * @o2cn 级联选择框
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var input = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var input = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS|Portal}
  13. * @hideconstructor
  14. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|Element UI Cascader 级联选择器}
  15. */
  16. MWF.xApplication.process.Xform.Elcascader = MWF.APPElcascader = new Class(
  17. /** @lends o2.xApplication.process.Xform.Elcascader# */
  18. {
  19. Implements: [Events],
  20. Extends: MWF.APP$Elinput,
  21. options: {
  22. /**
  23. * 组件加载后触发。如果选项加载为异步,则异步处理完成后触发此事件
  24. * @event MWF.xApplication.process.Xform.Elcascader#load
  25. */
  26. "moduleEvents": ["load", "queryLoad", "postLoad"],
  27. /**
  28. * 当获得焦点时触发。this.event[0]指向Event
  29. * @event MWF.xApplication.process.Xform.Elcascader#focus
  30. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  31. */
  32. /**
  33. * 当失去焦点时触发。this.event[0]指向Event
  34. * @event MWF.xApplication.process.Xform.Elcascader#blur
  35. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  36. */
  37. /**
  38. * 当选中节点变化时触发。this.event[0]为选中节点的值
  39. * @event MWF.xApplication.process.Xform.Elcascader#change
  40. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  41. */
  42. /**
  43. * 下拉框出现/隐藏时触发。this.event[0]的值:出现则为 true,隐藏则为 false
  44. * @event MWF.xApplication.process.Xform.Elcascader#visible-change
  45. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  46. */
  47. /**
  48. * 在多选模式下,移除Tag时触发。this.event[0]为移除的Tag对应的节点的值
  49. * @event MWF.xApplication.process.Xform.Elcascader#remove-tag
  50. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  51. */
  52. /**
  53. * 当展开节点发生变化时触发。this.event[0]指向各父级选项值组成的数组
  54. * @event MWF.xApplication.process.Xform.Elcascader#expand-change
  55. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  56. */
  57. /**
  58. * 过滤函数调用之前的钩子函数。this.event[0]指向value参数:如果该函数的返回值是 false 或者是一个被拒绝的Promise,那么接下来的过滤便不会执行。
  59. * @event MWF.xApplication.process.Xform.Elcascader#before-filter
  60. * @see {@link https://element.eleme.cn/#/zh-CN/component/cascader|级联选择框的Cascader Events章节}
  61. */
  62. "elEvents": ["focus", "blur", "change", "visible-change", "remove-tag", "expand-change", "before-filter"]
  63. },
  64. // _loadNode: function(){
  65. // if (this.isReadonly()) this.json.disabled = true;
  66. // this._loadNodeEdit();
  67. // },
  68. resetOption: function(){
  69. this.reload();
  70. },
  71. _loadMergeReadContentNode: function( contentNode, data ){
  72. this._loadOptions();
  73. Promise.resolve(this.json.options).then(function(options){
  74. if (data.data){
  75. var text = this.__getOptionsText(options, data.data);
  76. contentNode.set("text", text);
  77. }
  78. }.bind(this));
  79. },
  80. _appendVueData: function(){
  81. this.form.Macro.environment.data.check(this.json.id);
  82. this.json[this.json.id] = this._getBusinessData();
  83. if (!this.json.options) this.json.options = [];
  84. if (!this.json.clearable) this.json.clearable = false;
  85. if (!this.json.size) this.json.size = "";
  86. if (!this.json.popperClass) this.json.popperClass = "";
  87. if (this.json.showAllLevels!==false) this.json.showAllLevels = true;
  88. if (!this.json.separator) this.json.separator = "/";
  89. if (!this.json.disabled) this.json.disabled = false;
  90. if (!this.json.description) this.json.description = "";
  91. if (!this.json.filterable) this.json.filterable = false;
  92. if (!this.json.collapseTags) this.json.collapseTags = false;
  93. if (!this.json.props) this.json.props = {};
  94. if (!this.json.props.expandTrigger) this.json.props.expandTrigger = "click";
  95. if (!this.json.props.multiple) this.json.props.multiple = false;
  96. if (this.json.props.emitPath!==false) this.json.props.emitPath = true;
  97. if (!this.json.props.lazy) this.json.props.lazy = false;
  98. if (!this.json.props.lazyLoad) this.json.props.lazyLoad = null;
  99. if (!this.json.props.value) this.json.props.value = "value";
  100. if (!this.json.props.label) this.json.props.label = "label";
  101. if (!this.json.props.children) this.json.props.children = "children";
  102. if (!this.json.props.disabled) this.json.props.disabled = "disabled";
  103. if (!this.json.props.leaf) this.json.props.leaf = "leaf";
  104. this._loadOptions();
  105. //if (this.json.props.multiple===true) if (!this.json[this.json.id] || !this.json[this.json.id].length) this.json[this.json.id] = [];
  106. if (this.json.props.multiple===true) if (!this.json[this.json.$id] || !this.json[this.json.$id].length) this.json[this.json.$id] = [];
  107. },
  108. appendVueMethods: function(methods){
  109. if (this.json.filterMethod && this.json.filterMethod.code){
  110. var fn = this.form.Macro.exec(this.json.filterMethod.code, this);
  111. methods.$filterMethod = function(){
  112. fn.apply(this, arguments);
  113. }.bind(this);
  114. }
  115. if (this.json.lazyLoadScript && this.json.lazyLoadScript.code){
  116. var fn = this.form.Macro.exec(this.json.lazyLoadScript.code, this);
  117. this.json.props.lazyLoad = function(){
  118. fn.apply(this, arguments);
  119. }.bind(this);
  120. }
  121. if (this.json.beforeFilter && this.json.beforeFilter.code){
  122. var fn = this.form.Macro.exec(this.json.beforeFilter.code, this);
  123. methods.$beforeFilter = function(){
  124. fn.apply(this, arguments);
  125. }.bind(this);
  126. }
  127. },
  128. _setOptionsWithCode: function(code){
  129. var v = this.form.Macro.exec(code, this);
  130. if (v.then){
  131. this.moduleSelectAG = v.then(function(o){
  132. if (o2.typeOf(o)==="array"){
  133. this.json.options = o;
  134. this.json.$options = o;
  135. }
  136. return this.json.options || [];
  137. }.bind(this));
  138. }else if (o2.typeOf(v)==="array"){
  139. this.json.options = v;
  140. this.json.$options = v;
  141. }
  142. },
  143. _loadOptions: function(){
  144. if (this.json.itemsScript && this.json.itemsScript.code) this._setOptionsWithCode(this.json.itemsScript.code);
  145. },
  146. _createElementHtml: function(){
  147. if (!this.json.options) this.json.options = [];
  148. if (!this.json.clearable) this.json.clearable = false;
  149. if (!this.json.size) this.json.size = "";
  150. if (!this.json.popperClass) this.json.popperClass = "";
  151. if (this.json.showAllLevels!==false) this.json.showAllLevels = true;
  152. if (!this.json.separator) this.json.separator = "/";
  153. if (!this.json.disabled) this.json.disabled = false;
  154. if (!this.json.description) this.json.description = "";
  155. if (!this.json.filterable) this.json.filterable = false;
  156. if (!this.json.props) this.json.props = {};
  157. var html = "<el-cascader ";
  158. html += " v-model=\""+this.json.$id+"\"";
  159. html += " :clearable=\"clearable\"";
  160. html += " :size=\"size\"";
  161. html += " :filterable=\"filterable\"";
  162. html += " :disabled=\"disabled\"";
  163. html += " :placeholder=\"description\"";
  164. html += " :options=\"options\"";
  165. html += " :collapse-tags=\"collapseTags\"";
  166. html += " :show-all-levels=\"showAllLevels\"";
  167. html += " :separator=\"separator\"";
  168. html += " :popper-class=\"popperClass\"";
  169. html += " :props=\"props\"";
  170. if (this.json.filterMethod && this.json.filterMethod.code){
  171. html += " :filter-method=\"$filterMethod\"";
  172. }
  173. if (this.json.beforeFilter && this.json.beforeFilter.code){
  174. html += " :before-filter=\"$beforeFilter\"";
  175. }
  176. this.options.elEvents.forEach(function(k){
  177. html += " @"+k+"=\"$loadElEvent_"+k.camelCase()+"\"";
  178. });
  179. if (this.json.elProperties){
  180. Object.keys(this.json.elProperties).forEach(function(k){
  181. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  182. }, this);
  183. }
  184. if (this.json.elStyles) html += " :style=\"elStyles\"";
  185. html += ">";
  186. if (this.json.vueSlot) html += this.json.vueSlot;
  187. html += "</el-cascader >";
  188. return html;
  189. },
  190. //__setReadonly: function(data){},
  191. getCheckedNodes: function(leafOnly){
  192. return (this.vm) ? this.vm.getCheckedNodes(leafOnly) : null;
  193. },
  194. __setReadonly: function(data){
  195. if (this.isReadonly()){
  196. this._loadOptions();
  197. this.fireEvent("postLoad");
  198. Promise.resolve(this.json.options || this.moduleSelectAG).then(function(options){
  199. if (data){
  200. var text = this.__getOptionsText(options, data);
  201. this.node.set("text", text);
  202. if( this.json.elProperties ){
  203. this.node.set(this.json.elProperties );
  204. }
  205. if (this.json.elStyles){
  206. this.node.setStyles( this._parseStyles(this.json.elStyles) );
  207. }
  208. this.fireEvent("load");
  209. this.isLoaded = true;
  210. }
  211. }.bind(this));
  212. }
  213. },
  214. __getOptionsText: function(options, values){
  215. if (!!this.json.props.multiple){
  216. var text = [];
  217. values.forEach(function(v){
  218. if( typeOf( v ) === "array" ){
  219. text = text.concat(this.__getOptionsTextValue(options, v));
  220. }else{
  221. text = text.concat(this.__getLastOptionsTextValue(options, v));
  222. }
  223. }.bind(this));
  224. return text.join(",");
  225. }else{
  226. if( typeOf( values ) === "array" ){
  227. return this.__getOptionsTextValue(options, values).join(",");
  228. }else{
  229. return this.__getLastOptionsTextValue(options, values)
  230. }
  231. }
  232. },
  233. __getOptionsTextValue: function(options, values, prefix, prefixLabel){
  234. var text = [];
  235. var v = typeOf( values ) === "string" ? values : values.join("/");
  236. options.forEach(function(op){
  237. var opValue = (prefix) ? prefix + "/" + op[this.json.props.value] : op[this.json.props.value];
  238. var opLabel = (prefixLabel) ? prefixLabel + "/" + op[this.json.props.label] : op[this.json.props.label];
  239. if (opValue == v) {
  240. text.push(opLabel);
  241. }else if (v.startsWith(opValue) && op[this.json.props.children] && op[this.json.props.children].length){
  242. text = text.concat(this.__getOptionsTextValue(op[this.json.props.children], values, opValue, opLabel));
  243. }
  244. }.bind(this));
  245. if (!this.json.showAllLevels){
  246. return text.map(function(t){
  247. return t.substring(t.indexOf("/")+1, t.length);
  248. });
  249. }else{
  250. return text;
  251. }
  252. },
  253. __getLastOptionsTextValue: function (options, value) {
  254. var text;
  255. for( var i=0; i<options.length; i++ ){
  256. var op = options[i];
  257. if( op[this.json.props.children] && op[this.json.props.children].length ){
  258. text = this.__getLastOptionsTextValue( op[this.json.props.children], value );
  259. if( text )return text;
  260. }else{
  261. var opValue = op[this.json.props.value];
  262. var opLabel = op[this.json.props.label];
  263. if( opValue === value ){
  264. text = opLabel;
  265. }
  266. }
  267. }
  268. return text;
  269. },
  270. getDataByText: function(text){
  271. this._loadOptions();
  272. var opt = this.json.options;
  273. if( !opt )return "";
  274. if( o2.typeOf(opt.then)==="function" ){
  275. return Promise.resolve(opt).then(function(options){
  276. return this._getDataByText(options, text);
  277. }.bind(this));
  278. }else{
  279. return this._getDataByText(opt, text);
  280. }
  281. },
  282. _getDataByText: function(options, text){
  283. var values = [];
  284. if (!!this.json.props.multiple){
  285. var texts = typeOf( text ) === "array" ? text : [text];
  286. texts.forEach(function(t){
  287. if( typeOf( t ) === "array" && t.length > 1 ){
  288. values = values.concat(this._getEachDataByText(options, t));
  289. }else{
  290. values = values.concat(this._getLastDataByText(options, typeOf( t ) === "array" ? (t[0] || "") : t));
  291. }
  292. }.bind(this));
  293. return values;
  294. }else{
  295. if( typeOf( text ) === "array" && typeOf( text[0] ) === "array" ){
  296. text = text[0];
  297. }
  298. if( typeOf( text ) === "array" && text.length > 1 ){
  299. values = this._getEachDataByText(options, text);
  300. return values.length ? values[0] : [];
  301. }else{
  302. return this._getLastDataByText(options, typeOf( text ) === "array" ? (text[0] || "") : text);
  303. }
  304. }
  305. },
  306. _getEachDataByText: function(options, texts, prefix, prefixLabel){
  307. var value = [];
  308. var t = typeOf( texts ) === "string" ? texts : texts.join("/");
  309. options.forEach(function(op){
  310. var opValue = (prefix) ? prefix + "/" + op[this.json.props.value] : op[this.json.props.value];
  311. var opLabel = (prefixLabel) ? prefixLabel + "/" + op[this.json.props.label] : op[this.json.props.label];
  312. if (opLabel === t) {
  313. value.push(opValue.split("/"));
  314. }else if (t.startsWith(opLabel) && op[this.json.props.children] && op[this.json.props.children].length){
  315. value = value.concat(this._getEachDataByText(op[this.json.props.children], texts, opValue, opLabel));
  316. }
  317. }.bind(this));
  318. if (!this.json.showAllLevels){
  319. return value.map(function(t){
  320. return typeOf( t ) === "array" ? t.getLast() : t;
  321. });
  322. }else{
  323. return value;
  324. }
  325. },
  326. _getLastDataByText: function (options, text) {
  327. var value;
  328. for( var i=0; i<options.length; i++ ){
  329. var op = options[i];
  330. if( op[this.json.props.children] && op[this.json.props.children].length ){
  331. value = this._getLastDataByText( op[this.json.props.children], text );
  332. if( value )return value;
  333. }else{
  334. var opValue = op[this.json.props.value];
  335. var opLabel = op[this.json.props.label];
  336. if( opLabel === text ){
  337. value = opValue;
  338. }
  339. }
  340. }
  341. return value;
  342. },
  343. getExcelData: function( type ){
  344. var data = this.json[this.json.$id];
  345. if( !data )return "";
  346. if( type === "value" )return data;
  347. var text, opt = this.json.options;
  348. if( !opt )return "";
  349. if( o2.typeOf(opt.then)==="function" ){
  350. return Promise.resolve(opt).then(function(options){
  351. text = this.__getOptionsText(options, data);
  352. return typeOf(text) === "array" ? text.join(", ") : (text || "");
  353. }.bind(this));
  354. }else{
  355. text = this.__getOptionsText(opt, data);
  356. return typeOf(text) === "array" ? text.join(", ") : (text || "");
  357. }
  358. },
  359. setExcelData: function(d, type){
  360. debugger;
  361. var arr = this.stringToArray(d);
  362. this.excelData = arr;
  363. arr = arr.map(function (a) {
  364. return a.contains("/") ? a.split("/") : a;
  365. });
  366. if( type === "value" ){
  367. this.setData(arr);
  368. }else{
  369. var data = this.getDataByText( arr );
  370. this.setData(data);
  371. }
  372. }
  373. });