$Input.js 20 KB

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