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 {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
39 minValue : Number.NEGATIVE_INFINITY,
41 * @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
43 maxValue : Number.MAX_VALUE,
45 * @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
47 minText : "The minimum value for this field is {0}",
49 * @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
51 maxText : "The maximum value for this field is {0}",
53 * @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
54 * if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
56 nanText : "{0} is not a valid number",
58 * @cfg {Boolean} castInt (true|false) cast int if true (defalut true)
69 getAutoCreate : function()
71 var align = this.labelAlign || this.parentLabelAlign();
83 cls : 'form-control roo-money-amount-input',
84 autocomplete: 'new-password'
88 input.name = this.name;
92 input.disabled = true;
95 var clg = 12 - this.inputlg;
96 var cmd = 12 - this.inputmd;
97 var csm = 12 - this.inputsm;
98 var cxs = 12 - this.inputxs;
102 cls : 'row roo-money-field',
106 cls : 'roo-money-currency column col-lg-' + clg + ' col-md-' + cmd + ' col-sm-' + csm + ' col-xs-' + cxs,
110 cls: 'roo-select2-container input-group',
114 cls : 'form-control roo-money-currency-input',
115 autocomplete: 'new-password'
119 cls : 'input-group-addon',
133 cls : 'roo-money-amount column col-lg-' + this.inputlg + ' col-md-' + this.inputmd + ' col-sm-' + this.inputsm + ' col-xs-' + this.inputxs,
137 cls: this.hasFeedback ? 'has-feedback' : '',
148 if (this.fieldLabel.length) {
151 tooltip: 'This field is required'
157 cls: 'control-label',
163 html: this.fieldLabel
166 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star left-indicator';
172 if(this.indicatorpos == 'right') {
173 indicator.cls = 'roo-required-indicator text-danger fa fa-lg fa-star right-indicator';
180 if(align == 'left') {
188 if(this.labelWidth > 12){
189 label.style = "width: " + this.labelWidth + 'px';
191 if(this.labelWidth < 13 && this.labelmd == 0){
192 this.labelmd = this.labelWidth;
194 if(this.labellg > 0){
195 label.cls += ' col-lg-' + this.labellg;
196 input.cls += ' col-lg-' + (12 - this.labellg);
198 if(this.labelmd > 0){
199 label.cls += ' col-md-' + this.labelmd;
200 container.cls += ' col-md-' + (12 - this.labelmd);
202 if(this.labelsm > 0){
203 label.cls += ' col-sm-' + this.labelsm;
204 container.cls += ' col-sm-' + (12 - this.labelsm);
206 if(this.labelxs > 0){
207 label.cls += ' col-xs-' + this.labelxs;
208 container.cls += ' col-xs-' + (12 - this.labelxs);
220 ['xs','sm','md','lg'].map(function(size){
221 if (settings[size]) {
222 cfg.cls += ' col-' + size + '-' + settings[size];
230 initEvents : function()
232 this.initCurrencyEvent();
234 this.initNumberEvent();
238 initCurrencyEvent : function()
241 throw "can not find store for combo";
244 this.store = Roo.factory(this.store, Roo.data);
245 this.store.parent = this;
249 this.indicator = this.indicatorEl();
251 this.triggerEl = this.el.select('.input-group-addon', true).first();
253 this.triggerEl.on("click", this.onTriggerClick, this, { preventDefault : true });
255 this.currencyEl = this.el.select('.roo-money-currency-input', true).first();
257 this.amountEl = this.el.select('.roo-money-amount-input', true).first();
262 var lw = _this.listWidth || Math.max(_this.inputEl().getWidth(), _this.minListWidth);
263 _this.list.setWidth(lw);
266 this.list.on('mouseover', this.onViewOver, this);
267 this.list.on('mousemove', this.onViewMove, this);
268 this.list.on('scroll', this.onViewScroll, this);
271 this.tpl = '<li><a href="#">{' + this.currencyField + '}</a></li>';
274 this.view = new Roo.View(this.list, this.tpl, {
275 singleSelect:true, store: this.store, selectedClass: this.selectedClass
278 this.view.on('click', this.onViewClick, this);
280 this.store.on('beforeload', this.onBeforeLoad, this);
281 this.store.on('load', this.onLoad, this);
282 this.store.on('loadexception', this.onLoadException, this);
284 this.keyNav = new Roo.KeyNav(this.currencyEl, {
286 this.inKeyMode = true;
290 "down" : function(e){
291 if(!this.isExpanded()){
292 this.onTriggerClick();
294 this.inKeyMode = true;
299 "enter" : function(e){
302 if(this.fireEvent("specialkey", this, e)){
303 this.onViewClick(false);
316 if(this.fireEvent("specialkey", this, e)){
317 this.onViewClick(false);
325 doRelay : function(foo, bar, hname){
326 if(hname == 'down' || this.scope.isExpanded()){
327 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
337 initNumberEvent : function(e)
339 var allowed = "0123456789";
341 if(this.allowDecimals){
342 allowed += this.decimalSeparator;
345 if(this.allowNegative){
349 this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
351 var keyPress = function(e){
355 var c = e.getCharCode();
358 (String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
359 allowed.indexOf(String.fromCharCode(c)) === -1
365 if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
369 if(allowed.indexOf(String.fromCharCode(c)) === -1){
374 this.amountEl.on("keypress", keyPress, this);
378 onTriggerClick : function(e)
385 this.loadNext = false;
387 if(this.isExpanded()){
392 this.hasFocus = true;
394 if(this.triggerAction == 'all') {
395 this.doQuery(this.allQuery, true);
399 this.doQuery(this.getRawValue());
402 getCurrency : function()
404 var v = this.currencyEl.getValue();
409 restrictHeight : function()
411 this.list.alignTo(this.currencyEl, this.listAlign);
412 this.list.alignTo(this.currencyEl, this.listAlign);
415 onViewClick : function(view, doFocus, el, e)
417 var index = this.view.getSelectedIndexes()[0];
419 var r = this.store.getAt(index);
422 this.onSelect(r, index);
426 onSelect : function(record, index){
428 if(this.fireEvent('beforeselect', this, record, index) !== false){
430 this.setFromCurrencyData(index > -1 ? record.data : false);
434 this.fireEvent('select', this, record, index);
438 setFromCurrencyData : function(o)
442 this.lastCurrency = o;
444 if (this.currencyName) {
445 currency = !o || typeof(o[this.currencyName]) == 'undefined' ? '' : o[this.currencyName];
447 Roo.log('no currencyName value set for '+ (this.name ? this.name : this.id));
450 this.lastSelectionText = currency;
452 this.setCurrency(currency);
455 setFromData : function(o)
457 this.setFromCurrencyData(o);
462 value = !o || typeof(o[this.name]) == 'undefined' ? '' : o[this.name];
464 Roo.log('no value set for '+ (this.name ? this.name : this.id));
467 this.setValue(value);
471 setCurrency : function(v)
473 this.currencyValue = v;
476 this.currencyEl.dom.value = (v === null || v === undefined ? '' : v);
481 setValue : function(v)
483 v = this.fixPrecision(v);
485 v = String(v).replace(".", this.decimalSeparator);
490 this.amountEl.dom.value = (v === null || v === undefined ? '' : v);
495 getRawValue : function()
497 var v = this.amountEl.getValue();
502 getValue : function()
504 return this.fixPrecision(this.parseValue(this.getRawValue()));
507 parseValue : function(value)
509 value = parseFloat(String(value).replace(this.decimalSeparator, "."));
510 return isNaN(value) ? '' : value;
513 fixPrecision : function(value)
515 var nan = isNaN(value);
517 if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
518 return nan ? '' : value;
521 return parseFloat(value).toFixed(this.decimalPrecision);
524 decimalPrecisionFcn : function(v)
526 return Math.floor(v);
529 validateValue : function(value)
531 if(!Roo.bootstrap.MoneyField.superclass.validateValue.call(this, value)){
535 var num = this.parseValue(value);
538 this.markInvalid(String.format(this.nanText, value));
542 if(num < this.minValue){
543 this.markInvalid(String.format(this.minText, this.minValue));
547 if(num > this.maxValue){
548 this.markInvalid(String.format(this.maxText, this.maxValue));
555 validate : function()
562 var currency = this.getCurrency();
564 if(this.validateValue(this.getRawValue()) && currency.length){