@eastsideco/escshopify
Version:
WIP JS library for Shopify, containing a variety of useful functionality.
365 lines (312 loc) • 18.9 kB
HTML
<html>
<head>
<meta charset="utf-8">
<base data-ice="baseUrl" href="../../../">
<title data-ice="title">src/easycurrency/EasyCurrency.js | @eastsideco/escshopify</title>
<link type="text/css" rel="stylesheet" href="css/style.css">
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
<script src="script/prettify/prettify.js"></script>
<script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">
<header>
<a href="./">Home</a>
<a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a>
<a href="identifiers.html">Reference</a>
<a href="source.html">Source</a>
<div class="search-box">
<span>
<img src="./image/search.png">
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
</span>
<ul class="search-result"></ul>
</div>
<a href="https://bitbucket.org/sigapps/escshopify.git">Repository</a></header>
<nav class="navigation" data-ice="nav"><div>
<ul>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-entities">entities</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-plugins">plugins</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-utils">utils</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-external">E</span><span data-ice="name"><span><a href="https://www.npmjs.com/package/promise-queue">promise-queue</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency">easycurrency</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/EasyCurrency.js~EasyCurrency.html">EasyCurrency</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/MoneySpan.js~MoneySpan.html">MoneySpan</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/MoneySpanSet.js~MoneySpanSet.html">MoneySpanSet</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/Session.js~Session.html">Session</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/config.js~Config.html">Config</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/state.js~State.html">State</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-formatters">easycurrency/formatters</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/formatters/DefaultFormatter.js~DefaultFormatter.html">DefaultFormatter</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/formatters/Formatter.js~Formatter.html">Formatter</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-helpers">easycurrency/helpers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/helpers/SelectHelper.js~SelectHelper.html">SelectHelper</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#easycurrency-resolvers">easycurrency/resolvers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver.html">CurrencyResolver</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/easycurrency/resolvers/GeoserviceResolver.js~GeoserviceResolver.html">GeoserviceResolver</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#entities">entities</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/entities/Cart.js~Cart.html">Cart</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/entities/Shop.js~Shop.html">Shop</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Cart">Cart</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-a">a</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyCart">ShopifyCart</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyCartLineItem">ShopifyCartLineItem</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyAddress">ShopifyAddress</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyPolicy">ShopifyPolicy</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-ShopifyShop">ShopifyShop</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#geoservice">geoservice</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/geoservice/API.js~API.html">API</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/geoservice/GeoService.js~GeoService.html">GeoService</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSContinent">GSContinent</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSCountry">GSCountry</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSCurrencyRates">GSCurrencyRates</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSCurrencyResponse">GSCurrencyResponse</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSGeoResponse">GSGeoResponse</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSLocation">GSLocation</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-GSTranslatedString">GSTranslatedString</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#http">http</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/http/RequestQueuable.js~RequestQueuable.html">RequestQueuable</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils">utils</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/Log.js~Log.html">Log</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/general.js~GeneralUtils.html">GeneralUtils</a></span></span></li>
<li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#utils-loggers">utils/loggers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/ConsoleLogger.js~ConsoleLogger.html">ConsoleLogger</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/Logger.js~Logger.html">Logger</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/utils/loggers/logLevel.js~LogLevel.html">LogLevel</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-logLevel">logLevel</a></span></span></li>
</ul>
</div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/easycurrency/EasyCurrency.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Evee from 'evee';
import Money from 'js-money';
import config from './config';
import state from './state';
import log from 'log';
import MoneySpanSet from './MoneySpanSet';
import MoneySpan from './MoneySpan';
import GeoserviceResolver from './resolvers/GeoserviceResolver';
import DefaultFormatter from './formatters/DefaultFormatter';
import SelectHelper from './helpers/SelectHelper';
import Session from './Session';
import {geoService} from 'index';
/** @ignore */
const TAG = 'EasyCurrency';
/** @ignore */
var instanceCount = 0;
/**
* Plugin for automatic currency conversion, tax handling, etc.
* @extends {evee}
* @private
*/
export default class EasyCurrency extends Evee {
/**
* Creates a new instance of EasyCurrency.
* This class is intended to be used as a singleton, and has global
* side-effects - do not create multiple instances.
*/
constructor() {
super();
instanceCount++;
if (instanceCount > 1) {
log.warn(TAG, 'More than one instance of EasyCurrency has been created. This may cause unexpected behavior.');
}
/** @type {src/easycurrency/config.js~Config} */
this._config = config;
/** @type {src/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} */
this._resolver = null;
/** @type {src/easycurrency/state.js~State} */
this._state = state;
/** @type {src/easycurrency/MoneySpanSet.js~MoneySpanSet} */
this._moneySpanSet = new MoneySpanSet;
/** @type {src/easycurrency/formatters/Formatter.js~Formatter} */
this._formatter = new DefaultFormatter;
/** @type {src/easycurrency/Session.js~Session} */
this._session = new Session;
/** @type {src/easycurrency/helpers/SelectHelper.js~SelectHelper} */
this.selectHelper = new SelectHelper(this);
}
/**
* Uses a default {@link GeoserviceResolver} as the currency resolver.
*/
useGeoserviceResolver() {
this.setCurrencyResolver(new GeoserviceResolver);
}
/**
* Start running EasyCurrency on the current page.
* @param {Object|src/easycurrency/config.js~Config} config
* @emits {Object|src/easycurrency/config.js~Config} beforeInitialize(config)
* @emits {Object|src/easycurrency/config.js~Config} initialize(config)
*/
initialize(config) {
this.emit('beforeInitialize', config);
log.debug(TAG, 'Initialized with config', config);
if (config) {
this._config = Object.assign({}, this._config, config);
}
var currency = this._session.getCurrency();
var fromSession = currency;
currency = currency || this._config.defaultCurrency;
this.setActiveCurrency(this._session.getCurrency() || this._config.defaultCurrency);
this.emit('initialize', config);
if (!fromSession && this._config.useGeoForCurrency) {
geoService.lookupGeo().then((result) => {
log.debug(TAG, 'Got currency from geoservice: ' + result.currency);
this.setActiveCurrency(result.currency);
});
}
}
/**
* Sets the active currency, and refreshs any money spans.
* @param {String} currency Currency code
* @emits {String} beforeCurrencyChanged(currency)
* @emits {String} currencyChanged(currency)
*/
setActiveCurrency(currency) {
this.emit('beforeCurrencyChanged', currency);
log.debug(TAG, 'Set active currency', currency);
this._state.currency = currency;
this._session.saveCurrency(currency);
this.parse();
this.render();
this.emit('currencyChanged', currency);
}
/**
* Returns the active currency.
* @return {String}
*/
getActiveCurrency() {
return this._state.currency;
}
/**
* Returns the available currency codes.
* @return {String[]}
*/
async getAvailableCurrencies() {
return this._resolver.listCurrencyCodes();
}
/**
* Attempts to find new money spans on the page.
* Change moneySpanSelectors / moneySpanParser config options to modify
* how money spans are parsed.
* @emits {null} beforeParse
* @emits {null} parse
*/
parse() {
this.emit('beforeParse');
log.debug(TAG, 'Parsing');
var els = [];
for (let selector of this._config.moneySpanSelectors) {
var els = document.querySelectorAll(selector);
for (let i = 0; i < els.length; i++) {
var el = els[i];
this._processElement(el);
}
}
this.emit('parse');
}
/**
* @param {DOMNode} el - Element to process
*/
_processElement(el) {
var money = this._config.moneySpanParser(el);
var moneySpan = new MoneySpan(money, this);
moneySpan.setElement(el);
this._moneySpanSet.add(moneySpan);
}
/**
* Forces all money spans to re-render.
* @emits {null} beforeRender
* @emits {null} render
*/
render() {
this.emit('beforeRender');
log.debug(TAG, 'Rendering');
for (let moneySpan of this._moneySpanSet.list()) {
moneySpan.render();
}
this.emit('render');
}
/**
* Returns the current state.
* @return {src/easycurrency/state.js~State}
*/
getState() {
return this._state;
}
/**
* Format a value as a currency
* @param {js-money|Number} amount
* @param {String|null} [currency] If null, uses current currency.
* @return {String}
*/
format(amount, currency) {
if (typeof amount == 'number') {
amount = new Money(amount, currency || this._state.currency);
}
var str = this._formatter.format(amount.amount, amount.currency);
return str;
}
/**
* Convert an amount to another currency.
* @param {js-money|Number} amount
* @param {String} [to] - If null, uses current currency
* @param {null|String} [from] Ignored if amount is a Money object.
* @return {Promise<js-money>}
*/
async convert(amount, to, from) {
if (typeof amount == 'number') {
amount = new Money(amount, from || this._state.currency);
}
var rate = await this._resolver.getConversionRate(amount.currency, to || this._state.currency);
return new Money(amount.multiply(rate).amount, Money[to || this._state.currency]);
}
/**
* Adds the current tax rate to the given amount
* @param {js-money|Number} amount
* @return {Promise<js-money>}
*/
async addTax(amount) {
if (typeof amount == 'number') {
amount = new Money(amount, this._state.currency);
}
return amount.multiply(1 + this._state.taxRate);
}
/**
* Sets the current tax rate.
* @param {type} taxRate - Tax rate as a decimal percentage (i.e. 20% = 0.2)
* @emits {null} beforeChangeTaxRate
* @emits {null} taxRateChanged
*/
setTaxRate(taxRate) {
this.emit('beforeChangeTaxRate');
this._state.taxRate = taxRate;
this.parse();
this.render();
this.emit('taxRateChanged');
}
/**
* Overrides the default currency resolver, which provides information
* about the available currencies.
* @param {src/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} resolver
* @emits {src/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} currencyResolverChanged(resolver)
*/
setCurrencyResolver(resolver) {
this._resolver = resolver;
this.emit('currencyResolverChanged', resolver);
}
/**
* Overrides the default formatter.
* @param {src/easycurrency/formatters/Formatter.js~Formatter} formatter
* @emits {src/easycurrency/formatters/Formatter.js~Formatter} formatterChanged(formatter)
*/
setFormatter(formatter) {
this._formatter = formatter;
this.emit('formatterChanged', resolver);
}
}
</code></pre>
</div>
<footer class="footer">
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.0.4)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>
<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>