$Input.js 15 KB

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