Elradio.js 14 KB

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