$Input.js 13 KB

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