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