$Input.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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), function(){});
  205. this.moduleValueAG = p;
  206. p.then(function(){
  207. this.moduleValueAG = null;
  208. }.bind(this), function(){
  209. this.moduleValueAG = null;
  210. }.bind(this));
  211. }else{
  212. this.moduleValueAG = null;
  213. this.__setValue(value);
  214. }
  215. //this.__setValue(value);
  216. // }
  217. },
  218. __setValue: function(value){
  219. this._setBusinessData(value);
  220. if (this.node.getFirst()) this.node.getFirst().set("value", value || "");
  221. if (this.readonly || this.json.isReadonly) this.node.set("text", value);
  222. this.moduleValueAG = null;
  223. return value;
  224. },
  225. _loadValue: function(){
  226. this._setValue(this.getValue());
  227. },
  228. clickSelect: function(){
  229. },
  230. _afterLoaded: function(){
  231. // if (this.iconNode){
  232. //// var p = this.node.getPosition();
  233. //// var s = this.node.getSize();
  234. //// var is = this.iconNode.getSize();
  235. ////
  236. //// var y = p.y;
  237. //// var x = p.x+s.x-is.x;
  238. // this.iconNode.setStyles({
  239. // "top": "5px",
  240. // "left": "-18px"
  241. // });
  242. // }
  243. if (!this.readonly && !this.json.isReadonly ){
  244. this.loadDescription();
  245. }
  246. },
  247. getTextData: function(){
  248. //var value = this.node.get("value");
  249. //var text = this.node.get("text");
  250. var value = (this.node.getFirst()) ? this.node.getFirst().get("value") : this.node.get("text");
  251. var text = (this.node.getFirst()) ? this.node.getFirst().get("text") : this.node.get("text");
  252. return {"value": [value || ""] , "text": [text || value || ""]};
  253. },
  254. /**
  255. * 判断组件值是否为空.
  256. * @return {boolean}.
  257. */
  258. isEmpty : function(){
  259. var data = this.getData();
  260. return !data || !data.trim();
  261. },
  262. /**
  263. * 获取组件值.
  264. * @return {object/string}.
  265. */
  266. getData: function(when){
  267. if (this.json.compute == "save") this._setValue(this._computeValue());
  268. return this.getInputData();
  269. },
  270. getInputData: function(){
  271. if (this.node.getFirst()){
  272. return this.node.getFirst().get("value");
  273. }else{
  274. return this._getBusinessData();
  275. }
  276. },
  277. /**
  278. * 重置组件的值,如果设置了默认值,则设置为默认值,否则置空。
  279. */
  280. resetData: function(){
  281. this.setData(this.getValue());
  282. },
  283. /**
  284. * 为控件赋值。
  285. * @param {string/number/jsonObject} .
  286. */
  287. setData: function(data){
  288. // if (data && data.isAG){
  289. // var ag = o2.AG.all(data).then(function(v){
  290. // if (o2.typeOf(v)=="array") v = v[0];
  291. // this.__setData(v);
  292. // }.bind(this));
  293. // this.moduleValueAG = ag;
  294. // ag.then(function(){
  295. // this.moduleValueAG = null;
  296. // }.bind(this));
  297. // }else{
  298. if (!!data && o2.typeOf(data.then)=="function"){
  299. var p = o2.promiseAll(data).then(function(v){
  300. this.__setValue(v);
  301. }.bind(this), function(){});
  302. this.moduleValueAG = p;
  303. p.then(function(){
  304. this.moduleValueAG = null;
  305. }.bind(this), function(){
  306. this.moduleValueAG = null;
  307. }.bind(this));
  308. }else{
  309. this.moduleValueAG = null;
  310. this.__setValue(data);
  311. }
  312. //this.__setData(data);
  313. //}
  314. },
  315. __setData: function(data){
  316. this._setBusinessData(data);
  317. if (this.node.getFirst()){
  318. this.node.getFirst().set("value", data);
  319. this.checkDescription();
  320. this.validationMode();
  321. }else{
  322. this.node.set("text", data);
  323. }
  324. this.moduleValueAG = null;
  325. },
  326. createErrorNode: function(text){
  327. //var size = this.node.getFirst().getSize();
  328. //var w = size.x-3;
  329. //if (COMMON.Browser.safari) w = w-20;
  330. //node.setStyles({
  331. // "width": ""+w+"px",
  332. // "height": ""+size.y+"px",
  333. // "line-height": ""+size.y+"px",
  334. // "position": "absolute",
  335. // "top": "0px"
  336. //});
  337. var node;
  338. if( this.form.json.errorStyle ){
  339. if( this.form.json.errorStyle.type === "notice" ){
  340. if( !this.form.errorNoticing ){ //如果是弹出
  341. this.form.errorNoticing = true;
  342. this.form.notice(text, "error", this.node, null, null, {
  343. onClose : function () {
  344. this.form.errorNoticing = false;
  345. }.bind(this)
  346. });
  347. }
  348. }else{
  349. node = new Element("div",{
  350. "styles" : this.form.json.errorStyle.node,
  351. "text": text
  352. });
  353. if( this.form.json.errorStyle.close ){
  354. var closeNode = new Element("div",{
  355. "styles" : this.form.json.errorStyle.close ,
  356. "events": {
  357. "click" : function(){
  358. this.destroy();
  359. }.bind(node)
  360. }
  361. }).inject(node);
  362. }
  363. }
  364. }else{
  365. node = new Element("div");
  366. var iconNode = new Element("div", {
  367. "styles": {
  368. "width": "20px",
  369. "height": "20px",
  370. "float": "left",
  371. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  372. }
  373. }).inject(node);
  374. var textNode = new Element("div", {
  375. "styles": {
  376. "height": "20px",
  377. "line-height": "20px",
  378. "margin-left": "20px",
  379. "color": "red",
  380. "word-break": "keep-all"
  381. },
  382. "text": text
  383. }).inject(node);
  384. }
  385. return node;
  386. },
  387. notValidationMode: function(text){
  388. if (!this.isNotValidationMode){
  389. this.isNotValidationMode = true;
  390. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  391. this.node.setStyle("border-color", "red");
  392. this.errNode = this.createErrorNode(text);
  393. //if (this.iconNode){
  394. // this.errNode.inject(this.iconNode, "after");
  395. //}else{
  396. this.errNode.inject(this.node, "after");
  397. //}
  398. this.showNotValidationMode(this.node);
  399. var parentNode = this.node;
  400. while( parentNode.offsetParent === null ){
  401. parentNode = parentNode.getParent();
  402. }
  403. if (!parentNode.isIntoView()) parentNode.scrollIntoView();
  404. }
  405. },
  406. showNotValidationMode: function(node){
  407. var p = node.getParent("div");
  408. if (p){
  409. var mwftype = p.get("MWFtype") || p.get("mwftype");
  410. if (mwftype == "tab$Content"){
  411. if (p.getParent("div").getStyle("display")=="none"){
  412. var contentAreaNode = p.getParent("div").getParent("div");
  413. var tabAreaNode = contentAreaNode.getPrevious("div");
  414. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  415. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  416. tabNode.click();
  417. p = tabAreaNode.getParent("div");
  418. }
  419. }
  420. this.showNotValidationMode(p);
  421. }
  422. },
  423. validationMode: function(){
  424. if (this.isNotValidationMode){
  425. this.isNotValidationMode = false;
  426. this.node.setStyles(this.node.retrieve("borderStyle"));
  427. if (this.errNode){
  428. this.errNode.destroy();
  429. this.errNode = null;
  430. }
  431. }
  432. },
  433. validationConfigItem: function(routeName, data){
  434. var flag = (data.status==="all") ? true: (routeName === data.decision);
  435. if (flag){
  436. var n = this.getInputData();
  437. var v = (data.valueType==="value") ? n : n.length;
  438. switch (data.operateor){
  439. case "isnull":
  440. if (!v){
  441. this.notValidationMode(data.prompt);
  442. return false;
  443. }
  444. break;
  445. case "notnull":
  446. if (v){
  447. this.notValidationMode(data.prompt);
  448. return false;
  449. }
  450. break;
  451. case "gt":
  452. if (v>data.value){
  453. this.notValidationMode(data.prompt);
  454. return false;
  455. }
  456. break;
  457. case "lt":
  458. if (v<data.value){
  459. this.notValidationMode(data.prompt);
  460. return false;
  461. }
  462. break;
  463. case "equal":
  464. if (v==data.value){
  465. this.notValidationMode(data.prompt);
  466. return false;
  467. }
  468. break;
  469. case "neq":
  470. if (v!=data.value){
  471. this.notValidationMode(data.prompt);
  472. return false;
  473. }
  474. break;
  475. case "contain":
  476. if (v.indexOf(data.value)!=-1){
  477. this.notValidationMode(data.prompt);
  478. return false;
  479. }
  480. break;
  481. case "notcontain":
  482. if (v.indexOf(data.value)==-1){
  483. this.notValidationMode(data.prompt);
  484. return false;
  485. }
  486. break;
  487. }
  488. }
  489. return true;
  490. },
  491. validationConfig: function(routeName, opinion){
  492. if (this.json.validationConfig){
  493. if (this.json.validationConfig.length){
  494. for (var i=0; i<this.json.validationConfig.length; i++) {
  495. var data = this.json.validationConfig[i];
  496. if (!this.validationConfigItem(routeName, data)) return false;
  497. }
  498. }
  499. return true;
  500. }
  501. return true;
  502. },
  503. /**
  504. * 根据组件的校验设置进行校验。
  505. * @param {string} routeName-路由名称.
  506. * @return {boolean} 是否通过校验
  507. */
  508. validation: function(routeName, opinion){
  509. if (!this.readonly && !this.json.isReadonly){
  510. if (!this.validationConfig(routeName, opinion)) return false;
  511. if (!this.json.validation) return true;
  512. if (!this.json.validation.code) return true;
  513. this.currentRouteName = routeName;
  514. var flag = this.form.Macro.exec(this.json.validation.code, this);
  515. this.currentRouteName = "";
  516. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  517. if (flag.toString()!="true"){
  518. this.notValidationMode(flag);
  519. return false;
  520. }
  521. }
  522. return true;
  523. }
  524. });