3 * @class Roo.bootstrap.MoneyField
4 * @extends Roo.bootstrap.ComboBox
5 * Bootstrap MoneyField class
8 * Create a new MoneyField.
9 * @param {Object} config Configuration options
12 Roo.bootstrap.MoneyField = function(config) {
14 Roo.bootstrap.MoneyField.superclass.constructor.call(this, config);
18 Roo.extend(Roo.bootstrap.MoneyField, Roo.bootstrap.ComboBox, {
21 * @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
25 * @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
27 decimalSeparator : ".",
29 * @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
33 * @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
37 * @cfg {Boolean} allowZero False to blank out if the user enters '0' (defaults to true)
41 * @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
43 minValue : Number.NEGATIVE_INFINITY,
45 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
47 maxValue : Number.MAX_VALUE,
49 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
51 minText : "The minimum value for this field is {0}",
53 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
55 maxText : "The maximum value for this field is {0}",
57 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
58 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
60 nanText : "{0} is not a valid number",
62 * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
66 * @cfg {String} defaults currency of the MoneyField
67 * value should be in lkey
69 defaultCurrency : false,
71 * @cfg {String} thousandsDelimiter Symbol of thousandsDelimiter
73 thousandsDelimiter : false,
75 * @cfg {Number} max_length Maximum input field length allowed (defaults to Number.MAX_VALUE)
86 getAutoCreate : function()
88 var align = this.labelAlign || this.parentLabelAlign();
100 cls : 'form-control roo-money-amount-input',
101 autocomplete: 'new-password'
108 cls: 'hidden-number-input'
113 Number.isInteger(this.max_length * 1)
115 input.maxlength = this.max_length;
119 hiddenInput.name = this.name;
123 input.disabled = true;
126 var clg = 12 - this.inputlg;
127 var cmd = 12 - this.inputmd;
128 var csm = 12 - this.inputsm;
129 var cxs = 12 - this.inputxs;
133 cls : 'row roo-money-field',
137 cls : 'roo-money-currency column col-lg-' + clg + ' col-md-' + cmd + ' col-sm-' + csm + ' col-xs-' + cxs,
141 cls: 'roo-select2-container input-group',
145 cls : 'form-control roo-money-currency-input',
146 autocomplete: 'new-password',
148 name : this.currencyName
152 cls : 'input-group-addon',
166 cls : 'roo-money-amount column col-lg-' + this.inputlg + ' col-md-' + this.inputmd + ' col-sm-' + this.inputsm + ' col-xs-' + this.inputxs,
170 cls: this.hasFeedback ? 'has-feedback' : '',
181 if (this.fieldLabel.length) {
184 tooltip: 'This field is required'
190 cls: 'control-label',
196 html: this.fieldLabel
199 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
205 if(this.indicatorpos == 'right') {
206 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
213 if(align == 'left') {
221 if(this.labelWidth > 12){
222 label.style = "width: " + this.labelWidth + 'px';
224 if(this.labelWidth < 13 && this.labelmd == 0){
225 this.labelmd = this.labelWidth;
227 if(this.labellg > 0){
228 label.cls += ' col-lg-' + this.labellg;
229 input.cls += ' col-lg-' + (12 - this.labellg);
231 if(this.labelmd > 0){
232 label.cls += ' col-md-' + this.labelmd;
233 container.cls += ' col-md-' + (12 - this.labelmd);
235 if(this.labelsm > 0){
236 label.cls += ' col-sm-' + this.labelsm;
237 container.cls += ' col-sm-' + (12 - this.labelsm);
239 if(this.labelxs > 0){
240 label.cls += ' col-xs-' + this.labelxs;
241 container.cls += ' col-xs-' + (12 - this.labelxs);
254 ['xs','sm','md','lg'].map(function(size){
255 if (settings[size]) {
256 cfg.cls += ' col-' + size + '-' + settings[size];
263 initEvents : function()
265 this.indicator = this.indicatorEl();
267 this.initCurrencyEvent();
269 this.initNumberEvent();
272 initCurrencyEvent : function()
275 throw "can not find store for combo";
278 this.store = Roo.factory(this.store, Roo.data);
279 this.store.parent = this;
283 this.triggerEl = this.el.select('.input-group-addon', true).first();
285 this.triggerEl.on("click", this.onTriggerClick, this, { preventDefault : true });
290 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
291 _this.list.setWidth(lw);
294 this.list.on('mouseover', this.onViewOver, this);
295 this.list.on('mousemove', this.onViewMove, this);
296 this.list.on('scroll', this.onViewScroll, this);
299 this.tpl = '<li><a href="#">{' + this.currencyField + '}</a></li>';
302 this.view = new Roo.View(this.list, this.tpl, {
303 singleSelect:true, store: this.store, selectedClass: this.selectedClass
306 this.view.on('click', this.onViewClick, this);
308 this.store.on('beforeload', this.onBeforeLoad, this);
309 this.store.on('load', this.onLoad, this);
310 this.store.on('loadexception', this.onLoadException, this);
312 this.keyNav = new Roo.KeyNav(this.currencyEl(), {
314 this.inKeyMode = true;
318 "down" : function(e){
319 if(!this.isExpanded()){
320 this.onTriggerClick();
322 this.inKeyMode = true;
327 "enter" : function(e){
330 if(this.fireEvent("specialkey", this, e)){
331 this.onViewClick(false);
344 if(this.fireEvent("specialkey", this, e)){
345 this.onViewClick(false);
353 doRelay : function(foo, bar, hname){
354 if(hname == 'down' || this.scope.isExpanded()){
355 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
363 this.currencyEl().on("click", this.onTriggerClick, this, { preventDefault : true });
367 initNumberEvent : function(e)
369 this.inputEl().on("keydown" , this.fireKey, this);
370 this.inputEl().on("focus", this.onFocus, this);
371 this.inputEl().on("blur", this.onBlur, this);
373 this.inputEl().relayEvent('keyup', this);
376 this.indicator.addClass('invisible');
379 this.originalValue = this.getValue();
381 if(this.validationEvent == 'keyup'){
382 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
383 this.inputEl().on('keyup', this.filterValidation, this);
385 else if(this.validationEvent !== false){
386 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
389 if(this.selectOnFocus){
390 this.on("focus", this.preFocus, this);
393 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
394 this.inputEl().on("keypress", this.filterKeys, this);
396 this.inputEl().relayEvent('keypress', this);
399 var allowed = "0123456789";
401 if(this.allowDecimals){
402 allowed += this.decimalSeparator;
405 if(this.allowNegative){
409 if(this.thousandsDelimiter) {
413 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
415 var keyPress = function(e){
419 var c = e.getCharCode();
422 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
423 allowed.indexOf(String.fromCharCode(c)) === -1
429 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
433 if(allowed.indexOf(String.fromCharCode(c)) === -1){
438 this.inputEl().on("keypress", keyPress, this);
442 onTriggerClick : function(e)
449 this.loadNext = false;
451 if(this.isExpanded()){
456 this.hasFocus = true;
458 if(this.triggerAction == 'all') {
459 this.doQuery(this.allQuery, true);
463 this.doQuery(this.getRawValue());
466 getCurrency : function()
468 var v = this.currencyEl().getValue();
473 restrictHeight : function()
475 this.list.alignTo(this.currencyEl(), this.listAlign);
476 this.list.alignTo(this.currencyEl(), this.listAlign);
479 onViewClick : function(view, doFocus, el, e)
481 var index = this.view.getSelectedIndexes()[0];
483 var r = this.store.getAt(index);
486 this.onSelect(r, index);
490 onSelect : function(record, index){
492 if(this.fireEvent('beforeselect', this, record, index) !== false){
494 this.setFromCurrencyData(index > -1 ? record.data : false);
498 this.fireEvent('select', this, record, index);
502 setFromCurrencyData : function(o)
506 this.lastCurrency = o;
508 if (this.currencyField) {
509 currency = !o || typeof(o[this.currencyField]) == 'undefined' ? '' : o[this.currencyField];
511 Roo.log('no currencyField value set for '+ (this.name ? this.name : this.id));
514 this.lastSelectionText = currency;
516 //setting default currency
517 if(o[this.currencyField] * 1 == 0 && this.defaultCurrency) {
518 this.setCurrency(this.defaultCurrency);
522 this.setCurrency(currency);
525 setFromData : function(o)
529 c[this.currencyField] = !o || typeof(o[this.currencyName]) == 'undefined' ? '' : o[this.currencyName];
531 this.setFromCurrencyData(c);
536 value = !o || typeof(o[this.name]) == 'undefined' ? '' : o[this.name];
538 Roo.log('no value set for '+ (this.name ? this.name : this.id));
541 this.setValue(value);
545 setCurrency : function(v)
547 this.currencyValue = v;
550 this.currencyEl().dom.value = (v === null || v === undefined ? '' : v);
555 setValue : function(v)
557 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
563 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
565 this.inputEl().dom.value = (v == '') ? '' :
566 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
568 if(!this.allowZero && v === '0') {
569 this.hiddenEl().dom.value = '';
570 this.inputEl().dom.value = '';
577 getRawValue : function()
579 var v = this.inputEl().getValue();
584 getValue : function()
586 return this.fixPrecision(this.parseValue(this.getRawValue()));
589 parseValue : function(value)
591 if(this.thousandsDelimiter) {
593 r = new RegExp(",", "g");
594 value = value.replace(r, "");
597 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
598 return isNaN(value) ? '' : value;
602 fixPrecision : function(value)
604 if(this.thousandsDelimiter) {
606 r = new RegExp(",", "g");
607 value = value.replace(r, "");
610 var nan = isNaN(value);
612 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
613 return nan ? '' : value;
615 return parseFloat(value).toFixed(this.decimalPrecision);
618 decimalPrecisionFcn : function(v)
620 return Math.floor(v);
623 validateValue : function(value)
625 if(!Roo.bootstrap.MoneyField.superclass.validateValue.call(this, value)){
629 var num = this.parseValue(value);
632 this.markInvalid(String.format(this.nanText, value));
636 if(num < this.minValue){
637 this.markInvalid(String.format(this.minText, this.minValue));
641 if(num > this.maxValue){
642 this.markInvalid(String.format(this.maxText, this.maxValue));
649 validate : function()
651 if(this.disabled || this.allowBlank){
656 var currency = this.getCurrency();
658 if(this.validateValue(this.getRawValue()) && currency.length){
672 beforeBlur : function()
678 var v = this.parseValue(this.getRawValue());
689 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
690 //this.el.removeClass(this.focusClass);
693 this.hasFocus = false;
695 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
699 var v = this.getValue();
701 if(String(v) !== String(this.startValue)){
702 this.fireEvent('change', this, v, this.startValue);
705 this.fireEvent("blur", this);
710 return this.el.select('.roo-money-amount-input', true).first();
713 currencyEl : function()
715 return this.el.select('.roo-money-currency-input', true).first();
718 hiddenEl : function()
720 return this.el.select('input.hidden-number-input',true).first();