11# Licensed under the MIT license - http://opensource.org/licenses/MIT
22# Copyright (C) 2016 Andrew Prasetya
3+ # Version: Thu 6 Oct 12:44:15 2016
34
45class @StackUp
56
67 containerElement : undefined
7- itemElements : undefined
8- containerHeight : 0
9- containerWidth : 0
10- items : [] # format: [index][item, itemHeight, left, top]
11- numberOfColumns : 0
12- boundary : height : 0 , width : 0
8+ itemElements : undefined
9+ containerHeight : 0
10+ containerWidth : 0
11+ items : [] # format: [index][item, itemHeight, left, top]
12+ numberOfColumns : 0
13+ boundary : height : 0 , width : 0
1314
1415 config :
15- containerSelector : undefined
16- itemsSelector : undefined
17- boundary : window
18- columnWidth : 320
19- gutter : 18
20- isFluid : false
21- layout : ' ordinal'
22- numberOfColumns : 3
16+ containerSelector : undefined
17+ itemsSelector : undefined
18+ boundary : window
19+ columnWidth : 320
20+ gutter : 18
21+ isFluid : false
22+ layout : ' ordinal'
23+ numberOfColumns : 3
2324 resizeDebounceDelay : 350
2425 moveItem : (item , left , top , callback ) ->
2526 item .style .left = left + ' px'
26- item .style .top = top + ' px'
27+ item .style .top = top + ' px'
2728 callback ()
2829 scaleContainer : (container , width , height , callback ) ->
2930 container .style .height = height + ' px'
30- container .style .width = width + ' px'
31+ container .style .width = width + ' px'
3132 callback ()
3233
3334 constructor : (properties ) ->
@@ -36,10 +37,10 @@ class @StackUp
3637 initialize : ->
3738 window .addEventListener ' resize' , @resizeHandler
3839 @ boundaryUpdate ()
39- # Update grid selectors. - reset
40+ # update grid selectors. - reset
4041 @ updateSelectors ()
4142 @ populateItems ()
42- # Update grid selectors. - stacking
43+ # update grid selectors. - stacking
4344 @ updateNumberOfColumns ()
4445 @ applyLayout ()
4546 @ draw ()
@@ -48,12 +49,13 @@ class @StackUp
4849 if @config .boundary isnt window
4950 style = @config .boundary .currentStyle || window .getComputedStyle (@config .boundary )
5051 horizontalPaddings = parseFloat (style .paddingLeft ) + parseFloat (style .paddingRight )
51- verticalPaddings = parseFloat (style .paddingTop ) + parseFloat (style .paddingBottom )
52- @boundary .height = @config .boundary .offsetHeight - verticalPaddings
53- @boundary .width = @config .boundary .offsetWidth - horizontalPaddings
52+ verticalPaddings = parseFloat (style .paddingTop ) + parseFloat (style .paddingBottom )
53+ @boundary .height = @config .boundary .offsetHeight - verticalPaddings
54+ @boundary .width = @config .boundary .offsetWidth - horizontalPaddings
5455 else
5556 @boundary .height = window .innerHeight
56- @boundary .width = window .innerWidth
57+ @boundary .width = window .innerWidth
58+ this
5759
5860 resizeDebounceTimeout : undefined
5961 resizeDebounce : (fn , delay ) ->
@@ -67,16 +69,18 @@ class @StackUp
6769 @ boundaryUpdate ()
6870 @ resizeDebounce @resizeComplete , @config .resizeDebounceDelay
6971
70- # Update grid selectors. (1) - reset
71- # Required stackgrid .initialize to be called first.
72+ # update grid selectors. (1) - reset
73+ # required stack-up .initialize to be called first.
7274 updateSelectors : ->
7375 @containerElement = document .querySelector @config .containerSelector
7476 @itemElements = document .querySelectorAll " #{ @config .containerSelector } > #{ @config .itemsSelector } "
77+ this
7578
7679 # This only updates @items, it does not update the selectors.
7780 appendItem : (item ) ->
7881 item .style .width = " #{ @config .columnWidth } px"
7982 @items .push [item, item .offsetHeight , 0 , 0 ]
83+ this
8084
8185 # Populate grid items. (2) - reset
8286 populateItems : ->
@@ -93,7 +97,7 @@ class @StackUp
9397 numberOfColumns = 1 if @items .length and numberOfColumns <= 0
9498 return numberOfColumns
9599
96- # Update _grid.numberOfColumns. (3) - stack
100+ # update _grid.numberOfColumns. (3) - stack
97101 updateNumberOfColumns : ->
98102 @numberOfColumns = @ calculateNumberOfColumns ()
99103
@@ -106,7 +110,7 @@ class @StackUp
106110 callback = ->
107111 @config .moveItem item[0 ], item[2 ], item[3 ], callback for item, index in @items
108112
109- # Stack (4)
113+ # stack (4)
110114 # Layout updates the _grid.containerHeight and updates _grid.items.
111115 layout :
112116 columnPointer : 0
@@ -122,7 +126,6 @@ class @StackUp
122126 c .containerHeight = @stack [c .layout .columnPointer ] if @stack [c .layout .columnPointer ] > c .containerHeight
123127 c .layout .columnPointer ++
124128 c .layout .columnPointer = 0 if c .layout .columnPointer >= c .numberOfColumns
125-
126129 loop : ->
127130 @ plot i for i in [0 .. @context .items .length - 1 ]
128131 optimized :
@@ -145,21 +148,23 @@ class @StackUp
145148 @layout [@config .layout ].context = this
146149 @layout [@config .layout ].setup ()
147150 @layout [@config .layout ].loop () if @items .length
151+ this
148152
149153 resetLayout : ->
150- @containerHeight = 0
154+ @containerHeight = 0
151155 @layout .columnPointer = 0
156+ this
152157
153158 # This should be called when any item are being modified, added, or removed.
154159 reset : ->
155- @containerWidth = 0
160+ @containerWidth = 0
156161 @containerHeight = 0
157- @items = []
162+ @items = []
158163 @ updateSelectors ()
159164 @ populateItems ()
160165 @ resetLayout ()
161166 @ restack ()
162- return
167+ this
163168
164169 append : (item , callback ) ->
165170 itemIndex = @items .length
@@ -169,9 +174,11 @@ class @StackUp
169174 @ draw ()
170175 else
171176 @ restack ()
177+ this
172178
173179 restack : ->
174180 @ updateNumberOfColumns ()
175181 @ resetLayout ()
176182 @ applyLayout ()
177183 @ draw ()
184+ this
0 commit comments