2 var DropZone = require('dropzone');
4 var toggleSwitchTemplate = require('./components/toggle-switch.html');
5 var imagePickerTemplate = require('./components/image-picker.html');
6 var dropZoneTemplate = require('./components/drop-zone.html');
8 module.exports = function (ngApp, events) {
12 * Has basic on/off functionality.
13 * Use string values of 'true' & 'false' to dictate the current state.
15 ngApp.directive('toggleSwitch', function () {
18 template: toggleSwitchTemplate,
20 link: function (scope, element, attrs) {
21 scope.name = attrs.name;
22 scope.value = attrs.value;
23 scope.isActive = scope.value == true && scope.value != 'false';
24 scope.value = (scope.value == true && scope.value != 'false') ? 'true' : 'false';
26 scope.switch = function () {
27 scope.isActive = !scope.isActive;
28 scope.value = scope.isActive ? 'true' : 'false';
38 * Is a simple front-end interface that connects to an ImageManager if present.
40 ngApp.directive('imagePicker', ['$http', 'imageManagerService', function ($http, imageManagerService) {
43 template: imagePickerTemplate,
55 link: function (scope, element, attrs) {
56 var usingIds = typeof scope.currentId !== 'undefined' || scope.currentId === 'false';
57 scope.image = scope.currentImage;
58 scope.value = scope.currentImage || '';
59 if (usingIds) scope.value = scope.currentId;
61 function setImage(imageModel, imageUrl) {
62 scope.image = imageUrl;
63 scope.value = usingIds ? imageModel.id : imageUrl;
66 scope.reset = function () {
67 setImage({id: 0}, scope.defaultImage);
70 scope.remove = function () {
75 scope.showImageManager = function () {
76 imageManagerService.show((image) => {
77 scope.updateImageFromModel(image);
81 scope.updateImageFromModel = function (model) {
82 var isResized = scope.resizeWidth && scope.resizeHeight;
86 setImage(model, model.url);
91 var cropped = scope.resizeCrop ? 'true' : 'false';
92 var requestString = '/images/thumb/' + model.id + '/' + scope.resizeWidth + '/' + scope.resizeHeight + '/' + cropped;
93 $http.get(requestString).then((response) => {
94 setImage(model, response.data.url);
104 * Used for uploading images
106 ngApp.directive('dropZone', [function () {
109 template: dropZoneTemplate,
116 link: function (scope, element, attrs) {
117 var dropZone = new DropZone(element[0].querySelector('.dropzone-container'), {
118 url: scope.uploadUrl,
121 dz.on('sending', function (file, xhr, data) {
122 var token = window.document.querySelector('meta[name=token]').getAttribute('content');
123 data.append('_token', token);
124 var uploadedTo = typeof scope.uploadedTo === 'undefined' ? 0 : scope.uploadedTo;
125 data.append('uploaded_to', uploadedTo);
127 if (typeof scope.eventSuccess !== 'undefined') dz.on('success', scope.eventSuccess);
128 dz.on('success', function (file, data) {
129 $(file.previewElement).fadeOut(400, function () {
133 if (typeof scope.eventError !== 'undefined') dz.on('error', scope.eventError);
134 dz.on('error', function (file, errorMessage, xhr) {
135 console.log(errorMessage);
137 function setMessage(message) {
138 $(file.previewElement).find('[data-dz-errormessage]').text(message);
141 if (xhr.status === 413) setMessage('The server does not allow uploads of this size. Please try a smaller file.');
142 if (errorMessage.file) setMessage(errorMessage.file[0]);
152 ngApp.directive('dropdown', [function () {
155 link: function (scope, element, attrs) {
156 var menu = element.find('ul');
157 element.find('[dropdown-toggle]').on('click', function () {
158 menu.show().addClass('anim menuIn');
159 element.mouseleave(function () {
161 menu.removeClass('anim menuIn');
168 ngApp.directive('tinymce', ['$timeout', function($timeout) {
176 link: function (scope, element, attrs) {
178 function tinyMceSetup(editor) {
179 editor.on('ExecCommand change NodeChange ObjectResized', (e) => {
180 var content = editor.getContent();
182 scope.mceModel = content;
184 scope.mceChange(content);
187 editor.on('init', (e) => {
188 scope.mceModel = editor.getContent();
191 scope.$on('html-update', (event, value) => {
192 editor.setContent(value);
193 editor.selection.select(editor.getBody(), true);
194 editor.selection.collapse(false);
195 scope.mceModel = editor.getContent();
199 scope.tinymce.extraSetups.push(tinyMceSetup);
200 tinymce.init(scope.tinymce);