Elradio.js 12 KB

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