$Input.js 20 KB

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