$Input.js 17 KB

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