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