Elcheckbox.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. MWF.xDesktop.requireApp("process.Xform", "Elradio", null, false);
  2. /** @class Elradio 基于Element UI的多选组件。
  3. * @o2cn 多选组件
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var radio = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var radio = 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/checkbox|Element UI Checkbox 多选框}
  15. */
  16. MWF.xApplication.process.Xform.Elcheckbox = MWF.APPElcheckbox = new Class(
  17. /** @lends MWF.xApplication.process.Xform.Elcheckbox# */
  18. {
  19. Implements: [Events],
  20. Extends: MWF.APPElradio,
  21. options: {
  22. /**
  23. * 组件加载前触发。当前组件的queryLoad事件还没有在form里注册,通过this.form.get("fieldId")不能获取到当前组件,需要用this.target获取当前组件。
  24. * @event MWF.xApplication.process.Xform.Elcheckbox#queryLoad
  25. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  26. */
  27. /**
  28. * 组件加载后触发。如果选项加载为异步,则异步处理完成后触发此事件
  29. * @event MWF.xApplication.process.Xform.Elcheckbox#load
  30. */
  31. /**
  32. * 组件加载后触发.
  33. * @event MWF.xApplication.process.Xform.Elcheckbox#postLoad
  34. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  35. */
  36. "moduleEvents": ["load", "queryLoad", "postLoad", "change"],
  37. /**
  38. * 当绑定值变化时触发的事件。this.event[0]为更新后的值
  39. * @event MWF.xApplication.process.Xform.Elcheckbox#change
  40. * @see {@link https://element.eleme.cn/#/zh-CN/component/checkbox|多选框的Checkbox Events章节}
  41. */
  42. "elEvents": ["change"]
  43. },
  44. /**
  45. * @summary 组件的配置信息,同时也是Vue组件的data。
  46. * @member MWF.xApplication.process.Xform.Elradio#json {JsonObject}
  47. * @example
  48. * //可以在脚本中获取此对象,下面两行代码是等价的,它们获取的是同一个对象
  49. * var json = this.form.get("elcheckbox").json; //获取组件的json对象
  50. * var json = this.form.get("elcheckbox").vm.$data; //获取Vue组件的data数据,
  51. *
  52. * //通过json对象操作Element组件
  53. * json.border = true; //带边框的多选按钮
  54. * json.size = "small"; //设置多选按钮大小,仅border为true时有效
  55. * json.textColor = "#ff0000"; //设置按钮样式时,选中状态的文本颜色
  56. * json.fillColor = "#ff0000"; //设置按钮样式时,选中状态的背景颜色
  57. */
  58. load: function(){
  59. this._loadModuleEvents();
  60. if (this.fireEvent("queryLoad")){
  61. this._queryLoaded();
  62. this._loadUserInterface();
  63. }
  64. },
  65. _loadNode: function(){
  66. this.node.empty();
  67. if (this.isReadonly()){
  68. this._loadNodeRead();
  69. }else{
  70. this._loadNodeEdit();
  71. }
  72. },
  73. _loadMergeReadContentNode: function( contentNode, data ){
  74. this._showValue(contentNode, data.data)
  75. },
  76. _loadMergeEditNodeByDefault: function(){
  77. var data = this.getSortedSectionData();
  78. var businessData = [];
  79. data.each(function(d){
  80. businessData = businessData.concat( d.data || [] );
  81. });
  82. this._setBusinessData( businessData );
  83. this._loadNode();
  84. },
  85. _loadNodeRead: function(){
  86. this.node.empty();
  87. this.node.set({
  88. "nodeId": this.json.id,
  89. "MWFType": this.json.type
  90. });
  91. var value = this.getValue();
  92. this._showValue(this.node, value);
  93. if( this.json.elProperties ){
  94. this.node.set(this.json.elProperties );
  95. }
  96. if (this.json.elStyles){
  97. this.node.setStyles( this._parseStyles(this.json.elStyles) );
  98. }
  99. this.fireEvent("postLoad");
  100. if( this.moduleSelectAG && typeOf(this.moduleSelectAG.then) === "function" ){
  101. this.moduleSelectAG.then(function () {
  102. this.fireEvent("load");
  103. this.isLoaded = true;
  104. }.bind(this));
  105. }else{
  106. this.fireEvent("load");
  107. this.isLoaded = true;
  108. }
  109. },
  110. __showValue: function(node, value, optionItems){
  111. debugger;
  112. if (value){
  113. var texts = [];
  114. optionItems.each(function(item){
  115. var tmps = item.split("|");
  116. var t = tmps[0];
  117. var v = tmps[1] || t;
  118. if (value.indexOf(v)!=-1){
  119. texts.push(t || v);
  120. }
  121. });
  122. node.set("text", texts.join(", "));
  123. }
  124. },
  125. _resetNodeEdit: function(){
  126. var div = new Element("div");
  127. div.inject(this.node, "after");
  128. this.node.destroy();
  129. this.node = div;
  130. },
  131. _loadNodeEdit: function(){
  132. if (!this.json.preprocessing) this._resetNodeEdit();
  133. this.setOptions();
  134. },
  135. setOptions: function(){
  136. var optionItems = this.getOptions();
  137. this._setOptions(optionItems);
  138. },
  139. _setOptions: function(optionItems){
  140. var p = o2.promiseAll(optionItems).then(function(radioValues){
  141. //this.moduleSelectAG = null;
  142. return this._loadElradio(radioValues);
  143. }.bind(this), function(){
  144. this.moduleSelectAG = null;
  145. }.bind(this));
  146. this.moduleSelectAG = Promise.resolve(p);
  147. },
  148. _loadElradio: function(radioValues){
  149. return new Promise(function(resolve){
  150. if (radioValues && o2.typeOf(radioValues)==="array"){
  151. this.node.appendHTML(this._createElementHtml(radioValues), "before");
  152. var button = this.node.getPrevious();
  153. this.node.destroy();
  154. this.node = button;
  155. this.node.set({
  156. "id": this.json.id,
  157. "MWFType": this.json.type
  158. });
  159. this._createVueApp(resolve);
  160. }
  161. }.bind(this));
  162. },
  163. _createElementHtml: function(radioValues){
  164. var id = (this.json.id.indexOf("..")!==-1) ? this.json.id.replace(/\.\./g, "_") : this.json.id;
  165. this.json["$id"] = (id.indexOf("-")!==-1) ? id.replace(/-/g, "_") : id;
  166. var html = "<el-checkbox-group class='o2_vue' style='box-sizing: border-box!important'";
  167. html += " v-model=\""+this.json.$id+"\"";
  168. html += " :text-color=\"textColor\"";
  169. html += " :fill=\"fillColor\"";
  170. html += " :size=\"size\"";
  171. html += " @change=\"change\"";
  172. if (this.json.elGroupProperties){
  173. Object.keys(this.json.elGroupProperties).forEach(function(k){
  174. if (this.json.elGroupProperties[k]) html += " "+k+"=\""+this.json.elGroupProperties[k]+"\"";
  175. }, this);
  176. }
  177. // if (this.json.elGroupStyles){
  178. // var style = "";
  179. // Object.keys(this.json.elGroupStyles).forEach(function(k){
  180. // if (this.json.elGroupStyles[k]) style += k+":"+this.json.elGroupStyles[k]+";";
  181. // }, this);
  182. // html += " style=\""+style+"\"";
  183. // }
  184. if (this.json.elGroupStyles) html += " :style=\"elGroupStyles\"";
  185. html += " >";
  186. radioValues.each(function(item){
  187. var tmps = item.split("|");
  188. var text = tmps[0];
  189. var value = tmps[1] || text;
  190. html += (this.json.buttonRadio) ? "<el-checkbox-button" : "<el-checkbox";
  191. html += " label=\""+value+"\"";
  192. html += " :border=\"border\"";
  193. if (this.json.elProperties){
  194. Object.keys(this.json.elProperties).forEach(function(k){
  195. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  196. }, this);
  197. }
  198. // var radiostyle = "box-sizing: border-box!important;";
  199. // if (this.json.elStyles){
  200. // Object.keys(this.json.elStyles).forEach(function(k){
  201. // if (this.json.elStyles[k]) radiostyle += k+":"+this.json.elStyles[k]+";";
  202. // }, this);
  203. // }
  204. // html += " style=\""+radiostyle+"\"";
  205. if (this.json.elStyles) html += " :style=\"elStyles\"";
  206. html += " >"+text;
  207. html += (this.json.buttonRadio) ? "</el-checkbox-button>" : "</el-checkbox>";
  208. }.bind(this));
  209. html += "</el-checkbox-group>";
  210. return html;
  211. },
  212. _setValue: function(value, m){
  213. var mothed = m || "__setValue";
  214. if (!!value){
  215. var p = o2.promiseAll(value).then(function(v){
  216. //if (o2.typeOf(v)=="array") v = v[0];
  217. if (this.moduleSelectAG){
  218. this.moduleValueAG = this.moduleSelectAG;
  219. this.moduleSelectAG.then(function(){
  220. this[mothed](v);
  221. return v;
  222. }.bind(this), function(){});
  223. }else{
  224. this[mothed](v)
  225. }
  226. return v;
  227. }.bind(this), function(){});
  228. this.moduleValueAG = p;
  229. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  230. this.moduleValueAG = null;
  231. }.bind(this), function(){
  232. this.moduleValueAG = null;
  233. }.bind(this));
  234. }else{
  235. this[mothed](value);
  236. }
  237. },
  238. __setValue: function(value){
  239. this._setBusinessData(value);
  240. this.json[this.json.$id] = (value) ? value : [];
  241. },
  242. __setData: function(data){
  243. this._setBusinessData(data);
  244. this.json[this.json.$id] = data;
  245. this.validationMode();
  246. this.fireEvent("setData");
  247. },
  248. _createVueApp: function(callback){
  249. if (!this.vm){
  250. this._loadVue(function(){
  251. this._mountVueApp(callback);
  252. }.bind(this));
  253. }else{
  254. if (callback) callback();
  255. }
  256. },
  257. // _loadVue: function(callback){
  258. // if (!window.Vue){
  259. // var vue = (o2.session.isDebugger) ? "vue_develop" : "vue";
  260. // o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": [vue, "elementui"]}, { "sequence": true }, callback);
  261. // }else{
  262. // if (callback) callback();
  263. // }
  264. // },
  265. _mountVueApp: function(callback){
  266. if (!this.vueApp) this.vueApp = this._createVueExtend(callback);
  267. /**
  268. * @summary Vue对象实例
  269. * @see https://vuejs.org/
  270. * @member {VueInstance}
  271. */
  272. this.vm = new Vue(this.vueApp);
  273. this.vm.$mount(this.node);
  274. },
  275. _createVueExtend: function(callback){
  276. var _self = this;
  277. return {
  278. data: this._createVueData(),
  279. mounted: function(){
  280. _self._afterMounted(this.$el);
  281. if (callback) callback();
  282. },
  283. methods: {
  284. change: function(v){
  285. _self.validationMode();
  286. if (_self.validation()) {
  287. _self._setBusinessData(v || []);
  288. _self.fireEvent("change");
  289. }
  290. }
  291. }
  292. };
  293. },
  294. _createVueData: function(){
  295. if (this.json.$id===this.json.id) this.form.Macro.environment.data.check(this.json.$id);
  296. this.json[this.json.$id] = this._getBusinessData();
  297. if (!this.json[this.json.$id] || !this.json[this.json.$id].length) this.json[this.json.$id] = [];
  298. if (this.json.vueData && this.json.vueData.code){
  299. var d = this.form.Macro.exec(this.json.vueData.code, this);
  300. this.json = Object.merge(d, this.json);
  301. }
  302. if (!this.json.textColor) this.json.textColor = "";
  303. if (!this.json.fillColor) this.json.fillColor = "";
  304. if (!this.json.size) this.json.size = "";
  305. return this.json;
  306. },
  307. _afterMounted: function(el){
  308. this.node = el;
  309. this.node.set({
  310. "id": this.json.id,
  311. "MWFType": this.json.type
  312. });
  313. this._loadVueCss();
  314. this._loadDomEvents();
  315. this._afterLoaded();
  316. this.fireEvent("postLoad");
  317. this.fireEvent("load");
  318. },
  319. getInputData: function(){
  320. return this.json[this.json.$id];
  321. },
  322. _loadVueCss: function(){
  323. if (this.styleNode){
  324. this.node.removeClass(this.styleNode.get("id"));
  325. }
  326. if (this.json.vueCss && this.json.vueCss.code){
  327. this.styleNode = this.node.loadCssText(this.json.vueCss.code, {"notInject": true});
  328. this.styleNode.inject(this.node, "before");
  329. }
  330. },
  331. getExcelData: function( type ){
  332. var value = this.getData();
  333. if( type === "value" )return value;
  334. var options = this.getOptionsObj();
  335. return Promise.resolve(options).then(function (opts) {
  336. value = o2.typeOf(value) === "array" ? value : [value];
  337. var arr = [];
  338. value.each( function( a, i ){
  339. var idx = opts.valueList.indexOf( a );
  340. var text = idx > -1 ? opts.textList[ idx ] : "";
  341. if( !text )text = value;
  342. arr.push( text );
  343. });
  344. return arr.join(", ");
  345. });
  346. },
  347. setExcelData: function(d, type){
  348. var arr = this.stringToArray(d);
  349. this.excelData = arr;
  350. if( type === "value" ){
  351. this.setData(value, true);
  352. }else{
  353. var options = this.getOptionsObj();
  354. this.moduleExcelAG = Promise.resolve(options).then(function (opts) {
  355. arr.each( function( a, i ){
  356. var idx = opts.textList.indexOf( a );
  357. var v = idx > -1 ? opts.valueList[ idx ] : null;
  358. arr[ i ] = v || a;
  359. });
  360. arr.clean();
  361. this.json[this.json.$id] = arr;
  362. this._setBusinessData(arr);
  363. this.setData(arr, true);
  364. this.moduleExcelAG = null;
  365. }.bind(this));
  366. }
  367. }
  368. });