$Input.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. var value = this._getBusinessData();
  181. if (!value && this.moduleValueAG) value = this.moduleValueAG;
  182. if (!value) value = this._computeValue();
  183. return value || "";
  184. },
  185. _setValue: function(value){
  186. if (o2.typeOf(value)==="function" && value.addResolve){
  187. this.moduleValueAG = value;
  188. value.addResolve(function(v){
  189. this._setValue(v);
  190. // this._setBusinessData(v);
  191. // if (this.node.getFirst()) this.node.getFirst().set("value", v || "");
  192. // if (this.readonly || this.json.isReadonly) this.node.set("text", v);
  193. }.bind(this));
  194. }else{
  195. this._setBusinessData(value);
  196. if (this.node.getFirst()) this.node.getFirst().set("value", value || "");
  197. if (this.readonly || this.json.isReadonly) this.node.set("text", value);
  198. this.moduleValueAG = null;
  199. }
  200. },
  201. _loadValue: function(){
  202. this._setValue(this.getValue());
  203. },
  204. clickSelect: function(){
  205. },
  206. _afterLoaded: function(){
  207. // if (this.iconNode){
  208. //// var p = this.node.getPosition();
  209. //// var s = this.node.getSize();
  210. //// var is = this.iconNode.getSize();
  211. ////
  212. //// var y = p.y;
  213. //// var x = p.x+s.x-is.x;
  214. // this.iconNode.setStyles({
  215. // "top": "5px",
  216. // "left": "-18px"
  217. // });
  218. // }
  219. if (!this.readonly && !this.json.isReadonly ){
  220. this.loadDescription();
  221. }
  222. },
  223. getTextData: function(){
  224. //var value = this.node.get("value");
  225. //var text = this.node.get("text");
  226. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  227. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  228. return {"value": [value || ""] , "text": [text || value || ""]};
  229. },
  230. isEmpty : function(){
  231. var data = this.getData();
  232. return !data || !data.trim();
  233. },
  234. getData: function(when){
  235. if (this.json.compute == "save") this._setValue(this._computeValue());
  236. return this.getInputData();
  237. },
  238. getInputData: function(){
  239. if (this.node.getFirst()){
  240. return this.node.getFirst().get("value");
  241. }else{
  242. return this._getBusinessData();
  243. }
  244. },
  245. resetData: function(){
  246. this.setData(this.getValue());
  247. },
  248. setData: function(data){
  249. if (o2.typeOf(data)==="function" && data.addResolve){
  250. data.addResolve(function(v){
  251. this.setData(v);
  252. }.bind(this));
  253. }else{
  254. this._setBusinessData(data);
  255. if (this.node.getFirst()){
  256. this.node.getFirst().set("value", data);
  257. this.checkDescription();
  258. this.validationMode();
  259. }else{
  260. this.node.set("text", data);
  261. }
  262. }
  263. },
  264. createErrorNode: function(text){
  265. //var size = this.node.getFirst().getSize();
  266. //var w = size.x-3;
  267. //if (COMMON.Browser.safari) w = w-20;
  268. //node.setStyles({
  269. // "width": ""+w+"px",
  270. // "height": ""+size.y+"px",
  271. // "line-height": ""+size.y+"px",
  272. // "position": "absolute",
  273. // "top": "0px"
  274. //});
  275. var node;
  276. if( this.form.json.errorStyle ){
  277. if( this.form.json.errorStyle.type === "notice" ){
  278. if( !this.form.errorNoticing ){ //如果是弹出
  279. this.form.errorNoticing = true;
  280. this.form.notice(text, "error", this.node, null, null, {
  281. onClose : function () {
  282. this.form.errorNoticing = false;
  283. }.bind(this)
  284. });
  285. }
  286. }else{
  287. node = new Element("div",{
  288. "styles" : this.form.json.errorStyle.node,
  289. "text": text
  290. });
  291. if( this.form.json.errorStyle.close ){
  292. var closeNode = new Element("div",{
  293. "styles" : this.form.json.errorStyle.close ,
  294. "events": {
  295. "click" : function(){
  296. this.destroy();
  297. }.bind(node)
  298. }
  299. }).inject(node);
  300. }
  301. }
  302. }else{
  303. node = new Element("div");
  304. var iconNode = new Element("div", {
  305. "styles": {
  306. "width": "20px",
  307. "height": "20px",
  308. "float": "left",
  309. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  310. }
  311. }).inject(node);
  312. var textNode = new Element("div", {
  313. "styles": {
  314. "height": "20px",
  315. "line-height": "20px",
  316. "margin-left": "20px",
  317. "color": "red",
  318. "word-break": "keep-all"
  319. },
  320. "text": text
  321. }).inject(node);
  322. }
  323. return node;
  324. },
  325. notValidationMode: function(text){
  326. if (!this.isNotValidationMode){
  327. debugger;
  328. this.isNotValidationMode = true;
  329. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  330. this.node.setStyle("border-color", "red");
  331. this.errNode = this.createErrorNode(text);
  332. //if (this.iconNode){
  333. // this.errNode.inject(this.iconNode, "after");
  334. //}else{
  335. this.errNode.inject(this.node, "after");
  336. //}
  337. this.showNotValidationMode(this.node);
  338. var parentNode = this.node;
  339. while( parentNode.offsetParent === null ){
  340. parentNode = parentNode.getParent();
  341. }
  342. if (!parentNode.isIntoView()) parentNode.scrollIntoView();
  343. }
  344. },
  345. showNotValidationMode: function(node){
  346. var p = node.getParent("div");
  347. if (p){
  348. var mwftype = p.get("MWFtype") || p.get("mwftype");
  349. if (mwftype == "tab$Content"){
  350. if (p.getParent("div").getStyle("display")=="none"){
  351. var contentAreaNode = p.getParent("div").getParent("div");
  352. var tabAreaNode = contentAreaNode.getPrevious("div");
  353. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  354. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  355. tabNode.click();
  356. p = tabAreaNode.getParent("div");
  357. }
  358. }
  359. this.showNotValidationMode(p);
  360. }
  361. },
  362. validationMode: function(){
  363. if (this.isNotValidationMode){
  364. this.isNotValidationMode = false;
  365. this.node.setStyles(this.node.retrieve("borderStyle"));
  366. if (this.errNode){
  367. this.errNode.destroy();
  368. this.errNode = null;
  369. }
  370. }
  371. },
  372. validationConfigItem: function(routeName, data){
  373. var flag = (data.status==="all") ? true: (routeName === data.decision);
  374. if (flag){
  375. var n = this.getInputData();
  376. var v = (data.valueType==="value") ? n : n.length;
  377. switch (data.operateor){
  378. case "isnull":
  379. if (!v){
  380. this.notValidationMode(data.prompt);
  381. return false;
  382. }
  383. break;
  384. case "notnull":
  385. if (v){
  386. this.notValidationMode(data.prompt);
  387. return false;
  388. }
  389. break;
  390. case "gt":
  391. if (v>data.value){
  392. this.notValidationMode(data.prompt);
  393. return false;
  394. }
  395. break;
  396. case "lt":
  397. if (v<data.value){
  398. this.notValidationMode(data.prompt);
  399. return false;
  400. }
  401. break;
  402. case "equal":
  403. if (v==data.value){
  404. this.notValidationMode(data.prompt);
  405. return false;
  406. }
  407. break;
  408. case "neq":
  409. if (v!=data.value){
  410. this.notValidationMode(data.prompt);
  411. return false;
  412. }
  413. break;
  414. case "contain":
  415. if (v.indexOf(data.value)!=-1){
  416. this.notValidationMode(data.prompt);
  417. return false;
  418. }
  419. break;
  420. case "notcontain":
  421. if (v.indexOf(data.value)==-1){
  422. this.notValidationMode(data.prompt);
  423. return false;
  424. }
  425. break;
  426. }
  427. }
  428. return true;
  429. },
  430. validationConfig: function(routeName, opinion){
  431. if (this.json.validationConfig){
  432. if (this.json.validationConfig.length){
  433. for (var i=0; i<this.json.validationConfig.length; i++) {
  434. var data = this.json.validationConfig[i];
  435. if (!this.validationConfigItem(routeName, data)) return false;
  436. }
  437. }
  438. return true;
  439. }
  440. return true;
  441. },
  442. validation: function(routeName, opinion){
  443. if (!this.readonly && !this.json.isReadonly){
  444. if (!this.validationConfig(routeName, opinion)) return false;
  445. if (!this.json.validation) return true;
  446. if (!this.json.validation.code) return true;
  447. this.currentRouteName = routeName;
  448. var flag = this.form.Macro.exec(this.json.validation.code, this);
  449. this.currentRouteName = "";
  450. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  451. if (flag.toString()!="true"){
  452. this.notValidationMode(flag);
  453. return false;
  454. }
  455. }
  456. return true;
  457. }
  458. });