Radio.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. MWF.xDesktop.requireApp("process.Xform", "$Selector", null, false);
  2. MWF.require("MWF.widget.UUID", null, false);
  3. /** @class Radio 单选按钮。
  4. * @o2cn 单选按钮
  5. * @example
  6. * //可以在脚本中获取该组件
  7. * //方法1:
  8. * var field = this.form.get("fieldId"); //获取组件对象
  9. * //方法2
  10. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  11. *
  12. * var data = field.getData(); //获取值
  13. * field.setData("字符串值"); //设置值
  14. * field.hide(); //隐藏字段
  15. * var id = field.json.id; //获取字段标识
  16. * var flag = field.isEmpty(); //字段是否为空
  17. * @extends MWF.xApplication.process.Xform.$Selector
  18. * @o2category FormComponents
  19. * @o2range {Process|CMS|Portal}
  20. * @hideconstructor
  21. */
  22. MWF.xApplication.process.Xform.Radio = MWF.APPRadio = new Class(
  23. /** @lends MWF.xApplication.process.Xform.Radio# */
  24. {
  25. Implements: [Events],
  26. Extends: MWF.APP$Selector,
  27. /**
  28. * 组件加载后触发。如果选项加载为异步,则异步处理完成后触发此事件
  29. * @event MWF.xApplication.process.Xform.Radio#load
  30. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  31. */
  32. /**
  33. * 值改变时触发。可以通过this.event获取修改后的选择项(Dom对象)。
  34. * @event MWF.xApplication.process.Xform.Radio#change
  35. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  36. */
  37. /**
  38. * @ignore
  39. * @member {Element} descriptionNode
  40. * @memberOf MWF.xApplication.process.Xform.Radio#
  41. */
  42. loadDescription: function(){},
  43. _loadNode: function(){
  44. if (this.isReadonly()){
  45. this._loadNodeRead();
  46. }else{
  47. this._loadNodeEdit();
  48. }
  49. },
  50. _loadMergeReadContentNode: function( contentNode, data ){
  51. this._showValue(contentNode, data.data)
  52. },
  53. _loadNodeRead: function(){
  54. this.node.empty();
  55. this.node.set({
  56. "nodeId": this.json.id,
  57. "MWFType": this.json.type
  58. });
  59. var value = this.getValue();
  60. this._showValue(this.node, value);
  61. },
  62. __showValue: function(node, value, optionItems){
  63. if (value){
  64. var texts = "";
  65. for (var i=0; i<optionItems.length; i++){
  66. var item = optionItems[i];
  67. var tmps = item.split("|");
  68. var t = tmps[0];
  69. var v = tmps[1] || t;
  70. if (value == v){
  71. texts = t;
  72. break;
  73. }
  74. }
  75. node.set("text", texts);
  76. }
  77. },
  78. _resetNodeEdit: function(){
  79. var div = new Element("div");
  80. div.set(this.json.properties);
  81. div.inject(this.node, "after");
  82. this.node.destroy();
  83. this.node = div;
  84. },
  85. _loadNodeEdit: function(){
  86. //this.container = new Element("select");
  87. if (!this.json.preprocessing) this._resetNodeEdit();
  88. this.node.set({
  89. "id": this.json.id,
  90. "MWFType": this.json.type
  91. });
  92. if( this.json.recoveryStyles && this.json.recoveryStyles.display ){
  93. this.node.setStyle("display", this.json.recoveryStyles.display);
  94. }else if( this.json.styles && this.json.styles.display ){
  95. this.node.setStyle("display", this.json.styles.display);
  96. }else{
  97. this.node.setStyle("display", "inline");
  98. }
  99. this.setOptions();
  100. },
  101. _loadDomEvents: function(){
  102. },
  103. _loadEvents: function(){
  104. Object.each(this.json.events, function(e, key){
  105. if (e.code){
  106. if (this.options.moduleEvents.indexOf(key)!=-1){
  107. this.addEvent(key, function(event){
  108. return this.form.Macro.fire(e.code, this, event);
  109. }.bind(this));
  110. }else{
  111. //this.node.addEvent(key, function(event){
  112. // return this.form.Macro.fire(e.code, this, event);
  113. //}.bind(this));
  114. }
  115. }
  116. }.bind(this));
  117. },
  118. addModuleEvent: function(key, fun){
  119. if (this.options.moduleEvents.indexOf(key)!==-1){
  120. this.addEvent(key, function(event){
  121. return (fun) ? fun(this, event) : null;
  122. }.bind(this));
  123. }else{
  124. var inputs = this.node.getElements("input");
  125. inputs.each(function(input){
  126. input.addEvent(key, function(event){
  127. return (fun) ? fun(this, event) : null;
  128. }.bind(this));
  129. }.bind(this));
  130. }
  131. },
  132. isNumber : function( d ){
  133. return parseInt(d).toString() !== "NaN"
  134. },
  135. _setOptions: function(optionItems){
  136. var p = o2.promiseAll(optionItems).then(function(radioValues){
  137. this.moduleSelectAG = null;
  138. if (!radioValues) radioValues = [];
  139. var node;
  140. if (o2.typeOf(radioValues)==="array"){
  141. var flag = (new MWF.widget.UUID).toString();
  142. radioValues.each(function(item, i){
  143. var tmps = item.split("|");
  144. var text = tmps[0];
  145. var value = tmps[1] || text;
  146. if( !this.isNumber(this.json.countPerline) ) {
  147. if( this.json.newline ){
  148. node = new Element("div").inject(this.node);
  149. }else{
  150. node = this.node;
  151. }
  152. }else{
  153. var countPerLine = this.json.countPerline.toInt();
  154. if( countPerLine === 0 && i===0 ){
  155. node = new Element("div").inject(this.node);
  156. }else if( i % countPerLine === 0){
  157. node = new Element("div").inject(this.node);
  158. }
  159. }
  160. var radio = new Element("input", {
  161. "type": "radio",
  162. "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  163. "value": value,
  164. "showText": text,
  165. "styles": this.json.buttonStyles
  166. }).inject(node);
  167. //radio.appendText(text, "after");
  168. var textNode = new Element( "span", {
  169. "text" : text,
  170. "styles" : { "cursor" : "default" }
  171. }).inject(node);
  172. textNode.addEvent("click", function( ev ){
  173. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  174. this.radio.checked = true;
  175. this.radio.fireEvent("change", [this.radio]);
  176. this.radio.fireEvent("click");
  177. }.bind( {radio : radio} ) );
  178. radio.addEvent("click", function(){
  179. this.validationMode();
  180. if (this.validation()) {
  181. this._setBusinessData(this.getInputData("change"));
  182. this.fireEvent("change", [radio]);
  183. }
  184. }.bind(this));
  185. Object.each(this.json.events, function(e, key){
  186. if (e.code){
  187. if (this.options.moduleEvents.indexOf(key)!=-1){
  188. }else{
  189. radio.addEvent(key, function(event){
  190. return this.form.Macro.fire(e.code, this, event);
  191. }.bind(this));
  192. }
  193. }
  194. }.bind(this));
  195. }.bind(this));
  196. }
  197. }.bind(this), function(){
  198. this.moduleSelectAG = null;
  199. }.bind(this));
  200. this.moduleSelectAG = p;
  201. if (p) p.then(function(){
  202. this.moduleSelectAG = null;
  203. }.bind(this), function(){
  204. this.moduleSelectAG = null;
  205. }.bind(this));
  206. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(radioValues){
  207. // this.moduleSelectAG = null;
  208. //
  209. // if (!radioValues) radioValues = [];
  210. // if (o2.typeOf(radioValues)==="array"){
  211. // var flag = (new MWF.widget.UUID).toString();
  212. // radioValues.each(function(item){
  213. // var tmps = item.split("|");
  214. // var text = tmps[0];
  215. // var value = tmps[1] || text;
  216. //
  217. // var radio = new Element("input", {
  218. // "type": "radio",
  219. // "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  220. // "value": value,
  221. // "showText": text,
  222. // "styles": this.json.buttonStyles
  223. // }).inject(this.node);
  224. // //radio.appendText(text, "after");
  225. //
  226. // var textNode = new Element( "span", {
  227. // "text" : text,
  228. // "styles" : { "cursor" : "default" }
  229. // }).inject(this.node);
  230. // textNode.addEvent("click", function( ev ){
  231. // if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  232. // this.radio.checked = true;
  233. // this.radio.fireEvent("change");
  234. // this.radio.fireEvent("click");
  235. // }.bind( {radio : radio} ) );
  236. //
  237. // radio.addEvent("click", function(){
  238. // this.validationMode();
  239. // if (this.validation()) this._setBusinessData(this.getInputData("change"));
  240. // }.bind(this));
  241. //
  242. // Object.each(this.json.events, function(e, key){
  243. // if (e.code){
  244. // if (this.options.moduleEvents.indexOf(key)!=-1){
  245. // }else{
  246. // radio.addEvent(key, function(event){
  247. // return this.form.Macro.fire(e.code, this, event);
  248. // }.bind(this));
  249. // }
  250. // }
  251. // }.bind(this));
  252. // }.bind(this));
  253. // }
  254. // }.bind(this))
  255. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  256. // this.moduleSelectAG = null;
  257. // }.bind(this));
  258. },
  259. _setValue: function(value, m, fireChange){
  260. var mothed = m || "__setValue";
  261. if (!!value){
  262. var p = o2.promiseAll(value).then(function(v){
  263. if (o2.typeOf(v)=="array") v = v[0];
  264. if (this.moduleSelectAG){
  265. this.moduleValueAG = this.moduleSelectAG;
  266. this.moduleSelectAG.then(function(){
  267. this[mothed](v, fireChange);
  268. return v;
  269. }.bind(this), function(){});
  270. }else{
  271. this[mothed](v, fireChange)
  272. }
  273. return v;
  274. }.bind(this), function(){});
  275. this.moduleValueAG = p;
  276. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  277. this.moduleValueAG = null;
  278. }.bind(this), function(){
  279. this.moduleValueAG = null;
  280. }.bind(this));
  281. }else{
  282. this[mothed](value, fireChange);
  283. }
  284. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  285. // if (o2.typeOf(v)=="array") v = v[0];
  286. // if (this.moduleSelectAG){
  287. // this.moduleValueAG = this.moduleSelectAG;
  288. // this.moduleSelectAG.then(function(){
  289. // this.__setValue(v);
  290. // }.bind(this));
  291. // }else{
  292. // this.__setValue(v)
  293. // }
  294. // return v;
  295. // }.bind(this));
  296. //
  297. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  298. // this.moduleValueAG = null;
  299. // }.bind(this));
  300. },
  301. __setValue: function(value){
  302. this.moduleValueAG = null;
  303. this._setBusinessData(value);
  304. if (this.isReadonly()){
  305. this._loadNodeRead();
  306. }else{
  307. var radios = this.node.getElements("input");
  308. for (var i=0; i<radios.length; i++){
  309. var radio = radios[i];
  310. if (radio.value==value){
  311. radio.checked = true;
  312. break;
  313. }
  314. }
  315. }
  316. this.fieldModuleLoaded = true;
  317. },
  318. _getInputTextData: function(){
  319. var inputs = this.node.getElements("input");
  320. var value = "";
  321. var text = "";
  322. if (inputs.length){
  323. for (var i=0; i<inputs.length; i++){
  324. var input = inputs[i];
  325. if (input.checked){
  326. value = input.get("value");
  327. text = input.get("showText");
  328. break;
  329. }
  330. }
  331. }
  332. return {"value": [value || ""] , "text": [text || value || ""]};
  333. },
  334. /**
  335. * @summary 获取选中项的text。
  336. * @return {String} 返回选中项的text
  337. * @example
  338. * var text = this.form.get('fieldId').getText(); //获取选中项的文本
  339. */
  340. getText: function(){
  341. var d = this.getTextData();
  342. if( typeOf(d.then) === "function" ){
  343. return d.then(function( d1 ){
  344. var texts = d1.text;
  345. return (texts && texts.length) ? texts[0] : "";
  346. })
  347. }else{
  348. var texts = d.text;
  349. return (texts && texts.length) ? texts[0] : "";
  350. }
  351. },
  352. getInputData: function(){
  353. if (this.isReadonly()){
  354. return this._getBusinessData();
  355. }else{
  356. var inputs = this.node.getElements("input");
  357. var value = "";
  358. if (inputs.length){
  359. for (var i=0; i<inputs.length; i++){
  360. var input = inputs[i];
  361. if (input.checked){
  362. value = input.get("value");
  363. break;
  364. }
  365. }
  366. }
  367. return value;
  368. }
  369. },
  370. resetData: function(){
  371. this.setData(this.getValue());
  372. },
  373. /**
  374. * @summary 获取选中的Dom对象。
  375. * @return {Element} 返回选中的Dom对象
  376. * @example
  377. * var input = this.form.get('fieldId').getSelectedInput();
  378. */
  379. getSelectedInput: function(){
  380. var inputs = this.node.getElements("input");
  381. if (inputs.length){
  382. for (var i=0; i<inputs.length; i++){
  383. if (inputs[i].checked) return inputs[i];
  384. }
  385. }
  386. return null;
  387. },
  388. setData: function(data, fireChange){
  389. return this._setValue(data, "__setData", fireChange);
  390. // if (data && data.isAG){
  391. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  392. // if (o2.typeOf(v)=="array") v = v[0];
  393. // this.__setData(v);
  394. // }.bind(this));
  395. // }else{
  396. // this.__setData(data);
  397. // }
  398. // if (data && data.isAG){
  399. // this.moduleValueAG = data;
  400. // data.addResolve(function(v){
  401. // this.setData(v);
  402. // }.bind(this));
  403. // }else{
  404. // this.__setData(data);
  405. // this.moduleValueAG = null;
  406. // }
  407. },
  408. __setData: function(data, fireChange){
  409. this.moduleValueAG = null;
  410. var old = this.getInputData();
  411. this._setBusinessData(data);
  412. var inputs = this.node.getElements("input");
  413. if (inputs.length){
  414. for (var i=0; i<inputs.length; i++){
  415. if (data==inputs[i].get("value")){
  416. inputs[i].set("checked", true);
  417. }else{
  418. inputs[i].set("checked", false);
  419. }
  420. }
  421. this.validationMode();
  422. }
  423. this.fieldModuleLoaded = true;
  424. this.fireEvent("setData");
  425. if (fireChange && old!==data) this.fireEvent("change");
  426. },
  427. notValidationMode: function(text){
  428. if (!this.isNotValidationMode){
  429. this.isNotValidationMode = true;
  430. this.node.store("background", this.node.getStyles("background"));
  431. this.node.setStyle("background", "#ffdcdc");
  432. this.errNode = this.createErrorNode(text);
  433. if (this.iconNode){
  434. this.errNode.inject(this.iconNode, "after");
  435. }else{
  436. this.errNode.inject(this.node, "after");
  437. }
  438. this.showNotValidationMode(this.node);
  439. if (!this.errNode.isIntoView()) this.errNode.scrollIntoView(false);
  440. }
  441. },
  442. validationMode: function(routeName, opinion){
  443. // if (!this.validationConfig(routeName, opinion)) return false;
  444. if (this.isNotValidationMode){
  445. this.isNotValidationMode = false;
  446. this.node.setStyles(this.node.retrieve("background"));
  447. if (this.errNode){
  448. this.errNode.destroy();
  449. this.errNode = null;
  450. }
  451. }
  452. },
  453. getExcelData: function( type ){
  454. var value = this.getData();
  455. if( type === "value" )return value;
  456. var options = this.getOptionsObj();
  457. return Promise.resolve(options).then(function (opts) {
  458. var idx = opts.valueList.indexOf( value );
  459. var text = idx > -1 ? opts.textList[ idx ] : "";
  460. return text;
  461. });
  462. },
  463. setExcelData: function(d, type){
  464. var value = d.replace(/&#10;/g,""); //换行符&#10;
  465. this.excelData = value;
  466. if( type === "value" ){
  467. this.setData(value, true);
  468. }else{
  469. var options = this.getOptionsObj();
  470. this.moduleExcelAG = Promise.resolve(options).then(function (opts) {
  471. var idx = opts.textList.indexOf( value );
  472. value = idx > -1 ? opts.valueList[ idx ] : "";
  473. this.setData(value, true);
  474. this.moduleExcelAG = null;
  475. }.bind(this));
  476. }
  477. }
  478. });