(function () { 'use strict'; //controller that handles backpack stuff var controllerId = 'collectionsController'; angular.module('app').controller(controllerId, ['common', 'user', 'backpackDatacontext', 'config', '$scope', '$modal', '$rootScope', 'myUsersDataContext', collectionsController]); function collectionsController(common, user, backpackDatacontext, config, $scope, $modal, $rootScope, myUsersDataContext) { var getLogFn = common.logger.getLogFn; var log = getLogFn(controllerId); var logSuccess = getLogFn(controllerId, "success"); var vm = this; var initialized = false $scope.noCollections = false; $scope.orderByChoice = 'Order by date created'; $scope.orderByPredicate = '-dateCreated'; $scope.collections = []; $scope.badgesToAdd = []; function activate() { NProgress.done(); log('Activated Collections View'); } activate(); $scope.getCollections = function () { $scope.loadingCollectionDone = false; backpackDatacontext.getCollectionsForUser().then(function (data) { $scope.collections = data; if (data.length == 0) { $scope.noCollections = true; } else { $scope.noCollections = false; } if (!initialized) { initialized = true; $scope.loadingCollectionsInitialDone = true; } $scope.loadingCollectionDone = true; }); } $scope.getCollections(); // get badges function getBadges() { backpackDatacontext.getAllBadgesForUser().then(function (data) { $scope.hiddenItemCount = 0; $scope.hiddenCount = 0 $scope.allBadgesAvailable = []; $scope.availableBadges = []; $scope.allHidden = false; $scope.allBadges = angular.copy(data.backpackItems); if ($scope.currentCollection.draftCollection.backpackCollectionItems.length > 0) { for (var i = 0; i < data.backpackItems.length; i++) { for (var j = 0; j < $scope.currentCollection.draftCollection.backpackCollectionItems.length; j++) { if (data.backpackItems[i].badgeImageUrl == $scope.currentCollection.draftCollection.backpackCollectionItems[j].backpackItem.badgeImageUrl && data.backpackItems[i].badgeName == $scope.currentCollection.draftCollection.backpackCollectionItems[j].backpackItem.badgeName && data.backpackItems[i].badgeUnhashedEmail == $scope.currentCollection.draftCollection.backpackCollectionItems[j].backpackItem.badgeUnhashedEmail || data.backpackItems[i].deleted) { $scope.allBadges[i].hide = true; $scope.hiddenCount = $scope.hiddenCount + 1; if ($scope.hiddenCount == data.backpackItems.length) { $scope.allHidden = true; } } } } for (var i = 0; i < $scope.allBadges.length; i++) { if (!$scope.allBadges[i].hide) { if ($scope.allBadges[i].badgeName) { $scope.allBadgesAvailable.push($scope.allBadges[i]); $scope.availableBadges.push($scope.allBadges[i]); } } if ($scope.allBadges[i].hide) { $scope.hiddenItemCount = $scope.hiddenItemCount + 1; } } } else { for (var i = 0; i < $scope.allBadges.length; i++) { if (!$scope.allBadges[i].deleted && $scope.allBadges[i].badgeName) { $scope.allBadgesAvailable.push($scope.allBadges[i]); $scope.availableBadges.push($scope.allBadges[i]); } if ($scope.allBadges[i].deleted || !$scope.allBadges[i].badgeName) { $scope.hiddenItemCount = $scope.hiddenItemCount + 1; } } } }); } $scope.saveCollection = function (collection) { backpackDatacontext.updateCollection(collection).then(function (data) { logSuccess('Collection successfully updated'); $scope.getCollections(); }); } $scope.deleteCollection = function (collection) { backpackDatacontext.deleteCollection(collection).then(function (data) { logSuccess('Collection successfully deleted'); $scope.getCollections(); }); } $scope.addToCollection = function (collection) { $scope.showDetail = false; $scope.loadingBadgesDone = false; $scope.availableBadges = []; $scope.currentCollection = collection; $scope.badgesToAdd = []; getBadges(); $scope.showAdd = true; } $scope.addBadgeToQue = function (badge) { if ($scope.badgesToAdd.indexOf(badge) >= 0) { $scope.badgesToAdd.splice($scope.badgesToAdd.indexOf(badge), 1) } else { $scope.badgesToAdd.push(badge); } } $scope.addBadgesToCollection = function () { var model = { id: $scope.currentCollection.draftCollection.id, backpackCollectionItems: $scope.badgesToAdd } backpackDatacontext.addItemsToCollection(model).then(function (data) { $scope.getCollections(); $scope.showAdd = false; logSuccess('Badges successfully added to collection'); }); } $scope.removeBadgeItem = function (badge, collectionId) { $scope.refreshing = true; var model = { id: collectionId, backpackCollectionItems: [badge] } backpackDatacontext.removeItemsFromCollection(model).then(function (data) { logSuccess('Badges successfully removed from collection'); $scope.getCollections(); }); } $scope.makeCollectionPublic = function (collection) { collection.publishedCollection.public = true; collection.draftCollection.public = true; backpackDatacontext.updateCollection(collection.draftCollection).then(function (data) { logSuccess('Collection successfully made public'); $scope.getCollections(); }); } $scope.publishCollectionContent = function (collection) { collection.published = true; backpackDatacontext.publishCollectionContent(collection.id).then(function (data) { logSuccess('Collection successfully published'); $scope.getCollections(); }); } $scope.makeCollectionPrivate = function (collection) { collection.draftCollection.public = false; backpackDatacontext.updateCollection(collection.draftCollection).then(function (data) { logSuccess('Collection successfully hidden'); backpackDatacontext.publishCollectionContent(collection.id).then(function (data) { $scope.getCollections(); }); }); } // Modal to create a new collection $scope.createCollection = function () { $modal.open({ templateUrl: '/app/collections/createcollection.html', controller: createCollectionController, size: 'sm', backdrop: 'static', resolve: { getCollections: function () { return $scope.getCollections; }, backpackDatacontext: function () { return backpackDatacontext; }, badgeUser: function () { return $rootScope.badgeUserConfig; } } }); } // Controller for previewing badge template var createCollectionController = function (common, $scope, $modalInstance, backpackDatacontext, getCollections, badgeUser) { $scope.collection = { name: '', description: '', backpackId: badgeUser.backpack.id, createdBy: badgeUser.id }; $scope.ok = function () { $scope.collectionExists = false; backpackDatacontext.createCollection($scope.collection).then(function (data) { if (data == 'NAMEEXISTS') { $scope.collectionExists = true; } else { logSuccess('Collection successfully created'); $modalInstance.dismiss('cancel'); getCollections(); } }); } // close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } // Modal to create a new collection $scope.editCollection = function (collection) { $modal.open({ templateUrl: '/app/collections/createcollection.html', controller: editCollectionController, size: 'sm', backdrop: 'static', resolve: { getCollections: function () { return $scope.getCollections; }, backpackDatacontext: function () { return backpackDatacontext; }, badgeUser: function () { return $rootScope.badgeUserConfig; }, collection: function () { return collection; } } }); } // Controller for previewing badge template var editCollectionController = function (common, $scope, $modalInstance, backpackDatacontext, getCollections, badgeUser, collection) { $scope.collection = angular.copy(collection.draftCollection); $scope.ok = function () { backpackDatacontext.updateCollection($scope.collection).then(function (data) { logSuccess('Collection successfully updated'); $modalInstance.dismiss('cancel'); getCollections(); }); } // close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } // Modal to add comments a badge $scope.shareCollection = function (collection) { $modal.open({ templateUrl: '/app/collections/sharecollection.html', controller: shareCollectionController, size: 'sm', backdrop: 'static', resolve: { collection: function () { return collection; } } }); } // Controller for adding comments to a badge var shareCollectionController = function (common, $scope, $modalInstance, collection) { $scope.collection = collection; $scope.sysUrl = sysUrl; $scope.embedUrl = ''; // close the modal $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; } } })();