Elcheckbox.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.Elradio
  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. if( !this.eventLoaded ){
  100. this._loadDomEvents();
  101. this.eventLoaded = true;
  102. }
  103. this.fireEvent("postLoad");
  104. if( this.moduleSelectAG && typeOf(this.moduleSelectAG.then) === "function" ){
  105. this.moduleSelectAG.then(function () {
  106. this.fireEvent("load");
  107. this.isLoaded = true;
  108. }.bind(this));
  109. }else{
  110. this.fireEvent("load");
  111. this.isLoaded = true;
  112. }
  113. },
  114. __showValue: function(node, value, optionItems){
  115. if (value){
  116. var texts = [];
  117. optionItems.each(function(item){
  118. var tmps = item.split("|");
  119. var t = tmps[0];
  120. var v = tmps[1] || t;
  121. if (value.indexOf(v)!=-1){
  122. texts.push(t || v);
  123. }
  124. });
  125. node.set("text", texts.join(", "));
  126. }
  127. },
  128. _resetNodeEdit: function(){
  129. var div = new Element("div");
  130. div.inject(this.node, "after");
  131. this.node.destroy();
  132. this.node = div;
  133. },
  134. _loadNodeEdit: function(){
  135. if (!this.json.preprocessing) this._resetNodeEdit();
  136. this.setOptions();
  137. },
  138. setOptions: function(){
  139. var optionItems = this.getOptions();
  140. this._setOptions(optionItems);
  141. },
  142. _setOptions: function(optionItems){
  143. var p = o2.promiseAll(optionItems).then(function(radioValues){
  144. //this.moduleSelectAG = null;
  145. return this._loadElradio(radioValues);
  146. }.bind(this), function(){
  147. this.moduleSelectAG = null;
  148. }.bind(this));
  149. this.moduleSelectAG = Promise.resolve(p);
  150. },
  151. _loadElradio: function(radioValues){
  152. return new Promise(function(resolve){
  153. if (radioValues && o2.typeOf(radioValues)==="array"){
  154. this.node.appendHTML(this._createElementHtml(radioValues), "before");
  155. var button = this.node.getPrevious();
  156. this.node.destroy();
  157. this.node = button;
  158. this.node.set({
  159. "id": this.json.id,
  160. "MWFType": this.json.type
  161. });
  162. this._createVueApp(resolve);
  163. }
  164. }.bind(this));
  165. },
  166. _createElementHtml: function(radioValues){
  167. var id = (this.json.id.indexOf("..")!==-1) ? this.json.id.replace(/\.\./g, "_") : this.json.id;
  168. this.json["$id"] = (id.indexOf("-")!==-1) ? id.replace(/-/g, "_") : id;
  169. var html = "<el-checkbox-group class='o2_vue' style='box-sizing: border-box!important'";
  170. html += " v-model=\""+this.json.$id+"\"";
  171. html += " :text-color=\"textColor\"";
  172. html += " :fill=\"fillColor\"";
  173. html += " :size=\"size\"";
  174. html += " @change=\"change\"";
  175. if (this.json.elGroupProperties){
  176. Object.keys(this.json.elGroupProperties).forEach(function(k){
  177. if (this.json.elGroupProperties[k]) html += " "+k+"=\""+this.json.elGroupProperties[k]+"\"";
  178. }, this);
  179. }
  180. // if (this.json.elGroupStyles){
  181. // var style = "";
  182. // Object.keys(this.json.elGroupStyles).forEach(function(k){
  183. // if (this.json.elGroupStyles[k]) style += k+":"+this.json.elGroupStyles[k]+";";
  184. // }, this);
  185. // html += " style=\""+style+"\"";
  186. // }
  187. if (this.json.elGroupStyles) html += " :style=\"elGroupStyles\"";
  188. html += " >";
  189. radioValues.each(function(item){
  190. var tmps = item.split("|");
  191. var text = tmps[0];
  192. var value = tmps[1] || text;
  193. html += (this.json.buttonRadio) ? "<el-checkbox-button" : "<el-checkbox";
  194. html += " label=\""+value+"\"";
  195. html += " :border=\"border\"";
  196. if (this.json.elProperties){
  197. Object.keys(this.json.elProperties).forEach(function(k){
  198. if (this.json.elProperties[k]) html += " "+k+"=\""+this.json.elProperties[k]+"\"";
  199. }, this);
  200. }
  201. // var radiostyle = "box-sizing: border-box!important;";
  202. // if (this.json.elStyles){
  203. // Object.keys(this.json.elStyles).forEach(function(k){
  204. // if (this.json.elStyles[k]) radiostyle += k+":"+this.json.elStyles[k]+";";
  205. // }, this);
  206. // }
  207. // html += " style=\""+radiostyle+"\"";
  208. if (this.json.elStyles) html += " :style=\"elStyles\"";
  209. html += " >"+text;
  210. html += (this.json.buttonRadio) ? "</el-checkbox-button>" : "</el-checkbox>";
  211. }.bind(this));
  212. html += "</el-checkbox-group>";
  213. return html;
  214. },
  215. _setValue: function(value, m){
  216. var mothed = m || "__setValue";
  217. if (!!value){
  218. var p = o2.promiseAll(value).then(function(v){
  219. //if (o2.typeOf(v)=="array") v = v[0];
  220. if (this.moduleSelectAG){
  221. this.moduleValueAG = this.moduleSelectAG;
  222. this.moduleSelectAG.then(function(){
  223. this[mothed](v);
  224. return v;
  225. }.bind(this), function(){});
  226. }else{
  227. this[mothed](v)
  228. }
  229. return v;
  230. }.bind(this), function(){});
  231. this.moduleValueAG = p;
  232. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  233. this.moduleValueAG = null;
  234. }.bind(this), function(){
  235. this.moduleValueAG = null;
  236. }.bind(this));
  237. }else{
  238. this[mothed](value);
  239. }
  240. },
  241. __setValue: function(value){
  242. this._setBusinessData(value);
  243. this.json[this.json.$id] = (value) ? value : [];
  244. },
  245. __setData: function(data){
  246. this._setBusinessData(data);
  247. this.json[this.json.$id] = data;
  248. this.validationMode();
  249. this.fireEvent("setData");
  250. },
  251. _createVueApp: function(callback){
  252. if (!this.vm){
  253. this._loadVue(function(){
  254. this._mountVueApp(callback);
  255. }.bind(this));
  256. }else{
  257. if (callback) callback();
  258. }
  259. },
  260. // _loadVue: function(callback){
  261. // if (!window.Vue){
  262. // var vue = (o2.session.isDebugger) ? "vue_develop" : "vue";
  263. // o2.loadAll({"css": "../o2_lib/vue/element/index.css", "js": [vue, "elementui"]}, { "sequence": true }, callback);
  264. // }else{
  265. // if (callback) callback();
  266. // }
  267. // },
  268. _mountVueApp: function(callback){
  269. if (!this.vueApp) this.vueApp = this._createVueExtend(callback);
  270. /**
  271. * @summary Vue对象实例
  272. * @see https://vuejs.org/
  273. * @member {VueInstance}
  274. */
  275. this.vm = new Vue(this.vueApp);
  276. this.vm.$mount(this.node);
  277. },
  278. _createVueExtend: function(callback){
  279. var _self = this;
  280. return {
  281. data: this._createVueData(),
  282. mounted: function(){
  283. _self._afterMounted(this.$el);
  284. if (callback) callback();
  285. },
  286. methods: {
  287. change: function(v){
  288. _self.validationMode();
  289. if (_self.validation()) {
  290. _self._setBusinessData(v || []);
  291. _self.fireEvent("change");
  292. }
  293. }
  294. }
  295. };
  296. },
  297. _createVueData: function(){
  298. if (this.json.$id===this.json.id) this.form.Macro.environment.data.check(this.json.$id);
  299. this.json[this.json.$id] = this._getBusinessData();
  300. if (!this.json[this.json.$id] || !this.json[this.json.$id].length) this.json[this.json.$id] = [];
  301. if (this.json.vueData && this.json.vueData.code){
  302. var d = this.form.Macro.exec(this.json.vueData.code, this);
  303. this.json = Object.merge(d, this.json);
  304. }
  305. if (!this.json.textColor) this.json.textColor = "";
  306. if (!this.json.fillColor) this.json.fillColor = "";
  307. if (!this.json.size) this.json.size = "";
  308. return this.json;
  309. },
  310. _afterMounted: function(el){
  311. this.node = el;
  312. this.node.set({
  313. "id": this.json.id,
  314. "MWFType": this.json.type
  315. });
  316. this._loadVueCss();
  317. this._loadDomEvents();
  318. this._afterLoaded();
  319. this.fireEvent("postLoad");
  320. this.fireEvent("load");
  321. },
  322. getInputData: function(){
  323. return this.json[this.json.$id];
  324. },
  325. /**
  326. * @summary 获取选中项的text。
  327. * @return {String[]} 返回选中项的text数组
  328. * @example
  329. * var texts = this.form.get('fieldId').getText(); //获取选中项的文本数组
  330. */
  331. getText: function(){
  332. var d = this.getTextData();
  333. if( typeOf(d.then) === "function" ){
  334. return d.then(function( d1 ){
  335. var texts = d1.text;
  336. return (texts && texts.length) ? texts : [];
  337. })
  338. }else{
  339. var texts = d.text;
  340. return (texts && texts.length) ? texts : [];
  341. }
  342. },
  343. _loadVueCss: function(){
  344. if (this.styleNode){
  345. this.node.removeClass(this.styleNode.get("id"));
  346. }
  347. if (this.json.vueCss && this.json.vueCss.code){
  348. this.styleNode = this.node.loadCssText(this.json.vueCss.code, {"notInject": true});
  349. this.styleNode.inject(this.node, "before");
  350. }
  351. },
  352. getExcelData: function( type ){
  353. var value = this.getData();
  354. if( type === "value" )return value;
  355. var options = this.getOptionsObj();
  356. return Promise.resolve(options).then(function (opts) {
  357. value = o2.typeOf(value) === "array" ? value : [value];
  358. var arr = [];
  359. value.each( function( a, i ){
  360. var idx = opts.valueList.indexOf( a );
  361. var text = idx > -1 ? opts.textList[ idx ] : "";
  362. if( !text )text = value;
  363. arr.push( text );
  364. });
  365. return arr.join(", ");
  366. });
  367. },
  368. setExcelData: function(d, type){
  369. var arr = this.stringToArray(d);
  370. this.excelData = arr;
  371. if( type === "value" ){
  372. this.setData(value, true);
  373. }else{
  374. var options = this.getOptionsObj();
  375. this.moduleExcelAG = Promise.resolve(options).then(function (opts) {
  376. arr.each( function( a, i ){
  377. var idx = opts.textList.indexOf( a );
  378. var v = idx > -1 ? opts.valueList[ idx ] : null;
  379. arr[ i ] = v || a;
  380. });
  381. arr.clean();
  382. this.json[this.json.$id] = arr;
  383. this._setBusinessData(arr);
  384. this.setData(arr, true);
  385. this.moduleExcelAG = null;
  386. }.bind(this));
  387. }
  388. }
  389. });