$Input.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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.json.isReadonly) 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. if (this.node.getFirst()){
  190. return this.node.getFirst().get("value");
  191. }else{
  192. return this._getBusinessData();
  193. }
  194. },
  195. resetData: function(){
  196. this.setData(this.getValue());
  197. },
  198. setData: function(data){
  199. this._setBusinessData(data);
  200. if (this.node.getFirst()){
  201. this.node.getFirst().set("value", data);
  202. this.checkDescription();
  203. this.validationMode();
  204. }else{
  205. this.node.set("text", data);
  206. }
  207. },
  208. createErrorNode: function(text){
  209. var node = new Element("div");
  210. //var size = this.node.getFirst().getSize();
  211. //var w = size.x-3;
  212. //if (COMMON.Browser.safari) w = w-20;
  213. //node.setStyles({
  214. // "width": ""+w+"px",
  215. // "height": ""+size.y+"px",
  216. // "line-height": ""+size.y+"px",
  217. // "position": "absolute",
  218. // "top": "0px"
  219. //});
  220. var iconNode = new Element("div", {
  221. "styles": {
  222. "width": "20px",
  223. "height": "20px",
  224. "float": "left",
  225. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  226. }
  227. }).inject(node);
  228. var textNode = new Element("div", {
  229. "styles": {
  230. "height": "20px",
  231. "line-height": "20px",
  232. "margin-left": "20px",
  233. "color": "red",
  234. "word-break": "keep-all"
  235. },
  236. "text": text
  237. }).inject(node);
  238. return node;
  239. },
  240. notValidationMode: function(text){
  241. if (!this.isNotValidationMode){
  242. this.isNotValidationMode = true;
  243. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  244. this.node.setStyle("border-color", "red");
  245. this.errNode = this.createErrorNode(text);
  246. //if (this.iconNode){
  247. // this.errNode.inject(this.iconNode, "after");
  248. //}else{
  249. this.errNode.inject(this.node, "after");
  250. //}
  251. this.showNotValidationMode(this.node);
  252. if (!this.node.isIntoView()) this.node.scrollIntoView();
  253. }
  254. },
  255. showNotValidationMode: function(node){
  256. debugger;
  257. var p = node.getParent("div");
  258. if (p){
  259. var mwftype = p.get("MWFtype") || p.get("mwftype");
  260. if (mwftype == "tab$Content"){
  261. if (p.getParent("div").getStyle("display")=="none"){
  262. var contentAreaNode = p.getParent("div").getParent("div");
  263. var tabAreaNode = contentAreaNode.getPrevious("div");
  264. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  265. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  266. tabNode.click();
  267. p = tabAreaNode.getParent("div");
  268. }
  269. }
  270. this.showNotValidationMode(p);
  271. }
  272. },
  273. validationMode: function(){
  274. if (this.isNotValidationMode){
  275. this.isNotValidationMode = false;
  276. this.node.setStyles(this.node.retrieve("borderStyle"));
  277. if (this.errNode){
  278. this.errNode.destroy();
  279. this.errNode = null;
  280. }
  281. }
  282. },
  283. validationConfigItem: function(routeName, data){
  284. var flag = (data.status==="all") ? true: (routeName === data.decision);
  285. if (flag){
  286. var n = this.getInputData();
  287. var v = (data.valueType==="value") ? n : n.length;
  288. switch (data.operateor){
  289. case "isnull":
  290. if (!v){
  291. this.notValidationMode(data.prompt);
  292. return false;
  293. }
  294. break;
  295. case "notnull":
  296. if (v){
  297. this.notValidationMode(data.prompt);
  298. return false;
  299. }
  300. break;
  301. case "gt":
  302. if (v>data.value){
  303. this.notValidationMode(data.prompt);
  304. return false;
  305. }
  306. break;
  307. case "lt":
  308. if (v<data.value){
  309. this.notValidationMode(data.prompt);
  310. return false;
  311. }
  312. break;
  313. case "equal":
  314. if (v==data.value){
  315. this.notValidationMode(data.prompt);
  316. return false;
  317. }
  318. break;
  319. case "neq":
  320. if (v!=data.value){
  321. this.notValidationMode(data.prompt);
  322. return false;
  323. }
  324. break;
  325. case "contain":
  326. if (v.indexOf(data.value)!=-1){
  327. this.notValidationMode(data.prompt);
  328. return false;
  329. }
  330. break;
  331. case "notcontain":
  332. if (v.indexOf(data.value)==-1){
  333. this.notValidationMode(data.prompt);
  334. return false;
  335. }
  336. break;
  337. }
  338. }
  339. return true;
  340. },
  341. validationConfig: function(routeName, opinion){
  342. if (this.json.validationConfig){
  343. if (this.json.validationConfig.length){
  344. for (var i=0; i<this.json.validationConfig.length; i++) {
  345. var data = this.json.validationConfig[i];
  346. if (!this.validationConfigItem(routeName, data)) return false;
  347. }
  348. }
  349. return true;
  350. }
  351. return true;
  352. },
  353. validation: function(routeName, opinion){
  354. if (!this.validationConfig(routeName, opinion)) return false;
  355. if (!this.json.validation) return true;
  356. if (!this.json.validation.code) return true;
  357. var flag = this.form.Macro.exec(this.json.validation.code, this);
  358. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  359. if (flag.toString()!="true"){
  360. this.notValidationMode(flag);
  361. return false;
  362. }
  363. return true;
  364. }
  365. });