Number.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. MWF.xDesktop.requireApp("process.Xform", "Textfield", null, false);
  2. /** @class Number 数字输入组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var field = this.form.get("name"); //获取组件
  7. * //方法2
  8. * var field = this.target; //在组件事件脚本中获取
  9. * @extends MWF.xApplication.process.Xform.Textfield
  10. * @o2category FormComponents
  11. * @o2range {Process|CMS}
  12. * @hideconstructor
  13. */
  14. MWF.xApplication.process.Xform.Number = MWF.APPNumber = new Class({
  15. Implements: [Events],
  16. Extends: MWF.APPTextfield,
  17. iconStyle: "numberIcon",
  18. isEmpty : function(){
  19. return !this.getData();
  20. },
  21. getInputData: function(){
  22. if (this.node.getFirst()){
  23. var v = this.node.getElement("input").get("value");
  24. var n = v.toFloat();
  25. return (isNaN(n)) ? 0 : n;
  26. }else{
  27. return this._getBusinessData();
  28. }
  29. return v;
  30. },
  31. // getInputData: function(){
  32. // var n = this.node.getElement("input").get("value").toFloat();
  33. // if ((isNaN(n))) {this.setData('0')};
  34. // return (isNaN(n)) ? 0 : n;
  35. // },
  36. formatNumber: function(str){
  37. var v = str.toFloat();
  38. if (v){
  39. if (this.json.decimals && (this.json.decimals!="*")){
  40. var decimals = this.json.decimals.toInt();
  41. var p = Math.pow(10,decimals);
  42. var f_x = Math.round(v*p)/p;
  43. str = f_x.toString();
  44. if (decimals>0){
  45. var pos_decimal = str.indexOf('.');
  46. if (pos_decimal < 0){
  47. pos_decimal = str.length;
  48. str += '.';
  49. }
  50. decimalStr = (str).substr(pos_decimal+1, (str).length);
  51. while (decimalStr.length < decimals){
  52. str += '0';
  53. decimalStr += 0;
  54. }
  55. }
  56. }
  57. }
  58. return str;
  59. },
  60. validationFormat: function(){
  61. debugger;
  62. if( !this.node.getElement("input") )return true;
  63. var n = this.node.getElement("input").get("value");
  64. if (isNaN(n)) {
  65. this.notValidationMode(MWF.xApplication.process.Xform.LP.notValidation_number);
  66. return false;
  67. }
  68. this.node.getFirst().set("value", this.formatNumber(n));
  69. // var v = n.toFloat();
  70. // if (v){
  71. // if (this.json.decimals && (this.json.decimals!="*")){
  72. //
  73. // var decimals = this.json.decimals.toInt();
  74. //
  75. // var p = Math.pow(10,decimals);
  76. // var f_x = Math.round(v*p)/p;
  77. // var s_x = f_x.toString();
  78. //
  79. // if (decimals>0){
  80. // var pos_decimal = s_x.indexOf('.');
  81. // if (pos_decimal < 0){
  82. // pos_decimal = s_x.length;
  83. // s_x += '.';
  84. // }
  85. // decimalStr = (s_x).substr(pos_decimal+1, (s_x).length);
  86. // while (decimalStr.length < decimals){
  87. // s_x += '0';
  88. // decimalStr += 0;
  89. // }
  90. // }
  91. //
  92. // this.node.getFirst().set("value", s_x);
  93. // }
  94. // }
  95. return true;
  96. },
  97. validationConfigItem: function(routeName, data){
  98. var flag = (data.status=="all") ? true: (routeName == data.decision);
  99. if (flag){
  100. var n = this.getInputData();
  101. var v = (data.valueType=="value") ? n : n.length;
  102. switch (data.operateor){
  103. case "isnull":
  104. if (!v && v.toString()!=='0'){
  105. this.notValidationMode(data.prompt);
  106. return false;
  107. }
  108. break;
  109. case "notnull":
  110. if (v){
  111. this.notValidationMode(data.prompt);
  112. return false;
  113. }
  114. break;
  115. case "gt":
  116. if (v>data.value){
  117. this.notValidationMode(data.prompt);
  118. return false;
  119. }
  120. break;
  121. case "lt":
  122. if (v<data.value){
  123. this.notValidationMode(data.prompt);
  124. return false;
  125. }
  126. break;
  127. case "equal":
  128. if (v==data.value){
  129. this.notValidationMode(data.prompt);
  130. return false;
  131. }
  132. break;
  133. case "neq":
  134. if (v!=data.value){
  135. this.notValidationMode(data.prompt);
  136. return false;
  137. }
  138. break;
  139. case "contain":
  140. if (v.indexOf(data.value)!=-1){
  141. this.notValidationMode(data.prompt);
  142. return false;
  143. }
  144. break;
  145. case "notcontain":
  146. if (v.indexOf(data.value)==-1){
  147. this.notValidationMode(data.prompt);
  148. return false;
  149. }
  150. break;
  151. }
  152. }
  153. return true;
  154. },
  155. validationConfig: function(routeName, opinion){
  156. if (this.json.validationConfig){
  157. if (this.json.validationConfig.length){
  158. for (var i=0; i<this.json.validationConfig.length; i++) {
  159. var data = this.json.validationConfig[i];
  160. if (!this.validationConfigItem(routeName, data)) return false;
  161. }
  162. }
  163. return true;
  164. }
  165. return true;
  166. },
  167. validation: function(routeName, opinion){
  168. if (!this.readonly && !this.json.isReadonly){
  169. if (!this.validationFormat()) return false;
  170. if (!this.validationConfig(routeName, opinion)) return false;
  171. if (!this.json.validation) return true;
  172. if (!this.json.validation.code) return true;
  173. this.currentRouteName = routeName;
  174. var flag = this.form.Macro.exec(this.json.validation.code, this);
  175. this.currentRouteName = "";
  176. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  177. if (flag.toString() != "true") {
  178. this.notValidationMode(flag);
  179. return false;
  180. }
  181. }
  182. return true;
  183. },
  184. _resetNodeEdit: function(){
  185. var input = new Element("input", {
  186. "styles": {
  187. "background": "transparent",
  188. "width": "100%",
  189. "border": "0px"
  190. }
  191. });
  192. var node = new Element("div", {"styles": {
  193. "overflow": "hidden",
  194. "position": "relative",
  195. "margin-right": "20px",
  196. "padding-right": "4px"
  197. }}).inject(this.node, "after");
  198. input.inject(node);
  199. this.node.destroy();
  200. this.node = node;
  201. },
  202. _loadNodeEdit: function(){
  203. if (!this.json.preprocessing) this._resetNodeEdit();
  204. var input = this.node.getFirst();
  205. input.set(this.json.properties);
  206. this.node.set({
  207. "id": this.json.id,
  208. "MWFType": this.json.type,
  209. "events": {
  210. "click": this.clickSelect.bind(this)
  211. }
  212. });
  213. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  214. this.iconNode = new Element("div", {
  215. "styles": this.form.css[this.iconStyle]
  216. }).inject(this.node, "before");
  217. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  218. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  219. }
  220. this.node.getFirst().addEvent("change", function(){
  221. this.validationMode();
  222. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  223. }.bind(this));
  224. this.node.getFirst().addEvent("blur", function(){
  225. this.validation();
  226. }.bind(this));
  227. this.node.getFirst().addEvent("keyup", function(){
  228. this.validationMode();
  229. }.bind(this));
  230. },
  231. _computeValue: function(value){
  232. return (this.json.defaultValue && this.json.defaultValue.code) ? this.form.Macro.exec(this.json.defaultValue.code, this): (value || "0");
  233. },
  234. getValue: function(){
  235. if (this.moduleValueAG) return this.moduleValueAG;
  236. var value = this._getBusinessData();
  237. if (!value) value = this._computeValue();
  238. value = this.formatNumber(value);
  239. return value || "0";
  240. },
  241. __setValue: function(value){
  242. this._setBusinessData(value);
  243. if (this.node.getFirst()) this.node.getFirst().set("value", value || "0");
  244. if (this.readonly || this.json.isReadonly) this.node.set("text", value);
  245. this.moduleValueAG = null;
  246. return value;
  247. }
  248. });