Radio.js 16 KB

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