2 require("babel-polyfill");
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;
12 // Global Event System
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];
28 listen(eventName, callback) {
29 if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
30 this.listeners[eventName].push(callback);
35 window.Events = new EventManager();
37 const Vue = require("vue");
38 const axios = require("axios");
40 let axiosInstance = axios.create({
42 'X-CSRF-TOKEN': document.querySelector('meta[name=token]').getAttribute('content'),
43 'baseURL': window.baseUrl('')
46 axiosInstance.interceptors.request.use(resp => {
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);
53 window.$http = axiosInstance;
55 Vue.prototype.$http = axiosInstance;
56 Vue.prototype.$events = window.Events;
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");
66 let ngApp = angular.module('bookStack', ['ngResource', 'ngAnimate', 'ngSanitize', 'ui.sortable']);
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);
75 require("./vues/vues");
76 require("./components");
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);
84 //Global jQuery Config & Extensions
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)
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;
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);
112 successNotification.show();
115 window.Events.listen('warning', function (text) {
116 warningNotification.find('span').text(text);
117 warningNotification.show();
119 window.Events.listen('error', function (text) {
120 errorNotification.find('span').text(text);
121 errorNotification.show();
124 // Notification hiding
125 notifications.click(function () {
126 $(this).fadeOut(100);
129 // Chapter page list toggles
130 $('.chapter-toggle').click(function (e) {
132 $(this).toggleClass('open');
133 $(this).closest('.chapter').find('.inset-list').slideToggle(180);
136 // Back to top button
137 $('#back-to-top').click(function() {
138 $('#header').smoothScrollTo();
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;
149 scrollTop.style.opacity = 0.4;
151 } else if (scrollTopShowing && scrollTopPos < scrollTopBreakpoint) {
152 scrollTop.style.opacity = 0;
153 scrollTopShowing = false;
155 scrollTop.style.display = 'none';
160 // Common jQuery actions
161 $('[data-action="expand-entity-list-details"]').click(function() {
162 $('.entity-list.compact').find('p').not('.empty-text').slideToggle(240);
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);
173 $('.popup-close').click(function() {
174 $(this).closest('.overlay').fadeOut(240);
176 $('.overlay').click(function(event) {
177 if (!$(event.target).hasClass('overlay')) return;
178 $(this).fadeOut(240);
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');
188 // Page specific items
189 require("./pages/page-show");