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