UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

213 lines (204 loc) 6.68 kB
/*! * NENT 2022 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-1829aebc.js'); const index$1 = require('./index-637e8c28.js'); const logging = require('./logging-37c154cf.js'); const state = require('./state-f97ff0e6.js'); const state$1 = require('./state-d62be6f1.js'); const strings = require('./strings-fe5a8e44.js'); require('./index-96f3ab3f.js'); require('./values-b2399e33.js'); let provider; /** * `setAppProvider` is a function that takes a string and an object as parameters and sets the value of * the `provider` variable to the object. * @param {string} name - The name of the provider. * @param {any} p - the provider object */ function setAppProvider(name, p) { logging.debugIf(state.state.debug, `app-provider: ${name} registered`); provider = p; } /** * It returns the provider if it exists, otherwise it returns null * @returns The provider variable. */ function getAppProvider() { return provider; } /* istanbul ignore file */ const APP_TOPIC = 'app'; var APP_COMMANDS; (function (APP_COMMANDS) { APP_COMMANDS["RegisterProvider"] = "register-provider"; APP_COMMANDS["Log"] = "log"; APP_COMMANDS["Warn"] = "warn"; APP_COMMANDS["Dir"] = "dir"; APP_COMMANDS["SetDarkMode"] = "set-dark-mode"; })(APP_COMMANDS || (APP_COMMANDS = {})); var APP_EVENTS; (function (APP_EVENTS) { APP_EVENTS["ThemeChanged"] = "theme"; })(APP_EVENTS || (APP_EVENTS = {})); class DefaultAppProvider { constructor(win = window, eventBus) { this.win = win; this.disposeThemeSubscription = state$1.onChange('darkMode', t => { if (t == null) win === null || win === void 0 ? void 0 : win.localStorage.removeItem('darkMode'); else win === null || win === void 0 ? void 0 : win.localStorage.setItem('darkMode', t.toString()); eventBus === null || eventBus === void 0 ? void 0 : eventBus.emit(APP_EVENTS.ThemeChanged, t); }); win.addEventListener('storage', () => { this.getTheme(); }); this.getTheme(); } getTheme() { var _a; const mode = (_a = this.win) === null || _a === void 0 ? void 0 : _a.localStorage.getItem('darkMode'); if (mode != null) { state$1.state.darkMode = mode == 'true'; } else { state$1.state.darkMode = null; } } setDarkMode(data) { const { value } = data; state$1.state.darkMode = value != undefined ? Boolean(value) : null; } log(data) { const { message } = data; if (message) { logging.log(message); } else { logging.table(data); } } warn(data) { const { message } = data; if (message) { logging.warn(message); } else { logging.table(data); } } dir(data) { logging.dir(data); } destroy() { this.disposeThemeSubscription(); } } /* It listens for actions on the `APP_TOPIC` topic, and then calls the appropriate function on the `AppProvider` that is registered for the current document */ class AppActionListener { /** * > This function is called by the `AppManager` to initialize the `AppProvider` with the `Window` * object, the `actionBus` and the `eventBus` * @param {Window} win - Window - the window object * @param {IEventEmitter} actionBus - This is the event bus that the app will use to listen for * actions. * @param {IEventEmitter} eventBus - This is the event bus that the app will use to communicate with * the rest of the application. */ initialize(win, actionBus, eventBus) { this.eventBus = eventBus; this.actionsSubscription = actionBus.on(APP_TOPIC, e => { this.handleAction(e); }); this.defaultProvider = new DefaultAppProvider(win, eventBus); } handleAction(actionEvent) { logging.debugIf(state.state.debug, `document-listener: action received ${JSON.stringify(actionEvent)}`); if (actionEvent.command === APP_COMMANDS.RegisterProvider) { const { name = 'unknown', provider } = actionEvent.data; if (provider) { setAppProvider(name, provider); } } else { const currentProvider = getAppProvider(); const commandFuncKey = strings.kebabToCamelCase(actionEvent.command); // Use the registered provider unless it doesn't implement this command let commandFunc = currentProvider ? currentProvider[commandFuncKey] : null; if (!commandFunc) commandFunc = this.defaultProvider[commandFuncKey]; if (commandFunc && typeof commandFunc === 'function') { commandFunc.call(this.defaultProvider, actionEvent.data); } } } /** * It unsubscribes from the actions subscription and destroys the default provider. */ destroy() { var _a; this.actionsSubscription(); (_a = this.defaultProvider) === null || _a === void 0 ? void 0 : _a.destroy(); } } const App = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.events = index.createEvent(this, "nent:events", 6); this.actions = index.createEvent(this, "nent:actions", 3); /** * Turn on debugging to get helpful messages from the * app, routing, data and action systems. */ this.debug = false; } /** * Listen for outside command-requests. These events are delegated into * the internal action-bus. */ delegateActionEventFromDOM(ev) { const action = ev.detail; index$1.actionBus.emit(action.topic, action); } componentWillLoad() { state.state.debug = this.debug; if (this.debug) { logging.log('n-app: initializing <debug>'); } else { logging.log(`n-app: initializing`); } this.listener = new AppActionListener(); this.listener.initialize(window, index$1.actionBus, index$1.eventBus); logging.debugIf(state.state.debug, `n-app: services and listener registered`); this.actionsSubscription = index$1.actionBus.on('*', (...args) => { this.actions.emit(...args); }); this.eventSubscription = index$1.eventBus.on('*', (...args) => { this.events.emit(...args); }); } render() { return index.h(index.Host, { style: { display: 'block' } }); } componentDidLoad() { logging.log('n-app: initialized'); } disconnectedCallback() { var _a, _b; this.listener.destroy(); (_a = this.actionsSubscription) === null || _a === void 0 ? void 0 : _a.call(this); (_b = this.eventSubscription) === null || _b === void 0 ? void 0 : _b.call(this); index$1.eventBus.removeAllListeners(); index$1.actionBus.removeAllListeners(); } get el() { return index.getElement(this); } }; App.style = "[n-cloak] { display: inherit; }"; exports.n_app = App;