Checkbox.js 6.1 KB

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