Checkbox.js 8.2 KB

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