$Input.js 19 KB

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