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'
111 if(this.max_length) {
112 input.maxlength = this.max_length;
116 hiddenInput.name = this.name;
120 input.disabled = true;
123 var clg = 12 - this.inputlg;
124 var cmd = 12 - this.inputmd;
125 var csm = 12 - this.inputsm;
126 var cxs = 12 - this.inputxs;
130 cls : 'row roo-money-field',
134 cls : 'roo-money-currency column col-lg-' + clg + ' col-md-' + cmd + ' col-sm-' + csm + ' col-xs-' + cxs,
138 cls: 'roo-select2-container input-group',
142 cls : 'form-control roo-money-currency-input',
143 autocomplete: 'new-password',
145 name : this.currencyName
149 cls : 'input-group-addon',
163 cls : 'roo-money-amount column col-lg-' + this.inputlg + ' col-md-' + this.inputmd + ' col-sm-' + this.inputsm + ' col-xs-' + this.inputxs,
167 cls: this.hasFeedback ? 'has-feedback' : '',
178 if (this.fieldLabel.length) {
181 tooltip: 'This field is required'
187 cls: 'control-label',
193 html: this.fieldLabel
196 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
202 if(this.indicatorpos == 'right') {
203 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
210 if(align == 'left') {
218 if(this.labelWidth > 12){
219 label.style = "width: " + this.labelWidth + 'px';
221 if(this.labelWidth < 13 && this.labelmd == 0){
222 this.labelmd = this.labelWidth;
224 if(this.labellg > 0){
225 label.cls += ' col-lg-' + this.labellg;
226 input.cls += ' col-lg-' + (12 - this.labellg);
228 if(this.labelmd > 0){
229 label.cls += ' col-md-' + this.labelmd;
230 container.cls += ' col-md-' + (12 - this.labelmd);
232 if(this.labelsm > 0){
233 label.cls += ' col-sm-' + this.labelsm;
234 container.cls += ' col-sm-' + (12 - this.labelsm);
236 if(this.labelxs > 0){
237 label.cls += ' col-xs-' + this.labelxs;
238 container.cls += ' col-xs-' + (12 - this.labelxs);
251 ['xs','sm','md','lg'].map(function(size){
252 if (settings[size]) {
253 cfg.cls += ' col-' + size + '-' + settings[size];
260 initEvents : function()
262 this.indicator = this.indicatorEl();
264 this.initCurrencyEvent();
266 this.initNumberEvent();
269 initCurrencyEvent : function()
272 throw "can not find store for combo";
275 this.store = Roo.factory(this.store, Roo.data);
276 this.store.parent = this;
280 this.triggerEl = this.el.select('.input-group-addon', true).first();
282 this.triggerEl.on("click", this.onTriggerClick, this, { preventDefault : true });
287 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
288 _this.list.setWidth(lw);
291 this.list.on('mouseover', this.onViewOver, this);
292 this.list.on('mousemove', this.onViewMove, this);
293 this.list.on('scroll', this.onViewScroll, this);
296 this.tpl = '<li><a href="#">{' + this.currencyField + '}</a></li>';
299 this.view = new Roo.View(this.list, this.tpl, {
300 singleSelect:true, store: this.store, selectedClass: this.selectedClass
303 this.view.on('click', this.onViewClick, this);
305 this.store.on('beforeload', this.onBeforeLoad, this);
306 this.store.on('load', this.onLoad, this);
307 this.store.on('loadexception', this.onLoadException, this);
309 this.keyNav = new Roo.KeyNav(this.currencyEl(), {
311 this.inKeyMode = true;
315 "down" : function(e){
316 if(!this.isExpanded()){
317 this.onTriggerClick();
319 this.inKeyMode = true;
324 "enter" : function(e){
327 if(this.fireEvent("specialkey", this, e)){
328 this.onViewClick(false);
341 if(this.fireEvent("specialkey", this, e)){
342 this.onViewClick(false);
350 doRelay : function(foo, bar, hname){
351 if(hname == 'down' || this.scope.isExpanded()){
352 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
360 this.currencyEl().on("click", this.onTriggerClick, this, { preventDefault : true });
364 initNumberEvent : function(e)
366 this.inputEl().on("keydown" , this.fireKey, this);
367 this.inputEl().on("focus", this.onFocus, this);
368 this.inputEl().on("blur", this.onBlur, this);
370 this.inputEl().relayEvent('keyup', this);
373 this.indicator.addClass('invisible');
376 this.originalValue = this.getValue();
378 if(this.validationEvent == 'keyup'){
379 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
380 this.inputEl().on('keyup', this.filterValidation, this);
382 else if(this.validationEvent !== false){
383 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
386 if(this.selectOnFocus){
387 this.on("focus", this.preFocus, this);
390 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
391 this.inputEl().on("keypress", this.filterKeys, this);
393 this.inputEl().relayEvent('keypress', this);
396 var allowed = "0123456789";
398 if(this.allowDecimals){
399 allowed += this.decimalSeparator;
402 if(this.allowNegative){
406 if(this.thousandsDelimiter) {
410 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
412 var keyPress = function(e){
416 var c = e.getCharCode();
419 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
420 allowed.indexOf(String.fromCharCode(c)) === -1
426 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
430 if(allowed.indexOf(String.fromCharCode(c)) === -1){
435 this.inputEl().on("keypress", keyPress, this);
439 onTriggerClick : function(e)
446 this.loadNext = false;
448 if(this.isExpanded()){
453 this.hasFocus = true;
455 if(this.triggerAction == 'all') {
456 this.doQuery(this.allQuery, true);
460 this.doQuery(this.getRawValue());
463 getCurrency : function()
465 var v = this.currencyEl().getValue();
470 restrictHeight : function()
472 this.list.alignTo(this.currencyEl(), this.listAlign);
473 this.list.alignTo(this.currencyEl(), this.listAlign);
476 onViewClick : function(view, doFocus, el, e)
478 var index = this.view.getSelectedIndexes()[0];
480 var r = this.store.getAt(index);
483 this.onSelect(r, index);
487 onSelect : function(record, index){
489 if(this.fireEvent('beforeselect', this, record, index) !== false){
491 this.setFromCurrencyData(index > -1 ? record.data : false);
495 this.fireEvent('select', this, record, index);
499 setFromCurrencyData : function(o)
503 this.lastCurrency = o;
505 if (this.currencyField) {
506 currency = !o || typeof(o[this.currencyField]) == 'undefined' ? '' : o[this.currencyField];
508 Roo.log('no currencyField value set for '+ (this.name ? this.name : this.id));
511 this.lastSelectionText = currency;
513 //setting default currency
514 if(o[this.currencyField] * 1 == 0 && this.defaultCurrency) {
515 this.setCurrency(this.defaultCurrency);
519 this.setCurrency(currency);
522 setFromData : function(o)
526 c[this.currencyField] = !o || typeof(o[this.currencyName]) == 'undefined' ? '' : o[this.currencyName];
528 this.setFromCurrencyData(c);
533 value = !o || typeof(o[this.name]) == 'undefined' ? '' : o[this.name];
535 Roo.log('no value set for '+ (this.name ? this.name : this.id));
538 this.setValue(value);
542 setCurrency : function(v)
544 this.currencyValue = v;
547 this.currencyEl().dom.value = (v === null || v === undefined ? '' : v);
552 setValue : function(v)
554 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
560 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
562 this.inputEl().dom.value = (v == '') ? '' :
563 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
565 if(!this.allowZero && v === '0') {
566 this.hiddenEl().dom.value = '';
567 this.inputEl().dom.value = '';
574 getRawValue : function()
576 var v = this.inputEl().getValue();
581 getValue : function()
583 return this.fixPrecision(this.parseValue(this.getRawValue()));
586 parseValue : function(value)
588 if(this.thousandsDelimiter) {
590 r = new RegExp(",", "g");
591 value = value.replace(r, "");
594 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
595 return isNaN(value) ? '' : value;
599 fixPrecision : function(value)
601 if(this.thousandsDelimiter) {
603 r = new RegExp(",", "g");
604 value = value.replace(r, "");
607 var nan = isNaN(value);
609 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
610 return nan ? '' : value;
612 return parseFloat(value).toFixed(this.decimalPrecision);
615 decimalPrecisionFcn : function(v)
617 return Math.floor(v);
620 validateValue : function(value)
622 if(!Roo.bootstrap.MoneyField.superclass.validateValue.call(this, value)){
626 var num = this.parseValue(value);
629 this.markInvalid(String.format(this.nanText, value));
633 if(num < this.minValue){
634 this.markInvalid(String.format(this.minText, this.minValue));
638 if(num > this.maxValue){
639 this.markInvalid(String.format(this.maxText, this.maxValue));
646 validate : function()
648 if(this.disabled || this.allowBlank){
653 var currency = this.getCurrency();
655 if(this.validateValue(this.getRawValue()) && currency.length){
669 beforeBlur : function()
675 var v = this.parseValue(this.getRawValue());
686 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
687 //this.el.removeClass(this.focusClass);
690 this.hasFocus = false;
692 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
696 var v = this.getValue();
698 if(String(v) !== String(this.startValue)){
699 this.fireEvent('change', this, v, this.startValue);
702 this.fireEvent("blur", this);
707 return this.el.select('.roo-money-amount-input', true).first();
710 currencyEl : function()
712 return this.el.select('.roo-money-currency-input', true).first();
715 hiddenEl : function()
717 return this.el.select('input.hidden-number-input',true).first();