$Input.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** Class Input组件 */
  3. MWF.xApplication.process.Xform.$Input = MWF.APP$Input = new Class({
  4. Implements: [Events],
  5. Extends: MWF.APP$Module,
  6. iconStyle: "personfieldIcon",
  7. initialize: function(node, json, form, options){
  8. this.node = $(node);
  9. this.node.store("module", this);
  10. this.json = json;
  11. this.form = form;
  12. this.field = true;
  13. },
  14. _loadUserInterface: function(){
  15. this._loadNode();
  16. if (this.json.compute === "show"){
  17. this._setValue(this._computeValue());
  18. }else{
  19. this._loadValue();
  20. }
  21. },
  22. _loadDomEvents: function(){
  23. Object.each(this.json.events, function(e, key){
  24. if (e.code){
  25. if (this.options.moduleEvents.indexOf(key)===-1){
  26. (this.node.getFirst() || this.node).addEvent(key, function(event){
  27. return this.form.Macro.fire(e.code, this, event);
  28. }.bind(this));
  29. }
  30. }
  31. }.bind(this));
  32. },
  33. _loadEvents: function(){
  34. Object.each(this.json.events, function(e, key){
  35. if (e.code){
  36. if (this.options.moduleEvents.indexOf(key)!==-1){
  37. this.addEvent(key, function(event){
  38. return this.form.Macro.fire(e.code, this, event);
  39. }.bind(this));
  40. }else{
  41. (this.node.getFirst() || this.node).addEvent(key, function(event){
  42. return this.form.Macro.fire(e.code, this, event);
  43. }.bind(this));
  44. }
  45. }
  46. }.bind(this));
  47. },
  48. addModuleEvent: function(key, fun){
  49. if (this.options.moduleEvents.indexOf(key)!==-1){
  50. this.addEvent(key, function(event){
  51. return (fun) ? fun(this, event) : null;
  52. }.bind(this));
  53. }else{
  54. (this.node.getFirst() || this.node).addEvent(key, function(event){
  55. return (fun) ? fun(this, event) : null;
  56. }.bind(this));
  57. }
  58. },
  59. _loadNode: function(){
  60. if (this.readonly){
  61. this._loadNodeRead();
  62. }else{
  63. this._loadNodeEdit();
  64. }
  65. },
  66. _loadNodeRead: function(){
  67. this.node.empty();
  68. },
  69. loadDescription: function(){
  70. if (this.readonly || this.json.isReadonly)return;
  71. var v = this._getBusinessData();
  72. if (!v){
  73. if (this.json.description){
  74. var size = this.node.getFirst().getSize();
  75. var w = size.x-3;
  76. if( this.json.showIcon!='no' && !this.form.json.hideModuleIcon ){
  77. if (COMMON.Browser.safari) w = w-20;
  78. }
  79. this.descriptionNode = new Element("div", {"styles": this.form.css.descriptionNode, "text": this.json.description}).inject(this.node);
  80. this.descriptionNode.setStyles({
  81. "width": ""+w+"px",
  82. "height": ""+size.y+"px",
  83. "line-height": ""+size.y+"px"
  84. });
  85. this.setDescriptionEvent();
  86. }
  87. }
  88. },
  89. setDescriptionEvent: function(){
  90. if (this.descriptionNode){
  91. this.descriptionNode.addEvents({
  92. "mousedown": function(){
  93. this.descriptionNode.setStyle("display", "none");
  94. this.clickSelect();
  95. }.bind(this)
  96. });
  97. this.node.getFirst().addEvents({
  98. "focus": function(){
  99. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  100. }.bind(this),
  101. "blur": function(){
  102. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  103. }.bind(this)
  104. });
  105. }
  106. },
  107. checkDescription: function(){
  108. if (!this.node.getFirst().get("value")){
  109. if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  110. }else{
  111. if (this.descriptionNode) this.descriptionNode.setStyle("display", "none");
  112. }
  113. },
  114. _resetNodeEdit: function(){
  115. var input = new Element("input", {
  116. "styles": {
  117. "background": "transparent",
  118. "width": "100%",
  119. "border": "0px"
  120. },
  121. "readonly": true
  122. });
  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. },
  133. _loadNodeEdit: function(){
  134. if (!this.json.preprocessing) this._resetNodeEdit();
  135. var input = this.node.getFirst();
  136. input.set(this.json.properties);
  137. this.node.set({
  138. "id": this.json.id,
  139. "MWFType": this.json.type,
  140. "readonly": true,
  141. "events": {
  142. "click": this.clickSelect.bind(this)
  143. }
  144. });
  145. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon ){
  146. this.iconNode = new Element("div", {
  147. "styles": this.form.css[this.iconStyle],
  148. "events": {
  149. "click": this.clickSelect.bind(this)
  150. }
  151. }).inject(this.node, "before");
  152. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  153. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  154. }
  155. this.node.getFirst().addEvent("change", function(){
  156. this.validationMode();
  157. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  158. }.bind(this));
  159. },
  160. _loadStyles: function(){
  161. if (this.json.styles) this.node.setStyles(this.json.styles);
  162. if (this.json.inputStyles) if (this.node.getFirst()) this.node.getFirst().setStyles(this.json.inputStyles);
  163. if (this.iconNode && this.iconNode.offsetParent !== null){
  164. var size = this.node.getSize();
  165. //if (!size.y){
  166. // var y1 = this.node.getStyle("height");
  167. // var y2 = this.node.getFirst().getStyle("height");
  168. // alert(y1+"," +y2);
  169. // var y = ((y1!="auto" && y1>y2) || y2=="auto") ? y1 : y2;
  170. // size.y = (y=="auto") ? "auto" : y.toInt();
  171. // //alert(size.y)
  172. //}
  173. this.iconNode.setStyle("height", ""+size.y+"px");
  174. //alert(this.iconNode.getStyle("height"))
  175. }
  176. },
  177. _computeValue: function(value){
  178. return (this.json.defaultValue && this.json.defaultValue.code) ? this.form.Macro.exec(this.json.defaultValue.code, this): (value || "");
  179. },
  180. getValue: function(){
  181. debugger;
  182. if (this.moduleValueAG) return this.moduleValueAG;
  183. var value = this._getBusinessData();
  184. if (!value) value = this._computeValue();
  185. return value || "";
  186. },
  187. _setValue: function(value){
  188. debugger;
  189. // if (value && value.isAG){
  190. // var ag = o2.AG.all(value).then(function(v){
  191. // if (o2.typeOf(v)=="array") v = v[0];
  192. // this.__setValue(v);
  193. // }.bind(this));
  194. // this.moduleValueAG = ag;
  195. // ag.then(function(){
  196. // this.moduleValueAG = null;
  197. // }.bind(this));
  198. // }else {
  199. if (o2.typeOf(value.then)=="function"){
  200. var p = o2.promiseAll(value).then(function(v){
  201. this.__setValue(v);
  202. }.bind(this));
  203. this.moduleValueAG = p;
  204. p.then(function(){
  205. this.moduleValueAG = null;
  206. }.bind(this));
  207. }else{
  208. this.moduleValueAG = null;
  209. this.__setValue(value);
  210. }
  211. //this.__setValue(value);
  212. // }
  213. },
  214. __setValue: function(value){
  215. this._setBusinessData(value);
  216. if (this.node.getFirst()) this.node.getFirst().set("value", value || "");
  217. if (this.readonly || this.json.isReadonly) this.node.set("text", value);
  218. this.moduleValueAG = null;
  219. return value;
  220. },
  221. _loadValue: function(){
  222. this._setValue(this.getValue());
  223. },
  224. clickSelect: function(){
  225. },
  226. _afterLoaded: function(){
  227. // if (this.iconNode){
  228. //// var p = this.node.getPosition();
  229. //// var s = this.node.getSize();
  230. //// var is = this.iconNode.getSize();
  231. ////
  232. //// var y = p.y;
  233. //// var x = p.x+s.x-is.x;
  234. // this.iconNode.setStyles({
  235. // "top": "5px",
  236. // "left": "-18px"
  237. // });
  238. // }
  239. if (!this.readonly && !this.json.isReadonly ){
  240. this.loadDescription();
  241. }
  242. },
  243. getTextData: function(){
  244. //var value = this.node.get("value");
  245. //var text = this.node.get("text");
  246. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  247. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  248. return {"value": [value || ""] , "text": [text || value || ""]};
  249. },
  250. /**
  251. * 判断组件值是否为空.
  252. * @return {boolean}.
  253. */
  254. isEmpty : function(){
  255. var data = this.getData();
  256. return !data || !data.trim();
  257. },
  258. /**
  259. * 获取组件值.
  260. * @return {object/string}.
  261. */
  262. getData: function(when){
  263. if (this.json.compute == "save") this._setValue(this._computeValue());
  264. return this.getInputData();
  265. },
  266. getInputData: function(){
  267. if (this.node.getFirst()){
  268. return this.node.getFirst().get("value");
  269. }else{
  270. return this._getBusinessData();
  271. }
  272. },
  273. /**
  274. * 重置组件的值,如果设置了默认值,则设置为默认值,否则置空。
  275. */
  276. resetData: function(){
  277. this.setData(this.getValue());
  278. },
  279. /**
  280. * 为控件赋值。
  281. * @param {string/number/jsonObject} .
  282. */
  283. setData: function(data){
  284. // if (data && data.isAG){
  285. // var ag = o2.AG.all(data).then(function(v){
  286. // if (o2.typeOf(v)=="array") v = v[0];
  287. // this.__setData(v);
  288. // }.bind(this));
  289. // this.moduleValueAG = ag;
  290. // ag.then(function(){
  291. // this.moduleValueAG = null;
  292. // }.bind(this));
  293. // }else{
  294. if (o2.typeOf(data.then)=="function"){
  295. var p = o2.promiseAll(data).then(function(v){
  296. this.__setValue(v);
  297. }.bind(this));
  298. this.moduleValueAG = p;
  299. p.then(function(){
  300. this.moduleValueAG = null;
  301. }.bind(this));
  302. }else{
  303. this.moduleValueAG = null;
  304. this.__setValue(data);
  305. }
  306. //this.__setData(data);
  307. //}
  308. },
  309. __setData: function(data){
  310. this._setBusinessData(data);
  311. if (this.node.getFirst()){
  312. this.node.getFirst().set("value", data);
  313. this.checkDescription();
  314. this.validationMode();
  315. }else{
  316. this.node.set("text", data);
  317. }
  318. this.moduleValueAG = null;
  319. },
  320. createErrorNode: function(text){
  321. //var size = this.node.getFirst().getSize();
  322. //var w = size.x-3;
  323. //if (COMMON.Browser.safari) w = w-20;
  324. //node.setStyles({
  325. // "width": ""+w+"px",
  326. // "height": ""+size.y+"px",
  327. // "line-height": ""+size.y+"px",
  328. // "position": "absolute",
  329. // "top": "0px"
  330. //});
  331. var node;
  332. if( this.form.json.errorStyle ){
  333. if( this.form.json.errorStyle.type === "notice" ){
  334. if( !this.form.errorNoticing ){ //如果是弹出
  335. this.form.errorNoticing = true;
  336. this.form.notice(text, "error", this.node, null, null, {
  337. onClose : function () {
  338. this.form.errorNoticing = false;
  339. }.bind(this)
  340. });
  341. }
  342. }else{
  343. node = new Element("div",{
  344. "styles" : this.form.json.errorStyle.node,
  345. "text": text
  346. });
  347. if( this.form.json.errorStyle.close ){
  348. var closeNode = new Element("div",{
  349. "styles" : this.form.json.errorStyle.close ,
  350. "events": {
  351. "click" : function(){
  352. this.destroy();
  353. }.bind(node)
  354. }
  355. }).inject(node);
  356. }
  357. }
  358. }else{
  359. node = new Element("div");
  360. var iconNode = new Element("div", {
  361. "styles": {
  362. "width": "20px",
  363. "height": "20px",
  364. "float": "left",
  365. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  366. }
  367. }).inject(node);
  368. var textNode = new Element("div", {
  369. "styles": {
  370. "height": "20px",
  371. "line-height": "20px",
  372. "margin-left": "20px",
  373. "color": "red",
  374. "word-break": "keep-all"
  375. },
  376. "text": text
  377. }).inject(node);
  378. }
  379. return node;
  380. },
  381. notValidationMode: function(text){
  382. if (!this.isNotValidationMode){
  383. debugger;
  384. this.isNotValidationMode = true;
  385. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  386. this.node.setStyle("border-color", "red");
  387. this.errNode = this.createErrorNode(text);
  388. //if (this.iconNode){
  389. // this.errNode.inject(this.iconNode, "after");
  390. //}else{
  391. this.errNode.inject(this.node, "after");
  392. //}
  393. this.showNotValidationMode(this.node);
  394. var parentNode = this.node;
  395. while( parentNode.offsetParent === null ){
  396. parentNode = parentNode.getParent();
  397. }
  398. if (!parentNode.isIntoView()) parentNode.scrollIntoView();
  399. }
  400. },
  401. showNotValidationMode: function(node){
  402. var p = node.getParent("div");
  403. if (p){
  404. var mwftype = p.get("MWFtype") || p.get("mwftype");
  405. if (mwftype == "tab$Content"){
  406. if (p.getParent("div").getStyle("display")=="none"){
  407. var contentAreaNode = p.getParent("div").getParent("div");
  408. var tabAreaNode = contentAreaNode.getPrevious("div");
  409. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  410. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  411. tabNode.click();
  412. p = tabAreaNode.getParent("div");
  413. }
  414. }
  415. this.showNotValidationMode(p);
  416. }
  417. },
  418. validationMode: function(){
  419. if (this.isNotValidationMode){
  420. this.isNotValidationMode = false;
  421. this.node.setStyles(this.node.retrieve("borderStyle"));
  422. if (this.errNode){
  423. this.errNode.destroy();
  424. this.errNode = null;
  425. }
  426. }
  427. },
  428. validationConfigItem: function(routeName, data){
  429. var flag = (data.status==="all") ? true: (routeName === data.decision);
  430. if (flag){
  431. var n = this.getInputData();
  432. var v = (data.valueType==="value") ? n : n.length;
  433. switch (data.operateor){
  434. case "isnull":
  435. if (!v){
  436. this.notValidationMode(data.prompt);
  437. return false;
  438. }
  439. break;
  440. case "notnull":
  441. if (v){
  442. this.notValidationMode(data.prompt);
  443. return false;
  444. }
  445. break;
  446. case "gt":
  447. if (v>data.value){
  448. this.notValidationMode(data.prompt);
  449. return false;
  450. }
  451. break;
  452. case "lt":
  453. if (v<data.value){
  454. this.notValidationMode(data.prompt);
  455. return false;
  456. }
  457. break;
  458. case "equal":
  459. if (v==data.value){
  460. this.notValidationMode(data.prompt);
  461. return false;
  462. }
  463. break;
  464. case "neq":
  465. if (v!=data.value){
  466. this.notValidationMode(data.prompt);
  467. return false;
  468. }
  469. break;
  470. case "contain":
  471. if (v.indexOf(data.value)!=-1){
  472. this.notValidationMode(data.prompt);
  473. return false;
  474. }
  475. break;
  476. case "notcontain":
  477. if (v.indexOf(data.value)==-1){
  478. this.notValidationMode(data.prompt);
  479. return false;
  480. }
  481. break;
  482. }
  483. }
  484. return true;
  485. },
  486. validationConfig: function(routeName, opinion){
  487. if (this.json.validationConfig){
  488. if (this.json.validationConfig.length){
  489. for (var i=0; i<this.json.validationConfig.length; i++) {
  490. var data = this.json.validationConfig[i];
  491. if (!this.validationConfigItem(routeName, data)) return false;
  492. }
  493. }
  494. return true;
  495. }
  496. return true;
  497. },
  498. /**
  499. * 根据组件的校验设置进行校验。
  500. * @param {string} routeName-路由名称.
  501. * @return {boolean} 是否通过校验
  502. */
  503. validation: function(routeName, opinion){
  504. if (!this.readonly && !this.json.isReadonly){
  505. if (!this.validationConfig(routeName, opinion)) return false;
  506. if (!this.json.validation) return true;
  507. if (!this.json.validation.code) return true;
  508. this.currentRouteName = routeName;
  509. var flag = this.form.Macro.exec(this.json.validation.code, this);
  510. this.currentRouteName = "";
  511. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  512. if (flag.toString()!="true"){
  513. this.notValidationMode(flag);
  514. return false;
  515. }
  516. }
  517. return true;
  518. }
  519. });