$Input.js 16 KB

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