$Input.js 13 KB

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