Home Manual Reference Source

src/easycurrency/main.js

import Evee from 'evee';
import config from './config';
import state from './state';
import log from 'log';

const TAG = 'EasyCurrency';

/**
 * Plugin for automatic currency conversion, tax handling, etc.
 * @private
 */
export default class EasyCurrency extends Evee {
    /**  This is a singleton class - do not create instances directly. */
    constructor() {
        super();

        /** @type {src/easycurrency/config.js~Config} */
        this._config = config;
        /** @type {src/easycurrency/resolver.js~CurrencyResolver} */
        this._resolver = null;
        /** @type {src/easycurrency/state.js~State} */
        this._state = state;
    }


    /**
     * Start running EasyCurrency on the current page.
     */
    initialize(config) {
        log.debug(TAG, 'Initialized with config', config);
        if (config) {
            this._config = Object.assign({}, this._config, config);
        }

        this.setActiveCurrency(this._config.defaultCurrency);
    }

    /**
     * Sets the active currency, and refreshs any money spans.
     * @param {String} currency Currency code
     */
    setActiveCurrency(currency) {
        log.debug(TAG, 'Set active currency', currency);
        this._state.currency = currency;
    }


    getState() {
        return this._state;
    }


    /**
     * Format a value as a currency
     * @param {js-money|Number} amount
     * @param {String|null} currency If null, uses current currency.
     */
    format(amount, currency) {

    }

    /**
     * Convert an amount to another currency.
     * @param {js-money|Number} amount
     * @param {String} to
     * @param {null|String} from Ignored if amount is a Money object.
     */
    convert(amount, to, from) {

    }

    /**
     * Adds the current tax rate to the given amount
     * @param {js-money|Number} amount
     */
    addTax(amount) {

    }




    /**
     * Overrides the default currency resolver, which provides information
     * about the available currencies.
     * @param {src/easycurrency/resolver.js~CurrencyResolver} resolver
     */
    setCurrencyResolver(resolver) {
        this._resolver = resolver;
    }

}