Number.js 7.4 KB

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