]> BookStack Code Mirror - bookstack/blob - resources/assets/js/global.js
Added comment reply and delete confirmation.
[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 window.trans_choice = translator.getPlural.bind(translator);
77
78
79 require("./vues/vues");
80 require("./components");
81
82 // Load in angular specific items
83 const Directives = require('./directives');
84 const Controllers = require('./controllers');
85 Directives(ngApp, window.$events);
86 Controllers(ngApp, window.$events);
87
88 //Global jQuery Config & Extensions
89
90 /**
91  * Scroll the view to a specific element.
92  * @param {HTMLElement} element
93  */
94 window.scrollToElement = function(element) {
95     if (!element) return;
96     let top = element.getBoundingClientRect().top + document.body.scrollTop;
97     $('html, body').animate({
98         scrollTop: top - 60 // Adjust to change final scroll position top margin
99     }, 300);
100 };
101
102 /**
103  * Scroll and highlight an element.
104  * @param {HTMLElement} element
105  */
106 window.scrollAndHighlight = function(element) {
107     if (!element) return;
108     window.scrollToElement(element);
109     let color = document.getElementById('custom-styles').getAttribute('data-color-light');
110     let initColor = window.getComputedStyle(element).getPropertyValue('background-color');
111     element.style.backgroundColor = color;
112     setTimeout(() => {
113         element.classList.add('selectFade');
114         element.style.backgroundColor = initColor;
115     }, 10);
116     setTimeout(() => {
117         element.classList.remove('selectFade');
118         element.style.backgroundColor = '';
119     }, 3000);
120 };
121
122 // Smooth scrolling
123 jQuery.fn.smoothScrollTo = function () {
124     if (this.length === 0) return;
125     window.scrollToElement(this[0]);
126     return this;
127 };
128
129 // Making contains text expression not worry about casing
130 jQuery.expr[":"].contains = $.expr.createPseudo(function (arg) {
131     return function (elem) {
132         return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
133     };
134 });
135
136 // Detect IE for css
137 if(navigator.userAgent.indexOf('MSIE')!==-1
138     || navigator.appVersion.indexOf('Trident/') > 0
139     || navigator.userAgent.indexOf('Safari') !== -1){
140     document.body.classList.add('flexbox-support');
141 }
142
143 // Page specific items
144 require("./pages/page-show");