$Input.js 19 KB

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