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
|
@ -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_();
|
||||
};
|
||||
|
||||
|
|
Reference in a new issue