UNPKG

@eastsideco/escshopify

Version:

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

83 lines (72 loc) 1.95 kB
import config from './config'; import Money from 'js-money'; /** * Represents an element which renders a monetary value. */ export default class MoneySpan { /** * Create a new MoneySpan instance. * @param {js-money} money The initial value of the element. * @param {src/plugins/easycurrency/main.js~EasyCurrency} easyCurrency EasyCurrency */ constructor(money, easyCurrency) { /** @type {DOMNode} */ this._element = null; /** @type {js-money} */ this._money = money; /** @type {src/plugins/easycurrency/main.js~EasyCurrency} */ this._easyCurrency = easyCurrency; } /** * Retrieves the element this money span is attached to. * @return {DOMNode} */ getElement() { return this._element; } /** * Attaches this money span to the given element. * @param {DOMNode} el - The element to attach to. */ setElement(el) { this._element = el; } /** * Destroys this MoneySpan. */ destroy() { this._element = null; this._easyCurrency = null; this._money = null; } /** * Set an element which this money value will render to when changed. * @param {DOMNode} element - The element to render to. */ setRenderElement(element) { this._element = element; this.render(); } /** * Change the value of this MoneySpan. * @param {js-money} money */ set(money) { this._money = money; this.render(); } /** * Re-render the element. */ render() { if (!this._element) { return; } this._easyCurrency.addTax(this._money) .then((v) => this._easyCurrency.convert(v)) .then((v) => this._easyCurrency.format(v)) .then((str) => { this._element.innerText = str; }); } }