(function () { 'use strict'; var serviceId = 'myBadgesSelectorDataContext'; var app = angular.module('myBadgesSelector'); app.factory(serviceId, ['$q', '$http', 'myBadgesSelectorConfig', '$modal', 'myBadgesAdminService', datacontext]); function datacontext($q, $http, config, $modal, myBadgesAdminService) { var currentLicense; var service = { showSelectorModal: showSelectorModal, }; return service; function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if ( !angular.isObject(response.data) || !response.data.message ) { return ($q.reject("An unknown error occurred.")); } // Otherwise, use expected error message. return ($q.reject(response.data.message)); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return (response.data); } function showSelectorModal(badgeIds, singleSelect, publishedOnly) { var modalOptions = { templateUrl: config.templatePath + 'myBadgesSelectorModal.html', controller: myBadgesSelectorModalController, size: 'lg', resolve: { badgeIds: function () { return badgeIds; }, singleSelect: function () { return singleSelect; }, publishedOnly: function(){ return publishedOnly; } } }; var modalInstance = $modal.open(modalOptions); } function myBadgesSelectorModalController($scope, $modalInstance, $q, badgeIds, singleSelect, publishedOnly) { $scope.badgeIds = badgeIds; function activate() { var promise = publishedOnly ? myBadgesAdminService.getPublishedBadgeTemplates : myBadgesAdminService.getBadgeTemplates; promise().then(function (data) { $scope.badges = data; $scope.badgesLoaded = true; selectBadges(); }); } // Add a checkbox to any badge that is currently selected function selectBadges() { $scope.badges.forEach(function (badge, index) { for (i in $scope.badgeIds) { if ($scope.badgeIds[i].badgeId == badge.id) { $scope.badges[index].selected = true; break; } } }); } // Select a badge $scope.selectBadge = function (badge) { if (!singleSelect) { if (badge.selected) { var exists = false; for (i in $scope.badgeIds) { if ($scope.badgeIds[i].badgeId == badge.id) { exists = true; } } if (!exists) { $scope.badgeIds.push({ badgeId: badge.id, automaticIssue: false }); } } else { for (i in $scope.badgeIds) { if ($scope.badgeIds[i].badgeId == badge.id) { var index = i; } } $scope.badgeIds.splice(index, 1); } } else { // unselect all other badges $scope.badges.forEach(function (item, index) { if (badge.id != item.id) { $scope.badges[index].selected = false; } }); if ($scope.badgeIds.length === 0) $scope.badgeIds.push({ badgeId: badge.id, automaticIssue: false }); else $scope.badgeIds.splice(0, $scope.badgeIds.length); } } //close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; activate(); } app.controller('myBadgesSelectorModalController', ['$scope', '$modalInstance', myBadgesSelectorModalController]); } })();