Elcommon.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. o2.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. o2.xDesktop.requireApp("process.Xform", "$ElModule", null, false);
  3. /** @class Elcommon 基于Element UI的通用组件。
  4. * @o2cn 通用组件
  5. * @example
  6. * //可以在脚本中获取该组件
  7. * //方法1:
  8. * var button = this.form.get("name"); //获取组件
  9. * //方法2
  10. * var button = this.target; //在组件事件脚本中获取
  11. * @extends o2.xApplication.process.Xform.$Module
  12. * @o2category FormComponents
  13. * @o2range {Process|CMS|Portal}
  14. * @hideconstructor
  15. */
  16. o2.xApplication.process.Xform.Elcommon = o2.APPElcommon = new Class(
  17. /** @lends MWF.xApplication.process.Xform.Elcommon# */
  18. {
  19. Implements: [Events],
  20. Extends: o2.APP$ElModule,
  21. /**
  22. * @summary 组件的配置信息,同时也是Vue组件的data。
  23. * @member o2.xApplication.process.Xform.Elcommon#json {JsonObject}
  24. * @example
  25. * //可以在脚本中获取此对象,下面两行代码是等价的,它们获取的是同一个对象
  26. * var json = this.form.get("elcommon").json; //获取组件的json对象
  27. * var json = this.form.get("elcommon").vm.$data; //获取Vue组件的data数据,
  28. *
  29. */
  30. // load: function(){
  31. // this._loadModuleEvents();
  32. // if (this.fireEvent("queryLoad")){
  33. // this._queryLoaded();
  34. // this._loadUserInterface();
  35. // }
  36. // },
  37. //
  38. // initialize: function(node, json, form, options){
  39. // this.node = $(node);
  40. // this.node.store("module", this);
  41. // this.json = json;
  42. // this.form = form;
  43. // this.field = true;
  44. // this.parentLine = null;
  45. // },
  46. _checkVueHtml: function(){
  47. var nodes = this.node.querySelectorAll("*[v-model]");
  48. this.tmpVueData = {};
  49. var arrs = ["el-checkbox-group"];
  50. nodes.forEach(function(node){
  51. var model = node.get("v-model");
  52. if (model) this.tmpVueData[model] = (arrs.indexOf(node.tagName.toString().toLowerCase())===-1) ? "" : [];
  53. }.bind(this));
  54. },
  55. _loadUserInterface: function(){
  56. this.node.set("html", this._createElementHtml());
  57. //this._checkVueHtml();
  58. this._checkVmodel();
  59. this.node.set({
  60. "id": this.json.id,
  61. "MWFType": this.json.type
  62. });
  63. this.node.addClass("o2_vue");
  64. this._createVueApp();
  65. },
  66. // _createVueApp: function(){
  67. // if (!this.vm) this._loadVue(this._mountVueApp.bind(this));
  68. // },
  69. //
  70. // _loadVue: function(callback){
  71. // if (!window.Vue){
  72. // o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": ["vue", "elementui"]}, { "sequence": true }, callback);
  73. // }else{
  74. // if (callback) callback();
  75. // }
  76. // },
  77. // _mountVueApp: function(){
  78. // if (!this.vueApp) this.vueApp = this._createVueExtend();
  79. //
  80. // /**
  81. // * @summary Vue对象实例
  82. // * @see https://vuejs.org/
  83. // * @member {VueInstance}
  84. // */
  85. // this.vm = new Vue(this.vueApp).$mount(this.node);
  86. // },
  87. _createVueExtend: function(){
  88. // if (this.tmpVueData){
  89. // Object.keys(this.tmpVueData).each(function(k){
  90. // this.form.Macro.environment.data.check(k, this.tmpVueData[k]);
  91. // }.bind(this));
  92. // }
  93. if (this.vModels && this.vModels.length){
  94. this.vModels.forEach(function(m){
  95. if (!this.json.hasOwnProperty(m)) this.json[m] = "";
  96. }.bind(this));
  97. }
  98. var app = {};
  99. if (this.json.vueApp && this.json.vueApp.code) app = this.form.Macro.exec(this.json.vueApp.code, this);
  100. if (!app) app = {};
  101. if (app.data){
  102. var ty = o2.typeOf(app.data);
  103. switch (ty){
  104. case "object":
  105. Object.keys(app.data).each(function(k){
  106. this.json[k] = app.data[k];
  107. //this.form.Macro.environment.data.add(k, app.data[k]);
  108. }.bind(this));
  109. app.data = this.json;
  110. // app.data = this.json;
  111. // app.data = Object.merge(this.json, this.form.Macro.environment.data);
  112. break;
  113. case "function":
  114. var dataFun = app.data;
  115. var _slef = this;
  116. app.data = function(){
  117. var d = dataFun();
  118. //_self.form.Macro.environment.data.add(_self.json.id, d);
  119. Object.keys(d).each(function(k){
  120. _self.json[k] = d[k];
  121. //_self.form.Macro.environment.data.add(k, d[k]);
  122. });
  123. //var data = Object.merge(_slef.json);
  124. //return _self.form.Macro.environment.data;
  125. return _self.json;
  126. };
  127. break;
  128. }
  129. }else{
  130. //app.data = this.form.Macro.environment.data;
  131. app.data = this.json;
  132. }
  133. var _self = this;
  134. var mountedFun = app.mounted;
  135. app.mounted = function(){
  136. _self._afterMounted(this.$el);
  137. if (mountedFun && o2.typeOf(mountedFun)=="function") return mountedFun.apply(this);
  138. };
  139. this.appendVueWatch(app);
  140. return app;
  141. },
  142. // _afterMounted: function(el){
  143. // this.node = el;
  144. // this.node.set({
  145. // "id": this.json.id,
  146. // "MWFType": this.json.type
  147. // });
  148. // this._loadVueCss();
  149. // this._loadDomEvents();
  150. // this._afterLoaded();
  151. // this.fireEvent("postLoad");
  152. // this.fireEvent("load");
  153. // },
  154. // _loadVueCss: function(){
  155. // if (this.json.vueCss && this.json.vueCss.code){
  156. // this.styleNode = this.node.loadCssText(this.json.vueCss.code, {"notInject": true});
  157. // this.styleNode.inject(this.node, "before");
  158. // }
  159. // },
  160. // _filterHtml: function(html){
  161. // var tmp = new Element("div", {"html": html});
  162. // var nodes = tmp.querySelectorAll("*[v-model]");
  163. // this.tmpVueData = {};
  164. // nodes.forEach(function(node){
  165. // this.tmpVueData[node.get("v-model")] = "";
  166. // }.bind(this));
  167. // return html;
  168. // },
  169. _checkVmodel: function(text){
  170. // if (text){
  171. // this.vModels = [];
  172. // var reg = /(?:v-model)(?:.lazy|.number|.trim)?(?:\s*=\s*)(?:["'])?([^"']*)/g;
  173. // var arr;
  174. // while ((arr = reg.exec(text)) !== null) {
  175. // if (arr.length>1 && arr[1]){
  176. // var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  177. // modelId = (modelId) ? modelId+".."+arr[1] : arr[1];
  178. // this.json[arr[1]] = this._getBusinessData(modelId);
  179. // this.vModels.push(arr[1]);
  180. // }
  181. // }
  182. // }
  183. var nodes = this.node.querySelectorAll("*[v-model]");
  184. this.vModels = [];
  185. var arrs = ["el-checkbox-group"];
  186. nodes.forEach(function(node){
  187. var model = node.get("v-model");
  188. var tag = node.tagName.toString().toLowerCase();
  189. if (model){
  190. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  191. modelId = (modelId) ? modelId+".."+model : model;
  192. this.json[model] = this._getBusinessData(modelId);
  193. if (!this.json[model]){
  194. this.json[model] = (arrs.indexOf(tag)===-1) ? "" : []
  195. }
  196. this.vModels.push(model);
  197. }
  198. }.bind(this));
  199. },
  200. _createElementHtml: function(){
  201. var html = this.json.vueTemplate || "";
  202. // if (html) this._checkVmodel(html);
  203. // return this._filterHtml(html);
  204. return html;
  205. },
  206. resetData: function(){
  207. if (this.vModels && this.vModels.length){
  208. this.vModels.forEach(function(m){
  209. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  210. modelId = (modelId) ? modelId+".."+m : m;
  211. this.json[m] = this._getBusinessData(modelId);
  212. }.bind(this));
  213. }
  214. },
  215. });