]> BookStack Code Mirror - bookstack/blob - resources/assets/js/global.js
Merge pull request #358 from jendrol/master
[bookstack] / resources / assets / js / global.js
1 "use strict";
2
3 // Url retrieval function
4 window.baseUrl = function(path) {
5     let basePath = document.querySelector('meta[name="base-url"]').getAttribute('content');
6     if (basePath[basePath.length-1] === '/') basePath = basePath.slice(0, basePath.length-1);
7     if (path[0] === '/') path = path.slice(1);
8     return basePath + '/' + path;
9 };
10
11 const Vue = require("vue");
12 const axios = require("axios");
13
14 let axiosInstance = axios.create({
15     headers: {
16         'X-CSRF-TOKEN': document.querySelector('meta[name=token]').getAttribute('content'),
17         'baseURL': window.baseUrl('')
18     }
19 });
20
21 Vue.prototype.$http = axiosInstance;
22
23 require("./vues/vues");
24
25
26 // AngularJS - Create application and load components
27 const angular = require("angular");
28 require("angular-resource");
29 require("angular-animate");
30 require("angular-sanitize");
31 require("angular-ui-sortable");
32
33 let ngApp = angular.module('bookStack', ['ngResource', 'ngAnimate', 'ngSanitize', 'ui.sortable']);
34
35 // Translation setup
36 // Creates a global function with name 'trans' to be used in the same way as Laravel's translation system
37 const Translations = require("./translations");
38 let translator = new Translations(window.translations);
39 window.trans = translator.get.bind(translator);
40
41 // Global Event System
42 class EventManager {
43     constructor() {
44         this.listeners = {};
45     }
46
47     emit(eventName, eventData) {
48         if (typeof this.listeners[eventName] === 'undefined') return this;
49         let eventsToStart = this.listeners[eventName];
50         for (let i = 0; i < eventsToStart.length; i++) {
51             let event = eventsToStart[i];
52             event(eventData);
53         }
54         return this;
55     }
56
57     listen(eventName, callback) {
58         if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
59         this.listeners[eventName].push(callback);
60         return this;
61     }
62 }
63
64 window.Events = new EventManager();
65 Vue.prototype.$events = window.Events;
66
67 // Load in angular specific items
68 const Services = require('./services');
69 const Directives = require('./directives');
70 const Controllers = require('./controllers');
71 Services(ngApp, window.Events);
72 Directives(ngApp, window.Events);
73 Controllers(ngApp, window.Events);
74
75 //Global jQuery Config & Extensions
76
77 // Smooth scrolling
78 jQuery.fn.smoothScrollTo = function () {
79     if (this.length === 0) return;
80     $('html, body').animate({
81         scrollTop: this.offset().top - 60 // Adjust to change final scroll position top margin
82     }, 300); // Adjust to change animations speed (ms)
83     return this;
84 };
85
86 // Making contains text expression not worry about casing
87 jQuery.expr[":"].contains = $.expr.createPseudo(function (arg) {
88     return function (elem) {
89         return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
90     };
91 });
92
93 // Global jQuery Elements
94 let notifications = $('.notification');
95 let successNotification = notifications.filter('.pos');
96 let errorNotification = notifications.filter('.neg');
97 let warningNotification = notifications.filter('.warning');
98 // Notification Events
99 window.Events.listen('success', function (text) {
100     successNotification.hide();
101     successNotification.find('span').text(text);
102     setTimeout(() => {
103         successNotification.show();
104     }, 1);
105 });
106 window.Events.listen('warning', function (text) {
107     warningNotification.find('span').text(text);
108     warningNotification.show();
109 });
110 window.Events.listen('error', function (text) {
111     errorNotification.find('span').text(text);
112     errorNotification.show();
113 });
114
115 // Notification hiding
116 notifications.click(function () {
117     $(this).fadeOut(100);
118 });
119
120 // Chapter page list toggles
121 $('.chapter-toggle').click(function (e) {
122     e.preventDefault();
123     $(this).toggleClass('open');
124     $(this).closest('.chapter').find('.inset-list').slideToggle(180);
125 });
126
127 // Back to top button
128 $('#back-to-top').click(function() {
129      $('#header').smoothScrollTo();
130 });
131 let scrollTopShowing = false;
132 let scrollTop = document.getElementById('back-to-top');
133 let scrollTopBreakpoint = 1200;
134 window.addEventListener('scroll', function() {
135     let scrollTopPos = document.documentElement.scrollTop || document.body.scrollTop || 0;
136     if (!scrollTopShowing && scrollTopPos > scrollTopBreakpoint) {
137         scrollTop.style.display = 'block';
138         scrollTopShowing = true;
139         setTimeout(() => {
140             scrollTop.style.opacity = 0.4;
141         }, 1);
142     } else if (scrollTopShowing && scrollTopPos < scrollTopBreakpoint) {
143         scrollTop.style.opacity = 0;
144         scrollTopShowing = false;
145         setTimeout(() => {
146             scrollTop.style.display = 'none';
147         }, 500);
148     }
149 });
150
151 // Common jQuery actions
152 $('[data-action="expand-entity-list-details"]').click(function() {
153     $('.entity-list.compact').find('p').not('.empty-text').slideToggle(240);
154 });
155
156 // Popup close
157 $('.popup-close').click(function() {
158     $(this).closest('.overlay').fadeOut(240);
159 });
160 $('.overlay').click(function(event) {
161     if (!$(event.target).hasClass('overlay')) return;
162     $(this).fadeOut(240);
163 });
164
165 // Detect IE for css
166 if(navigator.userAgent.indexOf('MSIE')!==-1
167     || navigator.appVersion.indexOf('Trident/') > 0
168     || navigator.userAgent.indexOf('Safari') !== -1){
169     $('body').addClass('flexbox-support');
170 }
171
172 // Page specific items
173 require("./pages/page-show");