import ItemList from './utils/ItemList'; import Button from './components/Button'; import ModalManager from './components/ModalManager'; import AlertManager from './components/AlertManager'; import RequestErrorModal from './components/RequestErrorModal'; import Translator from './Translator'; import Store from './Store'; import Session from './Session'; import extract from './utils/extract'; import Drawer from './utils/Drawer'; import mapRoutes from './utils/mapRoutes'; import RequestError from './utils/RequestError'; import ScrollListener from './utils/ScrollListener'; import liveHumanTimes from './utils/liveHumanTimes'; import { extend } from './extend'; import Forum from './models/Forum'; import User from './models/User'; import Discussion from './models/Discussion'; import Post from './models/Post'; import Group from './models/Group'; import Notification from './models/Notification'; import PageState from './states/PageState'; import ModalManagerState from './states/ModalManagerState'; import AlertManagerState from './states/AlertManagerState'; /** * The `App` class provides a container for an application, as well as various * utilities for the rest of the app to use. */ export default class Application { /** * The forum model for this application. * * @type {Forum} * @public */ forum = null; /** * A map of routes, keyed by a unique route name. Each route is an object * containing the following properties: * * - `path` The path that the route is accessed at. * - `component` The Mithril component to render when this route is active. * * @example * app.routes.discussion = {path: '/d/:id', component: DiscussionPage.component()}; * * @type {Object} * @public */ routes = {}; /** * An ordered list of initializers to bootstrap the application. * * @type {ItemList} * @public */ initializers = new ItemList(); /** * The app's session. * * @type {Session} * @public */ session = null; /** * The app's translator. * * @type {Translator} * @public */ translator = new Translator(); /** * The app's data store. * * @type {Store} * @public */ store = new Store({ forums: Forum, users: User, discussions: Discussion, posts: Post, groups: Group, notifications: Notification, }); /** * A local cache that can be used to store data at the application level, so * that is persists between different routes. * * @type {Object} * @public */ cache = {}; /** * Whether or not the app has been booted. * * @type {Boolean} * @public */ booted = false; /** * The key for an Alert that was shown as a result of an AJAX request error. * If present, it will be dismissed on the next successful request. * * @type {int} * @private */ requestErrorAlert = null; /** * The page the app is currently on. * * This object holds information about the type of page we are currently * visiting, and sometimes additional arbitrary page state that may be * relevant to lower-level components. * * @type {PageState} */ current = new PageState(null); /** * The page the app was on before the current page. * * Once the application navigates to another page, the object previously * assigned to this.current will be moved to this.previous, while this.current * is re-initialized. * * @type {PageState} */ previous = new PageState(null); /* * An object that manages modal state. * * @type {ModalManagerState} */ modal = new ModalManagerState(); /** * An object that manages the state of active alerts. * * @type {AlertManagerState} */ alerts = new AlertManagerState(); data; title = ''; titleCount = 0; initialRoute; load(payload) { this.data = payload; this.translator.setLocale(payload.locale); } boot() { this.initializers.toArray().forEach((initializer) => initializer(this)); this.store.pushPayload({ data: this.data.resources }); this.forum = this.store.getById('forums', 1); this.session = new Session(this.store.getById('users', this.data.session.userId), this.data.session.csrfToken); this.mount(); this.initialRoute = window.location.href; } // TODO: This entire system needs a do-over for v2 bootExtensions(extensions) { Object.keys(extensions).forEach((name) => { const extension = extensions[name]; // If an extension doesn't define extenders, there's nothing more to do here. if (!extension.extend) return; const extenders = extension.extend.flat(Infinity); for (const extender of extenders) { extender.extend(this, { name, exports: extension }); } }); } mount(basePath = '') { // An object with a callable view property is used in order to pass arguments to the component; see https://mithril.js.org/mount.html m.mount(document.getElementById('modal'), { view: () => ModalManager.component({ state: this.modal }) }); m.mount(document.getElementById('alerts'), { view: () => AlertManager.component({ state: this.alerts }) }); this.drawer = new Drawer(); m.route(document.getElementById('content'), basePath + '/', mapRoutes(this.routes, basePath)); // Add a class to the body which indicates that the page has been scrolled // down. When this happens, we'll add classes to the header and app body // which will set the navbar's position to fixed. We don't want to always // have it fixed, as that could overlap with custom headers. const scrollListener = new ScrollListener((top) => { const $app = $('#app'); const offset = $app.offset().top; $app.toggleClass('affix', top >= offset).toggleClass('scrolled', top > offset); $('.App-header').toggleClass('navbar-fixed-top', top >= offset); }); scrollListener.start(); scrollListener.update(); $(() => { $('body').addClass('ontouchstart' in window ? 'touch' : 'no-touch'); }); liveHumanTimes(); } /** * Get the API response document that has been preloaded into the application. * * @return {Object|null} * @public */ preloadedApiDocument() { // If the URL has changed, the preloaded Api document is invalid. if (this.data.apiDocument && window.location.href === this.initialRoute) { const results = this.store.pushPayload(this.data.apiDocument); this.data.apiDocument = null; return results; } return null; } /** * Determine the current screen mode, based on our media queries. * * @returns {String} - one of "phone", "tablet", "desktop" or "desktop-hd" */ screen() { const styles = getComputedStyle(document.documentElement); return styles.getPropertyValue('--flarum-screen'); } /** * Set the