Radio.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.xApplication.process.Xform.Radio = MWF.APPRadio = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Input,
  5. loadDescription: function(){},
  6. _loadNode: function(){
  7. if (this.readonly){
  8. this._loadNodeRead();
  9. }else{
  10. this._loadNodeEdit();
  11. }
  12. },
  13. _loadNodeRead: function(){
  14. this.node.empty();
  15. var radioValues = this.getOptions();
  16. var value = this.getValue();
  17. if (value){
  18. var texts = "";
  19. for (var i=0; i<radioValues.length; i++){
  20. var item = radioValues[i];
  21. var tmps = item.split("|");
  22. var t = tmps[0];
  23. var v = tmps[1] || t;
  24. // if (value.indexOf(v)!=-1){
  25. // texts = t;
  26. // break;
  27. // }
  28. if (value == v){
  29. texts = t;
  30. break;
  31. }
  32. }
  33. this.node.set("text", texts);
  34. }
  35. },
  36. _loadNodeEdit: function(){
  37. //this.container = new Element("select");
  38. var div = new Element("div");
  39. div.set(this.json.properties);
  40. div.inject(this.node, "after");
  41. this.node.destroy();
  42. this.node = div;
  43. this.node.set({
  44. "id": this.json.id,
  45. "MWFType": this.json.type,
  46. "styles": {
  47. "display": "inline"
  48. }
  49. });
  50. this.setOptions();
  51. },
  52. _loadEvents: function(){
  53. Object.each(this.json.events, function(e, key){
  54. if (e.code){
  55. if (this.options.moduleEvents.indexOf(key)!=-1){
  56. this.addEvent(key, function(event){
  57. return this.form.Macro.fire(e.code, this, event);
  58. }.bind(this));
  59. }else{
  60. //this.node.addEvent(key, function(event){
  61. // return this.form.Macro.fire(e.code, this, event);
  62. //}.bind(this));
  63. }
  64. }
  65. }.bind(this));
  66. },
  67. resetOption: function(){
  68. this.node.empty();
  69. this.setOptions();
  70. },
  71. getOptions: function(){
  72. if (this.json.itemType == "values"){
  73. return this.json.itemValues;
  74. }else{
  75. return this.form.Macro.exec(this.json.itemScript.code, this);
  76. }
  77. return [];
  78. },
  79. setOptions: function(){
  80. var radioValues = this.getOptions();
  81. if (!radioValues) radioValues = [];
  82. if (o2.typeOf(radioValues)==="array"){
  83. var flag = (new MWF.widget.UUID).toString();
  84. radioValues.each(function(item){
  85. var tmps = item.split("|");
  86. var text = tmps[0];
  87. var value = tmps[1] || text;
  88. var radio = new Element("input", {
  89. "type": "radio",
  90. "name": this.json.properties.name || flag+this.json.id,
  91. "value": value,
  92. "showText": text,
  93. "styles": this.json.buttonStyles
  94. }).inject(this.node);
  95. radio.appendText(text, "after");
  96. radio.addEvent("click", function(){
  97. this.validationMode();
  98. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  99. //this._setBusinessData(this.getInputData());
  100. }.bind(this));
  101. Object.each(this.json.events, function(e, key){
  102. if (e.code){
  103. if (this.options.moduleEvents.indexOf(key)!=-1){
  104. }else{
  105. radio.addEvent(key, function(event){
  106. return this.form.Macro.fire(e.code, this, event);
  107. }.bind(this));
  108. }
  109. }
  110. }.bind(this));
  111. }.bind(this));
  112. }
  113. },
  114. _setValue: function(value){
  115. this._setBusinessData(value);
  116. var radios = this.node.getElements("input");
  117. for (var i=0; i<radios.length; i++){
  118. var radio = radios[i];
  119. if (radio.value==value){
  120. radio.checked = true;
  121. break;
  122. }
  123. }
  124. },
  125. getTextData: function(){
  126. var inputs = this.node.getElements("input");
  127. var value = "";
  128. var text = "";
  129. if (inputs.length){
  130. for (var i=0; i<inputs.length; i++){
  131. var input = inputs[i];
  132. if (input.checked){
  133. value = input.get("value");
  134. text = input.get("showText");
  135. break;
  136. }
  137. }
  138. }
  139. return {"value": [value] || "", "text": [text || value || ""]};
  140. },
  141. getInputData: function(){
  142. var inputs = this.node.getElements("input");
  143. var value = "";
  144. if (inputs.length){
  145. for (var i=0; i<inputs.length; i++){
  146. var input = inputs[i];
  147. if (input.checked){
  148. value = input.get("value");
  149. break;
  150. }
  151. }
  152. }
  153. return value;
  154. },
  155. resetData: function(){
  156. this.setData(this.getValue());
  157. },
  158. getSelectedInput: function(){
  159. var inputs = this.node.getElements("input");
  160. if (inputs.length){
  161. for (var i=0; i<inputs.length; i++){
  162. if (inputs[i].checked) return inputs[i];
  163. }
  164. }
  165. return null;
  166. },
  167. setData: function(data){
  168. this._setBusinessData(data);
  169. var inputs = this.node.getElements("input");
  170. if (inputs.length){
  171. for (var i=0; i<inputs.length; i++){
  172. if (data==inputs[i].get("value")){
  173. inputs[i].set("checked", true);
  174. }else{
  175. inputs[i].set("checked", false);
  176. }
  177. }
  178. }
  179. },
  180. notValidationMode: function(text){
  181. if (!this.isNotValidationMode){
  182. this.isNotValidationMode = true;
  183. this.node.store("background", this.node.getStyles("background"));
  184. this.node.setStyle("background", "#ffdcdc");
  185. this.errNode = this.createErrorNode(text);
  186. if (this.iconNode){
  187. this.errNode.inject(this.iconNode, "after");
  188. }else{
  189. this.errNode.inject(this.node, "after");
  190. }
  191. this.showNotValidationMode(this.node);
  192. }
  193. },
  194. validationMode: function(routeName, opinion){
  195. if (!this.validationConfig(routeName, opinion)) return false;
  196. if (this.isNotValidationMode){
  197. this.isNotValidationMode = false;
  198. this.node.setStyles(this.node.retrieve("background"));
  199. if (this.errNode){
  200. this.errNode.destroy();
  201. this.errNode = null;
  202. }
  203. }
  204. }
  205. });