-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
308 lines (242 loc) · 7.99 KB
/
index.coffee
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#*--------------------------------------------------------*#
# KBD Iso
#*--------------------------------------------------------*#
Isotope = require 'isotope-layout'
_includes = require 'lodash/collection/includes'
_toArray = require 'lodash/lang/toArray'
_debounce = require 'lodash/function/debounce'
dropdown = require 'kbd-dropdown'
kbd = require './kbd'
imagesLoaded = require 'imagesloaded'
class Iso
# Set all options
constructor: (options = {}) ->
@iso = null
@isoFilters = {}
@container = options.container ? null
@items = options.items ? null
@filters = if options.filterType is 'sidebarFilters' then document.querySelectorAll('.' + options.filters + ' input') else document.getElementsByClassName(options.filters)
@gutter = options.gutter ? 0
@lazy = options.lazy ? false
@filterType = options.filterType ? 'simple'
@sidebarButton = document.getElementsByClassName(options.sidebarTrigger)[0] ? document.getElementsByClassName('sidebar-button')[0]
@sidebar = document.getElementsByClassName(options.sidebar)[0] ? document.getElementsByClassName('iso__sidebar')[0]
@menus = document.getElementsByClassName(options.menus) ? document.getElementsByClassName('dropdown')
@notify = options.notify ? false
@clearFiltersButton = document.getElementsByClassName(options.clearFiltersButton)[0] ? null
@sort = options.sort ? null
@setHeightToWidth = options.setHeightToWidth ? false
# Initialize Isotope instance
isoInit: ->
if @sort?
@iso = new Isotope '#' + @container,
itemSelector: '.' + @items
masonry:
columnWidth: '.' + @items
gutter: @gutter
getSortData:
order: @sort.order
sortBy: @sort.sortBy
sortAscending: @sort.sortAscending
return
else
@iso = new Isotope '#' + @container,
itemSelector: '.' + @items
masonry:
columnWidth: '.' + @items
gutter: @gutter
return
# Clears active filter on @filters
clearFilters: ->
for filter in @filters
do (filter) ->
if classie.has(filter, 'iso__filter--active')
classie.remove(filter, 'iso__filter--active')
return
# Sets simple (one-layer) Isotope filters
simpleFilters: ->
_this = @
for el in @filters
el.addEventListener 'click', ->
# Add active filter class
_this.clearFilters()
classie.add(@, 'iso__filter--active')
# Get the filter
filter = @.getAttribute 'data-filter'
# Run the filter
_this.iso.arrange
filter: filter
_this.iso.layout()
# If necessary, notify user
_this.notifyOnEmpty()
return
# Sets sidebar Isotope filters
sidebarFilters: ->
_this = @
# Filters for Portfolio Nav
for filter in @filters
do (filter) ->
filter.addEventListener 'click', ->
# Get value
value = @.value
# Fetch the group filter accordingly
group = kbd.closest(@, 'sidebar__filter-group').getAttribute 'data-filter-group'
if @.checked
# If the object already has a value for that group, add it to the existing array
if _includes( Object.keys(_this.isoFilters), group )
_this.isoFilters[ group ].push( value )
# If the group does not yet exist, init an empty array and add it to the array
else
_this.isoFilters[ group ] = []
_this.isoFilters[ group ].push( value )
else
# If the group has multiple values, find the value and remove it from the array
if _this.isoFilters[ group ].length > 1
i = _this.isoFilters[ group ].indexOf( value )
_this.isoFilters[ group ].splice(i, 1)
# If the group only has one value, just delete the group altogether
else
delete _this.isoFilters[ group ]
# Set an empty filter string
filters = ''
# Concat all values of filters to string
permute = (arrays) ->
result = []
max = arrays.length - 1
# Iterate through arrays until last array is reached
iterate = ( arr, i ) ->
for el, j in arrays[i]
# Clone the incoming array
clone = arr.slice(0)
# Push each element onto the clone
clone.push( arrays[i][j] )
# Recusively call the iteration until all arrays have been iterated
if i is max then result.push( clone ) else iterate( clone, i + 1 )
iterate( [], 0 )
return result
# Check if filters object is empty
if JSON.stringify( _this.isoFilters ) isnt '{}'
# Run permutation on the object converted to an array
results = permute( _toArray( _this.isoFilters ) )
# For each array in results, join into a selector string
for r, i in results
do (r, i) ->
if (i + 1) is results.length
filters += r.join('')
else
# Add a comma after each selector string unless it's the last string
filters += r.join('') + ', '
# If it is empty, set the filters to all
else
filters = '*'
# Run the filter
_this.iso.arrange
filter: filters
_this.iso.layout()
# If necessary, notify user
_this.notifyOnEmpty()
return
# Sets dropdown Isotope Filters
dropdownFilters: ->
_this = @
for filter in @filters
filter.addEventListener 'click', ->
# Fetch the group filter accordingly
group = kbd.closest(@, 'iso__menu').getAttribute 'data-filter-group'
# Get the filter and add it to the object
_this.isoFilters[ group ] = @.getAttribute 'data-filter'
filters = ''
for key, value of _this.isoFilters
filters += _this.isoFilters[ key ]
# Run the filter
_this.iso.arrange
filter: filters
_this.iso.layout()
# If necessary, notify user
_this.notifyOnEmpty()
return
# Add dropdowns
dropdownInit: ->
for menu in @menus
do (menu) ->
d = new dropdown(menu).init()
# Option to lazy load images
lazyLoad: ->
items = document.getElementsByClassName(@items)
for item in items
do (item) ->
dataSrc = item.getAttribute 'data-src'
if dataSrc?
image = new Image()
image.onload = ->
classie.add(image, 'loaded')
image.src = dataSrc
item.appendChild(image)
return
# Trigger Sidebar
sidebarToggle: ->
_this = @
@sidebarButton.addEventListener 'click', ->
if !classie.has(_this.sidebar, 'nav-visible')
classie.add(_this.sidebar, 'nav-visible')
return
# Optionally notify user if no results are returned
notifyOnEmpty: ->
if @notify is true
# Define container as DOM Node
container = document.getElementById(@container)
notification = container.getElementsByClassName('iso--empty')[0]
# If no elements are returned, notify user
if @iso.filteredItems.length > 0
# Remove notification if present
if notification?
container.removeChild(notification)
return
else
# Only add notification if not already present
if not notification?
# Create notification node
notification = document.createElement 'p'
classie.add(notification, 'iso--empty')
notification.textContent = 'Sorry, this filter set has returned no results. Try clearing the filters and sorting again.'
# Append notification
container.appendChild(notification)
return
# Clear filters on click
clearFiltersTrigger: ->
_this = @
@clearFiltersButton.addEventListener 'click', (event) ->
event.preventDefault()
_this.clearFilters()
return
# Init
init: ->
_this = @
# Init Main
@isoInit()
# Init filters based on sort-type
if @filterType is 'simple'
@simpleFilters()
if @filterType is 'sidebarFilters'
@sidebarFilters()
@sidebarToggle()
if @filterType is 'dropdownFilters'
@dropdownInit()
@dropdownFilters()
# Lazy
if @lazy is true
@lazyLoad()
# Re-layout
isoLoad = imagesLoaded( '#' + @container )
isoLoad.on 'progress', ->
if _this.setHeightToWidth?
kbd.setHeightToWidth( document.getElementsByClassName(_this.items), true )
_this.iso.layout()
# Clear Filters Button
if @clearFiltersButton?
@clearFiltersTrigger()
# Resize, obvi
window.addEventListener 'resize', _debounce ->
kbd.setHeightToWidth( document.getElementsByClassName(_this.items), true )
, 150
module.exports = Iso