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;
11 const Vue = require("vue");
12 const axios = require("axios");
14 let axiosInstance = axios.create({
16 'X-CSRF-TOKEN': document.querySelector('meta[name=token]').getAttribute('content'),
17 'baseURL': window.baseUrl('')
20 window.$http = axiosInstance;
22 Vue.prototype.$http = axiosInstance;
24 require("./vues/vues");
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");
34 let ngApp = angular.module('bookStack', ['ngResource', 'ngAnimate', 'ngSanitize', 'ui.sortable']);
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);
42 // Global Event System
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];
58 listen(eventName, callback) {
59 if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
60 this.listeners[eventName].push(callback);
65 window.Events = new EventManager();
66 Vue.prototype.$events = window.Events;
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);
76 //Global jQuery Config & Extensions
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)
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;
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);
104 successNotification.show();
107 window.Events.listen('warning', function (text) {
108 warningNotification.find('span').text(text);
109 warningNotification.show();
111 window.Events.listen('error', function (text) {
112 errorNotification.find('span').text(text);
113 errorNotification.show();
116 // Notification hiding
117 notifications.click(function () {
118 $(this).fadeOut(100);
121 // Chapter page list toggles
122 $('.chapter-toggle').click(function (e) {
124 $(this).toggleClass('open');
125 $(this).closest('.chapter').find('.inset-list').slideToggle(180);
128 // Back to top button
129 $('#back-to-top').click(function() {
130 $('#header').smoothScrollTo();
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;
141 scrollTop.style.opacity = 0.4;
143 } else if (scrollTopShowing && scrollTopPos < scrollTopBreakpoint) {
144 scrollTop.style.opacity = 0;
145 scrollTopShowing = false;
147 scrollTop.style.display = 'none';
152 // Common jQuery actions
153 $('[data-action="expand-entity-list-details"]').click(function() {
154 $('.entity-list.compact').find('p').not('.empty-text').slideToggle(240);
158 $('.popup-close').click(function() {
159 $(this).closest('.overlay').fadeOut(240);
161 $('.overlay').click(function(event) {
162 if (!$(event.target).hasClass('overlay')) return;
163 $(this).fadeOut(240);
167 if(navigator.userAgent.indexOf('MSIE')!==-1
168 || navigator.appVersion.indexOf('Trident/') > 0
169 || navigator.userAgent.indexOf('Safari') !== -1){
170 $('body').addClass('flexbox-support');
173 // Page specific items
174 require("./pages/page-show");