Skip to content

Instantly share code, notes, and snippets.

@lluchs
Created July 16, 2011 14:05
Show Gist options
  • Save lluchs/1086382 to your computer and use it in GitHub Desktop.
Save lluchs/1086382 to your computer and use it in GitHub Desktop.
Otherland Shop Search
form = '''
<form id=search>
<input type=text autofocus=autofocus><button type=submit>Suchen</button>
</form>
'''
# insert form
$('#nobotd > br').after form
form = $('#search')
input = $('input', form)
cache = []
class Item
constructor: (img) ->
@title = img.attr 'title'
td = img.parent()
@td = td.add(td.prev()).add(td.next())
highlight: (flag) ->
color = if flag then 'yellow' else 'transparent'
@td.css 'background-color', color
test: (str) ->
@highlight str.length isnt 0 and @title.toLowerCase().indexOf(str.toLowerCase()) isnt -1
# fill cache
$('.mid img').each ->
cache.push new Item($(this))
form.submit (event) ->
event.preventDefault()
input.keyup ->
needle = input.val()
for item in cache
item.test needle
null
(function() {
var Item, cache, form, input;
form = '<form id=search>\n <input type=text autofocus=autofocus><button type=submit>Suchen</button>\n</form>';
$('#nobotd > br').after(form);
form = $('#search');
input = $('input', form);
cache = [];
Item = (function() {
function Item(img) {
var td;
this.title = img.attr('title');
td = img.parent();
this.td = td.add(td.prev()).add(td.next());
}
Item.prototype.highlight = function(flag) {
var color;
color = flag ? 'yellow' : 'transparent';
return this.td.css('background-color', color);
};
Item.prototype.test = function(str) {
return this.highlight(str.length !== 0 && this.title.toLowerCase().indexOf(str.toLowerCase()) !== -1);
};
return Item;
})();
$('.mid img').each(function() {
return cache.push(new Item($(this)));
});
form.submit(function(event) {
return event.preventDefault();
});
input.keyup(function() {
var item, needle, _i, _len;
needle = input.val();
for (_i = 0, _len = cache.length; _i < _len; _i++) {
item = cache[_i];
item.test(needle);
}
return null;
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment