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,
83 getAutoCreate : function()
85 var align = this.labelAlign || this.parentLabelAlign();
97 cls : 'form-control roo-money-amount-input',
98 autocomplete: 'new-password'
105 cls: 'hidden-number-input'
109 hiddenInput.name = this.name;
113 input.disabled = true;
116 var clg = 12 - this.inputlg;
117 var cmd = 12 - this.inputmd;
118 var csm = 12 - this.inputsm;
119 var cxs = 12 - this.inputxs;
123 cls : 'row roo-money-field',
127 cls : 'roo-money-currency column col-lg-' + clg + ' col-md-' + cmd + ' col-sm-' + csm + ' col-xs-' + cxs,
131 cls: 'roo-select2-container input-group',
135 cls : 'form-control roo-money-currency-input',
136 autocomplete: 'new-password',
138 name : this.currencyName
142 cls : 'input-group-addon',
156 cls : 'roo-money-amount column col-lg-' + this.inputlg + ' col-md-' + this.inputmd + ' col-sm-' + this.inputsm + ' col-xs-' + this.inputxs,
160 cls: this.hasFeedback ? 'has-feedback' : '',
171 if (this.fieldLabel.length) {
174 tooltip: 'This field is required'
180 cls: 'control-label',
186 html: this.fieldLabel
189 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
195 if(this.indicatorpos == 'right') {
196 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
203 if(align == 'left') {
211 if(this.labelWidth > 12){
212 label.style = "width: " + this.labelWidth + 'px';
214 if(this.labelWidth < 13 && this.labelmd == 0){
215 this.labelmd = this.labelWidth;
217 if(this.labellg > 0){
218 label.cls += ' col-lg-' + this.labellg;
219 input.cls += ' col-lg-' + (12 - this.labellg);
221 if(this.labelmd > 0){
222 label.cls += ' col-md-' + this.labelmd;
223 container.cls += ' col-md-' + (12 - this.labelmd);
225 if(this.labelsm > 0){
226 label.cls += ' col-sm-' + this.labelsm;
227 container.cls += ' col-sm-' + (12 - this.labelsm);
229 if(this.labelxs > 0){
230 label.cls += ' col-xs-' + this.labelxs;
231 container.cls += ' col-xs-' + (12 - this.labelxs);
244 ['xs','sm','md','lg'].map(function(size){
245 if (settings[size]) {
246 cfg.cls += ' col-' + size + '-' + settings[size];
253 initEvents : function()
255 this.indicator = this.indicatorEl();
257 this.initCurrencyEvent();
259 this.initNumberEvent();
262 initCurrencyEvent : function()
265 throw "can not find store for combo";
268 this.store = Roo.factory(this.store, Roo.data);
269 this.store.parent = this;
273 this.triggerEl = this.el.select('.input-group-addon', true).first();
275 this.triggerEl.on("click", this.onTriggerClick, this, { preventDefault : true });
280 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
281 _this.list.setWidth(lw);
284 this.list.on('mouseover', this.onViewOver, this);
285 this.list.on('mousemove', this.onViewMove, this);
286 this.list.on('scroll', this.onViewScroll, this);
289 this.tpl = '<li><a href="#">{' + this.currencyField + '}</a></li>';
292 this.view = new Roo.View(this.list, this.tpl, {
293 singleSelect:true, store: this.store, selectedClass: this.selectedClass
296 this.view.on('click', this.onViewClick, this);
298 this.store.on('beforeload', this.onBeforeLoad, this);
299 this.store.on('load', this.onLoad, this);
300 this.store.on('loadexception', this.onLoadException, this);
302 this.keyNav = new Roo.KeyNav(this.currencyEl(), {
304 this.inKeyMode = true;
308 "down" : function(e){
309 if(!this.isExpanded()){
310 this.onTriggerClick();
312 this.inKeyMode = true;
317 "enter" : function(e){
320 if(this.fireEvent("specialkey", this, e)){
321 this.onViewClick(false);
334 if(this.fireEvent("specialkey", this, e)){
335 this.onViewClick(false);
343 doRelay : function(foo, bar, hname){
344 if(hname == 'down' || this.scope.isExpanded()){
345 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
353 this.currencyEl().on("click", this.onTriggerClick, this, { preventDefault : true });
357 initNumberEvent : function(e)
359 this.inputEl().on("keydown" , this.fireKey, this);
360 this.inputEl().on("focus", this.onFocus, this);
361 this.inputEl().on("blur", this.onBlur, this);
363 this.inputEl().relayEvent('keyup', this);
366 this.indicator.addClass('invisible');
369 this.originalValue = this.getValue();
371 if(this.validationEvent == 'keyup'){
372 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
373 this.inputEl().on('keyup', this.filterValidation, this);
375 else if(this.validationEvent !== false){
376 this.inputEl().on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
379 if(this.selectOnFocus){
380 this.on("focus", this.preFocus, this);
383 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
384 this.inputEl().on("keypress", this.filterKeys, this);
386 this.inputEl().relayEvent('keypress', this);
389 var allowed = "0123456789";
391 if(this.allowDecimals){
392 allowed += this.decimalSeparator;
395 if(this.allowNegative){
399 if(this.thousandsDelimiter) {
403 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
405 var keyPress = function(e){
409 var c = e.getCharCode();
412 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
413 allowed.indexOf(String.fromCharCode(c)) === -1
419 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
423 if(allowed.indexOf(String.fromCharCode(c)) === -1){
428 this.inputEl().on("keypress", keyPress, this);
432 onTriggerClick : function(e)
439 this.loadNext = false;
441 if(this.isExpanded()){
446 this.hasFocus = true;
448 if(this.triggerAction == 'all') {
449 this.doQuery(this.allQuery, true);
453 this.doQuery(this.getRawValue());
456 getCurrency : function()
458 var v = this.currencyEl().getValue();
463 restrictHeight : function()
465 this.list.alignTo(this.currencyEl(), this.listAlign);
466 this.list.alignTo(this.currencyEl(), this.listAlign);
469 onViewClick : function(view, doFocus, el, e)
471 var index = this.view.getSelectedIndexes()[0];
473 var r = this.store.getAt(index);
476 this.onSelect(r, index);
480 onSelect : function(record, index){
482 if(this.fireEvent('beforeselect', this, record, index) !== false){
484 this.setFromCurrencyData(index > -1 ? record.data : false);
488 this.fireEvent('select', this, record, index);
492 setFromCurrencyData : function(o)
496 this.lastCurrency = o;
498 if (this.currencyField) {
499 currency = !o || typeof(o[this.currencyField]) == 'undefined' ? '' : o[this.currencyField];
501 Roo.log('no currencyField value set for '+ (this.name ? this.name : this.id));
504 this.lastSelectionText = currency;
506 //setting default currency
507 if(o[this.currencyField] * 1 == 0 && this.defaultCurrency) {
508 this.setCurrency(this.defaultCurrency);
512 this.setCurrency(currency);
515 setFromData : function(o)
519 c[this.currencyField] = !o || typeof(o[this.currencyName]) == 'undefined' ? '' : o[this.currencyName];
521 this.setFromCurrencyData(c);
526 value = !o || typeof(o[this.name]) == 'undefined' ? '' : o[this.name];
528 Roo.log('no value set for '+ (this.name ? this.name : this.id));
531 this.setValue(value);
535 setCurrency : function(v)
537 this.currencyValue = v;
540 this.currencyEl().dom.value = (v === null || v === undefined ? '' : v);
545 setValue : function(v)
547 v = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
553 this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
555 this.inputEl().dom.value = (v == '') ? '' :
556 Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
558 if(!this.allowZero && v === '0') {
559 this.hiddenEl().dom.value = '';
560 this.inputEl().dom.value = '';
567 getRawValue : function()
569 var v = this.inputEl().getValue();
574 getValue : function()
576 return this.fixPrecision(this.parseValue(this.getRawValue()));
579 parseValue : function(value)
581 if(this.thousandsDelimiter) {
583 r = new RegExp(",", "g");
584 value = value.replace(r, "");
587 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
588 return isNaN(value) ? '' : value;
592 fixPrecision : function(value)
594 if(this.thousandsDelimiter) {
596 r = new RegExp(",", "g");
597 value = value.replace(r, "");
600 var nan = isNaN(value);
602 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
603 return nan ? '' : value;
605 return parseFloat(value).toFixed(this.decimalPrecision);
608 decimalPrecisionFcn : function(v)
610 return Math.floor(v);
613 validateValue : function(value)
615 if(!Roo.bootstrap.MoneyField.superclass.validateValue.call(this, value)){
619 var num = this.parseValue(value);
622 this.markInvalid(String.format(this.nanText, value));
626 if(num < this.minValue){
627 this.markInvalid(String.format(this.minText, this.minValue));
631 if(num > this.maxValue){
632 this.markInvalid(String.format(this.maxText, this.maxValue));
639 validate : function()
641 if(this.disabled || this.allowBlank){
646 var currency = this.getCurrency();
648 if(this.validateValue(this.getRawValue()) && currency.length){
662 beforeBlur : function()
668 var v = this.parseValue(this.getRawValue());
679 if(!Roo.isOpera && this.focusClass){ // don't touch in Opera
680 //this.el.removeClass(this.focusClass);
683 this.hasFocus = false;
685 if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
689 var v = this.getValue();
691 if(String(v) !== String(this.startValue)){
692 this.fireEvent('change', this, v, this.startValue);
695 this.fireEvent("blur", this);
700 return this.el.select('.roo-money-amount-input', true).first();
703 currencyEl : function()
705 return this.el.select('.roo-money-currency-input', true).first();
708 hiddenEl : function()
710 return this.el.select('input.hidden-number-input',true).first();