BookStack/resources/assets/js/directives.js

238 lines
8.5 KiB
JavaScript
Raw Normal View History

2015-12-31 02:38:18 +08:00
"use strict";
var DropZone = require('dropzone');
2016-03-25 22:41:15 +08:00
var markdown = require( "marked" );
2015-12-30 00:39:25 +08:00
var toggleSwitchTemplate = require('./components/toggle-switch.html');
2015-12-31 02:38:18 +08:00
var imagePickerTemplate = require('./components/image-picker.html');
var dropZoneTemplate = require('./components/drop-zone.html');
2015-12-30 00:39:25 +08:00
module.exports = function (ngApp, events) {
2015-12-30 00:39:25 +08:00
/**
* Toggle Switches
2015-12-31 02:38:18 +08:00
* Has basic on/off functionality.
2015-12-30 00:39:25 +08:00
* Use string values of 'true' & 'false' to dictate the current state.
*/
ngApp.directive('toggleSwitch', function () {
2015-12-30 00:39:25 +08:00
return {
restrict: 'E',
template: toggleSwitchTemplate,
scope: true,
link: function (scope, element, attrs) {
2015-12-30 00:39:25 +08:00
scope.name = attrs.name;
scope.value = attrs.value;
scope.isActive = scope.value == true && scope.value != 'false';
scope.value = (scope.value == true && scope.value != 'false') ? 'true' : 'false';
scope.switch = function () {
2015-12-30 00:39:25 +08:00
scope.isActive = !scope.isActive;
scope.value = scope.isActive ? 'true' : 'false';
}
}
};
});
2015-12-31 02:38:18 +08:00
/**
* Image Picker
* Is a simple front-end interface that connects to an ImageManager if present.
*/
ngApp.directive('imagePicker', ['$http', 'imageManagerService', function ($http, imageManagerService) {
2015-12-31 02:38:18 +08:00
return {
restrict: 'E',
template: imagePickerTemplate,
scope: {
name: '@',
resizeHeight: '@',
resizeWidth: '@',
resizeCrop: '@',
showRemove: '=',
currentImage: '@',
currentId: '@',
defaultImage: '@',
imageClass: '@'
},
link: function (scope, element, attrs) {
2015-12-31 02:38:18 +08:00
var usingIds = typeof scope.currentId !== 'undefined' || scope.currentId === 'false';
scope.image = scope.currentImage;
scope.value = scope.currentImage || '';
if (usingIds) scope.value = scope.currentId;
2015-12-31 02:38:18 +08:00
function setImage(imageModel, imageUrl) {
scope.image = imageUrl;
scope.value = usingIds ? imageModel.id : imageUrl;
}
scope.reset = function () {
2015-12-31 02:38:18 +08:00
setImage({id: 0}, scope.defaultImage);
};
scope.remove = function () {
2015-12-31 02:38:18 +08:00
scope.image = 'none';
scope.value = 'none';
};
scope.showImageManager = function () {
2015-12-31 02:38:18 +08:00
imageManagerService.show((image) => {
scope.updateImageFromModel(image);
});
};
scope.updateImageFromModel = function (model) {
2015-12-31 02:38:18 +08:00
var isResized = scope.resizeWidth && scope.resizeHeight;
if (!isResized) {
scope.$apply(() => {
setImage(model, model.url);
});
return;
}
var cropped = scope.resizeCrop ? 'true' : 'false';
var requestString = '/images/thumb/' + model.id + '/' + scope.resizeWidth + '/' + scope.resizeHeight + '/' + cropped;
$http.get(requestString).then((response) => {
setImage(model, response.data.url);
});
};
}
};
}]);
/**
* DropZone
* Used for uploading images
*/
ngApp.directive('dropZone', [function () {
2015-12-31 02:38:18 +08:00
return {
restrict: 'E',
template: dropZoneTemplate,
scope: {
uploadUrl: '@',
eventSuccess: '=',
eventError: '=',
uploadedTo: '@'
2015-12-31 02:38:18 +08:00
},
link: function (scope, element, attrs) {
2015-12-31 02:38:18 +08:00
var dropZone = new DropZone(element[0].querySelector('.dropzone-container'), {
url: scope.uploadUrl,
init: function () {
2015-12-31 02:38:18 +08:00
var dz = this;
dz.on('sending', function (file, xhr, data) {
2015-12-31 02:38:18 +08:00
var token = window.document.querySelector('meta[name=token]').getAttribute('content');
data.append('_token', token);
var uploadedTo = typeof scope.uploadedTo === 'undefined' ? 0 : scope.uploadedTo;
data.append('uploaded_to', uploadedTo);
2015-12-31 02:38:18 +08:00
});
if (typeof scope.eventSuccess !== 'undefined') dz.on('success', scope.eventSuccess);
dz.on('success', function (file, data) {
2015-12-31 02:38:18 +08:00
$(file.previewElement).fadeOut(400, function () {
dz.removeFile(file);
});
});
if (typeof scope.eventError !== 'undefined') dz.on('error', scope.eventError);
dz.on('error', function (file, errorMessage, xhr) {
console.log(errorMessage);
console.log(xhr);
function setMessage(message) {
$(file.previewElement).find('[data-dz-errormessage]').text(message);
2015-12-31 02:38:18 +08:00
}
if (xhr.status === 413) setMessage('The server does not allow uploads of this size. Please try a smaller file.');
if (errorMessage.file) setMessage(errorMessage.file[0]);
2015-12-31 02:38:18 +08:00
});
}
});
}
};
}]);
ngApp.directive('dropdown', [function () {
2015-12-31 02:38:18 +08:00
return {
restrict: 'A',
link: function (scope, element, attrs) {
2015-12-31 02:38:18 +08:00
var menu = element.find('ul');
element.find('[dropdown-toggle]').on('click', function () {
2015-12-31 02:38:18 +08:00
menu.show().addClass('anim menuIn');
element.mouseleave(function () {
2015-12-31 02:38:18 +08:00
menu.hide();
menu.removeClass('anim menuIn');
});
});
}
};
}]);
2016-03-12 23:52:19 +08:00
ngApp.directive('tinymce', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
tinymce: '=',
mceModel: '=',
mceChange: '='
},
link: function (scope, element, attrs) {
function tinyMceSetup(editor) {
2016-03-12 23:52:19 +08:00
editor.on('ExecCommand change NodeChange ObjectResized', (e) => {
var content = editor.getContent();
2016-03-12 23:52:19 +08:00
$timeout(() => {
scope.mceModel = content;
});
scope.mceChange(content);
});
2016-03-12 23:52:19 +08:00
editor.on('init', (e) => {
scope.mceModel = editor.getContent();
});
scope.$on('html-update', (event, value) => {
editor.setContent(value);
editor.selection.select(editor.getBody(), true);
editor.selection.collapse(false);
scope.mceModel = editor.getContent();
});
}
scope.tinymce.extraSetups.push(tinyMceSetup);
tinymce.init(scope.tinymce);
}
}
}])
2016-03-25 22:41:15 +08:00
ngApp.directive('markdownEditor', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
mdModel: '=',
mdChange: '='
},
link: function (scope, element, attrs) {
// Set initial model content
var content = element.val();
scope.mdModel = content;
scope.mdChange(markdown(content));
element.on('change input', (e) => {
content = element.val();
$timeout(() => {
scope.mdModel = content;
scope.mdChange(markdown(content));
});
});
scope.$on('markdown-update', (event, value) => {
element.val(value);
scope.mdModel= value;
scope.mdChange(markdown(value));
});
}
}
}])
2015-12-31 02:38:18 +08:00
2015-12-30 00:39:25 +08:00
};