forked from max-mapper/flavors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (138 loc) · 4.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var header = document.querySelector('.column-header')
var results = document.querySelector('.results-list')
var openMenuButton = document.querySelector('.open-menu')
var searchOptions = document.querySelector('.search-by')
var contentContainer = document.querySelector('.app')
var inputEl = document.querySelector('input')
var openLeft = document.querySelector('#open-left')
var snapDrawer = document.querySelector('.snap-drawer')
var snapper = new Snap({
element: contentContainer,
disable: 'right'
})
makeCrossBrowserCompatible()
var lists = {
ingredients: function getAllIngredients() {
return flavors.ingredients.map(function(ingredient) {
return {
title: ingredient.ingredient,
description: ingredient.complements,
extra: ingredient.techniques
}
})
},
seasonings: function getAllSeasonings() {
return flavors.seasonings.map(function(seasoning) {
return {
title: seasoning.seasoning,
description: seasoning.complements
}
})
},
regions: function getAllRegions() {
return flavors.regions.map(function(region) {
return {
title: region.region,
description: region.ingredients
}
})
}
}
var url = parseUrl()
var input = inputEl
var prefix = "eat with"
var title = url.list||"ingredients"
var activeList = lists.ingredients()
if(lists[url.list]) activeList = lists[url.list]()
input.value = url.search
updateResults(input.value, activeList, prefix, title)
input.addEventListener('keyup', throttle(function(e) {
updateResults(input.value, activeList, prefix, title)
}, 1000))
addEvent(openLeft, 'click', function(){
if (snapper.state().state === 'closed') snapper.open('left')
else snapper.close('left')
})
addEvent(searchOptions, 'click', function(e) {
e.preventDefault()
var searchBy = e.target.getAttribute('data-search')
prefix = e.target.getAttribute('data-prefix')
title = e.target.getAttribute('data-title')
if (searchBy) {
snapper.close('left')
activeList = lists[searchBy]()
updateResults(input.value, activeList, prefix, title)
}
return false
})
function makeCrossBrowserCompatible() {
// https://github.com/piatra/flavors/commit/c21bd731e6572ddce6d5c50ceb543fc8b037f559
if (navigator.userAgent.match(/Android 2/i)) {
snapDrawer.style.overflow = 'inherit'
}
}
function addEvent(element, eventName, func) {
if (element.addEventListener) {
return element.addEventListener(eventName, func, false)
} else if (element.attachEvent) {
return element.attachEvent("on" + eventName, func)
}
}
function setResults(objs, itemPrefix, titleName) {
results.innerHTML = ''
var htmls = ''
objs.map(function(obj) {
htmls += '<li class="topcoat-list__item">'
+ '<span class="title">' + obj.title + '</span>'
+ ' - ' + itemPrefix + ' <span class="description">' + obj.description + '</span>'
+ '</li>'
})
results.innerHTML += htmls
header.innerText = "Showing " + objs.length + " " + titleName
}
function filterResults(objs, str) {
var matches = []
str = str.trim().toLowerCase()
objs.map(function(obj) {
if (obj.title.trim().toLowerCase().match(str)) matches.push(obj)
if (obj.description.toLowerCase().match(str)) matches.push(obj)
})
return matches
}
function updateResults(value, activeList, prefix, title){
setUrl(title, value)
if (value.length === 0) setResults(activeList, prefix, title)
else setResults(filterResults(activeList, value), prefix, title)
}
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250)
var last
var deferTimer
return function () {
var context = scope || this
var now = +new Date
var args = arguments
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer)
deferTimer = setTimeout(function () {
last = now
fn.apply(context, args)
}, threshhold)
} else {
last = now
fn.apply(context, args)
}
}
}
function setUrl(list, terms) {
location.hash = list + '/' + terms
}
function parseUrl(){
var parts = (location.hash || '').replace('#', '').split('/')
var obj = { list: false, search: '' }
if (parts.length === 1) return obj
obj.list = parts[0]
obj.search = parts[1]
return obj
}