Checkbox.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var flag = (new MWF.widget.UUID).toString();
  77. radioValues.each(function(item){
  78. var tmps = item.split("|");
  79. var text = tmps[0];
  80. var value = tmps[1] || text;
  81. var radio = new Element("input", {
  82. "type": "checkbox",
  83. "name": this.json.properties.name || flag+this.json.id,
  84. "value": value,
  85. "showText": text,
  86. "styles": this.json.buttonStyles
  87. }).inject(this.node);
  88. radio.appendText(text, "after");
  89. radio.addEvent("click", function(){
  90. this.validationMode();
  91. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  92. }.bind(this));
  93. Object.each(this.json.events, function(e, key){
  94. if (e.code){
  95. if (this.options.moduleEvents.indexOf(key)!=-1){
  96. }else{
  97. radio.addEvent(key, function(event){
  98. return this.form.Macro.fire(e.code, this, event);
  99. }.bind(this));
  100. }
  101. }
  102. }.bind(this));
  103. }.bind(this));
  104. },
  105. _setValue: function(value){
  106. this._setBusinessData(value);
  107. var radios = this.node.getElements("input");
  108. for (var i=0; i<radios.length; i++){
  109. var radio = radios[i];
  110. radio.checked = value.indexOf(radio.value) != -1;
  111. }
  112. },
  113. getTextData: function(){
  114. var inputs = this.node.getElements("input");
  115. var value = [];
  116. var text = [];
  117. if (inputs.length){
  118. inputs.each(function(input){
  119. if (input.checked){
  120. var v = input.get("value");
  121. var t = input.get("showText");
  122. value.push(v || "");
  123. text.push(t || v || "");
  124. }
  125. });
  126. }
  127. if (!value.length) value = [""];
  128. if (!text.length) text = [""];
  129. return {"value": value, "text": text};
  130. },
  131. //getData: function(){
  132. //var inputs = this.node.getElements("input");
  133. //var value = [];
  134. //if (inputs.length){
  135. // inputs.each(function(input){
  136. // if (input.checked){
  137. // var v = input.get("value");
  138. // if (v) value.push(v || "");
  139. // }
  140. // });
  141. //}
  142. //return (value.length==1) ? value[0] : value;
  143. //},
  144. getInputData: function(){
  145. var inputs = this.node.getElements("input");
  146. var value = [];
  147. if (inputs.length){
  148. inputs.each(function(input){
  149. if (input.checked){
  150. var v = input.get("value");
  151. if (v) value.push(v || "");
  152. }
  153. });
  154. }
  155. return (value.length) ? value : null;
  156. },
  157. resetData: function(){
  158. this.setData(this.getValue());
  159. },
  160. setData: function(data){
  161. this._setBusinessData(data);
  162. var inputs = this.node.getElements("input");
  163. if (inputs.length){
  164. inputs.each(function(input){
  165. if (typeOf(data)=="array"){
  166. if (data.indexOf(input.get("value"))!=-1){
  167. input.set("checked", true);
  168. }else{
  169. input.set("checked", false);
  170. }
  171. }else{
  172. if (data == input.get("value")){
  173. input.set("checked", true);
  174. }else{
  175. input.set("checked", false);
  176. }
  177. }
  178. });
  179. }
  180. },
  181. notValidationMode: function(text){
  182. if (!this.isNotValidationMode){
  183. this.isNotValidationMode = true;
  184. this.node.store("background", this.node.getStyles("background"));
  185. this.node.setStyle("background", "#ffdcdc");
  186. this.errNode = this.createErrorNode(text);
  187. if (this.iconNode){
  188. this.errNode.inject(this.iconNode, "after");
  189. }else{
  190. this.errNode.inject(this.node, "after");
  191. }
  192. this.showNotValidationMode(this.node);
  193. }
  194. },
  195. validationMode: function(){
  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. });