Number.js 7.7 KB

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