BookStack/resources/assets/js/directives.js

165 lines
6.0 KiB
JavaScript
Raw Normal View History

2015-12-30 13:38:18 -05:00
"use strict";
var DropZone = require('dropzone');
2015-12-29 11:39:25 -05:00
var toggleSwitchTemplate = require('./components/toggle-switch.html');
2015-12-30 13:38:18 -05:00
var imagePickerTemplate = require('./components/image-picker.html');
var dropZoneTemplate = require('./components/drop-zone.html');
2015-12-29 11:39:25 -05:00
module.exports = function (ngApp) {
2015-12-29 11:39:25 -05:00
/**
* Toggle Switches
2015-12-30 13:38:18 -05:00
* Has basic on/off functionality.
2015-12-29 11:39:25 -05:00
* Use string values of 'true' & 'false' to dictate the current state.
*/
ngApp.directive('toggleSwitch', function () {
2015-12-29 11:39:25 -05:00
return {
restrict: 'E',
template: toggleSwitchTemplate,
scope: true,
link: function (scope, element, attrs) {
2015-12-29 11:39:25 -05: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-29 11:39:25 -05:00
scope.isActive = !scope.isActive;
scope.value = scope.isActive ? 'true' : 'false';
}
}
};
});
2015-12-30 13:38:18 -05: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-30 13:38:18 -05:00
return {
restrict: 'E',
template: imagePickerTemplate,
scope: {
name: '@',
resizeHeight: '@',
resizeWidth: '@',
resizeCrop: '@',
showRemove: '=',
currentImage: '@',
currentId: '@',
defaultImage: '@',
imageClass: '@'
},
link: function (scope, element, attrs) {
2015-12-30 13:38:18 -05:00
var usingIds = typeof scope.currentId !== 'undefined' || scope.currentId === 'false';
scope.image = scope.currentImage;
scope.value = scope.currentImage || '';
function setImage(imageModel, imageUrl) {
scope.image = imageUrl;
scope.value = usingIds ? imageModel.id : imageUrl;
}
scope.reset = function () {
2015-12-30 13:38:18 -05:00
setImage({id: 0}, scope.defaultImage);
};
scope.remove = function () {
2015-12-30 13:38:18 -05:00
scope.image = 'none';
scope.value = 'none';
};
scope.showImageManager = function () {
2015-12-30 13:38:18 -05:00
imageManagerService.show((image) => {
scope.updateImageFromModel(image);
});
};
scope.updateImageFromModel = function (model) {
2015-12-30 13:38:18 -05: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-30 13:38:18 -05:00
return {
restrict: 'E',
template: dropZoneTemplate,
scope: {
uploadUrl: '@',
eventSuccess: '=',
eventError: '='
},
link: function (scope, element, attrs) {
2015-12-30 13:38:18 -05:00
var dropZone = new DropZone(element[0].querySelector('.dropzone-container'), {
url: scope.uploadUrl,
init: function () {
2015-12-30 13:38:18 -05:00
var dz = this;
dz.on('sending', function (file, xhr, data) {
2015-12-30 13:38:18 -05:00
var token = window.document.querySelector('meta[name=token]').getAttribute('content');
data.append('_token', token);
});
if (typeof scope.eventSuccess !== 'undefined') dz.on('success', scope.eventSuccess);
dz.on('success', function (file, data) {
2015-12-30 13:38:18 -05: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-30 13:38:18 -05: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-30 13:38:18 -05:00
});
}
});
}
};
}]);
ngApp.directive('dropdown', [function () {
2015-12-30 13:38:18 -05:00
return {
restrict: 'A',
link: function (scope, element, attrs) {
2015-12-30 13:38:18 -05:00
var menu = element.find('ul');
element.find('[dropdown-toggle]').on('click', function () {
2015-12-30 13:38:18 -05:00
menu.show().addClass('anim menuIn');
element.mouseleave(function () {
2015-12-30 13:38:18 -05:00
menu.hide();
menu.removeClass('anim menuIn');
});
});
}
};
}]);
2015-12-29 11:39:25 -05:00
};