Checkbox.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.require("MWF.widget.UUID", null, false);
  3. MWF.xApplication.process.Xform.Checkbox = MWF.APPCheckbox = new Class({
  4. Implements: [Events],
  5. Extends: MWF.APP$Input,
  6. loadDescription: function(){},
  7. _loadNode: function(){
  8. if (this.readonly || this.json.isReadonly ){
  9. this._loadNodeRead();
  10. }else{
  11. this._loadNodeEdit();
  12. }
  13. },
  14. _loadNodeRead: function(){
  15. this.node.empty();
  16. var radioValues = this.getOptions();
  17. var value = this.getValue();
  18. if (value){
  19. var texts = [];
  20. radioValues.each(function(item){
  21. var tmps = item.split("|");
  22. var t = tmps[0];
  23. var v = tmps[1] || t;
  24. if (value.indexOf(v)!=-1){
  25. texts.push(t);
  26. }
  27. });
  28. this.node.set("text", texts.join(", "));
  29. }
  30. },
  31. _loadNodeEdit: function(){
  32. //this.container = new Element("select");
  33. var div = new Element("div");
  34. div.set(this.json.properties);
  35. div.inject(this.node, "after");
  36. this.node.destroy();
  37. this.node = div;
  38. this.node.set({
  39. "id": this.json.id,
  40. "MWFType": this.json.type,
  41. "styles": {
  42. "display": "inline"
  43. }
  44. });
  45. this.setOptions();
  46. },
  47. _loadDomEvents: function(){
  48. },
  49. _loadEvents: function(){
  50. Object.each(this.json.events, function(e, key){
  51. if (e.code){
  52. if (this.options.moduleEvents.indexOf(key)!=-1){
  53. this.addEvent(key, function(event){
  54. return this.form.Macro.fire(e.code, this, event);
  55. }.bind(this));
  56. }else{
  57. //this.node.addEvent(key, function(event){
  58. // return this.form.Macro.fire(e.code, this, event);
  59. //}.bind(this));
  60. }
  61. }
  62. }.bind(this));
  63. },
  64. addModuleEvent: function(key, fun){
  65. if (this.options.moduleEvents.indexOf(key)!==-1){
  66. this.addEvent(key, function(event){
  67. return (fun) ? fun(this, event) : null;
  68. }.bind(this));
  69. }else{
  70. var inputs = this.node.getElements("input");
  71. inputs.each(function(input){
  72. input.addEvent(key, function(event){
  73. return (fun) ? fun(this, event) : null;
  74. }.bind(this));
  75. }.bind(this));
  76. }
  77. },
  78. resetOption: function(){
  79. this.node.empty();
  80. this.setOptions();
  81. },
  82. getOptions: function(){
  83. if (this.json.itemType == "values"){
  84. return this.json.itemValues;
  85. }else{
  86. return this.form.Macro.exec(this.json.itemScript.code, this);
  87. }
  88. //return [];
  89. },
  90. setOptions: function(){
  91. var radioValues = this.getOptions();
  92. if (!radioValues) radioValues = [];
  93. if (o2.typeOf(radioValues)==="array"){
  94. var flag = (new MWF.widget.UUID).toString();
  95. radioValues.each(function(item){
  96. var tmps = item.split("|");
  97. var text = tmps[0];
  98. var value = tmps[1] || text;
  99. var radio = new Element("input", {
  100. "type": "checkbox",
  101. "name": this.json.properties.name || flag+this.json.id,
  102. "value": value,
  103. "showText": text,
  104. "styles": this.json.buttonStyles
  105. }).inject(this.node);
  106. //radio.appendText(text, "after");
  107. var textNode = new Element( "span", {
  108. "text" : text,
  109. "styles" : { "cursor" : "default" }
  110. }).inject(this.node);
  111. textNode.addEvent("click", function( ev ){
  112. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  113. this.radio.checked = ! this.radio.checked;
  114. this.radio.fireEvent("change");
  115. this.radio.fireEvent("click");
  116. }.bind( {radio : radio} ) );
  117. radio.addEvent("click", function(){
  118. this.validationMode();
  119. if (this.validation()) this._setBusinessData(this.getInputData("change") || []);
  120. }.bind(this));
  121. Object.each(this.json.events, function(e, key){
  122. if (e.code){
  123. if (this.options.moduleEvents.indexOf(key)!=-1){
  124. }else{
  125. radio.addEvent(key, function(event){
  126. return this.form.Macro.fire(e.code, this, event);
  127. }.bind(this));
  128. }
  129. }
  130. }.bind(this));
  131. }.bind(this));
  132. }
  133. },
  134. _setValue: function(value){
  135. this._setBusinessData(value);
  136. var radios = this.node.getElements("input");
  137. for (var i=0; i<radios.length; i++){
  138. var radio = radios[i];
  139. radio.checked = value.indexOf(radio.value) != -1;
  140. }
  141. },
  142. getTextData: function(){
  143. var inputs = this.node.getElements("input");
  144. var value = [];
  145. var text = [];
  146. if (inputs.length){
  147. inputs.each(function(input){
  148. if (input.checked){
  149. var v = input.get("value");
  150. var t = input.get("showText");
  151. value.push(v || "");
  152. text.push(t || v || "");
  153. }
  154. });
  155. }
  156. if (!value.length) value = [""];
  157. if (!text.length) text = [""];
  158. return {"value": value, "text": text};
  159. },
  160. //getData: function(){
  161. //var inputs = this.node.getElements("input");
  162. //var value = [];
  163. //if (inputs.length){
  164. // inputs.each(function(input){
  165. // if (input.checked){
  166. // var v = input.get("value");
  167. // if (v) value.push(v || "");
  168. // }
  169. // });
  170. //}
  171. //return (value.length==1) ? value[0] : value;
  172. //},
  173. getInputData: function(){
  174. var inputs = this.node.getElements("input");
  175. var value = [];
  176. if (inputs.length){
  177. inputs.each(function(input){
  178. if (input.checked){
  179. var v = input.get("value");
  180. if (v) value.push(v || "");
  181. }
  182. });
  183. }
  184. return (value.length) ? value : [];
  185. },
  186. resetData: function(){
  187. this.setData(this.getValue());
  188. },
  189. setData: function(data){
  190. this._setBusinessData(data);
  191. var inputs = this.node.getElements("input");
  192. if (inputs.length){
  193. inputs.each(function(input){
  194. if (typeOf(data)=="array"){
  195. if (data.indexOf(input.get("value"))!=-1){
  196. input.set("checked", true);
  197. }else{
  198. input.set("checked", false);
  199. }
  200. }else{
  201. if (data == input.get("value")){
  202. input.set("checked", true);
  203. }else{
  204. input.set("checked", false);
  205. }
  206. }
  207. });
  208. }
  209. this.fireEvent("setData");
  210. },
  211. notValidationMode: function(text){
  212. if (!this.isNotValidationMode){
  213. this.isNotValidationMode = true;
  214. this.node.store("background", this.node.getStyles("background"));
  215. this.node.setStyle("background", "#ffdcdc");
  216. this.errNode = this.createErrorNode(text);
  217. if (this.iconNode){
  218. this.errNode.inject(this.iconNode, "after");
  219. }else{
  220. this.errNode.inject(this.node, "after");
  221. }
  222. this.showNotValidationMode(this.node);
  223. if (!this.node.isIntoView()) this.node.scrollIntoView();
  224. }
  225. },
  226. validationMode: function(){
  227. if (this.isNotValidationMode){
  228. this.isNotValidationMode = false;
  229. this.node.setStyles(this.node.retrieve("background"));
  230. if (this.errNode){
  231. this.errNode.destroy();
  232. this.errNode = null;
  233. }
  234. }
  235. }
  236. });