Currency.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. MWF.xDesktop.requireApp("process.Xform", "Number", null, false);
  2. /** @class Currency 货币输入组件。
  3. * @o2cn 货币输入组件
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var field = this.form.get("name"); //获取组件
  8. * //方法2
  9. * var field = this.target; //在组件事件脚本中获取
  10. * @extends MWF.xApplication.process.Xform.Number
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Currency = MWF.APPCurrency = new Class({
  16. Implements: [Events],
  17. Extends: MWF.APPNumber,
  18. iconStyle: "currencyIcon",
  19. _loadMergeAmountReadNode: function(){
  20. var data = this.getBusinessDataById();
  21. var total = new Decimal(0);
  22. for( var key in data ){
  23. total = total.plus(new Decimal(data[key] || 0));
  24. }
  25. this.node.set("text", this.formatNumber(total.toString()));
  26. this.loadSymboleRead();
  27. },
  28. _loadMergeAverageReadNode: function(){
  29. var data = this.getBusinessDataById();
  30. var total = new Decimal(0);
  31. for( var key in data ){
  32. total = total.plus(new Decimal(data[key] || 0));
  33. }
  34. var average = total.div( new Decimal(Object.keys(data).length) );
  35. this.node.set("text", this.formatNumber(average.toString()));
  36. this.loadSymboleRead();
  37. },
  38. _resetNodeEdit: function(){
  39. var input = new Element("input", {
  40. "styles": {
  41. "background": "transparent",
  42. "width": "100%",
  43. "border": "0px"
  44. }
  45. });
  46. input.setStyles( this.json.recoveryInputStyles || {} );
  47. var node = new Element("div", {"styles": {
  48. "overflow": "hidden",
  49. "position": "relative",
  50. "margin-right": "20px",
  51. "padding-right": "4px"
  52. }}).inject(this.node, "after");
  53. node.setStyles( this.json.recoveryStyles || {} );
  54. input.inject(node);
  55. if( this.json.currencySymbol ){
  56. var symbole = new Element("span.MWFCurrencySymbol", {
  57. text: this.json.currencySymbol
  58. });
  59. symbole.inject( node.offsetParent !== null ? node : this.form.node );
  60. symbole.setStyles( this.json.symbolStyles || {} );
  61. var paddingLeft = symbole.getSize().x + 5;
  62. var inputPadding = input.getStyle("padding-right");
  63. var width = paddingLeft;
  64. if( this.isNumber( inputPadding ) ){
  65. width = width + parseFloat(inputPadding);
  66. }
  67. input.setStyles({
  68. "padding-left": paddingLeft + "px",
  69. "width": "calc( 100% - " + width +"px )"
  70. });
  71. if( node.offsetParent === null )symbole.inject( node );
  72. symbole.setStyles({
  73. "top": "0px",
  74. "left": "0px",
  75. "position": "absolute"
  76. });
  77. }
  78. this.node.destroy();
  79. this.node = node;
  80. },
  81. _loadNodeEdit: function(){
  82. //if (!this.json.preprocessing) this._resetNodeEdit();
  83. this._resetNodeEdit();
  84. var input = this.node.getFirst();
  85. if( !input && this.nodeHtml ){
  86. this.node.set("html", this.nodeHtml);
  87. input = this.node.getFirst();
  88. }
  89. input.set(this.json.properties);
  90. this.node.set({
  91. "id": this.json.id,
  92. "MWFType": this.json.type,
  93. "events": {
  94. "click": this.clickSelect.bind(this)
  95. }
  96. });
  97. if (this.json.showIcon!='no' && !this.form.json.hideModuleIcon) {
  98. this.iconNode = new Element("div", {
  99. "styles": this.form.css[this.iconStyle]
  100. }).inject(this.node, "before");
  101. }else if( this.form.json.nodeStyleWithhideModuleIcon ){
  102. this.node.setStyles(this.form.json.nodeStyleWithhideModuleIcon)
  103. }
  104. this.node.getFirst().addEvent("change", function(){
  105. this.validationMode();
  106. if (this.validation()) {
  107. var value = this.getInputData("change");
  108. this._setBusinessData(value);
  109. this.node.getFirst().set("value", this.formatNumber( value.toString() ));
  110. this.fireEvent("change");
  111. }
  112. }.bind(this));
  113. this.node.getFirst().addEvent("blur", function(){
  114. this.validation();
  115. }.bind(this));
  116. this.node.getFirst().addEvent("keyup", function(){
  117. this.validationMode();
  118. }.bind(this));
  119. },
  120. __setData: function(data, fireChange){
  121. var old = this.getInputData();
  122. this._setBusinessData(data);
  123. if (this.node.getFirst()){
  124. this.node.getFirst().set("value", this.formatNumber(data));
  125. this.checkDescription();
  126. this.validationMode();
  127. }else{
  128. this.node.set("text", this.formatNumber(data));
  129. this.loadSymboleRead();
  130. }
  131. if (fireChange && old!==data) this.fireEvent("change");
  132. this.moduleValueAG = null;
  133. },
  134. __setValue: function(value){
  135. var v = typeOf( value ) === "string" ? this.unformatNumber( value ) : value;
  136. v = this.isNumber( v ) ? parseFloat( v ) : v;
  137. this._setBusinessData(v);
  138. var val = value;
  139. if( this.json.emptyValue === "string" ){
  140. if( typeOf(v)==="null" )val = "";
  141. if( v === 0 )val = "0";
  142. }else{
  143. if( v === 0 || v === "" || typeOf(v)==="null" )val = "0";
  144. }
  145. if (this.node.getFirst()) this.node.getFirst().set("value", value || val);
  146. if (this.isReadonly()) {
  147. this.node.set("text", value || val);
  148. this.loadSymboleRead()
  149. }
  150. this.moduleValueAG = null;
  151. this.fieldModuleLoaded = true;
  152. return value;
  153. },
  154. loadSymboleRead: function () {
  155. var symbole = new Element("span.MWFCurrencySymbol", {
  156. text: this.json.currencySymbol,
  157. styles: this.json.symbolStyles || {}
  158. }).inject(this.node, "top");
  159. var paddingRight = symbole.getStyle("padding-right");
  160. if( typeOf(paddingRight) === "string" && parseInt(paddingRight) === 0 ){
  161. symbole.setStyle("padding-right", "5px");
  162. }
  163. }
  164. });