$Input.js 19 KB

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