Elcommon.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 o2.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. // debugger;
  40. // this.node = $(node);
  41. // this.node.store("module", this);
  42. // this.json = json;
  43. // this.form = form;
  44. // this.field = true;
  45. // this.parentLine = null;
  46. // },
  47. _checkVueHtml: function(){
  48. var nodes = this.node.querySelectorAll("*[v-model]");
  49. this.tmpVueData = {};
  50. var arrs = ["el-checkbox-group"];
  51. nodes.forEach(function(node){
  52. var model = node.get("v-model");
  53. if (model) this.tmpVueData[model] = (arrs.indexOf(node.tagName.toString().toLowerCase())===-1) ? "" : [];
  54. }.bind(this));
  55. },
  56. _loadUserInterface: function(){
  57. this.node.set("html", this._createElementHtml());
  58. //this._checkVueHtml();
  59. this._checkVmodel();
  60. this.node.set({
  61. "id": this.json.id,
  62. "MWFType": this.json.type
  63. });
  64. this.node.addClass("o2_vue");
  65. this._createVueApp();
  66. },
  67. // _createVueApp: function(){
  68. // if (!this.vm) this._loadVue(this._mountVueApp.bind(this));
  69. // },
  70. //
  71. // _loadVue: function(callback){
  72. // if (!window.Vue){
  73. // o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": ["vue", "elementui"]}, { "sequence": true }, callback);
  74. // }else{
  75. // if (callback) callback();
  76. // }
  77. // },
  78. // _mountVueApp: function(){
  79. // if (!this.vueApp) this.vueApp = this._createVueExtend();
  80. //
  81. // /**
  82. // * @summary Vue对象实例
  83. // * @see https://vuejs.org/
  84. // * @member {VueInstance}
  85. // */
  86. // this.vm = new Vue(this.vueApp).$mount(this.node);
  87. // },
  88. _createVueExtend: function(){
  89. // if (this.tmpVueData){
  90. // Object.keys(this.tmpVueData).each(function(k){
  91. // this.form.Macro.environment.data.check(k, this.tmpVueData[k]);
  92. // }.bind(this));
  93. // }
  94. if (this.vModels && this.vModels.length){
  95. this.vModels.forEach(function(m){
  96. if (!this.json.hasOwnProperty(m)) this.json[m] = "";
  97. }.bind(this));
  98. }
  99. var app = {};
  100. if (this.json.vueApp && this.json.vueApp.code) app = this.form.Macro.exec(this.json.vueApp.code, this);
  101. if (!app) app = {};
  102. if (app.data){
  103. var ty = o2.typeOf(app.data);
  104. switch (ty){
  105. case "object":
  106. Object.keys(app.data).each(function(k){
  107. this.json[k] = app.data[k];
  108. //this.form.Macro.environment.data.add(k, app.data[k]);
  109. }.bind(this));
  110. app.data = this.json;
  111. // app.data = this.json;
  112. // app.data = Object.merge(this.json, this.form.Macro.environment.data);
  113. break;
  114. case "function":
  115. var dataFun = app.data;
  116. var _slef = this;
  117. app.data = function(){
  118. var d = dataFun();
  119. //_self.form.Macro.environment.data.add(_self.json.id, d);
  120. Object.keys(d).each(function(k){
  121. _self.json[k] = d[k];
  122. //_self.form.Macro.environment.data.add(k, d[k]);
  123. });
  124. //var data = Object.merge(_slef.json);
  125. //return _self.form.Macro.environment.data;
  126. return _self.json;
  127. };
  128. break;
  129. }
  130. }else{
  131. //app.data = this.form.Macro.environment.data;
  132. app.data = this.json;
  133. }
  134. var _self = this;
  135. var mountedFun = app.mounted;
  136. app.mounted = function(){
  137. _self._afterMounted(this.$el);
  138. if (mountedFun && o2.typeOf(mountedFun)=="function") return mountedFun.apply(this);
  139. };
  140. this.appendVueWatch(app);
  141. return app;
  142. },
  143. // _afterMounted: function(el){
  144. // this.node = el;
  145. // this.node.set({
  146. // "id": this.json.id,
  147. // "MWFType": this.json.type
  148. // });
  149. // this._loadVueCss();
  150. // this._loadDomEvents();
  151. // this._afterLoaded();
  152. // this.fireEvent("postLoad");
  153. // this.fireEvent("load");
  154. // },
  155. // _loadVueCss: function(){
  156. // if (this.json.vueCss && this.json.vueCss.code){
  157. // this.styleNode = this.node.loadCssText(this.json.vueCss.code, {"notInject": true});
  158. // this.styleNode.inject(this.node, "before");
  159. // }
  160. // },
  161. // _filterHtml: function(html){
  162. // var tmp = new Element("div", {"html": html});
  163. // var nodes = tmp.querySelectorAll("*[v-model]");
  164. // this.tmpVueData = {};
  165. // nodes.forEach(function(node){
  166. // this.tmpVueData[node.get("v-model")] = "";
  167. // }.bind(this));
  168. // return html;
  169. // },
  170. _checkVmodel: function(text){
  171. // if (text){
  172. // this.vModels = [];
  173. // var reg = /(?:v-model)(?:.lazy|.number|.trim)?(?:\s*=\s*)(?:["'])?([^"']*)/g;
  174. // var arr;
  175. // while ((arr = reg.exec(text)) !== null) {
  176. // if (arr.length>1 && arr[1]){
  177. // var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  178. // modelId = (modelId) ? modelId+".."+arr[1] : arr[1];
  179. // this.json[arr[1]] = this._getBusinessData(modelId);
  180. // this.vModels.push(arr[1]);
  181. // }
  182. // }
  183. // }
  184. var nodes = this.node.querySelectorAll("*[v-model]");
  185. this.vModels = [];
  186. var arrs = ["el-checkbox-group"];
  187. nodes.forEach(function(node){
  188. var model = node.get("v-model");
  189. var tag = node.tagName.toString().toLowerCase();
  190. if (model){
  191. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  192. modelId = (modelId) ? modelId+".."+model : model;
  193. this.json[model] = this._getBusinessData(modelId);
  194. if (!this.json[model]){
  195. this.json[model] = (arrs.indexOf(tag)===-1) ? "" : []
  196. }
  197. this.vModels.push(model);
  198. }
  199. }.bind(this));
  200. },
  201. _createElementHtml: function(){
  202. var html = this.json.vueTemplate || "";
  203. // if (html) this._checkVmodel(html);
  204. // return this._filterHtml(html);
  205. return html;
  206. },
  207. resetData: function(){
  208. if (this.vModels && this.vModels.length){
  209. this.vModels.forEach(function(m){
  210. var modelId = this.json.id.substring(0, this.json.id.lastIndexOf(".."));
  211. modelId = (modelId) ? modelId+".."+m : m;
  212. this.json[m] = this._getBusinessData(modelId);
  213. }.bind(this));
  214. }
  215. },
  216. });