angular.module('voxality').controller('AdminDidsController', [ '$scope', '$http', '$window', '$mdDialog', function($scope, $http, $window, $mdDialog) { $scope.editMarkup = function(markup) { $mdDialog.show({ template: editMarkupDialogTemplate, controller: editMarkupDialogController, clickOutsideToClose: true, locals: { // clones the object markup: $.extend({}, markup) } }) }; $scope.deleteMarkup = function(markup) { var confirm = $mdDialog.confirm() .title('Confirm delete?') .textContent('Are you sure you want to delete this markup?') .ok('Delete') .cancel('Cancel'); $mdDialog.show(confirm).then(function() { deleteMarkup(markup); }); }; $scope.newMarkup = function() { $mdDialog.show({ template: newMarkupDialogTemplate, controller: newMarkupDialogController, clickOutsideToClose: true }) }; $scope.displayTextFor = function(markup) { if (markup.flat) { return '+ $' + markup.modifier; } else { return '+ ' + (markup.modifier * 100) + '%'; } }; var urlFromPath = function(path) { var protocol = $window.location.protocol; var host = $window.location.host; var url = protocol + '//' + host + path; return url; }; var filterMarkupTypes = function(markups) { $scope.initialMarkups = []; $scope.monthlyMarkups = []; var sortMarkup = function(markup) { if (markup.markup_type == 'initial') { $scope.initialMarkups.push(markup); } if (markup.markup_type == 'monthly') { $scope.monthlyMarkups.push(markup); } }; markups.forEach(sortMarkup); }; var loadMarkups = function() { var url = urlFromPath('/api/admin/dids/markups'); var token = $window.Voxality.API.getToken(); var params = { api_token: token } var onSuccess = function(response) { filterMarkupTypes(response.data.data); }; var onFailure = function(response) { alert(response.data.type + '\n' + response.data.exception); }; $http.get(url, { params: params }).then(onSuccess, onFailure); }; var saveMarkup = function(markup) { var url = urlFromPath('/api/admin/markups/' + markup.id); var token = $window.Voxality.API.getToken(); var params = { modifier: markup.modifier, flat: markup.flat, markup_type: markup.markup_type, api_token: token }; var onSuccess = function(response) { loadMarkups(); }; var onFailure = function(response) { alert(response.data.type + '\n' + response.data.exception); }; $http.patch(url, params).then(onSuccess, onFailure); }; var createMarkup = function(markup) { var url = urlFromPath('/api/admin/dids/create_markup'); var token = $window.Voxality.API.getToken(); var params = { modifier: markup.modifier, flat: markup.flat, markup_type: markup.markup_type, api_token: token }; var onSuccess = function(response) { loadMarkups(); }; var onFailure = function(response) { alert(response.data.type + '\n' + response.data.exception); }; $http.post(url, params).then(onSuccess, onFailure); }; var deleteMarkup = function(markup) { var url = urlFromPath('/api/admin/markups/' + markup.id); var token = $window.Voxality.API.getToken(); var params = { api_token: token } var onSuccess = function(response) { loadMarkups(); }; var onFailure = function(response) { alert(response.data.type + '\n' + response.data.exception); }; $http.delete(url, { params: params }).then(onSuccess, onFailure); }; var initialize = function() { loadMarkups(); }; initialize(); // I hate myself for having to write this... // but angular material doesn't support DOM templates // and every other alternative is worse... var editMarkupDialogTemplate = '' + '' + ' ' + '
' + '

Edit Markup

' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' {{ type }}' + ' ' + ' ' + ' ' + ' ' + ' flat fee?' + ' ' + ' Save' + '
' + '
' + '
'; var editMarkupDialogController = function($scope, $mdDialog, markup) { $scope.markup = markup; $scope.markupTypes = ['initial', 'monthly']; $scope.save = function() { saveMarkup($scope.markup); $mdDialog.hide(); }; }; // please no more :( var newMarkupDialogTemplate = '' + '' + ' ' + '
' + '

New Markup

' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' {{ type }}' + ' ' + ' ' + ' ' + ' ' + ' flat fee?' + ' ' + ' Save' + '
' + '
' + '
'; var newMarkupDialogController = function($scope, $mdDialog) { $scope.markup = { flat: false, markup_type: 'monthly' }; $scope.markupTypes = ['initial', 'monthly']; $scope.save = function() { createMarkup($scope.markup); $mdDialog.hide(); }; }; } ])