@alihbuzaid/ember-ui
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
97 lines (78 loc) • 3.26 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { later } from '@ember/runloop';
import { isNone } from '@ember/utils';
import numbersOnly from '../utils/numbers-only';
import getCurrency from '../utils/get-currency';
import AutoNumeric from 'autonumeric';
export default class MoneyInputComponent extends Component {
fetch;
currentUser;
currencies = getCurrency();
value;
currency;
currencyData;
autonumeric;
constructor() {
super(...arguments);
let whois = this.currentUser.getOption('whois');
this.value = this.args.value ?? 0;
this.currency = this.args.currency ?? whois?.currency?.code ?? 'USD';
this.currencyData = getCurrency(this.currency);
}
autoNumerize(element) {
const { onCurrencyChange } = this.args;
let currency = this.currencyData;
let value = numbersOnly(this.value);
let amount = !currency.decimalSeparator ? value : value / 100;
this.autonumeric = new AutoNumeric(element, amount, this.getCurrencyFormatOptions(currency));
// default the currency from currency data
if (typeof onCurrencyChange === 'function') {
onCurrencyChange(currency.code, currency);
}
element.addEventListener('autoNumeric:formatted', this.onFormatCompleted.bind(this));
}
setCurrency(currency) {
const { onCurrencyChange } = this.args;
if (this.autonumeric) {
this.autonumeric.set(numbersOnly(this.value, true), this.getCurrencyFormatOptions(currency));
}
this.currency = currency.code;
this.currencyData = currency;
if (typeof onCurrencyChange === 'function') {
onCurrencyChange(currency.code, currency);
}
}
onFormatCompleted({ detail }) {
const { onFormatCompleted, onChange } = this.args;
// 300ms for format to apply to input ?
later(
this,
() => {
if (typeof onFormatCompleted === 'function') {
onFormatCompleted(detail);
}
},
300
);
if (typeof onChange === 'function') {
onChange(detail);
}
}
getCurrencyFormatOptions(currency) {
let options = {
currencySymbol: isNone(currency.symbol) ? '$' : currency.symbol,
currencySymbolPlacement: currency.symbolPlacement === 'before' ? 'p' : 's',
decimalCharacter: isNone(currency.decimalSeperator) ? '.' : currency.decimalSeparator,
decimalPlaces: isNone(currency.precision) ? 2 : currency.precision,
digitGroupSeparator: isNone(currency.thousandSeparator) ? ',' : currency.thousandSeparator,
};
// decimal and thousand seperator cannot be the same, if they are revert the thousand seperator
if (options.decimalCharacter === options.digitGroupSeparator) {
options.digitGroupSeparator = ',';
}
return options;
}
}