$Input.js 22 KB

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