Switch CheckStateController to not be O(n) for the isChecked call
This commit is contained in:
parent
43330bcfad
commit
dde8d32984
2 changed files with 27 additions and 6 deletions
|
@ -21,7 +21,7 @@ angular.module('quay').directive('repoPanelTags', function () {
|
||||||
controller: function($scope, $element, $filter, $location, ApiService, UIService) {
|
controller: function($scope, $element, $filter, $location, ApiService, UIService) {
|
||||||
var orderBy = $filter('orderBy');
|
var orderBy = $filter('orderBy');
|
||||||
|
|
||||||
$scope.checkedTags = UIService.createCheckStateController([]);
|
$scope.checkedTags = UIService.createCheckStateController([], 'name');
|
||||||
$scope.options = {
|
$scope.options = {
|
||||||
'predicate': 'last_modified_datetime',
|
'predicate': 'last_modified_datetime',
|
||||||
'reverse': false
|
'reverse': false
|
||||||
|
@ -102,7 +102,7 @@ angular.module('quay').directive('repoPanelTags', function () {
|
||||||
$scope.tags = ordered;
|
$scope.tags = ordered;
|
||||||
$scope.allTags = allTags;
|
$scope.allTags = allTags;
|
||||||
|
|
||||||
$scope.checkedTags = UIService.createCheckStateController(ordered, checked);
|
$scope.checkedTags = UIService.createCheckStateController(ordered, 'name', checked);
|
||||||
$scope.checkedTags.listen(function(checked) {
|
$scope.checkedTags.listen(function(checked) {
|
||||||
$scope.selectedTags = checked.map(function(tag_info) {
|
$scope.selectedTags = checked.map(function(tag_info) {
|
||||||
return tag_info.name;
|
return tag_info.name;
|
||||||
|
@ -117,9 +117,9 @@ angular.module('quay').directive('repoPanelTags', function () {
|
||||||
$scope.$watch('selectedTags', function(selectedTags) {
|
$scope.$watch('selectedTags', function(selectedTags) {
|
||||||
if (!selectedTags || !$scope.repository || !$scope.imageMap) { return; }
|
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];
|
return $scope.repository.tags[tag];
|
||||||
});
|
}));
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
$scope.$watch('repository', function(repository) {
|
$scope.$watch('repository', function(repository) {
|
||||||
|
|
|
@ -2,10 +2,14 @@
|
||||||
* Service which provides helper methods for performing some simple UI operations.
|
* Service which provides helper methods for performing some simple UI operations.
|
||||||
*/
|
*/
|
||||||
angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$location', function($timeout, $rootScope, $location) {
|
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.items = items;
|
||||||
|
this.itemKey = itemKey;
|
||||||
this.checked = opt_checked || [];
|
this.checked = opt_checked || [];
|
||||||
|
this.checkedMap = {};
|
||||||
this.listeners_ = [];
|
this.listeners_ = [];
|
||||||
|
|
||||||
|
this.buildMap_();
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckStateController.prototype.listen = function(callback) {
|
CheckStateController.prototype.listen = function(callback) {
|
||||||
|
@ -13,7 +17,7 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckStateController.prototype.isChecked = function(item) {
|
CheckStateController.prototype.isChecked = function(item) {
|
||||||
return $.inArray(item, this.checked) >= 0;
|
return !!this.checkedMap[item[this.itemKey]];
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckStateController.prototype.toggleItem = function(item) {
|
CheckStateController.prototype.toggleItem = function(item) {
|
||||||
|
@ -27,19 +31,35 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio
|
||||||
CheckStateController.prototype.toggleItems = function() {
|
CheckStateController.prototype.toggleItems = function() {
|
||||||
if (this.checked.length) {
|
if (this.checked.length) {
|
||||||
this.checked = [];
|
this.checked = [];
|
||||||
|
this.checkedMap = {};
|
||||||
} else {
|
} else {
|
||||||
this.checked = this.items.slice();
|
this.checked = this.items.slice();
|
||||||
|
this.buildMap_();
|
||||||
}
|
}
|
||||||
this.callListeners_();
|
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) {
|
CheckStateController.prototype.checkByFilter = function(filter) {
|
||||||
this.checked = $.grep(this.items, filter);
|
this.checked = $.grep(this.items, filter);
|
||||||
|
this.buildMap_();
|
||||||
this.callListeners_();
|
this.callListeners_();
|
||||||
};
|
};
|
||||||
|
|
||||||
CheckStateController.prototype.checkItem = function(item) {
|
CheckStateController.prototype.checkItem = function(item) {
|
||||||
this.checked.push(item);
|
this.checked.push(item);
|
||||||
|
this.checkedMap[item[this.itemKey]] = true;
|
||||||
this.callListeners_();
|
this.callListeners_();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -47,6 +67,7 @@ angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$locatio
|
||||||
this.checked = $.grep(this.checked, function(cItem) {
|
this.checked = $.grep(this.checked, function(cItem) {
|
||||||
return cItem != item;
|
return cItem != item;
|
||||||
});
|
});
|
||||||
|
this.checkedMap[item[this.itemKey]] = false;
|
||||||
this.callListeners_();
|
this.callListeners_();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Reference in a new issue