$Input.js 23 KB

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