$Input.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.$Input = MWF.APP$Input = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Module,
  5. iconStyle: "personfieldIcon",
  6. initialize: function(node, json, form, options){
  7. this.node = $(node);
  8. this.node.store("module", this);
  9. this.json = json;
  10. this.form = form;
  11. this.field = true;
  12. },
  13. _loadUserInterface: function(){
  14. this._loadNode();
  15. if (this.json.compute == "show"){
  16. this._setValue(this._computeValue());
  17. }else{
  18. this._loadValue();
  19. }
  20. },
  21. _loadNode: function(){
  22. if (this.readonly){
  23. this._loadNodeRead();
  24. }else{
  25. this._loadNodeEdit();
  26. }
  27. },
  28. _loadNodeRead: function(){
  29. this.node.empty();
  30. },
  31. loadDescription: function(){
  32. var v = this._getBusinessData();
  33. if (!v){
  34. if (this.json.description){
  35. var size = this.node.getFirst().getSize();
  36. var w = size.x-3;
  37. if (COMMON.Browser.safari) w = w-20;
  38. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  39. this.descriptionNode.setStyles({
  40. "width": ""+w+"px",
  41. "height": ""+size.y+"px",
  42. "line-height": ""+size.y+"px"
  43. });
  44. this.setDescriptionEvent();
  45. }
  46. }
  47. },
  48. setDescriptionEvent: function(){
  49. if (this.descriptionNode){
  50. this.descriptionNode.addEvents({
  51. "mousedown": function(){
  52. this.descriptionNode.setStyle("display", "none");
  53. this.clickSelect();
  54. }.bind(this)
  55. });
  56. this.node.getFirst().addEvents({
  57. "focus": function(){
  58. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  59. }.bind(this),
  60. "blur": function(){
  61. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  62. }.bind(this)
  63. });
  64. }
  65. },
  66. _loadNodeEdit: function(){
  67. var input = new Element("input", {
  68. "styles": {
  69. "background": "transparent",
  70. "width": "100%",
  71. "border": "0px"
  72. },
  73. "readonly": true
  74. });
  75. input.set(this.json.properties);
  76. var node = new Element("div", {"styles": {
  77. "overflow": "hidden",
  78. "position": "relative",
  79. "margin-right": "20px"
  80. }}).inject(this.node, "after");
  81. input.inject(node);
  82. this.node.destroy();
  83. this.node = node;
  84. this.node.set({
  85. "id": this.json.id,
  86. "MWFType": this.json.type,
  87. "readonly": true,
  88. "events": {
  89. "click": this.clickSelect.bind(this)
  90. }
  91. });
  92. this.iconNode = new Element("div", {
  93. "styles": this.form.css[this.iconStyle],
  94. "events": {
  95. "click": this.clickSelect.bind(this)
  96. }
  97. }).inject(this.node, "before");
  98. this.node.getFirst().addEvent("change", function(){
  99. this.validationMode();
  100. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  101. }.bind(this));
  102. },
  103. _loadStyles: function(){
  104. if (this.json.styles) this.node.setStyles(this.json.styles);
  105. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  106. if (this.iconNode){
  107. var size = this.node.getSize();
  108. //if (!size.y){
  109. // var y1 = this.node.getStyle("height");
  110. // var y2 = this.node.getFirst().getStyle("height");
  111. // alert(y1+"," +y2);
  112. // var y = ((y1!="auto" && y1>y2) || y2=="auto") ? y1 : y2;
  113. // size.y = (y=="auto") ? "auto" : y.toInt();
  114. // //alert(size.y)
  115. //}
  116. this.iconNode.setStyle("height", ""+size.y+"px");
  117. //alert(this.iconNode.getStyle("height"))
  118. }
  119. },
  120. _computeValue: function(value){
  121. return (this.json.defaultValue.code) ? this.form.Macro.exec(this.json.defaultValue.code, this): (value || "");
  122. },
  123. getValue: function(){
  124. var value = this._getBusinessData();
  125. if (!value) value = this._computeValue();
  126. return value || "";
  127. },
  128. _setValue: function(value){
  129. this._setBusinessData(value);
  130. if (this.node.getFirst()) this.node.getFirst().set("value", value);
  131. if (this.readonly) this.node.set("text", value);
  132. },
  133. _loadValue: function(){
  134. this._setValue(this.getValue());
  135. },
  136. clickSelect: function(){
  137. },
  138. _afterLoaded: function(){
  139. // if (this.iconNode){
  140. //// var p = this.node.getPosition();
  141. //// var s = this.node.getSize();
  142. //// var is = this.iconNode.getSize();
  143. ////
  144. //// var y = p.y;
  145. //// var x = p.x+s.x-is.x;
  146. // this.iconNode.setStyles({
  147. // "top": "5px",
  148. // "left": "-18px"
  149. // });
  150. // }
  151. if (!this.readonly){
  152. this.loadDescription();
  153. }
  154. },
  155. getTextData: function(){
  156. //var value = this.node.get("value");
  157. //var text = this.node.get("text");
  158. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  159. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  160. return {"value": [value] || "", "text": [text || value || ""]};
  161. },
  162. getData: function(when){
  163. if (this.json.compute == "save") this._setValue(this._computeValue());
  164. return this.getInputData();
  165. },
  166. getInputData: function(){
  167. return this.node.getFirst().get("value");
  168. },
  169. resetData: function(){
  170. this.setData(this.getValue());
  171. },
  172. setData: function(data){
  173. this._setBusinessData(data);
  174. if (this.node.getFirst()){
  175. this.node.getFirst().set("value", data);
  176. }else{
  177. this.node.set("text", data);
  178. }
  179. },
  180. createErrorNode: function(text){
  181. var node = new Element("div");
  182. //var size = this.node.getFirst().getSize();
  183. //var w = size.x-3;
  184. //if (COMMON.Browser.safari) w = w-20;
  185. //node.setStyles({
  186. // "width": ""+w+"px",
  187. // "height": ""+size.y+"px",
  188. // "line-height": ""+size.y+"px",
  189. // "position": "absolute",
  190. // "top": "0px"
  191. //});
  192. var iconNode = new Element("div", {
  193. "styles": {
  194. "width": "20px",
  195. "height": "20px",
  196. "float": "left",
  197. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  198. }
  199. }).inject(node);
  200. var textNode = new Element("div", {
  201. "styles": {
  202. "height": "20px",
  203. "line-height": "20px",
  204. "margin-left": "20px",
  205. "color": "red"
  206. },
  207. "text": text
  208. }).inject(node);
  209. return node;
  210. },
  211. notValidationMode: function(text){
  212. if (!this.isNotValidationMode){
  213. this.isNotValidationMode = true;
  214. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  215. this.node.setStyle("border-color", "red");
  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. }
  224. },
  225. showNotValidationMode: function(node){
  226. var p = node.getParent("div");
  227. if (p){
  228. if (p.get("MWFtype") == "tab$Content"){
  229. if (p.getParent("div").getStyle("display")=="none"){
  230. var contentAreaNode = p.getParent("div").getParent("div");
  231. var tabAreaNode = contentAreaNode.getPrevious("div");
  232. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  233. var tabNode = tabAreaNode.getChildren()[idx];
  234. tabNode.click();
  235. p = tabAreaNode.getParent("div");
  236. }
  237. }
  238. this.showNotValidationMode(p);
  239. }
  240. },
  241. validationMode: function(){
  242. if (this.isNotValidationMode){
  243. this.isNotValidationMode = false;
  244. this.node.setStyles(this.node.retrieve("borderStyle"));
  245. if (this.errNode){
  246. this.errNode.destroy();
  247. this.errNode = null;
  248. }
  249. }
  250. },
  251. validationConfigItem: function(routeName, data){
  252. var flag = (data.status=="all") ? true: (routeName == data.decision);
  253. if (flag){
  254. var n = this.getInputData();
  255. var v = (data.valueType=="value") ? n : n.length;
  256. switch (data.operateor){
  257. case "isnull":
  258. if (!v){
  259. this.notValidationMode(data.prompt);
  260. return false;
  261. }
  262. break;
  263. case "notnull":
  264. if (v){
  265. this.notValidationMode(data.prompt);
  266. return false;
  267. }
  268. break;
  269. case "gt":
  270. if (v>data.value){
  271. this.notValidationMode(data.prompt);
  272. return false;
  273. }
  274. break;
  275. case "lt":
  276. if (v<data.value){
  277. this.notValidationMode(data.prompt);
  278. return false;
  279. }
  280. break;
  281. case "equal":
  282. if (v==data.value){
  283. this.notValidationMode(data.prompt);
  284. return false;
  285. }
  286. break;
  287. case "neq":
  288. if (v!=data.value){
  289. this.notValidationMode(data.prompt);
  290. return false;
  291. }
  292. break;
  293. case "contain":
  294. if (v.indexOf(data.value)!=-1){
  295. this.notValidationMode(data.prompt);
  296. return false;
  297. }
  298. break;
  299. case "notcontain":
  300. if (v.indexOf(data.value)==-1){
  301. this.notValidationMode(data.prompt);
  302. return false;
  303. }
  304. break;
  305. }
  306. }
  307. return true;
  308. },
  309. validationConfig: function(routeName, opinion){
  310. debugger;
  311. if (this.json.validationConfig){
  312. if (this.json.validationConfig.length){
  313. for (var i=0; i<this.json.validationConfig.length; i++) {
  314. var data = this.json.validationConfig[i];
  315. if (!this.validationConfigItem(routeName, data)) return false;
  316. }
  317. }
  318. return true;
  319. }
  320. return true;
  321. },
  322. validation: function(routeName, opinion){
  323. if (!this.validationConfig(routeName, opinion)) return false;
  324. if (!this.json.validation) return true;
  325. if (!this.json.validation.code) return true;
  326. var flag = this.form.Macro.exec(this.json.validation.code, this);
  327. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  328. if (flag.toString()!="true"){
  329. this.notValidationMode(flag);
  330. return false;
  331. }
  332. return true;
  333. }
  334. });