-
Notifications
You must be signed in to change notification settings - Fork 2
/
columns.coffee
45 lines (30 loc) · 1.1 KB
/
columns.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
(($) ->
class ColumnCalculator
constructor: (@items, @columnsRequired) ->
@itemCount = @items.length
toColumns: ->
columnCounts = @_calculateColumnCounts()
@_calculateColumns(columnCounts)
_calculateColumnCounts: ->
minItemsPerColumn = Math.floor(@itemCount / @columnsRequired)
maxItemsPerColumn = minItemsPerColumn + 1
numberOfMinColumns = @itemCount % @columnsRequired
columnCounts = []
for columnNumber in [1..@columnsRequired] by 1
columnCount = maxItemsPerColumn
if columnNumber > numberOfMinColumns
columnCount = minItemsPerColumn
columnCounts.push(columnCount)
columnCounts
_calculateColumns: (columnCounts) ->
columns = []
itemsProcessed = 0
for columnCount in columnCounts
columnItems = @items.slice(itemsProcessed, itemsProcessed + columnCount)
columns.push(columnItems)
itemsProcessed += columnCount
columns
$.fn.toColumns = (columnsRequired) ->
columnizr = new ColumnCalculator(@, columnsRequired)
return columnizr.toColumns()
)(jQuery)