$Input.js 18 KB

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