Checkbox.js 6.9 KB

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