Checkbox.js 7.7 KB

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