Radio.js 5.6 KB

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