Switch CheckStateController to not be O(n) for the isChecked call

This commit is contained in:
Joseph Schorr 2015-06-24 17:43:57 -04:00
parent 43330bcfad
commit dde8d32984
2 changed files with 27 additions and 6 deletions

View file

@ -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) {

View file

@ -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_();
};