CollectionSearch now fires if the field is not empty

This commit is contained in:
Felipe Martín 2015-09-22 19:54:03 +02:00
parent 331bdb1a4b
commit e4baaed1dd
1 changed files with 53 additions and 48 deletions

View File

@ -1,62 +1,67 @@
(function() { (function() {
var CollectionSearchComponent = function() { var CollectionSearchComponent = function() {
this.initialize(); this.initialize();
}; };
CollectionSearchComponent.prototype.initialize = function() { CollectionSearchComponent.prototype.initialize = function() {
var self = this; var self = this;
// Required DOM // Required DOM
this.$searchInput = document.querySelector('[data-component="collectionSearchInput"]'); this.$searchInput = document.querySelector('[data-component="collectionSearchInput"]');
this.$resetButton = document.querySelector('[data-component="collectionSearchReset"]'); this.$resetButton = document.querySelector('[data-component="collectionSearchReset"]');
this.$collectionList = document.querySelector('[data-component="collectionList"]').children; this.$collectionList = document.querySelector('[data-component="collectionList"]').children;
// Add event handlers // Add event handlers
this.$searchInput.addEventListener('keyup', function(event) { this.$searchInput.addEventListener('keyup', function(event) {
self.search(self.$searchInput.value); self.search(self.$searchInput.value);
}); });
this.$resetButton.addEventListener('click', function(event) { this.$resetButton.addEventListener('click', function(event) {
self.reset(); self.reset();
}); });
// Focus search input // Focus search input
this.$searchInput.focus(); this.$searchInput.focus();
};
CollectionSearchComponent.prototype.showItem = function(item) { // Trigger search
item.style.display = 'block'; if (this.$searchInput.value !== '') {
}; this.search(this.$searchInput.value);
}
};
CollectionSearchComponent.prototype.hideItem = function(item) { CollectionSearchComponent.prototype.showItem = function(item) {
item.style.display = 'none'; item.style.display = 'block';
}; };
CollectionSearchComponent.prototype.reset = function() { CollectionSearchComponent.prototype.hideItem = function(item) {
this.$searchInput.value = ''; item.style.display = 'none';
};
for (var index = 0; index < this.$collectionList.length; index++) { CollectionSearchComponent.prototype.reset = function() {
var item = this.$collectionList[index]; this.$searchInput.value = '';
this.showItem(item);
}
};
CollectionSearchComponent.prototype.search = function(searchQuery) { for (var index = 0; index < this.$collectionList.length; index++) {
if (!searchQuery || searchQuery === null || searchQuery === '') { var item = this.$collectionList[index];
this.reset(); this.showItem(item);
return false; }
} };
for (var index = 0; index < this.$collectionList.length; index++) { CollectionSearchComponent.prototype.search = function(searchQuery) {
var item = this.$collectionList[index]; if (!searchQuery || searchQuery === null || searchQuery === '') {
var names = item.getAttribute('data-amiibo-names'); this.reset();
if (names.indexOf(searchQuery.toLowerCase()) === -1) { return false;
this.hideItem(item); }
} else {
this.showItem(item);
}
}
};
SimpleViews.register('collection-search', CollectionSearchComponent); for (var index = 0; index < this.$collectionList.length; index++) {
var item = this.$collectionList[index];
var names = item.getAttribute('data-amiibo-names');
if (names.indexOf(searchQuery.toLowerCase()) === -1) {
this.hideItem(item);
} else {
this.showItem(item);
}
}
};
SimpleViews.register('collection-search', CollectionSearchComponent);
})(); })();