Checkbox.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. var p = o2.promiseAll(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. this.moduleSelectAG = p;
  143. if (p) p.then(function(){
  144. this.moduleSelectAG = null;
  145. }.bind(this));
  146. },
  147. _setValue: function(value){
  148. if (!!value){
  149. var p = o2.promiseAll(value).then(function(v){
  150. //if (o2.typeOf(v)=="array") v = v[0];
  151. if (this.moduleSelectAG){
  152. this.moduleValueAG = this.moduleSelectAG;
  153. this.moduleSelectAG.then(function(){
  154. this.__setValue(v);
  155. return v;
  156. }.bind(this));
  157. }else{
  158. this.__setValue(v)
  159. }
  160. return v;
  161. }.bind(this));
  162. this.moduleValueAG = p;
  163. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  164. this.moduleValueAG = null;
  165. }.bind(this));
  166. }else{
  167. this.__setValue(value);
  168. }
  169. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  170. // if (this.moduleSelectAG){
  171. // this.moduleValueAG = this.moduleSelectAG;
  172. // this.moduleSelectAG.then(function(){
  173. // this.moduleValueAG = null;
  174. // this.__setValue(v);
  175. // }.bind(this));
  176. // }else{
  177. // this.moduleValueAG = null;
  178. // this.__setValue(v);
  179. // }
  180. // return v;
  181. // }.bind(this));
  182. //
  183. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  184. // this.moduleValueAG = "";
  185. // }.bind(this));
  186. },
  187. __setValue: function(value){
  188. this._setBusinessData(value);
  189. var radios = this.node.getElements("input");
  190. for (var i=0; i<radios.length; i++){
  191. var radio = radios[i];
  192. radio.checked = value.indexOf(radio.value) != -1;
  193. }
  194. },
  195. getTextData: function(){
  196. var inputs = this.node.getElements("input");
  197. var value = [];
  198. var text = [];
  199. if (inputs.length){
  200. inputs.each(function(input){
  201. if (input.checked){
  202. var v = input.get("value");
  203. var t = input.get("showText");
  204. value.push(v || "");
  205. text.push(t || v || "");
  206. }
  207. });
  208. }
  209. if (!value.length) value = [""];
  210. if (!text.length) text = [""];
  211. return {"value": value, "text": text};
  212. },
  213. //getData: function(){
  214. //var inputs = this.node.getElements("input");
  215. //var value = [];
  216. //if (inputs.length){
  217. // inputs.each(function(input){
  218. // if (input.checked){
  219. // var v = input.get("value");
  220. // if (v) value.push(v || "");
  221. // }
  222. // });
  223. //}
  224. //return (value.length==1) ? value[0] : value;
  225. //},
  226. isEmpty: function(){
  227. var data = this.getData();
  228. if( typeOf(data) !== "array" )return true;
  229. if( data.length === 0 )return true;
  230. return false;
  231. },
  232. getInputData: function(){
  233. if (this.readonly || this.json.isReadonly ){
  234. return this._getBusinessData();
  235. }else{
  236. var inputs = this.node.getElements("input");
  237. var value = [];
  238. if (inputs.length){
  239. inputs.each(function(input){
  240. if (input.checked){
  241. var v = input.get("value");
  242. if (v) value.push(v || "");
  243. }
  244. });
  245. }
  246. return (value.length) ? value : [];
  247. }
  248. },
  249. resetData: function(){
  250. this.setData(this.getValue());
  251. },
  252. setData: function(data){
  253. return this._setValue(data);
  254. // if (data && data.isAG){
  255. // this.moduleValueAG = data;
  256. // data.addResolve(function(v){
  257. // this.setData(v);
  258. // }.bind(this));
  259. // }else{
  260. // this.__setData(data);
  261. // this.moduleValueAG = null;
  262. // }
  263. },
  264. __setData: function(data){
  265. this._setBusinessData(data);
  266. var inputs = this.node.getElements("input");
  267. if (inputs.length){
  268. inputs.each(function(input){
  269. if (typeOf(data)=="array"){
  270. if (data.indexOf(input.get("value"))!=-1){
  271. input.set("checked", true);
  272. }else{
  273. input.set("checked", false);
  274. }
  275. }else{
  276. if (data == input.get("value")){
  277. input.set("checked", true);
  278. }else{
  279. input.set("checked", false);
  280. }
  281. }
  282. });
  283. }
  284. this.fireEvent("setData");
  285. },
  286. notValidationMode: function(text){
  287. if (!this.isNotValidationMode){
  288. this.isNotValidationMode = true;
  289. this.node.store("background", this.node.getStyles("background"));
  290. this.node.setStyle("background", "#ffdcdc");
  291. this.errNode = this.createErrorNode(text);
  292. if (this.iconNode){
  293. this.errNode.inject(this.iconNode, "after");
  294. }else{
  295. this.errNode.inject(this.node, "after");
  296. }
  297. this.showNotValidationMode(this.node);
  298. if (!this.node.isIntoView()) this.node.scrollIntoView();
  299. }
  300. },
  301. validationMode: function(){
  302. if (this.isNotValidationMode){
  303. this.isNotValidationMode = false;
  304. this.node.setStyles(this.node.retrieve("background"));
  305. if (this.errNode){
  306. this.errNode.destroy();
  307. this.errNode = null;
  308. }
  309. }
  310. },
  311. validationConfigItem: function(routeName, data){
  312. var flag = (data.status==="all") ? true: (routeName === data.decision);
  313. if (flag){
  314. var n = this.getInputData();
  315. if( typeOf(n)==="array" && n.length === 0 )n = "";
  316. var v = (data.valueType==="value") ? n : n.length;
  317. switch (data.operateor){
  318. case "isnull":
  319. if (!v){
  320. this.notValidationMode(data.prompt);
  321. return false;
  322. }
  323. break;
  324. case "notnull":
  325. if (v){
  326. this.notValidationMode(data.prompt);
  327. return false;
  328. }
  329. break;
  330. case "gt":
  331. if (v>data.value){
  332. this.notValidationMode(data.prompt);
  333. return false;
  334. }
  335. break;
  336. case "lt":
  337. if (v<data.value){
  338. this.notValidationMode(data.prompt);
  339. return false;
  340. }
  341. break;
  342. case "equal":
  343. if (v==data.value){
  344. this.notValidationMode(data.prompt);
  345. return false;
  346. }
  347. break;
  348. case "neq":
  349. if (v!=data.value){
  350. this.notValidationMode(data.prompt);
  351. return false;
  352. }
  353. break;
  354. case "contain":
  355. if (v.indexOf(data.value)!=-1){
  356. this.notValidationMode(data.prompt);
  357. return false;
  358. }
  359. break;
  360. case "notcontain":
  361. if (v.indexOf(data.value)==-1){
  362. this.notValidationMode(data.prompt);
  363. return false;
  364. }
  365. break;
  366. }
  367. }
  368. return true;
  369. }
  370. });