Checkbox.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. this.node.set({
  17. "nodeId": this.json.id,
  18. "MWFType": this.json.type
  19. });
  20. var radioValues = this.getOptions();
  21. var value = this.getValue();
  22. if (value){
  23. var texts = [];
  24. radioValues.each(function(item){
  25. var tmps = item.split("|");
  26. var t = tmps[0];
  27. var v = tmps[1] || t;
  28. if (value.indexOf(v)!=-1){
  29. texts.push(t);
  30. }
  31. });
  32. this.node.set("text", texts.join(", "));
  33. }
  34. },
  35. _resetNodeEdit: function(){
  36. var div = new Element("div");
  37. div.set(this.json.properties);
  38. div.inject(this.node, "after");
  39. this.node.destroy();
  40. this.node = div;
  41. },
  42. _loadNodeEdit: function(){
  43. //this.container = new Element("select");
  44. if (!this.json.preprocessing) this._resetNodeEdit();
  45. this.node.set({
  46. "id": this.json.id,
  47. "MWFType": this.json.type,
  48. "styles": {
  49. "display": "inline"
  50. }
  51. });
  52. this.setOptions();
  53. },
  54. _loadDomEvents: function(){
  55. },
  56. _loadEvents: function(){
  57. Object.each(this.json.events, function(e, key){
  58. if (e.code){
  59. if (this.options.moduleEvents.indexOf(key)!=-1){
  60. this.addEvent(key, function(event){
  61. return this.form.Macro.fire(e.code, this, event);
  62. }.bind(this));
  63. }else{
  64. //this.node.addEvent(key, function(event){
  65. // return this.form.Macro.fire(e.code, this, event);
  66. //}.bind(this));
  67. }
  68. }
  69. }.bind(this));
  70. },
  71. addModuleEvent: function(key, fun){
  72. if (this.options.moduleEvents.indexOf(key)!==-1){
  73. this.addEvent(key, function(event){
  74. return (fun) ? fun(this, event) : null;
  75. }.bind(this));
  76. }else{
  77. var inputs = this.node.getElements("input");
  78. inputs.each(function(input){
  79. input.addEvent(key, function(event){
  80. return (fun) ? fun(this, event) : null;
  81. }.bind(this));
  82. }.bind(this));
  83. }
  84. },
  85. resetOption: function(){
  86. this.node.empty();
  87. this.setOptions();
  88. },
  89. getOptions: function(){
  90. if (this.json.itemType == "values"){
  91. return this.json.itemValues;
  92. }else{
  93. return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  94. }
  95. //return [];
  96. },
  97. setOptions: function(){
  98. var optionItems = this.getOptions();
  99. this._setOptions(optionItems);
  100. },
  101. _setOptions: function(optionItems){
  102. var p = o2.promiseAll(optionItems).then(function(radioValues){
  103. this.moduleSelectAG = null;
  104. if (!radioValues) radioValues = [];
  105. if (o2.typeOf(radioValues)==="array"){
  106. var flag = (new MWF.widget.UUID).toString();
  107. radioValues.each(function(item){
  108. var tmps = item.split("|");
  109. var text = tmps[0];
  110. var value = tmps[1] || text;
  111. var radio = new Element("input", {
  112. "type": "checkbox",
  113. "name": ((this.json.properties) ? this.json.properties.name : null) || flag+this.json.id,
  114. "value": value,
  115. "showText": text,
  116. "styles": this.json.buttonStyles
  117. }).inject(this.node);
  118. //radio.appendText(text, "after");
  119. var textNode = new Element( "span", {
  120. "text" : text,
  121. "styles" : { "cursor" : "default" }
  122. }).inject(this.node);
  123. textNode.addEvent("click", function( ev ){
  124. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  125. this.radio.checked = ! this.radio.checked;
  126. this.radio.fireEvent("change");
  127. this.radio.fireEvent("click");
  128. }.bind( {radio : radio} ) );
  129. radio.addEvent("click", function(){
  130. this.validationMode();
  131. if (this.validation()) this._setBusinessData(this.getInputData("change") || []);
  132. }.bind(this));
  133. Object.each(this.json.events, function(e, key){
  134. if (e.code){
  135. if (this.options.moduleEvents.indexOf(key)!=-1){
  136. }else{
  137. radio.addEvent(key, function(event){
  138. return this.form.Macro.fire(e.code, this, event);
  139. }.bind(this));
  140. }
  141. }
  142. }.bind(this));
  143. }.bind(this));
  144. }
  145. }.bind(this), function(){});
  146. this.moduleSelectAG = p;
  147. if (p) p.then(function(){
  148. this.moduleSelectAG = null;
  149. }.bind(this), function(){
  150. this.moduleSelectAG = null;
  151. }.bind(this));
  152. },
  153. _setValue: function(value, m){
  154. var mothed = m || "__setValue";
  155. if (!!value){
  156. var p = o2.promiseAll(value).then(function(v){
  157. //if (o2.typeOf(v)=="array") v = v[0];
  158. if (this.moduleSelectAG){
  159. this.moduleValueAG = this.moduleSelectAG;
  160. this.moduleSelectAG.then(function(){
  161. this[mothed](v);
  162. return v;
  163. }.bind(this), function(){});
  164. }else{
  165. this[mothed](v)
  166. }
  167. return v;
  168. }.bind(this), function(){});
  169. this.moduleValueAG = p;
  170. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  171. this.moduleValueAG = null;
  172. }.bind(this), function(){
  173. this.moduleValueAG = null;
  174. }.bind(this));
  175. }else{
  176. this[mothed](value);
  177. }
  178. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  179. // if (this.moduleSelectAG){
  180. // this.moduleValueAG = this.moduleSelectAG;
  181. // this.moduleSelectAG.then(function(){
  182. // this.moduleValueAG = null;
  183. // this.__setValue(v);
  184. // }.bind(this));
  185. // }else{
  186. // this.moduleValueAG = null;
  187. // this.__setValue(v);
  188. // }
  189. // return v;
  190. // }.bind(this));
  191. //
  192. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  193. // this.moduleValueAG = "";
  194. // }.bind(this));
  195. },
  196. __setValue: function(value){
  197. this._setBusinessData(value);
  198. var radios = this.node.getElements("input");
  199. for (var i=0; i<radios.length; i++){
  200. var radio = radios[i];
  201. radio.checked = value.indexOf(radio.value) != -1;
  202. }
  203. },
  204. getTextData: function(){
  205. var inputs = this.node.getElements("input");
  206. var value = [];
  207. var text = [];
  208. if (inputs.length){
  209. inputs.each(function(input){
  210. if (input.checked){
  211. var v = input.get("value");
  212. var t = input.get("showText");
  213. value.push(v || "");
  214. text.push(t || v || "");
  215. }
  216. });
  217. }
  218. if (!value.length) value = [""];
  219. if (!text.length) text = [""];
  220. return {"value": value, "text": text};
  221. },
  222. //getData: function(){
  223. //var inputs = this.node.getElements("input");
  224. //var value = [];
  225. //if (inputs.length){
  226. // inputs.each(function(input){
  227. // if (input.checked){
  228. // var v = input.get("value");
  229. // if (v) value.push(v || "");
  230. // }
  231. // });
  232. //}
  233. //return (value.length==1) ? value[0] : value;
  234. //},
  235. isEmpty: function(){
  236. var data = this.getData();
  237. if( typeOf(data) !== "array" )return true;
  238. if( data.length === 0 )return true;
  239. return false;
  240. },
  241. getInputData: function(){
  242. if (this.readonly || this.json.isReadonly ){
  243. return this._getBusinessData();
  244. }else{
  245. var inputs = this.node.getElements("input");
  246. var value = [];
  247. if (inputs.length){
  248. inputs.each(function(input){
  249. if (input.checked){
  250. var v = input.get("value");
  251. if (v) value.push(v || "");
  252. }
  253. });
  254. }
  255. return (value.length) ? value : [];
  256. }
  257. },
  258. resetData: function(){
  259. this.setData(this.getValue());
  260. },
  261. setData: function(data){
  262. return this._setValue(data, "__setData");
  263. // if (data && data.isAG){
  264. // this.moduleValueAG = data;
  265. // data.addResolve(function(v){
  266. // this.setData(v);
  267. // }.bind(this));
  268. // }else{
  269. // this.__setData(data);
  270. // this.moduleValueAG = null;
  271. // }
  272. },
  273. __setData: function(data){
  274. this._setBusinessData(data);
  275. var inputs = this.node.getElements("input");
  276. if (inputs.length){
  277. inputs.each(function(input){
  278. if (typeOf(data)=="array"){
  279. if (data.indexOf(input.get("value"))!=-1){
  280. input.set("checked", true);
  281. }else{
  282. input.set("checked", false);
  283. }
  284. }else{
  285. if (data == input.get("value")){
  286. input.set("checked", true);
  287. }else{
  288. input.set("checked", false);
  289. }
  290. }
  291. });
  292. }
  293. this.fireEvent("setData");
  294. },
  295. notValidationMode: function(text){
  296. if (!this.isNotValidationMode){
  297. this.isNotValidationMode = true;
  298. this.node.store("background", this.node.getStyles("background"));
  299. this.node.setStyle("background", "#ffdcdc");
  300. this.errNode = this.createErrorNode(text);
  301. if (this.iconNode){
  302. this.errNode.inject(this.iconNode, "after");
  303. }else{
  304. this.errNode.inject(this.node, "after");
  305. }
  306. this.showNotValidationMode(this.node);
  307. if (!this.node.isIntoView()) this.node.scrollIntoView();
  308. }
  309. },
  310. validationMode: function(){
  311. if (this.isNotValidationMode){
  312. this.isNotValidationMode = false;
  313. this.node.setStyles(this.node.retrieve("background"));
  314. if (this.errNode){
  315. this.errNode.destroy();
  316. this.errNode = null;
  317. }
  318. }
  319. },
  320. validationConfigItem: function(routeName, data){
  321. var flag = (data.status==="all") ? true: (routeName === data.decision);
  322. if (flag){
  323. var n = this.getInputData();
  324. if( typeOf(n)==="array" && n.length === 0 )n = "";
  325. var v = (data.valueType==="value") ? n : n.length;
  326. switch (data.operateor){
  327. case "isnull":
  328. if (!v){
  329. this.notValidationMode(data.prompt);
  330. return false;
  331. }
  332. break;
  333. case "notnull":
  334. if (v){
  335. this.notValidationMode(data.prompt);
  336. return false;
  337. }
  338. break;
  339. case "gt":
  340. if (v>data.value){
  341. this.notValidationMode(data.prompt);
  342. return false;
  343. }
  344. break;
  345. case "lt":
  346. if (v<data.value){
  347. this.notValidationMode(data.prompt);
  348. return false;
  349. }
  350. break;
  351. case "equal":
  352. if (v==data.value){
  353. this.notValidationMode(data.prompt);
  354. return false;
  355. }
  356. break;
  357. case "neq":
  358. if (v!=data.value){
  359. this.notValidationMode(data.prompt);
  360. return false;
  361. }
  362. break;
  363. case "contain":
  364. if (v.indexOf(data.value)!=-1){
  365. this.notValidationMode(data.prompt);
  366. return false;
  367. }
  368. break;
  369. case "notcontain":
  370. if (v.indexOf(data.value)==-1){
  371. this.notValidationMode(data.prompt);
  372. return false;
  373. }
  374. break;
  375. }
  376. }
  377. return true;
  378. }
  379. });