]> BookStack Code Mirror - bookstack/blobdiff - resources/assets/js/directives.js
Applied baseUrl to login redirect
[bookstack] / resources / assets / js / directives.js
index 43d55f092eab39e6e370c26e6039c7dbb53ae261..fc7b88259eccb92b17e4a2305abe75d96bd66027 100644 (file)
@@ -1,10 +1,10 @@
 "use strict";
-var DropZone = require('dropzone');
-var markdown = require('marked');
+const DropZone = require('dropzone');
+const markdown = require('marked');
 
-var toggleSwitchTemplate = require('./components/toggle-switch.html');
-var imagePickerTemplate = require('./components/image-picker.html');
-var dropZoneTemplate = require('./components/drop-zone.html');
+const toggleSwitchTemplate = require('./components/toggle-switch.html');
+const imagePickerTemplate = require('./components/image-picker.html');
+const dropZoneTemplate = require('./components/drop-zone.html');
 
 module.exports = function (ngApp, events) {
 
@@ -54,7 +54,7 @@ module.exports = function (ngApp, events) {
                 imageClass: '@'
             },
             link: function (scope, element, attrs) {
-                var usingIds = typeof scope.currentId !== 'undefined' || scope.currentId === 'false';
+                let usingIds = typeof scope.currentId !== 'undefined' || scope.currentId === 'false';
                 scope.image = scope.currentImage;
                 scope.value = scope.currentImage || '';
                 if (usingIds) scope.value = scope.currentId;
@@ -80,7 +80,7 @@ module.exports = function (ngApp, events) {
                 };
 
                 scope.updateImageFromModel = function (model) {
-                    var isResized = scope.resizeWidth && scope.resizeHeight;
+                    let isResized = scope.resizeWidth && scope.resizeHeight;
 
                     if (!isResized) {
                         scope.$apply(() => {
@@ -89,8 +89,9 @@ module.exports = function (ngApp, events) {
                         return;
                     }
 
-                    var cropped = scope.resizeCrop ? 'true' : 'false';
-                    var requestString = '/images/thumb/' + model.id + '/' + scope.resizeWidth + '/' + scope.resizeHeight + '/' + cropped;
+                    let cropped = scope.resizeCrop ? 'true' : 'false';
+                    let requestString = '/images/thumb/' + model.id + '/' + scope.resizeWidth + '/' + scope.resizeHeight + '/' + cropped;
+                    requestString = window.baseUrl(requestString);
                     $http.get(requestString).then((response) => {
                         setImage(model, response.data.url);
                     });
@@ -149,14 +150,30 @@ module.exports = function (ngApp, events) {
         };
     }]);
 
-
+    /**
+     * Dropdown
+     * Provides some simple logic to create small dropdown menus
+     */
     ngApp.directive('dropdown', [function () {
         return {
             restrict: 'A',
             link: function (scope, element, attrs) {
-                var menu = element.find('ul');
+                const menu = element.find('ul');
                 element.find('[dropdown-toggle]').on('click', function () {
                     menu.show().addClass('anim menuIn');
+                    let inputs = menu.find('input');
+                    let hasInput = inputs.length > 0;
+                    if (hasInput) {
+                        inputs.first().focus();
+                        element.on('keypress', 'input', event => {
+                            if (event.keyCode === 13) {
+                                event.preventDefault();
+                                menu.hide();
+                                menu.removeClass('anim menuIn');
+                                return false;
+                            }
+                        });
+                    }
                     element.mouseleave(function () {
                         menu.hide();
                         menu.removeClass('anim menuIn');
@@ -166,6 +183,10 @@ module.exports = function (ngApp, events) {
         };
     }]);
 
+    /**
+     * TinyMCE
+     * An angular wrapper around the tinyMCE editor.
+     */
     ngApp.directive('tinymce', ['$timeout', function ($timeout) {
         return {
             restrict: 'A',
@@ -231,6 +252,10 @@ module.exports = function (ngApp, events) {
         }
     }]);
 
+    /**
+     * Markdown input
+     * Handles the logic for just the editor input field.
+     */
     ngApp.directive('markdownInput', ['$timeout', function ($timeout) {
         return {
             restrict: 'A',
@@ -241,11 +266,14 @@ module.exports = function (ngApp, events) {
             link: function (scope, element, attrs) {
 
                 // Set initial model content
-                var content = element.val();
+                element = element.find('textarea').first();
+                let content = element.val();
                 scope.mdModel = content;
                 scope.mdChange(markdown(content));
 
-                element.on('change input', (e) => {
+                console.log('test');
+
+                element.on('change input', (event) => {
                     content = element.val();
                     $timeout(() => {
                         scope.mdModel = content;
@@ -263,13 +291,17 @@ module.exports = function (ngApp, events) {
         }
     }]);
 
+    /**
+     * Markdown Editor
+     * Handles all functionality of the markdown editor.
+     */
     ngApp.directive('markdownEditor', ['$timeout', function ($timeout) {
         return {
             restrict: 'A',
             link: function (scope, element, attrs) {
 
                 // Elements
-                const input = element.find('textarea[markdown-input]');
+                const input = element.find('[markdown-input] textarea').first();
                 const display = element.find('.markdown-display').first();
                 const insertImage = element.find('button[data-action="insertImage"]');
 
@@ -314,9 +346,9 @@ module.exports = function (ngApp, events) {
                     // Insert image shortcut
                     if (event.which === 73 && event.ctrlKey && event.shiftKey) {
                         event.preventDefault();
-                        var caretPos = input[0].selectionStart;
-                        var currentContent = input.val();
-                        var mdImageText = "![](http://)";
+                        let caretPos = input[0].selectionStart;
+                        let currentContent = input.val();
+                        const mdImageText = "![](http://)";
                         input.val(currentContent.substring(0, caretPos) + mdImageText + currentContent.substring(caretPos));
                         input.focus();
                         input[0].selectionStart = caretPos + ("![](".length);
@@ -330,9 +362,9 @@ module.exports = function (ngApp, events) {
                 // Insert image from image manager
                 insertImage.click(event => {
                     window.ImageManager.showExternal(image => {
-                        var caretPos = currentCaretPos;
-                        var currentContent = input.val();
-                        var mdImageText = "![" + image.name + "](" + image.url + ")";
+                        let caretPos = currentCaretPos;
+                        let currentContent = input.val();
+                        let mdImageText = "![" + image.name + "](" + image.url + ")";
                         input.val(currentContent.substring(0, caretPos) + mdImageText + currentContent.substring(caretPos));
                         input.change();
                     });
@@ -342,6 +374,11 @@ module.exports = function (ngApp, events) {
         }
     }]);
 
+    /**
+     * Page Editor Toolbox
+     * Controls all functionality for the sliding toolbox
+     * on the page edit view.
+     */
     ngApp.directive('toolbox', [function () {
         return {
             restrict: 'A',
@@ -378,6 +415,11 @@ module.exports = function (ngApp, events) {
         }
     }]);
 
+    /**
+     * Tag Autosuggestions
+     * Listens to child inputs and provides autosuggestions depending on field type
+     * and input. Suggestions provided by server.
+     */
     ngApp.directive('tagAutosuggestions', ['$http', function ($http) {
         return {
             restrict: 'A',
@@ -460,7 +502,7 @@ module.exports = function (ngApp, events) {
                         changeActiveTo(newActive, suggestionElems);
                     }
                     // Enter or tab key
-                    else if (event.keyCode === 13 || event.keyCode === 9) {
+                    else if ((event.keyCode === 13 || event.keyCode === 9) && !event.shiftKey) {
                         let text = suggestionElems[active].textContent;
                         currentInput[0].value = text;
                         currentInput.focus();
@@ -557,6 +599,67 @@ module.exports = function (ngApp, events) {
             }
         }
     }]);
+
+
+    ngApp.directive('entitySelector', ['$http', '$sce', function ($http, $sce) {
+        return {
+            restrict: 'A',
+            scope: true,
+            link: function (scope, element, attrs) {
+                scope.loading = true;
+                scope.entityResults = false;
+                scope.search = '';
+
+                // Add input for forms
+                const input = element.find('[entity-selector-input]').first();
+
+                // Listen to entity item clicks
+                element.on('click', '.entity-list a', function(event) {
+                    event.preventDefault();
+                    event.stopPropagation();
+                    let item = $(this).closest('[data-entity-type]');
+                    itemSelect(item);
+                });
+                element.on('click', '[data-entity-type]', function(event) {
+                    itemSelect($(this));
+                });
+
+                // Select entity action
+                function itemSelect(item) {
+                    let entityType = item.attr('data-entity-type');
+                    let entityId = item.attr('data-entity-id');
+                    let isSelected = !item.hasClass('selected');
+                    element.find('.selected').removeClass('selected').removeClass('primary-background');
+                    if (isSelected) item.addClass('selected').addClass('primary-background');
+                    let newVal = isSelected ? `${entityType}:${entityId}` : '';
+                    input.val(newVal);
+                }
+
+                // Get search url with correct types
+                function getSearchUrl() {
+                    let types = (attrs.entityTypes) ? encodeURIComponent(attrs.entityTypes) : encodeURIComponent('page,book,chapter');
+                    return window.baseUrl(`/ajax/search/entities?types=${types}`);
+                }
+
+                // Get initial contents
+                $http.get(getSearchUrl()).then(resp => {
+                    scope.entityResults = $sce.trustAsHtml(resp.data);
+                    scope.loading = false;
+                });
+
+                // Search when typing
+                scope.searchEntities = function() {
+                    scope.loading = true;
+                    input.val('');
+                    let url = getSearchUrl() + '&term=' + encodeURIComponent(scope.search);
+                    $http.get(url).then(resp => {
+                        scope.entityResults = $sce.trustAsHtml(resp.data);
+                        scope.loading = false;
+                    });
+                };
+            }
+        };
+    }]);
 };