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
|
|
|
|
2015-12-30 14:57:17 -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.
|
|
|
|
*/
|
2015-12-30 14:57:17 -05:00
|
|
|
ngApp.directive('toggleSwitch', function () {
|
2015-12-29 11:39:25 -05:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: toggleSwitchTemplate,
|
|
|
|
scope: true,
|
2015-12-30 14:57:17 -05:00
|
|
|
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';
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
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.
|
|
|
|
*/
|
2015-12-30 14:57:17 -05:00
|
|
|
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: '@'
|
|
|
|
},
|
2015-12-30 14:57:17 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
scope.reset = function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
setImage({id: 0}, scope.defaultImage);
|
|
|
|
};
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
scope.remove = function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
scope.image = 'none';
|
|
|
|
scope.value = 'none';
|
|
|
|
};
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
scope.showImageManager = function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
imageManagerService.show((image) => {
|
|
|
|
scope.updateImageFromModel(image);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
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
|
|
|
|
*/
|
2015-12-30 14:57:17 -05:00
|
|
|
ngApp.directive('dropZone', [function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: dropZoneTemplate,
|
|
|
|
scope: {
|
|
|
|
uploadUrl: '@',
|
|
|
|
eventSuccess: '=',
|
|
|
|
eventError: '='
|
|
|
|
},
|
2015-12-30 14:57:17 -05:00
|
|
|
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,
|
2015-12-30 14:57:17 -05:00
|
|
|
init: function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
var dz = this;
|
2015-12-30 14:57:17 -05:00
|
|
|
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);
|
2015-12-30 14:57:17 -05:00
|
|
|
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) {
|
2015-12-30 14:57:17 -05:00
|
|
|
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
|
|
|
}
|
2015-12-30 14:57:17 -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
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
2015-12-30 14:57:17 -05:00
|
|
|
ngApp.directive('dropdown', [function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2015-12-30 14:57:17 -05:00
|
|
|
link: function (scope, element, attrs) {
|
2015-12-30 13:38:18 -05:00
|
|
|
var menu = element.find('ul');
|
2015-12-30 14:57:17 -05:00
|
|
|
element.find('[dropdown-toggle]').on('click', function () {
|
2015-12-30 13:38:18 -05:00
|
|
|
menu.show().addClass('anim menuIn');
|
2015-12-30 14:57:17 -05:00
|
|
|
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
|
|
|
};
|