$Input.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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.__setValue(v);
  305. }.bind(this), function(){});
  306. this.moduleValueAG = p;
  307. p.then(function(){
  308. this.moduleValueAG = null;
  309. }.bind(this), function(){
  310. this.moduleValueAG = null;
  311. }.bind(this));
  312. }else{
  313. this.moduleValueAG = null;
  314. this.__setValue(data);
  315. }
  316. //this.__setData(data);
  317. //}
  318. },
  319. __setData: function(data){
  320. this._setBusinessData(data);
  321. if (this.node.getFirst()){
  322. this.node.getFirst().set("value", data);
  323. this.checkDescription();
  324. this.validationMode();
  325. }else{
  326. this.node.set("text", data);
  327. }
  328. this.moduleValueAG = null;
  329. },
  330. createErrorNode: function(text){
  331. //var size = this.node.getFirst().getSize();
  332. //var w = size.x-3;
  333. //if (COMMON.Browser.safari) w = w-20;
  334. //node.setStyles({
  335. // "width": ""+w+"px",
  336. // "height": ""+size.y+"px",
  337. // "line-height": ""+size.y+"px",
  338. // "position": "absolute",
  339. // "top": "0px"
  340. //});
  341. var node;
  342. if( this.form.json.errorStyle ){
  343. if( this.form.json.errorStyle.type === "notice" ){
  344. if( !this.form.errorNoticing ){ //如果是弹出
  345. this.form.errorNoticing = true;
  346. this.form.notice(text, "error", this.node, null, null, {
  347. onClose : function () {
  348. this.form.errorNoticing = false;
  349. }.bind(this)
  350. });
  351. }
  352. }else{
  353. node = new Element("div",{
  354. "styles" : this.form.json.errorStyle.node,
  355. "text": text
  356. });
  357. if( this.form.json.errorStyle.close ){
  358. var closeNode = new Element("div",{
  359. "styles" : this.form.json.errorStyle.close ,
  360. "events": {
  361. "click" : function(){
  362. this.destroy();
  363. }.bind(node)
  364. }
  365. }).inject(node);
  366. }
  367. }
  368. }else{
  369. node = new Element("div");
  370. var iconNode = new Element("div", {
  371. "styles": {
  372. "width": "20px",
  373. "height": "20px",
  374. "float": "left",
  375. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  376. }
  377. }).inject(node);
  378. var textNode = new Element("div", {
  379. "styles": {
  380. "height": "20px",
  381. "line-height": "20px",
  382. "margin-left": "20px",
  383. "color": "red",
  384. "word-break": "keep-all"
  385. },
  386. "text": text
  387. }).inject(node);
  388. }
  389. return node;
  390. },
  391. notValidationMode: function(text){
  392. if (!this.isNotValidationMode){
  393. this.isNotValidationMode = true;
  394. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  395. this.node.setStyle("border-color", "red");
  396. this.errNode = this.createErrorNode(text);
  397. //if (this.iconNode){
  398. // this.errNode.inject(this.iconNode, "after");
  399. //}else{
  400. this.errNode.inject(this.node, "after");
  401. //}
  402. this.showNotValidationMode(this.node);
  403. var parentNode = this.node;
  404. while( parentNode.offsetParent === null ){
  405. parentNode = parentNode.getParent();
  406. }
  407. if (!parentNode.isIntoView()) parentNode.scrollIntoView();
  408. }
  409. },
  410. showNotValidationMode: function(node){
  411. var p = node.getParent("div");
  412. if (p){
  413. var mwftype = p.get("MWFtype") || p.get("mwftype");
  414. if (mwftype == "tab$Content"){
  415. if (p.getParent("div").getStyle("display")=="none"){
  416. var contentAreaNode = p.getParent("div").getParent("div");
  417. var tabAreaNode = contentAreaNode.getPrevious("div");
  418. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  419. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  420. tabNode.click();
  421. p = tabAreaNode.getParent("div");
  422. }
  423. }
  424. this.showNotValidationMode(p);
  425. }
  426. },
  427. validationMode: function(){
  428. if (this.isNotValidationMode){
  429. this.isNotValidationMode = false;
  430. this.node.setStyles(this.node.retrieve("borderStyle"));
  431. if (this.errNode){
  432. this.errNode.destroy();
  433. this.errNode = null;
  434. }
  435. }
  436. },
  437. validationConfigItem: function(routeName, data){
  438. var flag = (data.status==="all") ? true: (routeName === data.decision);
  439. if (flag){
  440. var n = this.getInputData();
  441. var v = (data.valueType==="value") ? n : n.length;
  442. switch (data.operateor){
  443. case "isnull":
  444. if (!v){
  445. this.notValidationMode(data.prompt);
  446. return false;
  447. }
  448. break;
  449. case "notnull":
  450. if (v){
  451. this.notValidationMode(data.prompt);
  452. return false;
  453. }
  454. break;
  455. case "gt":
  456. if (v>data.value){
  457. this.notValidationMode(data.prompt);
  458. return false;
  459. }
  460. break;
  461. case "lt":
  462. if (v<data.value){
  463. this.notValidationMode(data.prompt);
  464. return false;
  465. }
  466. break;
  467. case "equal":
  468. if (v==data.value){
  469. this.notValidationMode(data.prompt);
  470. return false;
  471. }
  472. break;
  473. case "neq":
  474. if (v!=data.value){
  475. this.notValidationMode(data.prompt);
  476. return false;
  477. }
  478. break;
  479. case "contain":
  480. if (v.indexOf(data.value)!=-1){
  481. this.notValidationMode(data.prompt);
  482. return false;
  483. }
  484. break;
  485. case "notcontain":
  486. if (v.indexOf(data.value)==-1){
  487. this.notValidationMode(data.prompt);
  488. return false;
  489. }
  490. break;
  491. }
  492. }
  493. return true;
  494. },
  495. validationConfig: function(routeName, opinion){
  496. if (this.json.validationConfig){
  497. if (this.json.validationConfig.length){
  498. for (var i=0; i<this.json.validationConfig.length; i++) {
  499. var data = this.json.validationConfig[i];
  500. if (!this.validationConfigItem(routeName, data)) return false;
  501. }
  502. }
  503. return true;
  504. }
  505. return true;
  506. },
  507. /**
  508. * 根据组件的校验设置进行校验。
  509. * @param {string} routeName - 可选,路由名称.
  510. * @return {boolean} 是否通过校验
  511. */
  512. validation: function(routeName, opinion){
  513. if (!this.readonly && !this.json.isReadonly){
  514. if (!this.validationConfig(routeName, opinion)) return false;
  515. if (!this.json.validation) return true;
  516. if (!this.json.validation.code) return true;
  517. this.currentRouteName = routeName;
  518. var flag = this.form.Macro.exec(this.json.validation.code, this);
  519. this.currentRouteName = "";
  520. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  521. if (flag.toString()!="true"){
  522. this.notValidationMode(flag);
  523. return false;
  524. }
  525. }
  526. return true;
  527. }
  528. });