UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

85 lines (72 loc) 2.02 kB
/* eslint-disable import/no-mutable-exports */ import ZMP from 'zmp-core/lite'; import { extend, unsetRouterIds } from './utils'; var zmp; var zmpevents; var theme = {}; var zmprouters = { views: [], tabs: [], modals: null }; var setTheme = function setTheme() { if (!zmp) return; theme.ios = zmp.theme === 'ios'; theme.md = zmp.theme === 'md'; theme.aurora = zmp.theme === 'aurora'; }; var cleanup = function cleanup() { unsetRouterIds(); delete theme.ios; delete theme.md; delete theme.aurora; zmprouters.views = []; zmprouters.tabs = []; zmprouters.modals = null; }; var zmpinitEvents = function zmpinitEvents() { zmpevents = new ZMP.Events(); }; var zmpinit = function zmpinit(rootEl, params, init) { if (params === void 0) { params = {}; } if (init === void 0) { init = true; } var zmpParams = extend({}, params, { el: rootEl, init: init }); if (typeof params.store !== 'undefined') zmpParams.store = params.store; if (!zmpParams.routes) zmpParams.routes = []; if (zmpParams.userAgent && (zmpParams.theme === 'auto' || !zmpParams.theme)) { var device = ZMP.getDevice({ userAgent: zmpParams.userAgent }, true); theme.ios = !!device.ios; theme.aurora = device.desktop && device.electron; theme.md = !theme.ios && !theme.aurora; } // eslint-disable-next-line if (zmp && typeof window !== 'undefined') return; // eslint-disable-next-line if (typeof window === 'undefined') cleanup(); var instance = new ZMP(zmpParams); zmp = instance; setTheme(); if (instance.initialized) { zmp = instance; zmpevents.emit('ready', zmp); } else { instance.on('init', function () { zmp = instance; zmpevents.emit('ready', zmp); }); } }; var zmpready = function zmpready(callback) { if (!callback) return; if (zmp && zmp.initialized) callback(zmp);else { zmpevents.once('ready', callback); } }; export { zmp, theme, zmpready, zmpevents, zmpinit, zmprouters, zmpinitEvents, setTheme };