Number.js 7.7 KB

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