]> BookStack Code Mirror - bookstack/blob - resources/assets/js/global.js
Migrated attachment manager to vue
[bookstack] / resources / assets / js / global.js
1 "use strict";
2 require("babel-polyfill");
3
4 // Url retrieval function
5 window.baseUrl = function(path) {
6     let basePath = document.querySelector('meta[name="base-url"]').getAttribute('content');
7     if (basePath[basePath.length-1] === '/') basePath = basePath.slice(0, basePath.length-1);
8     if (path[0] === '/') path = path.slice(1);
9     return basePath + '/' + path;
10 };
11
12 // Global Event System
13 class EventManager {
14     constructor() {
15         this.listeners = {};
16     }
17
18     emit(eventName, eventData) {
19         if (typeof this.listeners[eventName] === 'undefined') return this;
20         let eventsToStart = this.listeners[eventName];
21         for (let i = 0; i < eventsToStart.length; i++) {
22             let event = eventsToStart[i];
23             event(eventData);
24         }
25         return this;
26     }
27
28     listen(eventName, callback) {
29         if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
30         this.listeners[eventName].push(callback);
31         return this;
32     }
33 }
34
35 window.Events = new EventManager();
36
37 const Vue = require("vue");
38 const axios = require("axios");
39
40 let axiosInstance = axios.create({
41     headers: {
42         'X-CSRF-TOKEN': document.querySelector('meta[name=token]').getAttribute('content'),
43         'baseURL': window.baseUrl('')
44     }
45 });
46 axiosInstance.interceptors.request.use(resp => {
47     return resp;
48 }, err => {
49     if (typeof err.response === "undefined" || typeof err.response.data === "undefined") return Promise.reject(err);
50     if (typeof err.response.data.error !== "undefined") window.Events.emit('error', err.response.data.error);
51     if (typeof err.response.data.message !== "undefined") window.Events.emit('error', err.response.data.message);
52 });
53 window.$http = axiosInstance;
54
55 Vue.prototype.$http = axiosInstance;
56 Vue.prototype.$events = window.Events;
57
58
59 // AngularJS - Create application and load components
60 const angular = require("angular");
61 require("angular-resource");
62 require("angular-animate");
63 require("angular-sanitize");
64 require("angular-ui-sortable");
65
66 let ngApp = angular.module('bookStack', ['ngResource', 'ngAnimate', 'ngSanitize', 'ui.sortable']);
67
68 // Translation setup
69 // Creates a global function with name 'trans' to be used in the same way as Laravel's translation system
70 const Translations = require("./translations");
71 let translator = new Translations(window.translations);
72 window.trans = translator.get.bind(translator);
73
74
75 require("./vues/vues");
76 require("./components");
77
78 // Load in angular specific items
79 const Directives = require('./directives');
80 const Controllers = require('./controllers');
81 Directives(ngApp, window.Events);
82 Controllers(ngApp, window.Events);
83
84 //Global jQuery Config & Extensions
85
86 // Smooth scrolling
87 jQuery.fn.smoothScrollTo = function () {
88     if (this.length === 0) return;
89     $('html, body').animate({
90         scrollTop: this.offset().top - 60 // Adjust to change final scroll position top margin
91     }, 300); // Adjust to change animations speed (ms)
92     return this;
93 };
94
95 // Making contains text expression not worry about casing
96 jQuery.expr[":"].contains = $.expr.createPseudo(function (arg) {
97     return function (elem) {
98         return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
99     };
100 });
101
102 // Detect IE for css
103 if(navigator.userAgent.indexOf('MSIE')!==-1
104     || navigator.appVersion.indexOf('Trident/') > 0
105     || navigator.userAgent.indexOf('Safari') !== -1){
106     document.body.classList.add('flexbox-support');
107 }
108
109 // Page specific items
110 require("./pages/page-show");