Radio.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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} 返回选择项数组,如果使用选择项脚本,根据脚本返回决定
  129. * @example
  130. * this.form.get('fieldId').getOptions();
  131. */
  132. getOptions: function(){
  133. if (this.json.itemType == "values"){
  134. return this.json.itemValues;
  135. }else{
  136. return this.form.Macro.exec(this.json.itemScript.code, this);
  137. }
  138. return [];
  139. },
  140. setOptions: function(){
  141. var optionItems = this.getOptions();
  142. this._setOptions(optionItems);
  143. },
  144. _setOptions: function(optionItems){
  145. var p = o2.promiseAll(optionItems).then(function(radioValues){
  146. this.moduleSelectAG = null;
  147. if (!radioValues) radioValues = [];
  148. if (o2.typeOf(radioValues)==="array"){
  149. var flag = (new MWF.widget.UUID).toString();
  150. radioValues.each(function(item){
  151. var tmps = item.split("|");
  152. var text = tmps[0];
  153. var value = tmps[1] || text;
  154. var radio = new Element("input", {
  155. "type": "radio",
  156. "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  157. "value": value,
  158. "showText": text,
  159. "styles": this.json.buttonStyles
  160. }).inject(this.node);
  161. //radio.appendText(text, "after");
  162. var textNode = new Element( "span", {
  163. "text" : text,
  164. "styles" : { "cursor" : "default" }
  165. }).inject(this.node);
  166. textNode.addEvent("click", function( ev ){
  167. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  168. this.radio.checked = true;
  169. this.radio.fireEvent("change");
  170. this.radio.fireEvent("click");
  171. }.bind( {radio : radio} ) );
  172. radio.addEvent("click", function(){
  173. this.validationMode();
  174. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  175. }.bind(this));
  176. Object.each(this.json.events, function(e, key){
  177. if (e.code){
  178. if (this.options.moduleEvents.indexOf(key)!=-1){
  179. }else{
  180. radio.addEvent(key, function(event){
  181. return this.form.Macro.fire(e.code, this, event);
  182. }.bind(this));
  183. }
  184. }
  185. }.bind(this));
  186. }.bind(this));
  187. }
  188. }.bind(this), function(){
  189. this.moduleSelectAG = null;
  190. }.bind(this));
  191. this.moduleSelectAG = p;
  192. if (p) p.then(function(){
  193. this.moduleSelectAG = null;
  194. }.bind(this), function(){
  195. this.moduleSelectAG = null;
  196. }.bind(this));
  197. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(radioValues){
  198. // this.moduleSelectAG = null;
  199. //
  200. // if (!radioValues) radioValues = [];
  201. // if (o2.typeOf(radioValues)==="array"){
  202. // var flag = (new MWF.widget.UUID).toString();
  203. // radioValues.each(function(item){
  204. // var tmps = item.split("|");
  205. // var text = tmps[0];
  206. // var value = tmps[1] || text;
  207. //
  208. // var radio = new Element("input", {
  209. // "type": "radio",
  210. // "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  211. // "value": value,
  212. // "showText": text,
  213. // "styles": this.json.buttonStyles
  214. // }).inject(this.node);
  215. // //radio.appendText(text, "after");
  216. //
  217. // var textNode = new Element( "span", {
  218. // "text" : text,
  219. // "styles" : { "cursor" : "default" }
  220. // }).inject(this.node);
  221. // textNode.addEvent("click", function( ev ){
  222. // if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  223. // this.radio.checked = true;
  224. // this.radio.fireEvent("change");
  225. // this.radio.fireEvent("click");
  226. // }.bind( {radio : radio} ) );
  227. //
  228. // radio.addEvent("click", function(){
  229. // this.validationMode();
  230. // if (this.validation()) this._setBusinessData(this.getInputData("change"));
  231. // }.bind(this));
  232. //
  233. // Object.each(this.json.events, function(e, key){
  234. // if (e.code){
  235. // if (this.options.moduleEvents.indexOf(key)!=-1){
  236. // }else{
  237. // radio.addEvent(key, function(event){
  238. // return this.form.Macro.fire(e.code, this, event);
  239. // }.bind(this));
  240. // }
  241. // }
  242. // }.bind(this));
  243. // }.bind(this));
  244. // }
  245. // }.bind(this))
  246. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  247. // this.moduleSelectAG = null;
  248. // }.bind(this));
  249. },
  250. _setValue: function(value, m){
  251. var mothed = m || "__setValue";
  252. if (!!value){
  253. var p = o2.promiseAll(value).then(function(v){
  254. if (o2.typeOf(v)=="array") v = v[0];
  255. if (this.moduleSelectAG){
  256. this.moduleValueAG = this.moduleSelectAG;
  257. this.moduleSelectAG.then(function(){
  258. this[mothed](v);
  259. return v;
  260. }.bind(this), function(){});
  261. }else{
  262. this[mothed](v)
  263. }
  264. return v;
  265. }.bind(this), function(){});
  266. this.moduleValueAG = p;
  267. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  268. this.moduleValueAG = null;
  269. }.bind(this), function(){
  270. this.moduleValueAG = null;
  271. }.bind(this));
  272. }else{
  273. this[mothed](value);
  274. }
  275. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  276. // if (o2.typeOf(v)=="array") v = v[0];
  277. // if (this.moduleSelectAG){
  278. // this.moduleValueAG = this.moduleSelectAG;
  279. // this.moduleSelectAG.then(function(){
  280. // this.__setValue(v);
  281. // }.bind(this));
  282. // }else{
  283. // this.__setValue(v)
  284. // }
  285. // return v;
  286. // }.bind(this));
  287. //
  288. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  289. // this.moduleValueAG = null;
  290. // }.bind(this));
  291. },
  292. __setValue: function(value){
  293. this._setBusinessData(value);
  294. var radios = this.node.getElements("input");
  295. for (var i=0; i<radios.length; i++){
  296. var radio = radios[i];
  297. if (radio.value==value){
  298. radio.checked = true;
  299. break;
  300. }
  301. }
  302. },
  303. /**
  304. * @summary 获取选中项的value和text。
  305. * @return {Object} 返回选中项的value和text,如:
  306. * <pre><code class='language-js'>{"value": ["male"], "text": ["男"]}
  307. * {"value": [""], "text": [""]}
  308. * </code></pre>
  309. * @example
  310. * var data = this.form.get('fieldId').getTextData();
  311. * var text = data.text[0] //获取选中项的文本
  312. */
  313. getTextData: function(){
  314. var inputs = this.node.getElements("input");
  315. var value = "";
  316. var text = "";
  317. if (inputs.length){
  318. for (var i=0; i<inputs.length; i++){
  319. var input = inputs[i];
  320. if (input.checked){
  321. value = input.get("value");
  322. text = input.get("showText");
  323. break;
  324. }
  325. }
  326. }
  327. return {"value": [value] || "", "text": [text || value || ""]};
  328. },
  329. getInputData: function(){
  330. if (this.readonly || this.json.isReadonly ){
  331. return this._getBusinessData();
  332. }else{
  333. var inputs = this.node.getElements("input");
  334. var value = "";
  335. if (inputs.length){
  336. for (var i=0; i<inputs.length; i++){
  337. var input = inputs[i];
  338. if (input.checked){
  339. value = input.get("value");
  340. break;
  341. }
  342. }
  343. }
  344. return value;
  345. }
  346. },
  347. resetData: function(){
  348. this.setData(this.getValue());
  349. },
  350. /**
  351. * @summary 获取选中的Dom对象。
  352. * @return {Element} 返回选中的Dom对象
  353. * @example
  354. * var input = this.form.get('fieldId').getSelectedInput();
  355. */
  356. getSelectedInput: function(){
  357. var inputs = this.node.getElements("input");
  358. if (inputs.length){
  359. for (var i=0; i<inputs.length; i++){
  360. if (inputs[i].checked) return inputs[i];
  361. }
  362. }
  363. return null;
  364. },
  365. setData: function(data){
  366. return this._setValue(data, "__setData");
  367. // if (data && data.isAG){
  368. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  369. // if (o2.typeOf(v)=="array") v = v[0];
  370. // this.__setData(v);
  371. // }.bind(this));
  372. // }else{
  373. // this.__setData(data);
  374. // }
  375. // if (data && data.isAG){
  376. // this.moduleValueAG = data;
  377. // data.addResolve(function(v){
  378. // this.setData(v);
  379. // }.bind(this));
  380. // }else{
  381. // this.__setData(data);
  382. // this.moduleValueAG = null;
  383. // }
  384. },
  385. __setData: function(data){
  386. this._setBusinessData(data);
  387. var inputs = this.node.getElements("input");
  388. if (inputs.length){
  389. for (var i=0; i<inputs.length; i++){
  390. if (data==inputs[i].get("value")){
  391. inputs[i].set("checked", true);
  392. }else{
  393. inputs[i].set("checked", false);
  394. }
  395. }
  396. this.validationMode();
  397. }
  398. this.fireEvent("setData");
  399. },
  400. notValidationMode: function(text){
  401. if (!this.isNotValidationMode){
  402. this.isNotValidationMode = true;
  403. this.node.store("background", this.node.getStyles("background"));
  404. this.node.setStyle("background", "#ffdcdc");
  405. this.errNode = this.createErrorNode(text);
  406. if (this.iconNode){
  407. this.errNode.inject(this.iconNode, "after");
  408. }else{
  409. this.errNode.inject(this.node, "after");
  410. }
  411. this.showNotValidationMode(this.node);
  412. if (!this.node.isIntoView()) this.node.scrollIntoView();
  413. }
  414. },
  415. validationMode: function(routeName, opinion){
  416. if (!this.validationConfig(routeName, opinion)) return false;
  417. if (this.isNotValidationMode){
  418. this.isNotValidationMode = false;
  419. this.node.setStyles(this.node.retrieve("background"));
  420. if (this.errNode){
  421. this.errNode.destroy();
  422. this.errNode = null;
  423. }
  424. }
  425. }
  426. });