Checkbox.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. getOptions: function(){
  47. if (this.json.itemType == "values"){
  48. return this.json.itemValues;
  49. }else{
  50. return this.form.Macro.exec(this.json.itemScript.code, this);
  51. }
  52. return [];
  53. },
  54. setOptions: function(){
  55. var radioValues = this.getOptions();
  56. if (!radioValues) radioValues = [];
  57. radioValues.each(function(item){
  58. var tmps = item.split("|");
  59. var text = tmps[0];
  60. var value = tmps[1] || text;
  61. var radio = new Element("input", {
  62. "type": "checkbox",
  63. "name": this.json.id,
  64. "value": value,
  65. "showText": text,
  66. "styles": this.json.buttonStyles
  67. }).inject(this.node);
  68. radio.appendText(text, "after");
  69. radio.addEvent("click", function(){
  70. this.validationMode();
  71. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  72. }.bind(this));
  73. }.bind(this));
  74. },
  75. _setValue: function(value){
  76. this._setBusinessData(value);
  77. var radios = this.node.getElements("input");
  78. for (var i=0; i<radios.length; i++){
  79. var radio = radios[i];
  80. if (value.indexOf(radio.value)!=-1){
  81. radio.checked = true;
  82. }else{
  83. radio.checked = false;
  84. }
  85. }
  86. },
  87. getTextData: function(){
  88. var inputs = this.node.getElements("input");
  89. var value = [];
  90. var text = [];
  91. if (inputs.length){
  92. inputs.each(function(input){
  93. if (input.checked){
  94. var v = input.get("value");
  95. var t = input.get("showText");
  96. value.push(v || "");
  97. text.push(t || v || "");
  98. }
  99. });
  100. }
  101. if (!value.length) value = [""];
  102. if (!text.length) text = [""];
  103. return {"value": value, "text": text};
  104. },
  105. //getData: function(){
  106. //var inputs = this.node.getElements("input");
  107. //var value = [];
  108. //if (inputs.length){
  109. // inputs.each(function(input){
  110. // if (input.checked){
  111. // var v = input.get("value");
  112. // if (v) value.push(v || "");
  113. // }
  114. // });
  115. //}
  116. //return (value.length==1) ? value[0] : value;
  117. //},
  118. getInputData: function(){
  119. var inputs = this.node.getElements("input");
  120. var value = [];
  121. if (inputs.length){
  122. inputs.each(function(input){
  123. if (input.checked){
  124. var v = input.get("value");
  125. if (v) value.push(v || "");
  126. }
  127. });
  128. }
  129. return (value.length) ? value : null;
  130. },
  131. resetData: function(){
  132. this.setData(this.getValue());
  133. },
  134. setData: function(data){
  135. this._setBusinessData(data);
  136. var inputs = this.node.getElements("input");
  137. if (inputs.length){
  138. inputs.each(function(input){
  139. if (typeOf(data)=="array"){
  140. if (data.indexOf(input.get("value"))!=-1){
  141. input.set("checked", true);
  142. }else{
  143. input.set("checked", false);
  144. }
  145. }else{
  146. if (data == input.get("value")){
  147. input.set("checked", true);
  148. }else{
  149. input.set("checked", false);
  150. }
  151. }
  152. });
  153. }
  154. },
  155. notValidationMode: function(text){
  156. if (!this.isNotValidationMode){
  157. this.isNotValidationMode = true;
  158. this.node.store("background", this.node.getStyles("background"));
  159. this.node.setStyle("background", "#ffdcdc");
  160. this.errNode = this.createErrorNode(text)
  161. if (this.iconNode){
  162. this.errNode.inject(this.iconNode, "after");
  163. }else{
  164. this.errNode.inject(this.node, "after");
  165. }
  166. this.showNotValidationMode(this.node);
  167. }
  168. },
  169. validationMode: function(){
  170. if (this.isNotValidationMode){
  171. this.isNotValidationMode = false;
  172. this.node.setStyles(this.node.retrieve("background"));
  173. if (this.errNode){
  174. this.errNode.destroy();
  175. this.errNode = null;
  176. }
  177. }
  178. }
  179. });