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
|
|
|
|
2016-02-07 18:14:11 +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.
|
|
|
|
*/
|
2015-12-31 03:57:17 +08:00
|
|
|
ngApp.directive('toggleSwitch', function () {
|
2015-12-30 00:39:25 +08:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: toggleSwitchTemplate,
|
|
|
|
scope: true,
|
2015-12-31 03:57:17 +08:00
|
|
|
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';
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
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.
|
|
|
|
*/
|
2015-12-31 03:57:17 +08:00
|
|
|
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: '@'
|
|
|
|
},
|
2015-12-31 03:57:17 +08:00
|
|
|
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 || '';
|
2016-01-17 23:19:26 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
scope.reset = function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
setImage({id: 0}, scope.defaultImage);
|
|
|
|
};
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
scope.remove = function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
scope.image = 'none';
|
|
|
|
scope.value = 'none';
|
|
|
|
};
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
scope.showImageManager = function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
imageManagerService.show((image) => {
|
|
|
|
scope.updateImageFromModel(image);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
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
|
|
|
|
*/
|
2015-12-31 03:57:17 +08:00
|
|
|
ngApp.directive('dropZone', [function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
return {
|
|
|
|
restrict: 'E',
|
|
|
|
template: dropZoneTemplate,
|
|
|
|
scope: {
|
|
|
|
uploadUrl: '@',
|
|
|
|
eventSuccess: '=',
|
2016-03-13 21:30:47 +08:00
|
|
|
eventError: '=',
|
|
|
|
uploadedTo: '@'
|
2015-12-31 02:38:18 +08:00
|
|
|
},
|
2015-12-31 03:57:17 +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,
|
2015-12-31 03:57:17 +08:00
|
|
|
init: function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
var dz = this;
|
2015-12-31 03:57:17 +08:00
|
|
|
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);
|
2016-03-13 21:30:47 +08:00
|
|
|
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);
|
2015-12-31 03:57:17 +08:00
|
|
|
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) {
|
2015-12-31 03:57:17 +08:00
|
|
|
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
|
|
|
}
|
2015-12-31 03:57:17 +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
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
2015-12-31 03:57:17 +08:00
|
|
|
ngApp.directive('dropdown', [function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
2015-12-31 03:57:17 +08:00
|
|
|
link: function (scope, element, attrs) {
|
2015-12-31 02:38:18 +08:00
|
|
|
var menu = element.find('ul');
|
2015-12-31 03:57:17 +08:00
|
|
|
element.find('[dropdown-toggle]').on('click', function () {
|
2015-12-31 02:38:18 +08:00
|
|
|
menu.show().addClass('anim menuIn');
|
2015-12-31 03:57:17 +08:00
|
|
|
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) {
|
2016-03-10 06:32:07 +08:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
scope: {
|
|
|
|
tinymce: '=',
|
2016-03-10 06:54:18 +08:00
|
|
|
mceModel: '=',
|
|
|
|
mceChange: '='
|
2016-03-10 06:32:07 +08:00
|
|
|
},
|
|
|
|
link: function (scope, element, attrs) {
|
|
|
|
|
|
|
|
function tinyMceSetup(editor) {
|
2016-03-12 23:52:19 +08:00
|
|
|
editor.on('ExecCommand change NodeChange ObjectResized', (e) => {
|
2016-03-10 06:32:07 +08:00
|
|
|
var content = editor.getContent();
|
2016-03-12 23:52:19 +08:00
|
|
|
$timeout(() => {
|
2016-03-10 06:54:18 +08:00
|
|
|
scope.mceModel = content;
|
2016-03-10 06:32:07 +08:00
|
|
|
});
|
2016-03-10 06:54:18 +08:00
|
|
|
scope.mceChange(content);
|
2016-03-10 06:32:07 +08:00
|
|
|
});
|
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();
|
|
|
|
});
|
2016-03-10 06:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|