$Input.js 20 KB

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