$Input.js 20 KB

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