Number.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. MWF.xDesktop.requireApp("process.Xform", "Textfield", null, false);
  2. MWF.xApplication.process.Xform.Number = MWF.APPNumber = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APPTextfield,
  5. iconStyle: "numberIcon",
  6. getInputData: function(){
  7. if (this.node.getFirst()){
  8. var v = this.node.getElement("input").get("value");
  9. var n = v.toFloat();
  10. return (isNaN(n)) ? 0 : n;
  11. }else{
  12. return this._getBusinessData();
  13. }
  14. return v;
  15. },
  16. // getInputData: function(){
  17. // var n = this.node.getElement("input").get("value").toFloat();
  18. // if ((isNaN(n))) {this.setData('0')};
  19. // return (isNaN(n)) ? 0 : n;
  20. // },
  21. validationFormat: function(){
  22. debugger;
  23. if( !this.node.getElement("input") )return true;
  24. var n = this.node.getElement("input").get("value");
  25. if (isNaN(n)) {
  26. this.notValidationMode(MWF.xApplication.process.Xform.LP.notValidation_number);
  27. return false;
  28. }
  29. var v = n.toFloat();
  30. if (v){
  31. if (this.json.decimals && (this.json.decimals!="*")){
  32. var p = Math.pow(10,this.json.decimals);
  33. var f_x = Math.round(v*p)/p;
  34. var s_x = f_x.toString();
  35. var pos_decimal = s_x.indexOf('.');
  36. if (pos_decimal < 0){
  37. pos_decimal = s_x.length;
  38. s_x += '.';
  39. }
  40. while (s_x.length <= pos_decimal + 2){
  41. s_x += '0';
  42. }
  43. this.node.getFirst().set("value", s_x);
  44. }
  45. }
  46. return true;
  47. },
  48. validationConfigItem: function(routeName, data){
  49. var flag = (data.status=="all") ? true: (routeName == data.decision);
  50. if (flag){
  51. var n = this.getInputData();
  52. var v = (data.valueType=="value") ? n : n.length;
  53. switch (data.operateor){
  54. case "isnull":
  55. if (!v && v.toString()!=='0'){
  56. this.notValidationMode(data.prompt);
  57. return false;
  58. }
  59. break;
  60. case "notnull":
  61. if (v){
  62. this.notValidationMode(data.prompt);
  63. return false;
  64. }
  65. break;
  66. case "gt":
  67. if (v>data.value){
  68. this.notValidationMode(data.prompt);
  69. return false;
  70. }
  71. break;
  72. case "lt":
  73. if (v<data.value){
  74. this.notValidationMode(data.prompt);
  75. return false;
  76. }
  77. break;
  78. case "equal":
  79. if (v==data.value){
  80. this.notValidationMode(data.prompt);
  81. return false;
  82. }
  83. break;
  84. case "neq":
  85. if (v!=data.value){
  86. this.notValidationMode(data.prompt);
  87. return false;
  88. }
  89. break;
  90. case "contain":
  91. if (v.indexOf(data.value)!=-1){
  92. this.notValidationMode(data.prompt);
  93. return false;
  94. }
  95. break;
  96. case "notcontain":
  97. if (v.indexOf(data.value)==-1){
  98. this.notValidationMode(data.prompt);
  99. return false;
  100. }
  101. break;
  102. }
  103. }
  104. return true;
  105. },
  106. validationConfig: function(routeName, opinion){
  107. if (this.json.validationConfig){
  108. if (this.json.validationConfig.length){
  109. for (var i=0; i<this.json.validationConfig.length; i++) {
  110. var data = this.json.validationConfig[i];
  111. if (!this.validationConfigItem(routeName, data)) return false;
  112. }
  113. }
  114. return true;
  115. }
  116. return true;
  117. },
  118. validation: function(routeName, opinion){
  119. if (!this.readonly && !this.json.isReadonly){
  120. if (!this.validationFormat()) return false;
  121. if (!this.validationConfig(routeName, opinion)) return false;
  122. if (!this.json.validation) return true;
  123. if (!this.json.validation.code) return true;
  124. var flag = this.form.Macro.exec(this.json.validation.code, this);
  125. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  126. if (flag.toString() != "true") {
  127. this.notValidationMode(flag);
  128. return false;
  129. }
  130. }
  131. return true;
  132. },
  133. _loadNodeEdit: function(){
  134. var input = new Element("input", {
  135. "styles": {
  136. "background": "transparent",
  137. "width": "100%",
  138. "border": "0px"
  139. }
  140. });
  141. input.set(this.json.properties);
  142. var node = new Element("div", {"styles": {
  143. "overflow": "hidden",
  144. "position": "relative",
  145. "margin-right": "20px",
  146. "padding-right": "4px"
  147. }}).inject(this.node, "after");
  148. input.inject(node);
  149. this.node.destroy();
  150. this.node = node;
  151. this.node.set({
  152. "id": this.json.id,
  153. "MWFType": this.json.type,
  154. "events": {
  155. "click": this.clickSelect.bind(this)
  156. }
  157. });
  158. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  159. this.iconNode = new Element("div", {
  160. "styles": this.form.css[this.iconStyle]
  161. }).inject(this.node, "before");
  162. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  163. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  164. }
  165. this.node.getFirst().addEvent("change", function(){
  166. this.validationMode();
  167. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  168. }.bind(this));
  169. this.node.getFirst().addEvent("blur", function(){
  170. this.validation();
  171. }.bind(this));
  172. this.node.getFirst().addEvent("keyup", function(){
  173. this.validationMode();
  174. }.bind(this));
  175. },
  176. getValue: function(){
  177. var value = this._getBusinessData();
  178. if (!value) value = this._computeValue();
  179. return value || "0";
  180. }
  181. });