]> BookStack Code Mirror - bookstack/blob - resources/assets/js/global.js
Resolved conflicts
[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 // Global jQuery Elements
103 let notifications = $('.notification');
104 let successNotification = notifications.filter('.pos');
105 let errorNotification = notifications.filter('.neg');
106 let warningNotification = notifications.filter('.warning');
107 // Notification Events
108 window.Events.listen('success', function (text) {
109     successNotification.hide();
110     successNotification.find('span').text(text);
111     setTimeout(() => {
112         successNotification.show();
113     }, 1);
114 });
115 window.Events.listen('warning', function (text) {
116     warningNotification.find('span').text(text);
117     warningNotification.show();
118 });
119 window.Events.listen('error', function (text) {
120     errorNotification.find('span').text(text);
121     errorNotification.show();
122 });
123
124 // Notification hiding
125 notifications.click(function () {
126     $(this).fadeOut(100);
127 });
128
129 // Chapter page list toggles
130 $('.chapter-toggle').click(function (e) {
131     e.preventDefault();
132     $(this).toggleClass('open');
133     $(this).closest('.chapter').find('.inset-list').slideToggle(180);
134 });
135
136 // Back to top button
137 $('#back-to-top').click(function() {
138      $('#header').smoothScrollTo();
139 });
140 let scrollTopShowing = false;
141 let scrollTop = document.getElementById('back-to-top');
142 let scrollTopBreakpoint = 1200;
143 window.addEventListener('scroll', function() {
144     let scrollTopPos = document.documentElement.scrollTop || document.body.scrollTop || 0;
145     if (!scrollTopShowing && scrollTopPos > scrollTopBreakpoint) {
146         scrollTop.style.display = 'block';
147         scrollTopShowing = true;
148         setTimeout(() => {
149             scrollTop.style.opacity = 0.4;
150         }, 1);
151     } else if (scrollTopShowing && scrollTopPos < scrollTopBreakpoint) {
152         scrollTop.style.opacity = 0;
153         scrollTopShowing = false;
154         setTimeout(() => {
155             scrollTop.style.display = 'none';
156         }, 500);
157     }
158 });
159
160 // Common jQuery actions
161 $('[data-action="expand-entity-list-details"]').click(function() {
162     $('.entity-list.compact').find('p').not('.empty-text').slideToggle(240);
163 });
164
165 // Toggle thumbnail::hide image and reduce grid size
166 $(document).ready(function(){
167    $('[data-action="expand-thumbnail"]').click(function(){
168      $('.gallery-item').toggleClass("collapse").find('img').slideToggle(50);
169    });
170 });
171
172 // Popup close
173 $('.popup-close').click(function() {
174     $(this).closest('.overlay').fadeOut(240);
175 });
176 $('.overlay').click(function(event) {
177     if (!$(event.target).hasClass('overlay')) return;
178     $(this).fadeOut(240);
179 });
180
181 // Detect IE for css
182 if(navigator.userAgent.indexOf('MSIE')!==-1
183     || navigator.appVersion.indexOf('Trident/') > 0
184     || navigator.userAgent.indexOf('Safari') !== -1){
185     document.body.classList.add('flexbox-support');
186 }
187
188 // Page specific items
189 require("./pages/page-show");