Number.js 6.5 KB

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