UNPKG

@eastsideco/escshopify

Version:

WIP JS library for Shopify, containing a variety of useful functionality.

395 lines (336 loc) 20.4 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../../"> <title data-ice="title">src/plugins/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/evee">evee</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/js-money">js-money</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#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-class">C</span><span data-ice="name"><span><a href="class/src/entities/index.js~Entities.html">Entities</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#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#plugins">plugins</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/index.js~Plugins.html">Plugins</a></span></span></li> <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#plugins-easycurrency">plugins/easycurrency</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/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/plugins/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/plugins/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/plugins/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/plugins/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/plugins/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#plugins-easycurrency-formatters">plugins/easycurrency/formatters</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/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/plugins/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#plugins-easycurrency-helpers">plugins/easycurrency/helpers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/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#plugins-easycurrency-resolvers">plugins/easycurrency/resolvers</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/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/plugins/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#plugins-geoservice">plugins/geoservice</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/plugins/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/plugins/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#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/plugins/easycurrency/EasyCurrency.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import Evee from &apos;evee&apos;; import Money from &apos;js-money&apos;; import config from &apos;./config&apos;; import state from &apos;./state&apos;; import log from &apos;log&apos;; import MoneySpanSet from &apos;./MoneySpanSet&apos;; import MoneySpan from &apos;./MoneySpan&apos;; import GeoserviceResolver from &apos;./resolvers/GeoserviceResolver&apos;; import DefaultFormatter from &apos;./formatters/DefaultFormatter&apos;; import Formatter from &apos;./formatters/Formatter&apos;; import CurrencyResolver from &apos;./resolvers/CurrencyResolver&apos;; import SelectHelper from &apos;./helpers/SelectHelper&apos;; import Session from &apos;./Session&apos;; import GeoService from &apos;~/plugins/geoservice/GeoService&apos;; /** @ignore */ const TAG = &apos;EasyCurrency&apos;; /** @ignore */ var instanceCount = 0; /** * Plugin for automatic currency conversion, tax handling, etc. * @extends {evee} * @private */ 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++; var styles = []; if (instanceCount &gt; 1) { log.warn(TAG, &apos;More than one instance of EasyCurrency has been created. This may cause unexpected behavior.&apos;); } /** @type {src/plugins/easycurrency/config.js~Config} */ this._config = config; /** @type {src/plugins/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} */ this._resolver = null; /** @type {src/plugins/easycurrency/state.js~State} */ this._state = state; /** @type {src/plugins/easycurrency/MoneySpanSet.js~MoneySpanSet} */ this._moneySpanSet = new MoneySpanSet; /** @type {src/plugins/easycurrency/formatters/Formatter.js~Formatter} */ this._formatter = new DefaultFormatter; /** @type {src/plugins/easycurrency/Session.js~Session} */ this._session = new Session; /** @type {src/plugins/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/plugins/easycurrency/config.js~Config} config * @emits {Object|src/plugins/easycurrency/config.js~Config} beforeInitialize(config) * @emits {Object|src/plugins/easycurrency/config.js~Config} initialize(config) */ initialize(config) { this.emit(&apos;beforeInitialize&apos;, config); log.debug(TAG, &apos;Initialized with config&apos;, 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(&apos;initialize&apos;, config); if (!fromSession &amp;&amp; this._config.useGeoForCurrency) { var geoService = new GeoService; geoService.lookupGeo().then((result) =&gt; { log.debug(TAG, &apos;Got currency from geoservice: &apos; + 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(&apos;beforeCurrencyChanged&apos;, currency); log.debug(TAG, &apos;Set active currency&apos;, currency); if (!currency || (this._config.allowedCurrencies != &apos;any&apos; &amp;&amp; this._config.allowedCurrencies.indexOf(currency) == -1)) { log.debug(TAG, &apos;Currency not in whitelist, switching to base currency.&apos;); currency = this._config.defaultCurrency; } this._state.currency = currency; this._session.saveCurrency(currency); this.parse(); this.render(); this.emit(&apos;currencyChanged&apos;, 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(&apos;beforeParse&apos;); log.debug(TAG, &apos;Parsing&apos;); var els = []; for (let selector of this._config.moneySpanSelectors) { var els = document.querySelectorAll(selector); for (let i = 0; i &lt; els.length; i++) { var el = els[i]; this._processElement(el); } } this.emit(&apos;parse&apos;); } /** * @param {DOMNode} el - Element to process */ _processElement(el) { var money = this._config.moneySpanParser(el, this); 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(&apos;beforeRender&apos;); log.debug(TAG, &apos;Rendering&apos;); for (let moneySpan of this._moneySpanSet.list()) { moneySpan.render(); } this.emit(&apos;render&apos;); } /** * Returns the current state. * @return {src/plugins/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 == &apos;number&apos;) { 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&lt;js-money&gt;} */ async convert(amount, to, from) { if (typeof amount == &apos;number&apos;) { 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&lt;js-money&gt;} */ async addTax(amount) { if (typeof amount == &apos;number&apos;) { 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(&apos;beforeChangeTaxRate&apos;); this._state.taxRate = taxRate; this.parse(); this.render(); this.emit(&apos;taxRateChanged&apos;); } /** * Overrides the default currency resolver, which provides information * about the available currencies. * @param {src/plugins/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} resolver * @emits {src/plugins/easycurrency/resolvers/CurrencyResolver.js~CurrencyResolver} currencyResolverChanged(resolver) */ setCurrencyResolver(resolver) { this._resolver = resolver; this.emit(&apos;currencyResolverChanged&apos;, resolver); } /** * Overrides the default formatter. * @param {src/plugins/easycurrency/formatters/Formatter.js~Formatter} formatter * @emits {src/plugins/easycurrency/formatters/Formatter.js~Formatter} formatterChanged(formatter) */ setFormatter(formatter) { this._formatter = formatter; this.emit(&apos;formatterChanged&apos;, resolver); } } EasyCurrency.formatters = { DefaultFormatter, Formatter }; EasyCurrency.helpers = { SelectHelper, }; EasyCurrency.resolvers = { CurrencyResolver, GeoserviceResolver }; export default EasyCurrency; </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>