Number.js 5.8 KB

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