]> BookStack Code Mirror - bookstack/blob - resources/assets/js/global.js
Converted image manager into vue component
[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 const Vue = require("vue");
13 const axios = require("axios");
14
15 let axiosInstance = axios.create({
16     headers: {
17         'X-CSRF-TOKEN': document.querySelector('meta[name=token]').getAttribute('content'),
18         'baseURL': window.baseUrl('')
19     }
20 });
21 window.$http = axiosInstance;
22 Vue.prototype.$http = axiosInstance;
23
24
25 // AngularJS - Create application and load components
26 const angular = require("angular");
27 require("angular-resource");
28 require("angular-animate");
29 require("angular-sanitize");
30 require("angular-ui-sortable");
31
32 let ngApp = angular.module('bookStack', ['ngResource', 'ngAnimate', 'ngSanitize', 'ui.sortable']);
33
34 // Translation setup
35 // Creates a global function with name 'trans' to be used in the same way as Laravel's translation system
36 const Translations = require("./translations");
37 let translator = new Translations(window.translations);
38 window.trans = translator.get.bind(translator);
39
40 // Global Event System
41 class EventManager {
42     constructor() {
43         this.listeners = {};
44     }
45
46     emit(eventName, eventData) {
47         if (typeof this.listeners[eventName] === 'undefined') return this;
48         let eventsToStart = this.listeners[eventName];
49         for (let i = 0; i < eventsToStart.length; i++) {
50             let event = eventsToStart[i];
51             event(eventData);
52         }
53         return this;
54     }
55
56     listen(eventName, callback) {
57         if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = [];
58         this.listeners[eventName].push(callback);
59         return this;
60     }
61 }
62
63 window.Events = new EventManager();
64 Vue.prototype.$events = window.Events;
65
66 require("./vues/vues");
67 require("./components");
68
69 // Load in angular specific items
70 const Directives = require('./directives');
71 const Controllers = require('./controllers');
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 // Detect IE for css
94 if(navigator.userAgent.indexOf('MSIE')!==-1
95     || navigator.appVersion.indexOf('Trident/') > 0
96     || navigator.userAgent.indexOf('Safari') !== -1){
97     document.body.classList.add('flexbox-support');
98 }
99
100 // Page specific items
101 require("./pages/page-show");