diff --git a/static/js/directives/repo-view/repo-panel-tags.js b/static/js/directives/repo-view/repo-panel-tags.js index 181510578..f1a76e383 100644 --- a/static/js/directives/repo-view/repo-panel-tags.js +++ b/static/js/directives/repo-view/repo-panel-tags.js @@ -21,7 +21,7 @@ angular.module('quay').directive('repoPanelTags', function () { controller: function($scope, $element, $filter, $location, ApiService, UIService) { var orderBy = $filter('orderBy'); - $scope.checkedTags = UIService.createCheckStateController([]); + $scope.checkedTags = UIService.createCheckStateController([], 'name'); $scope.options = { 'predicate': 'last_modified_datetime', 'reverse': false @@ -102,7 +102,7 @@ angular.module('quay').directive('repoPanelTags', function () { $scope.tags = ordered; $scope.allTags = allTags; - $scope.checkedTags = UIService.createCheckStateController(ordered, checked); + $scope.checkedTags = UIService.createCheckStateController(ordered, 'name', checked); $scope.checkedTags.listen(function(checked) { $scope.selectedTags = checked.map(function(tag_info) { return tag_info.name; @@ -117,9 +117,9 @@ angular.module('quay').directive('repoPanelTags', function () { $scope.$watch('selectedTags', function(selectedTags) { if (!selectedTags || !$scope.repository || !$scope.imageMap) { return; } - $scope.checkedTags.checked = selectedTags.map(function(tag) { + $scope.checkedTags.setChecked(selectedTags.map(function(tag) { return $scope.repository.tags[tag]; - }); + })); }, true); $scope.$watch('repository', function(repository) { diff --git a/static/js/services/ui-service.js b/static/js/services/ui-service.js index 86a326390..792f8ad11 100644 --- a/static/js/services/ui-service.js +++ b/static/js/services/ui-service.js @@ -2,10 +2,14 @@ * Service which provides helper methods for performing some simple UI operations. */ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$location', function($timeout, $rootScope, $location) { - var CheckStateController = function(items, opt_checked) { + var CheckStateController = function(items, itemKey, opt_checked) { this.items = items; + this.itemKey = itemKey; this.checked = opt_checked || []; + this.checkedMap = {}; this.listeners_ = []; + + this.buildMap_(); }; CheckStateController.prototype.listen = function(callback) { @@ -13,7 +17,7 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio }; CheckStateController.prototype.isChecked = function(item) { - return $.inArray(item, this.checked) >= 0; + return !!this.checkedMap[item[this.itemKey]]; }; CheckStateController.prototype.toggleItem = function(item) { @@ -27,19 +31,35 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio CheckStateController.prototype.toggleItems = function() { if (this.checked.length) { this.checked = []; + this.checkedMap = {}; } else { this.checked = this.items.slice(); + this.buildMap_(); } this.callListeners_(); }; + CheckStateController.prototype.setChecked = function(items) { + this.checked = items.slice(); + this.buildMap_(); + }; + + CheckStateController.prototype.buildMap_ = function() { + var that = this; + this.checked.forEach(function(item) { + that.checkedMap[item[that.itemKey]] = true; + }); + }; + CheckStateController.prototype.checkByFilter = function(filter) { this.checked = $.grep(this.items, filter); + this.buildMap_(); this.callListeners_(); }; CheckStateController.prototype.checkItem = function(item) { this.checked.push(item); + this.checkedMap[item[this.itemKey]] = true; this.callListeners_(); }; @@ -47,6 +67,7 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio this.checked = $.grep(this.checked, function(cItem) { return cItem != item; }); + this.checkedMap[item[this.itemKey]] = false; this.callListeners_(); };