Skip to content

Commit

Permalink
Added search filter
Browse files Browse the repository at this point in the history
  • Loading branch information
brymck committed Dec 16, 2011
1 parent c869120 commit e0da480
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 24 additions & 1 deletion app/assets/javascripts/main.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ EFFECT_REGEX = /effect-(\d+)/
FULL_MATCH = 0
ADD_TEXT = "+"
REMOVE_TEXT = "-"

# Search type indexes
AT_LEAST_ONE = 0
AT_LEAST_TWO = 1
ALL = 2
CUSTOM = 3

# Objects
$filters = null
$searchTypes = null

getEffectClass = (node) ->
node.className.match(EFFECT_REGEX)[FULL_MATCH]
Expand All @@ -13,8 +22,20 @@ getFilters = (effectClass) ->
getNames = (effectClass) ->
$(".#{effectClass}.name")

getMinMatches = (length) ->
switch $searchTypes.filter(":checked").data("index")
when AT_LEAST_ONE
Math.min length, 1
when AT_LEAST_TWO
Math.min length, 2
when ALL
length
when CUSTOM
if length > 1 then length - 1 else length

checkFilters = ->
filterLength = $("tr.custom").length
filterLength = getMinMatches($("tr.custom").length)
console.log filterLength
$(".filterable").each (i, tr) ->
$tr = $(tr)
hasMatch = false
Expand Down Expand Up @@ -60,5 +81,7 @@ unhighlight = ->

$ ->
$filters = $("#filters tbody")
$searchTypes = $("input[name=search_type]")
$(".filter").click filterEffect
$(".filter, .name").hover highlightMatches, unhighlight
$searchTypes.keyup(checkFilters).mouseup(checkFilters).change(checkFilters)
7 changes: 7 additions & 0 deletions app/views/ingredients/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
%h2 Filters
%p
Match:
- search_types = ["at least one", "at least two", "all", "custom (best fit)"]
- search_types.each_with_index do |search_type, index|
%label
%input{:type => :radio, :"data-index" => index, :name => "search_type", :checked => (search_type == search_types.last)}
= search_type
%table#filters
%tbody
%p Click the +/- signs next to an effect to add it to the list of filters.
Expand Down

0 comments on commit e0da480

Please sign in to comment.