$Input.js 16 KB

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