Checkbox.js 6.4 KB

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