$ElModule.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. o2.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @classdesc $ElModule ElementUI组件类,此类为所有ElementUI组件的父类。
  3. * @class
  4. * @o2category FormComponents
  5. * @hideconstructor
  6. * */
  7. o2.xApplication.process.Xform.$ElModule = MWF.APP$ElModule = new Class(
  8. /** @lends o2.xApplication.process.Xform.$ElModule# */
  9. {
  10. Implements: [Events],
  11. Extends: MWF.APP$Module,
  12. options: {
  13. /**
  14. * 组件加载前触发。queryLoad执行的时候,当前组件没有在form里注册,通过this.form.get("fieldId")不能获取到当前组件,需要用this.target获取。
  15. * @event MWF.xApplication.process.Xform.$ElModule#queryLoad
  16. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  17. */
  18. /**
  19. * 组件加载时触发.
  20. * @event MWF.xApplication.process.Xform.$ElModule#load
  21. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  22. */
  23. /**
  24. * 组件加载后触发.
  25. * @event MWF.xApplication.process.Xform.$ElModule#postLoad
  26. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  27. */
  28. "moduleEvents": ["load", "queryLoad", "postLoad"],
  29. "elEvents": ["focus", "blur", "change", "input", "clear"]
  30. },
  31. /**
  32. * @summary 组件的配置信息,同时也是Vue组件的data。
  33. * @member MWF.xApplication.process.Xform.Elinput#json {JsonObject}
  34. * @example
  35. * //可以在脚本中获取此对象,下面两行代码是等价的,它们获取的是同一个对象
  36. * var json = this.form.get("elinput").json; //获取组件的json对象
  37. * var json = this.form.get("elinput").vm.$data; //获取Vue组件的data数据,
  38. *
  39. * //通过json对象操作Element组件
  40. * json.size = "mini"; //改变输入框大小
  41. * json.disabled = true; //设置输入框为禁用
  42. */
  43. load: function(){
  44. this._loadModuleEvents();
  45. this.form.app.addEvent('queryClose', function(){
  46. if (this.vm) this.vm.$destroy();
  47. }.bind(this));
  48. if (this.fireEvent("queryLoad")){
  49. this._queryLoaded();
  50. this._loadUserInterface();
  51. }
  52. },
  53. reload: function(){
  54. if (!this.vm) return;
  55. var node = this.vm.$el;
  56. this.vm.$destroy();
  57. node.empty();
  58. this.vm = null;
  59. this.vueApp = null;
  60. this._loadUserInterface();
  61. },
  62. _checkVmodel: function(text){
  63. if (text){
  64. this.vModels = [];
  65. var reg = /(?:v-model)(?:.lazy|.number|.trim)?(?:\s*=\s*)(?:["'])?([^"']*)/g;
  66. var arr;
  67. while ((arr = reg.exec(text)) !== null) {
  68. if (arr.length>1 && arr[1]){
  69. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  70. modelId = (modelId) ? modelId+".."+arr[1] : arr[1];
  71. this.json[arr[1]] = this._getBusinessData(modelId) || "";
  72. this.vModels.push(arr[1]);
  73. }
  74. }
  75. }
  76. },
  77. _loadUserInterface: function(){
  78. this.node.appendHTML(this._createElementHtml(), "before");
  79. var input = this.node.getPrevious();
  80. this.node.destroy();
  81. this.node = input;
  82. this.node.set({
  83. "id": this.json.id,
  84. "MWFType": this.json.type
  85. });
  86. this.node.addClass("o2_vue");
  87. this._createVueApp();
  88. },
  89. _createVueApp: function(){
  90. if (this.json.vueSlot) this._checkVmodel(this.json.vueSlot);
  91. if (!this.vm) this._loadVue(this._mountVueApp.bind(this));
  92. },
  93. _loadVue: function(callback){
  94. var flag = (o2.session.isDebugger && this.form.app.inBrowser);
  95. var vue = flag ? "vue_develop" : "vue";
  96. //var vueName = flag ? "Vue" : "Cn";
  97. // if (!window.Vue || window.Vue.name!==vueName){
  98. // o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": [vue, "elementui"]}, { "sequence": true }, callback);
  99. // }else{
  100. // if (callback) callback();
  101. // }
  102. o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": [vue, "elementui"]}, { "sequence": true }, callback);
  103. },
  104. _mountVueApp: function(){
  105. if (!this.vueApp) this.vueApp = this._createVueExtend();
  106. /**
  107. * @summary Vue对象实例
  108. * @see https://vuejs.org/
  109. * @member {VueInstance}
  110. */
  111. this.vm = new Vue(this.vueApp);
  112. this.vm.$mount(this.node);
  113. },
  114. _createVueExtend: function(){
  115. var _self = this;
  116. var app = {
  117. data: this._createVueData(),
  118. mounted: function(){
  119. _self._afterMounted(this.$el);
  120. }
  121. };
  122. app.methods = this._createVueMethods(app);
  123. this.appendVueExtend(app);
  124. this.appendVueWatch(app);
  125. this._afterCreateVueExtend(app);
  126. return app;
  127. },
  128. appendVueWatch: function(app){
  129. if (this.vModels && this.vModels.length){
  130. app.watch = app.watch || {};
  131. this.vModels.forEach(function(m){
  132. app.watch[m] = function(val, oldVal){
  133. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  134. modelId = (modelId) ? modelId+".."+m : m;
  135. this._setBusinessData(val, modelId);
  136. }.bind(this);
  137. }.bind(this));
  138. }
  139. },
  140. appendVueMethods: function(methods){},
  141. appendVueExtend: function(app){
  142. // if (!app.methods) app.methods = {};
  143. // this.options.elEvents.forEach(function(k){
  144. // this._createEventFunction(app, k);
  145. // }.bind(this));
  146. },
  147. appendVueEvents: function(methods){
  148. this.options.elEvents.forEach(function(k){
  149. this._createEventFunction(methods, k);
  150. }.bind(this));
  151. },
  152. _createEventFunction: function(methods, k){
  153. methods["$loadElEvent_"+k.camelCase()] = function(){
  154. var flag = true;
  155. if (k==="change"){
  156. if (this.validationMode){
  157. this.validationMode();
  158. this._setBusinessData(this.getInputData());
  159. if( !this.validation() ) flag = false;
  160. }
  161. }
  162. if (this.json.events && this.json.events[k] && this.json.events[k].code){
  163. this.form.Macro.fire(this.json.events[k].code, this, arguments);
  164. }
  165. if( flag )this.fireEvent(k, arguments);
  166. }.bind(this);
  167. },
  168. _createVueMethods: function(){
  169. var methods = {};
  170. if (this.json.vueMethods && this.json.vueMethods.code){
  171. methods = this.form.Macro.exec(this.json.vueMethods.code, this);
  172. }
  173. this.appendVueEvents(methods);
  174. this.appendVueMethods(methods);
  175. return methods || {};
  176. },
  177. _createVueData: function(){
  178. if (this.vModels && this.vModels.length){
  179. this.vModels.forEach(function(m){
  180. if (!this.json.hasOwnProperty(m)) this.json[m] = "";
  181. }.bind(this));
  182. }
  183. if (this.json.vueData && this.json.vueData.code){
  184. var d = this.form.Macro.exec(this.json.vueData.code, this);
  185. this.json = Object.merge(d, this.json);
  186. }
  187. if (this.json.$id===this.json.id) this.form.Macro.environment.data.check(this.json.$id);
  188. this.json[this.json.$id] = this._getBusinessData();
  189. this._appendVueData();
  190. return this.json;
  191. },
  192. _afterMounted: function(el){
  193. this.node = el;
  194. this.node.set({
  195. "id": this.json.id,
  196. "MWFType": this.json.type
  197. });
  198. this._loadVueCss();
  199. this._loadDomEvents();
  200. this._afterLoaded();
  201. this.fireEvent("postLoad");
  202. this.fireEvent("load");
  203. },
  204. _loadVueCss: function(){
  205. if (this.styleNode){
  206. this.node.removeClass(this.styleNode.get("id"));
  207. }
  208. if (this.json.vueCss && this.json.vueCss.code){
  209. this.styleNode = this.node.getParent().loadCssText(this.json.vueCss.code, {"notInject": true});
  210. this.styleNode.inject(this.node.getParent(), "before");
  211. }
  212. },
  213. _appendVueData: function(){},
  214. _createElementHtml: function(){
  215. return "";
  216. },
  217. _afterCreateVueExtend: function (app) {}
  218. });