|
27 | 27 | widgetEventPrefix: 'tiler' |
28 | 28 | options: |
29 | 29 | isReversible: true |
| 30 | + startingActiveTile: 1 |
| 31 | + startingPreviousTile: 2 |
30 | 32 |
|
31 | 33 | _create: -> |
32 | | - @currentTileIndex = null |
| 34 | + @currentActiveTileIndex = @options.startingActiveTile |
| 35 | + @currentPreviousTileIndex = @options.startingPreviousTile |
33 | 36 |
|
34 | 37 | _init: -> |
| 38 | + @element.addClass 'animation-disabled' |
| 39 | + |
35 | 40 | # Collect all the tiles, except for those nested inside another tiler instance |
36 | 41 | @$tiles = $('.tiler-tile', @element).not(@element.find('.tiler-viewport .tiler-tile')) |
| 42 | + @$enterTile = @$tiles.eq @currentActiveTileIndex - 1 |
| 43 | + @$exitTile = @$tiles.eq @currentPreviousTileIndex - 1 |
| 44 | + |
37 | 45 | @_setupTiles() |
38 | 46 | @_setupLinks() |
39 | 47 |
|
|
45 | 53 | @element.trigger 'tiler.refresh' |
46 | 54 | @$enterTile?.trigger 'tiler.refresh' |
47 | 55 |
|
48 | | - goTo: (tile, animation = true) -> |
49 | | - # Find tile |
50 | | - # ...id as string |
51 | | - $tile = if typeof tile == 'string' |
52 | | - @$tiles.filter("##{tile}") |
| 56 | + # Find tile with various values |
| 57 | + # |
| 58 | + _getTile: (tileValue) -> |
| 59 | + # ...as a css ID (String) |
| 60 | + if typeof tileValue == 'string' |
| 61 | + $("##{tileValue}", @element) |
53 | 62 |
|
54 | 63 | # ...as jquery object |
55 | | - else if tile?.jquery |
56 | | - tile?.jquery |
| 64 | + else if tileValue.jquery |
| 65 | + tileValue.jquery |
57 | 66 |
|
58 | 67 | # ...as dom node |
59 | | - else if tile.nodeType |
60 | | - $(tile) |
| 68 | + else if tileValue.nodeType |
| 69 | + $(tileValue) |
61 | 70 |
|
62 | 71 | # ...as index (starting at 1) |
63 | 72 | else |
64 | | - @$tiles.eq tile - 1 |
| 73 | + @$tiles.eq tileValue - 1 |
65 | 74 |
|
66 | | - # Return if we are already on that tile |
67 | | - tileIndex = @$tiles.index $tile |
68 | | - return if !$tile.length || @currentTileIndex == tileIndex |
| 75 | + goTo: (tile, animation) -> |
| 76 | + # New active tile |
| 77 | + @$enterTile = @_getTile tile |
| 78 | + enterTileIndex = @$tiles.index @$enterTile |
| 79 | + @$exitTile = @_getTile @currentActiveTileIndex + 1 |
69 | 80 |
|
70 | | - # Get animating tiles |
71 | | - @$enterTile = $tile |
72 | | - @$exitTile = @$tiles.eq @currentTileIndex |
| 81 | + # Return if we are already on that tile |
| 82 | + return if !@$enterTile.length || @currentActiveTileIndex == enterTileIndex |
73 | 83 |
|
74 | | - @_transitionCss @_getAnimationClass animation |
| 84 | + @_transitionCss @_getAnimationClass(), animation |
75 | 85 |
|
76 | 86 | # Fire js events |
77 | 87 | # ...on viewport |
|
84 | 94 | @$exitTile.trigger 'tiler.exit' |
85 | 95 |
|
86 | 96 | # Update the current tile id |
87 | | - @currentTileIndex = tileIndex |
| 97 | + @currentActiveTileIndex = enterTileIndex |
| 98 | + @currentPreviousTileIndex = @currentActiveTileIndex |
88 | 99 | @element.attr 'data-tiler-active-tile', @$enterTile.attr('id') |
89 | 100 |
|
90 | 101 | return @$enterTile |
91 | 102 |
|
92 | 103 | # |
93 | 104 | # PRIVATE METHODS |
94 | 105 | # |
95 | | - _getAnimationClass: (animation) -> |
96 | | - # return explicitly passed animation |
97 | | - return animation if typeof animation == 'string' |
| 106 | + _getAnimationClass: -> |
| 107 | + @$enterTile.data('tiler-animation') || @element.data('tiler-animation') || '' |
| 108 | + |
| 109 | + _transitionCss: (animationClass, animation) -> |
| 110 | + animationClass = animation if typeof animation == 'string' |
98 | 111 |
|
99 | | - # use animation from markup if true, and no-active-class for false |
100 | | - if animation |
101 | | - @$enterTile.data('tiler-animation') || @element.data('tiler-animation') || '' |
| 112 | + position = if animation == false |
| 113 | + 'end' |
102 | 114 | else |
103 | | - '' |
| 115 | + 'start' |
104 | 116 |
|
105 | | - _transitionCss: (animationClass) -> |
106 | 117 | enterTileIndex = @$tiles.index @$enterTile |
107 | 118 |
|
108 | 119 | # Add reverse class if supported and navigating in reverse order (according to the dom) |
|
115 | 126 | @element.addClass 'animation-disabled' |
116 | 127 |
|
117 | 128 | # Build tile starting position animations classes |
118 | | - enterStartClass = "tiler-tile #{animationClass} active enter #{reverseClass} start" |
119 | | - exitStartClass = "tiler-tile #{animationClass} previous exit #{reverseClass} start" |
| 129 | + enterStartClass = "tiler-tile #{animationClass} active enter #{reverseClass} #{position}" |
| 130 | + exitStartClass = "tiler-tile #{animationClass} previous exit #{reverseClass} #{position}" |
120 | 131 | otherTileClass = 'tiler-tile' |
121 | 132 |
|
122 | 133 | # Set tile classes |
|
125 | 136 | @$tiles.not(@$enterTile).not(@$exitTile).attr 'class', otherTileClass |
126 | 137 |
|
127 | 138 | # setTimeout needed to give the browser time to repaint the tiles (if neccessary) with the animation starting position |
128 | | - setTimeout => |
129 | | - # Enable transitions |
130 | | - @element.removeClass 'animation-disabled' |
| 139 | + unless animation == false |
| 140 | + setTimeout => |
| 141 | + # Enable transitions |
| 142 | + @element.removeClass 'animation-disabled' |
131 | 143 |
|
132 | | - # Replace position classes to trigger animation |
133 | | - @$enterTile.add(@$exitTile).switchClass 'start', 'end' |
134 | | - , 10 |
| 144 | + # Replace position classes to trigger animation |
| 145 | + @$enterTile.add(@$exitTile).switchClass 'start', 'end' |
| 146 | + , 10 |
135 | 147 |
|
136 | 148 | # Find possible links throughout the entire page and set meta data on them |
137 | 149 | # |
|
164 | 176 | # Add a data attribute with the viewport id |
165 | 177 | $(@).attr 'data-tiler-viewport-id', self.element.attr('id') |
166 | 178 |
|
| 179 | + # Add animation class |
| 180 | + $(@).addClass self._getAnimationClass(true) |
| 181 | + |
167 | 182 | # Set sizes |
168 | 183 | @element.add(@$tiles).css |
169 | 184 | width: @element.outerWidth() |
|
172 | 187 | # Determine if we are advancing or retreating through our virtual tiles |
173 | 188 | # |
174 | 189 | _isNavigatingForward: (enterTileIndex) -> |
175 | | - enterTileIndex > @currentTileIndex |
| 190 | + enterTileIndex > @currentActiveTileIndex |
0 commit comments