"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) {
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;
};
scope.updateImageFromModel = function (model) {
- var isResized = scope.resizeWidth && scope.resizeHeight;
+ let isResized = scope.resizeWidth && scope.resizeHeight;
if (!isResized) {
scope.$apply(() => {
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);
});
};
}]);
-
+ /**
+ * 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');
};
}]);
+ /**
+ * TinyMCE
+ * An angular wrapper around the tinyMCE editor.
+ */
ngApp.directive('tinymce', ['$timeout', function ($timeout) {
return {
restrict: 'A',
}
}]);
+ /**
+ * Markdown input
+ * Handles the logic for just the editor input field.
+ */
ngApp.directive('markdownInput', ['$timeout', function ($timeout) {
return {
restrict: 'A',
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;
}
}]);
+ /**
+ * 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"]');
// Insert image shortcut
if (event.which === 73 && event.ctrlKey && event.shiftKey) {
event.preventDefault();
- var caretPos = input[0].selectionStart;
- var currentContent = input.val();
- var mdImageText = "";
+ let caretPos = input[0].selectionStart;
+ let currentContent = input.val();
+ const mdImageText = "";
input.val(currentContent.substring(0, caretPos) + mdImageText + currentContent.substring(caretPos));
input.focus();
input[0].selectionStart = caretPos + (";
// Insert image from image manager
insertImage.click(event => {
window.ImageManager.showExternal(image => {
- var caretPos = currentCaretPos;
- var currentContent = input.val();
- var mdImageText = "";
+ let caretPos = currentCaretPos;
+ let currentContent = input.val();
+ let mdImageText = "";
input.val(currentContent.substring(0, caretPos) + mdImageText + currentContent.substring(caretPos));
input.change();
});
}
}]);
+ /**
+ * Page Editor Toolbox
+ * Controls all functionality for the sliding toolbox
+ * on the page edit view.
+ */
ngApp.directive('toolbox', [function () {
return {
restrict: 'A',
}
}]);
+ /**
+ * 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',
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();
}
}
}]);
+
+
+ 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;
+ });
+ };
+ }
+ };
+ }]);
};