app.controller('EmailCtrl', function($scope, $rootScope, $http, $window, $location, $route, $routeParams) {
$scope.email = {};
function init() {
var req = {
method: 'GET',
url: '/email_template/getByEmailId/' + $routeParams.id,
}
$http(req).then(function(response) {
if (response.status && response.data.status) {
$scope.email = response.data.data;
$scope.makeElements();
} else {
$location.url('/page/emails');
}
}, function(error) {
$location.url('/page/emails');
});
}
init();
$scope.makeElements = function() {
$scope.htmlElements = '
';
//replace names elements
angular.forEach($scope.email.variables, function(variable, key){
$scope.htmlElements += '';
});
$scope.htmlElements += '';
if ($('#email_html')) {
var documentContents = $('#email_html').summernote('code');
$('#email_html').summernote('reset');
if (angular.isString(documentContents)) {
$('#email_html').summernote('code', documentContents);
}
}
}
$scope.cancelEmail = function() {
location.href = "/page/emails";
}
$scope.insertElement = function (context) {
var ui = $.summernote.ui;
var list = $('#elements-list').val();
var button = ui.buttonGroup([
ui.button({
className: 'dropdown-toggle',
contents: 'Voeg een variabele in',
tooltip: "Elements",
container: false,
data: {
toggle: 'dropdown'
},
click: function() {
// Cursor position must be saved because is lost when dropdown is opened.
context.invoke('editor.saveRange');
}
}),
ui.dropdown({
className: 'drop-default summernote-list',
contents: $scope.htmlElements,
callback: function($dropdown) {
$dropdown.find('ul').click(function() {
context.invoke('editor.restoreRange');
context.invoke('editor.focus');
context.invoke('editor.insertText', $(this)[0].innerText);
});
}
})
]);
return button.render();
}
//summernote config
$scope.summernoteoptions = {
toolbar: [
['edit',['undo','redo']],
['headline', ['style']],
['style', ['bold', 'italic', 'underline', 'clear']],
['fontface', ['fontname']],
['textsize', ['fontsize']],
['fontclr', ['color']],
['alignment', ['ul', 'ol', 'paragraph', 'lineheight']],
['height', ['height']],
['table', ['table']],
['insert', ['link','hr','picture']],
['view', ['fullscreen', 'codeview']],
['extraColom', ['element']],
],
buttons: {
element: $scope.insertElement
}
};
$scope.saveEmail = function() {
var req = {
method: 'POST',
url: '/email_template/addByEmailId/' + $routeParams.id,
data: {
subject: $scope.email.subject,
html: $scope.email.html,
}
}
$http(req).then(function(response) {
if (response.status && response.data.status) {
swal('Gelukt!', 'De email template is succesvol opgeslagen.', 'success');
} else {
swal('Foutmelding', 'De email template kon niet succesvol worden opgeslagen, probeer het opnieuw!', 'error');
}
});
}
});