Elautocomplete.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Elautocomplete 基于Element UI的自动完成输入框组件。
  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. */
  23. MWF.xApplication.process.Xform.Elautocomplete = MWF.APPElautocomplete = new Class(
  24. /** @lends o2.xApplication.process.Xform.Elautocomplete# */
  25. {
  26. Implements: [Events],
  27. Extends: MWF.APP$ElSelector,
  28. options: {
  29. "moduleEvents": ["load", "queryLoad", "postLoad"],
  30. /**
  31. * 当 input失去焦点且值有修改是触发,或点击建议面板的选项后且值有修改时触发。this.event[0]为组件值
  32. * @event MWF.xApplication.process.Xform.Elautocomplete#change
  33. * @see {@link https://element.eleme.io/#/zh-CN/component/input#dai-shu-ru-jian-yi|input组件的带输入建议章节}
  34. */
  35. /**
  36. * 点击建议面板的选项后时触发。this.event[0]为选中的选项
  37. * @event MWF.xApplication.process.Xform.Elautocomplete#select
  38. * @see {@link https://element.eleme.io/#/zh-CN/component/input#input-methods|input组件的Input Method章节}
  39. */
  40. "elEvents": ["select", "change"]
  41. },
  42. /**
  43. * @summary 组件的配置信息,同时也是Vue组件的data。
  44. * @member MWF.xApplication.process.Xform.Elautocomplete#json {JsonObject}
  45. * @example
  46. * //可以在脚本中获取此对象,下面两行代码是等价的,它们获取的是同一个对象
  47. * var json = this.form.get("elinput").json; //获取组件的json对象
  48. * var json = this.form.get("elinput").vm.$data; //获取Vue组件的data数据,
  49. *
  50. * //通过json对象操作Element组件
  51. * json.size = "mini"; //改变输入框大小
  52. * json.disabled = true; //设置输入框为禁用
  53. */
  54. _appendVueData: function(){
  55. this.form.Macro.environment.data.check(this.json.id);
  56. this.json[this.json.id] = this._getBusinessData();
  57. if (!this.json.placement) this.json.placement = "bottom-start";
  58. if (!this.json.popperClass) this.json.popperClass = "";
  59. if (!this.json.triggerOnFocus && this.json.triggerOnFocus!==false) this.json.triggerOnFocus = true;
  60. if (!this.json.prefixIcon) this.json.prefixIcon = "";
  61. if (!this.json.suffixIcon) this.json.suffixIcon = "";
  62. if (!this.json.description) this.json.description = "";
  63. },
  64. resetOption: function(){
  65. this.reload();
  66. },
  67. _createEventFunction: function(methods, k){
  68. methods["$loadElEvent_"+k.camelCase()] = function(){
  69. var flag = true;
  70. if (k==="change") {
  71. this.validationMode();
  72. this._setBusinessData(this.getInputData());
  73. if( !this.validation() )flag = false;
  74. }
  75. if (this.json.events && this.json.events[k] && this.json.events[k].code){
  76. this.form.Macro.fire(this.json.events[k].code, this, arguments);
  77. }
  78. if(k==="select"){
  79. this.validationMode();
  80. var arr = [];
  81. var d = this._getBusinessData();
  82. if( arguments[0] && arguments[0].value )arr.push(arguments[0].value);
  83. if( (d||"") !== (arr[0] || "")){
  84. if (this.json.events && this.json.events["change"] && this.json.events["change"].code){
  85. this.form.Macro.fire(this.json.events["change"].code, this, arr);
  86. }
  87. }
  88. this._setBusinessData(arr[0]);
  89. this.validation();
  90. }
  91. if( flag )this.fireEvent(k, arguments);
  92. }.bind(this);
  93. },
  94. appendVueExtend: function(app){
  95. if (!app.methods) app.methods = {};
  96. // app.methods.$loadElEvent = function(ev){
  97. // this.validationMode();
  98. // if (this.json.events && this.json.events[ev] && this.json.events[ev].code){
  99. // this.form.Macro.fire(this.json.events[ev].code, this, event);
  100. // }
  101. // }.bind(this);
  102. if (!this.json.itemType || this.json.itemType==='values'){
  103. app.methods.$fetchSuggestions = function(qs, cb){
  104. if (this.json.itemValues){
  105. var items = this.json.itemValues.filter(function(v){
  106. return !qs || v.indexOf(qs)!=-1;
  107. }).map(function(v){
  108. return {"value": v};
  109. });
  110. cb(items);
  111. //return items;
  112. }
  113. return [];
  114. }.bind(this);
  115. }else if(this.json.itemType==='script'){
  116. if (this.json.itemScript && this.json.itemScript.code){
  117. var fetchSuggestions = this.form.Macro.exec(this.json.itemScript.code, this);
  118. if (o2.typeOf(fetchSuggestions)==="function"){
  119. app.methods.$fetchSuggestions = function(){
  120. fetchSuggestions.apply(this, arguments);
  121. }.bind(this);
  122. }
  123. }
  124. }else{
  125. var options;
  126. Promise.resolve(this.getOptions()).then(function (opt) {
  127. options = opt;
  128. });
  129. app.methods.$fetchSuggestions = function(qs, cb){
  130. if (options){
  131. var items = options.filter(function(v){
  132. return !qs || v.indexOf(qs)!=-1;
  133. }).map(function(v){
  134. return {"value": v};
  135. });
  136. cb(items);
  137. //return items;
  138. }
  139. return [];
  140. }.bind(this);
  141. }
  142. // app.methods.$fetchSuggestions = function(qs, cb){
  143. // if (this.json.itemType!=='script'){
  144. // if (this.json.itemValues){
  145. // var items = this.json.itemValues.filter(function(v){
  146. // return !qs || v.indexOf(qs)!=-1;
  147. // }).map(function(v){
  148. // return {"value": v};
  149. // });
  150. // cb(items);
  151. // return;
  152. // }
  153. // cb();
  154. // }else{
  155. // this.form.Macro.environment.queryString = qs;
  156. // var list = this.form.Macro.exec(this.json.itemScript.code, this);
  157. // Promise.resolve(list).then(function(items){
  158. // cb(items);
  159. // delete this.form.Macro.environment.queryString;
  160. // }).catch(function(){
  161. // cb();
  162. // delete this.form.Macro.environment.queryString;
  163. // });
  164. // }
  165. // }.bind(this);
  166. },
  167. _createElementHtml: function(){
  168. var html = "<el-autocomplete";
  169. html += " v-model=\""+this.json.$id+"\"";
  170. html += " :placement=\"placement\"";
  171. html += " :popper-class=\"popperClass\"";
  172. html += " :trigger-on-focus=\"triggerOnFocus\"";
  173. html += " :prefix-icon=\"prefixIcon\"";
  174. html += " :suffix-icon=\"suffixIcon\"";
  175. html += " :placeholder=\"description\"";
  176. html += " :fetch-suggestions=\"$fetchSuggestions\"";
  177. html += " :hide-loading=\"false\"";
  178. html += " :popper-append-to-body=\"false\"";
  179. this.options.elEvents.forEach(function(k){
  180. html += " @"+k+"=\"$loadElEvent_"+k.camelCase()+"\"";
  181. });
  182. // this.options.elEvents.forEach(function(k){
  183. // html += " @"+k+"=\"$loadElEvent('"+k+"')\"";
  184. // });
  185. if (this.json.elProperties){
  186. Object.keys(this.json.elProperties).forEach(function(k){
  187. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  188. }, this);
  189. }
  190. if (this.json.elStyles) html += " :style=\"elStyles\"";
  191. // if (this.json.elStyles){
  192. // var style = "";
  193. // Object.keys(this.json.elStyles).forEach(function(k){
  194. // if (this.json.elStyles[k]) style += k+":"+this.json.elStyles[k]+";";
  195. // }, this);
  196. // html += " style=\""+style+"\"";
  197. // }
  198. html += ">";
  199. if (this.json.vueSlot) html += this.json.vueSlot;
  200. html += "</el-autocomplete>";
  201. return html;
  202. }
  203. });