$Input.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class $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. * @summary 描述信息节点,允许用户手工输入的组件才有此节点,只读情况下无此节点.
  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. /**
  258. * @summary 判断组件是否只读.
  259. * @example
  260. * var readonly = this.form.get('subject').isReadonly();
  261. * @return {Boolean} 是否只读.
  262. */
  263. isReadonly : function(){
  264. return !!(this.readonly || this.json.isReadonly);
  265. },
  266. getTextData: function(){
  267. //var value = this.node.get("value");
  268. //var text = this.node.get("text");
  269. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  270. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  271. return {"value": [value || ""] , "text": [text || value || ""]};
  272. },
  273. /**
  274. * @summary 判断组件值是否为空.
  275. * @example
  276. * if( this.form.get('subject').isEmpty() ){
  277. * this.form.notice('标题不能为空', 'warn');
  278. * }
  279. * @return {Boolean} 值是否为空.
  280. */
  281. isEmpty : function(){
  282. var data = this.getData();
  283. return !data || !data.trim();
  284. },
  285. /**
  286. * 在脚本中使用 this.data[fieldName] 也可以获取组件值。
  287. * 区别如下:<br/>
  288. * 1、当使用Promise的时候<br/>
  289. * 使用异步函数生成器(Promise)为组件赋值的时候,用getData方法立即获取数据,可能返回修改前的值,当Promise执行完成以后,会返回修改后的值。<br/>
  290. * this.data[fieldName] 立即获取数据,可能获取到异步函数生成器,当Promise执行完成以后,会返回修改后的值。<br/>
  291. * {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0#EggIl|具体差异请查看链接}<br/>
  292. * 2、当表单上没有对应组件的时候,可以使用this.data[fieldName]获取值,但是this.form.get('fieldName')无法获取到组件。
  293. * @summary 获取组件值。
  294. * @example
  295. * var data = this.form.get('fieldName').getData(); //没有使用promise的情况、
  296. * @example
  297. * //如果无法确定表单上是否有组件,需要判断
  298. * var data;
  299. * if( this.form.get('fieldName') ){ //判断表单是否有无对应组件
  300. * data = this.form.get('fieldName').getData();
  301. * }else{
  302. * data = this.data['fieldName']; //直接从数据中获取字段值
  303. * }
  304. * @example
  305. * //使用Promise的情况
  306. * var field = this.form.get("fieldName");
  307. * var dict = new this.Dict("test"); //test为数据字典名称
  308. * var promise = dict.get("tools", true); //异步使用数据字典的get方法时返回Promise,参数true表示异步
  309. * promise.then( function(){
  310. * var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获取到了数据字典的值
  311. * })
  312. * field.setData( promise );
  313. * @return 组件的数据.
  314. */
  315. getData: function(when){
  316. if (this.json.compute == "save") this._setValue(this._computeValue());
  317. return this.getInputData();
  318. },
  319. getInputData: function(){
  320. if (this.node.getFirst()){
  321. return this.node.getFirst().get("value");
  322. }else{
  323. return this._getBusinessData();
  324. }
  325. },
  326. /**
  327. * @summary 重置组件的值为默认值或置空。
  328. * @example
  329. * this.form.get('subject').resetData();
  330. */
  331. resetData: function(){
  332. this.setData(this.getValue());
  333. },
  334. /**当参数为Promise的时候,请参考文档: {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
  335. * 当表单上没有对应组件的时候,可以使用this.data[fieldName] = data赋值。
  336. * @summary 为组件赋值。
  337. * @param data{String|Promise} .
  338. * @example
  339. * this.form.get("fieldName").setData("test"); //赋文本值
  340. * @example
  341. * //如果无法确定表单上是否有组件,需要判断
  342. * if( this.form.get('fieldName') ){ //判断表单是否有无对应组件
  343. * this.form.get('fieldName').setData( data );
  344. * }else{
  345. * this.data['fieldName'] = data;
  346. * }
  347. * @example
  348. * //使用Promise
  349. * var field = this.form.get("fieldName");
  350. * var dict = new this.Dict("test"); //test为数据字典名称
  351. * var promise = dict.get("tools", true); //异步使用数据字典的get方法时返回Promise,参数true表示异步
  352. * field.setData( promise );
  353. */
  354. setData: function(data){
  355. // if (data && data.isAG){
  356. // var ag = o2.AG.all(data).then(function(v){
  357. // if (o2.typeOf(v)=="array") v = v[0];
  358. // this.__setData(v);
  359. // }.bind(this));
  360. // this.moduleValueAG = ag;
  361. // ag.then(function(){
  362. // this.moduleValueAG = null;
  363. // }.bind(this));
  364. // }else{
  365. if (!!data && o2.typeOf(data.then)=="function"){
  366. var p = o2.promiseAll(data).then(function(v){
  367. this.__setData(v);
  368. // if (this.node.getFirst() && !this.readonly && !this.json.isReadonly) {
  369. // this.checkDescription();
  370. // this.validationMode();
  371. // }
  372. }.bind(this), function(){});
  373. this.moduleValueAG = p;
  374. p.then(function(){
  375. this.moduleValueAG = null;
  376. }.bind(this), function(){
  377. this.moduleValueAG = null;
  378. }.bind(this));
  379. }else{
  380. this.moduleValueAG = null;
  381. this.__setData(data);
  382. // if (this.node.getFirst() && !this.readonly && !this.json.isReadonly) {
  383. // this.checkDescription();
  384. // this.validationMode();
  385. // }
  386. }
  387. //this.__setData(data);
  388. //}
  389. },
  390. __setData: function(data){
  391. this._setBusinessData(data);
  392. if (this.node.getFirst()){
  393. this.node.getFirst().set("value", data);
  394. this.checkDescription();
  395. this.validationMode();
  396. }else{
  397. this.node.set("text", data);
  398. }
  399. this.moduleValueAG = null;
  400. },
  401. createErrorNode: function(text){
  402. //var size = this.node.getFirst().getSize();
  403. //var w = size.x-3;
  404. //if (COMMON.Browser.safari) w = w-20;
  405. //node.setStyles({
  406. // "width": ""+w+"px",
  407. // "height": ""+size.y+"px",
  408. // "line-height": ""+size.y+"px",
  409. // "position": "absolute",
  410. // "top": "0px"
  411. //});
  412. var node;
  413. if( this.form.json.errorStyle ){
  414. if( this.form.json.errorStyle.type === "notice" ){
  415. if( !this.form.errorNoticing ){ //如果是弹出
  416. this.form.errorNoticing = true;
  417. this.form.notice(text, "error", this.node, null, null, {
  418. onClose : function () {
  419. this.form.errorNoticing = false;
  420. }.bind(this)
  421. });
  422. }
  423. }else{
  424. node = new Element("div",{
  425. "styles" : this.form.json.errorStyle.node,
  426. "text": text
  427. });
  428. if( this.form.json.errorStyle.close ){
  429. var closeNode = new Element("div",{
  430. "styles" : this.form.json.errorStyle.close ,
  431. "events": {
  432. "click" : function(){
  433. this.destroy();
  434. }.bind(node)
  435. }
  436. }).inject(node);
  437. }
  438. }
  439. }else{
  440. node = new Element("div");
  441. var iconNode = new Element("div", {
  442. "styles": {
  443. "width": "20px",
  444. "height": "20px",
  445. "float": "left",
  446. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  447. }
  448. }).inject(node);
  449. var textNode = new Element("div", {
  450. "styles": {
  451. "height": "20px",
  452. "line-height": "20px",
  453. "margin-left": "20px",
  454. "color": "red",
  455. "word-break": "keep-all"
  456. },
  457. "text": text
  458. }).inject(node);
  459. }
  460. return node;
  461. },
  462. notValidationMode: function(text){
  463. if (!this.isNotValidationMode){
  464. this.isNotValidationMode = true;
  465. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  466. this.node.setStyle("border-color", "red");
  467. this.errNode = this.createErrorNode(text);
  468. //if (this.iconNode){
  469. // this.errNode.inject(this.iconNode, "after");
  470. //}else{
  471. this.errNode.inject(this.node, "after");
  472. //}
  473. this.showNotValidationMode(this.node);
  474. var parentNode = this.node;
  475. while( parentNode.offsetParent === null ){
  476. parentNode = parentNode.getParent();
  477. }
  478. if (!parentNode.isIntoView()) parentNode.scrollIntoView();
  479. }
  480. },
  481. showNotValidationMode: function(node){
  482. var p = node.getParent("div");
  483. if (p){
  484. var mwftype = p.get("MWFtype") || p.get("mwftype");
  485. if (mwftype == "tab$Content"){
  486. if (p.getParent("div").getStyle("display")=="none"){
  487. var contentAreaNode = p.getParent("div").getParent("div");
  488. var tabAreaNode = contentAreaNode.getPrevious("div");
  489. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  490. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  491. tabNode.click();
  492. p = tabAreaNode.getParent("div");
  493. }
  494. }
  495. this.showNotValidationMode(p);
  496. }
  497. },
  498. validationMode: function(){
  499. if (this.isNotValidationMode){
  500. this.isNotValidationMode = false;
  501. this.node.setStyles(this.node.retrieve("borderStyle"));
  502. if (this.errNode){
  503. this.errNode.destroy();
  504. this.errNode = null;
  505. }
  506. }
  507. },
  508. validationConfigItem: function(routeName, data){
  509. var flag = (data.status==="all") ? true: (routeName === data.decision);
  510. if (flag){
  511. var n = this.getInputData();
  512. var v = (data.valueType==="value") ? n : n.length;
  513. switch (data.operateor){
  514. case "isnull":
  515. if (!v){
  516. this.notValidationMode(data.prompt);
  517. return false;
  518. }
  519. break;
  520. case "notnull":
  521. if (v){
  522. this.notValidationMode(data.prompt);
  523. return false;
  524. }
  525. break;
  526. case "gt":
  527. if (v>data.value){
  528. this.notValidationMode(data.prompt);
  529. return false;
  530. }
  531. break;
  532. case "lt":
  533. if (v<data.value){
  534. this.notValidationMode(data.prompt);
  535. return false;
  536. }
  537. break;
  538. case "equal":
  539. if (v==data.value){
  540. this.notValidationMode(data.prompt);
  541. return false;
  542. }
  543. break;
  544. case "neq":
  545. if (v!=data.value){
  546. this.notValidationMode(data.prompt);
  547. return false;
  548. }
  549. break;
  550. case "contain":
  551. if (v.indexOf(data.value)!=-1){
  552. this.notValidationMode(data.prompt);
  553. return false;
  554. }
  555. break;
  556. case "notcontain":
  557. if (v.indexOf(data.value)==-1){
  558. this.notValidationMode(data.prompt);
  559. return false;
  560. }
  561. break;
  562. }
  563. }
  564. return true;
  565. },
  566. validationConfig: function(routeName, opinion){
  567. if (this.json.validationConfig){
  568. if (this.json.validationConfig.length){
  569. for (var i=0; i<this.json.validationConfig.length; i++) {
  570. var data = this.json.validationConfig[i];
  571. if (!this.validationConfigItem(routeName, data)) return false;
  572. }
  573. }
  574. return true;
  575. }
  576. return true;
  577. },
  578. /**
  579. * @summary 根据组件的校验设置进行校验。
  580. * @param {String} [routeName] - 可选,路由名称.
  581. * @example
  582. * if( !this.form.get('fieldName').validation() ){
  583. * return false;
  584. * }
  585. * @return {Boolean} 是否通过校验
  586. */
  587. validation: function(routeName, opinion){
  588. if (!this.readonly && !this.json.isReadonly){
  589. if (!this.validationConfig(routeName, opinion)) return false;
  590. if (!this.json.validation) return true;
  591. if (!this.json.validation.code) return true;
  592. this.currentRouteName = routeName;
  593. var flag = this.form.Macro.exec(this.json.validation.code, this);
  594. this.currentRouteName = "";
  595. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  596. if (flag.toString()!="true"){
  597. this.notValidationMode(flag);
  598. return false;
  599. }
  600. }
  601. return true;
  602. }
  603. });