Checkbox.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 optionItems = this.getOptions();
  95. this._setOptions(optionItems);
  96. },
  97. _setOptions: function(optionItems){
  98. this.moduleSelectAG = o2.AG.all(optionItems).then(function(radioValues){
  99. this.moduleSelectAG = null;
  100. if (!radioValues) radioValues = [];
  101. if (o2.typeOf(radioValues)==="array"){
  102. var flag = (new MWF.widget.UUID).toString();
  103. radioValues.each(function(item){
  104. var tmps = item.split("|");
  105. var text = tmps[0];
  106. var value = tmps[1] || text;
  107. var radio = new Element("input", {
  108. "type": "checkbox",
  109. "name": ((this.json.properties) ? this.json.properties.name : null) || flag+this.json.id,
  110. "value": value,
  111. "showText": text,
  112. "styles": this.json.buttonStyles
  113. }).inject(this.node);
  114. //radio.appendText(text, "after");
  115. var textNode = new Element( "span", {
  116. "text" : text,
  117. "styles" : { "cursor" : "default" }
  118. }).inject(this.node);
  119. textNode.addEvent("click", function( ev ){
  120. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  121. this.radio.checked = ! this.radio.checked;
  122. this.radio.fireEvent("change");
  123. this.radio.fireEvent("click");
  124. }.bind( {radio : radio} ) );
  125. radio.addEvent("click", function(){
  126. this.validationMode();
  127. if (this.validation()) this._setBusinessData(this.getInputData("change") || []);
  128. }.bind(this));
  129. Object.each(this.json.events, function(e, key){
  130. if (e.code){
  131. if (this.options.moduleEvents.indexOf(key)!=-1){
  132. }else{
  133. radio.addEvent(key, function(event){
  134. return this.form.Macro.fire(e.code, this, event);
  135. }.bind(this));
  136. }
  137. }
  138. }.bind(this));
  139. }.bind(this));
  140. }
  141. }.bind(this));
  142. },
  143. _setValue: function(value){
  144. this.moduleValueAG = o2.AG.all(value).then(function(v){
  145. if (this.moduleSelectAG){
  146. this.moduleValueAG = this.moduleSelectAG;
  147. this.moduleSelectAG.then(function(){
  148. this.__setValue(v);
  149. }.bind(this));
  150. }else{
  151. this.__setValue(v)
  152. }
  153. return v;
  154. }.bind(this));
  155. },
  156. __setValue: function(value){
  157. this._setBusinessData(value);
  158. var radios = this.node.getElements("input");
  159. for (var i=0; i<radios.length; i++){
  160. var radio = radios[i];
  161. radio.checked = value.indexOf(radio.value) != -1;
  162. }
  163. },
  164. getTextData: function(){
  165. var inputs = this.node.getElements("input");
  166. var value = [];
  167. var text = [];
  168. if (inputs.length){
  169. inputs.each(function(input){
  170. if (input.checked){
  171. var v = input.get("value");
  172. var t = input.get("showText");
  173. value.push(v || "");
  174. text.push(t || v || "");
  175. }
  176. });
  177. }
  178. if (!value.length) value = [""];
  179. if (!text.length) text = [""];
  180. return {"value": value, "text": text};
  181. },
  182. //getData: function(){
  183. //var inputs = this.node.getElements("input");
  184. //var value = [];
  185. //if (inputs.length){
  186. // inputs.each(function(input){
  187. // if (input.checked){
  188. // var v = input.get("value");
  189. // if (v) value.push(v || "");
  190. // }
  191. // });
  192. //}
  193. //return (value.length==1) ? value[0] : value;
  194. //},
  195. isEmpty: function(){
  196. var data = this.getData();
  197. if( typeOf(data) !== "array" )return true;
  198. if( data.length === 0 )return true;
  199. return false;
  200. },
  201. getInputData: function(){
  202. if (this.readonly || this.json.isReadonly ){
  203. return this._getBusinessData();
  204. }else{
  205. var inputs = this.node.getElements("input");
  206. var value = [];
  207. if (inputs.length){
  208. inputs.each(function(input){
  209. if (input.checked){
  210. var v = input.get("value");
  211. if (v) value.push(v || "");
  212. }
  213. });
  214. }
  215. return (value.length) ? value : [];
  216. }
  217. },
  218. resetData: function(){
  219. this.setData(this.getValue());
  220. },
  221. setData: function(data){
  222. if (data && data.isAG){
  223. this.moduleValueAG = data;
  224. data.addResolve(function(v){
  225. this.setData(v);
  226. }.bind(this));
  227. }else{
  228. this.__setData(data);
  229. this.moduleValueAG = null;
  230. }
  231. },
  232. __setData: function(data){
  233. this._setBusinessData(data);
  234. var inputs = this.node.getElements("input");
  235. if (inputs.length){
  236. inputs.each(function(input){
  237. if (typeOf(data)=="array"){
  238. if (data.indexOf(input.get("value"))!=-1){
  239. input.set("checked", true);
  240. }else{
  241. input.set("checked", false);
  242. }
  243. }else{
  244. if (data == input.get("value")){
  245. input.set("checked", true);
  246. }else{
  247. input.set("checked", false);
  248. }
  249. }
  250. });
  251. }
  252. this.fireEvent("setData");
  253. },
  254. notValidationMode: function(text){
  255. if (!this.isNotValidationMode){
  256. this.isNotValidationMode = true;
  257. this.node.store("background", this.node.getStyles("background"));
  258. this.node.setStyle("background", "#ffdcdc");
  259. this.errNode = this.createErrorNode(text);
  260. if (this.iconNode){
  261. this.errNode.inject(this.iconNode, "after");
  262. }else{
  263. this.errNode.inject(this.node, "after");
  264. }
  265. this.showNotValidationMode(this.node);
  266. if (!this.node.isIntoView()) this.node.scrollIntoView();
  267. }
  268. },
  269. validationMode: function(){
  270. if (this.isNotValidationMode){
  271. this.isNotValidationMode = false;
  272. this.node.setStyles(this.node.retrieve("background"));
  273. if (this.errNode){
  274. this.errNode.destroy();
  275. this.errNode = null;
  276. }
  277. }
  278. },
  279. validationConfigItem: function(routeName, data){
  280. var flag = (data.status==="all") ? true: (routeName === data.decision);
  281. if (flag){
  282. var n = this.getInputData();
  283. if( typeOf(n)==="array" && n.length === 0 )n = "";
  284. var v = (data.valueType==="value") ? n : n.length;
  285. switch (data.operateor){
  286. case "isnull":
  287. if (!v){
  288. this.notValidationMode(data.prompt);
  289. return false;
  290. }
  291. break;
  292. case "notnull":
  293. if (v){
  294. this.notValidationMode(data.prompt);
  295. return false;
  296. }
  297. break;
  298. case "gt":
  299. if (v>data.value){
  300. this.notValidationMode(data.prompt);
  301. return false;
  302. }
  303. break;
  304. case "lt":
  305. if (v<data.value){
  306. this.notValidationMode(data.prompt);
  307. return false;
  308. }
  309. break;
  310. case "equal":
  311. if (v==data.value){
  312. this.notValidationMode(data.prompt);
  313. return false;
  314. }
  315. break;
  316. case "neq":
  317. if (v!=data.value){
  318. this.notValidationMode(data.prompt);
  319. return false;
  320. }
  321. break;
  322. case "contain":
  323. if (v.indexOf(data.value)!=-1){
  324. this.notValidationMode(data.prompt);
  325. return false;
  326. }
  327. break;
  328. case "notcontain":
  329. if (v.indexOf(data.value)==-1){
  330. this.notValidationMode(data.prompt);
  331. return false;
  332. }
  333. break;
  334. }
  335. }
  336. return true;
  337. }
  338. });