]> BookStack Code Mirror - bookstack/blob - resources/js/app.js
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / resources / js / app.js
1 import * as events from './services/events';
2 import * as httpInstance from './services/http';
3 import Translations from './services/translations';
4
5 import * as components from './services/components';
6 import * as componentMap from './components';
7
8 // Url retrieval function
9 window.baseUrl = function baseUrl(path) {
10     let targetPath = path;
11     let basePath = document.querySelector('meta[name="base-url"]').getAttribute('content');
12     if (basePath[basePath.length - 1] === '/') basePath = basePath.slice(0, basePath.length - 1);
13     if (targetPath[0] === '/') targetPath = targetPath.slice(1);
14     return `${basePath}/${targetPath}`;
15 };
16
17 window.importVersioned = function importVersioned(moduleName) {
18     const version = document.querySelector('link[href*="/dist/styles.css?version="]').href.split('?version=').pop();
19     const importPath = window.baseUrl(`dist/${moduleName}.js?version=${version}`);
20     return import(importPath);
21 };
22
23 // Set events and http services on window
24 window.$http = httpInstance;
25 window.$events = events;
26
27 // Translation setup
28 // Creates a global function with name 'trans' to be used in the same way as the Laravel translation system
29 const translator = new Translations();
30 window.trans = translator.get.bind(translator);
31 window.trans_choice = translator.getPlural.bind(translator);
32 window.trans_plural = translator.parsePlural.bind(translator);
33
34 // Load & initialise components
35 components.register(componentMap);
36 window.$components = components;
37 components.init();