UNPKG

simpa

Version:

Lightweight library for prototyping Single Page Applications.

140 lines 4.42 kB
"use strict"; /* MIT License * * Copyright (c) 2016-2023 Dariusz Depta * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Application = void 0; const component_1 = require("./component"); const utils_1 = require("./utils"); /** * Base class for Single Page Application. */ class Application extends component_1.Component { /** Key name of the entry in sessionStorage where the reloading flag is stored. */ _reloadKey; /** HTML element that is a container for the whole application. */ _appContainer; /** Collection of all views defined in application. */ _views; /** * Creates a new application's object. * * @param build Application's buildNumber number. * @param appContainerId Identifier of the HTML element that serves as a container for the whole application. * @param className Class name for the application element. */ constructor(appContainerId, className) { super(className); this._reloadKey = 'RELOADED_' + (0, utils_1.$uuid)(); this._appContainer = (0, utils_1.$id)(appContainerId); this._views = []; } /** * Returns the HTML element that serves as a container for the whole application. * * @return Application's HTML element being a container for the whole application. */ get appContainer() { return this._appContainer; } /** * Returns a collection of all views defined for the application. * * @return Collection of views. */ get views() { return this._views; } doCreate() { super.doCreate(); } doCreateViews() { this._views.forEach((view) => { view.doCreate(); }); } doBuild() { super.doBuild(); this.appContainer.appendChild(this.componentRoot); } doBuildViews() { this.views.forEach((view) => { view.doBuild(); }); } doInit() { super.doInit(); } doInitViews() { this.views.forEach((view) => { view.doInit(); }); } addView(view) { this.views.push(view); } hideViews() { this.views.forEach((view) => { view.hide(); }); } /** * Starts the application. */ start() { this.doCreate(); this.doBuild(); this.doInit(); } /** * Reloads the application unconditionally. */ reload() { setTimeout(() => { window.location.reload(); }, 10); } /** * Reloads the application only when it is older than the last * version deployed on the server. * * @param url - URL of the file containing the last version number, usually 'index.html'. * @param pattern - Regular expression pattern for retrieving a version number. * @param group - Name of the group where the version number is stored when regular expression matches. * @param expected - Expected version number, when retrieved value is different then reload the application. */ reloadWhenNeeded(url, pattern, group, expected) { const request = new Request(url); fetch(request) .then((response) => { if (!response.ok) { console.log('Could not fetch: ' + url); } return response.text(); }) .then((response) => { let regExp = new RegExp(pattern); let matches = regExp.exec(response); if (matches) { let groups = matches.groups; if (groups && groups[group]) { let actual = groups[group]; if (actual !== expected) { window.location.reload(); } } } }); } } exports.Application = Application; //# sourceMappingURL=application.js.map