Created
October 7, 2014 04:54
-
-
Save aantipov/0492e88b5b8508b29d21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('convertiser.components.manager') | |
.directive('changeStatus', | |
function ($state, | |
$modal, | |
gettextCatalog, | |
flash, | |
alertHelper, | |
managersService, | |
advertisersService, | |
publishersService) { | |
return { | |
require: '^profile', | |
scope: { | |
action: '@changeStatus' | |
}, | |
link: function ($scope, element, attrs, profileController) { | |
element.on('click', function (event) { | |
event.preventDefault(); | |
$scope.$apply(showModal); | |
}); | |
function showModal(scope) { | |
var modal = $modal({ | |
scope: scope, | |
show: true, | |
placement: 'center', | |
template: '/components/manager/accounts/changeStatus.html' | |
}); | |
var profile = profileController.getProfile(); | |
modal.$scope.remove = function () { | |
getAccountService().delete(profile).then(success, error); | |
}; | |
modal.$scope.changeStatus = function (status) { | |
getAccountService()[status](profile).then(success, error); | |
}; | |
function success() { | |
var state = 'manager.accounts.' + profileController.getProfileType() + 's.item'; | |
// Flash success message and redirect to track domains list. | |
flash.setSuccessMsg(gettextCatalog.getString('Account status was successfully updated')); | |
// Hide modal. | |
setTimeout(modal.hide); | |
// Reload state. | |
$state.go(state, {id: profile.id}, {inherit: false, reload: true}); | |
} | |
function error(response) { | |
// Hide modal. | |
setTimeout(modal.hide); | |
// Display alert with error msg. | |
alertHelper.error((response.data && response.data.detail) || gettextCatalog.getString('Some error occurred')); | |
} | |
} | |
function getAccountService() { | |
var accountType = profileController.getProfileType(); | |
switch (accountType) { | |
case 'manager': | |
return managersService; | |
case 'advertiser': | |
return advertisersService; | |
case 'publisher': | |
return publishersService; | |
default: | |
throw 'profileController.getProfileType() return wrong acount type \'' + accountType + '\''; | |
} | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment