diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-22 00:55:32 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-22 00:55:32 +0800 |
commit | b494a4a4dba6c781667b690f38387f03a22fe141 (patch) | |
tree | 935d42af3c0d8a4969ec98902f0f64c89747b14e /packages/instant | |
parent | e0beb7fb380e50e4566db4787092f5549a2d6db1 (diff) | |
download | dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar.gz dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar.bz2 dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar.lz dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar.xz dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.tar.zst dexon-sol-tools-b494a4a4dba6c781667b690f38387f03a22fe141.zip |
chore: clean up index.umd.ts a bit
Diffstat (limited to 'packages/instant')
-rw-r--r-- | packages/instant/public/index.html | 2 | ||||
-rw-r--r-- | packages/instant/src/index.umd.ts | 70 |
2 files changed, 39 insertions, 33 deletions
diff --git a/packages/instant/public/index.html b/packages/instant/public/index.html index 8f11502e4..df39994ef 100644 --- a/packages/instant/public/index.html +++ b/packages/instant/public/index.html @@ -64,7 +64,7 @@ window.history.pushState({ page: 1 }, '0x Instant'); window.history.pushState({ page: 2 }, '0x Instant'); window.history.pushState({ page: 3 }, '0x Instant'); - window.onpopstate = () => console.log('Integrators onpopstate called'); + window.onpopstate = () => console.log("Integrator's onpopstate called"); const removeUndefined = obj => { for (let k in obj) if (obj[k] === undefined) delete obj[k]; diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index 1d62084b5..15ca59323 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -8,11 +8,8 @@ import { assert } from './util/assert'; import { util } from './util/util'; const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID); -export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps { - shouldDisablePushToHistory?: boolean; -} -export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { +const validateInstantRenderConfig = (config: ZeroExInstantConfig, selector: string) => { assert.isValidOrderSource('orderSource', config.orderSource); if (!_.isUndefined(config.defaultSelectedAssetData)) { assert.isHexString('defaultSelectedAssetData', config.defaultSelectedAssetData); @@ -48,37 +45,46 @@ export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_Z assert.isBoolean('shouldDisableAnalyticsTracking', config.shouldDisableAnalyticsTracking); } assert.isString('selector', selector); - // Render instant and return a callback that allows you to remove it from the DOM. - const renderInstant = () => { - const appendToIfExists = document.querySelector(selector); - assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`); - const appendTo = appendToIfExists as Element; - const injectedDiv = document.createElement('div'); - injectedDiv.setAttribute('id', INJECTED_DIV_ID); - injectedDiv.setAttribute('class', INJECTED_DIV_CLASS); - appendTo.appendChild(injectedDiv); - const closeInstant = () => { - if (!_.isUndefined(config.onClose)) { - config.onClose(); - } - appendTo.removeChild(injectedDiv); - }; - const instantOverlayProps = { - ...config, - // If we are using the history API, just go back to close - onClose: () => (config.shouldDisablePushToHistory ? closeInstant() : window.history.back()), - }; - ReactDOM.render(React.createElement(ZeroExInstantOverlay, instantOverlayProps), injectedDiv); - return closeInstant; +}; + +// Render instant and return a callback that allows you to remove it from the DOM. +const renderInstant = (config: ZeroExInstantConfig, selector: string) => { + const appendToIfExists = document.querySelector(selector); + assert.assert(!_.isNull(appendToIfExists), `Could not find div with selector: ${selector}`); + const appendTo = appendToIfExists as Element; + const injectedDiv = document.createElement('div'); + injectedDiv.setAttribute('id', INJECTED_DIV_ID); + injectedDiv.setAttribute('class', INJECTED_DIV_CLASS); + appendTo.appendChild(injectedDiv); + const closeInstant = () => { + if (!_.isUndefined(config.onClose)) { + config.onClose(); + } + appendTo.removeChild(injectedDiv); + }; + const instantOverlayProps = { + ...config, + // If we are using the history API, just go back to close + onClose: () => (config.shouldDisablePushToHistory ? closeInstant() : window.history.back()), }; + ReactDOM.render(React.createElement(ZeroExInstantOverlay, instantOverlayProps), injectedDiv); + return closeInstant; +}; + +export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps { + shouldDisablePushToHistory?: boolean; +} + +export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => { + validateInstantRenderConfig(config, selector); if (config.shouldDisablePushToHistory) { if (!isInstantRendered()) { - renderInstant(); + renderInstant(config, selector); } } else { // Before we render, push to history saying that instant is showing for this part of the history. window.history.pushState({ zeroExInstantShowing: true }, '0x Instant'); - let removeInstant = renderInstant(); + let removeInstant = renderInstant(config, selector); let prevOnPopState = util.boundNoop; if (window.onpopstate) { @@ -86,12 +92,12 @@ export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_Z } window.onpopstate = (e: PopStateEvent) => { // Don't override integrators handler. - prevOnPopState(e); - // e.state represents the new state - if (e.state && e.state.zeroExInstantShowing) { + // prevOnPopState(e); + const newState = e.state; + if (newState && newState.zeroExInstantShowing) { // We have returned to a history state that expects instant to be rendered. if (!isInstantRendered()) { - removeInstant = renderInstant(); + removeInstant = renderInstant(config, selector); } } else { // History has changed to a different state. |