forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
backend/app/assets/javascripts/spree/backend/sticky.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
$.fn.offsetBottom = -> | ||
$(this).offset().top + $(this).height() | ||
|
||
class Viewport | ||
measure: => | ||
@top = document.documentElement.scrollTop || document.body.scrollTop | ||
@height = $(window).height() | ||
@bottom = @top + @height | ||
|
||
isScrollingUp: => | ||
@measure() | ||
goingUp = @top < @lastTop | ||
@lastTop = @top | ||
goingUp | ||
|
||
isAbove: (el) => | ||
@top < el.offset().top | ||
|
||
isBelow: (el) => | ||
@bottom > el.offsetBottom() | ||
|
||
fits: (el) => | ||
el.height() < @height | ||
|
||
viewport = new Viewport() | ||
|
||
$(window).on | ||
resize: _.debounce(viewport.measure, 100) | ||
|
||
class Sticky | ||
@checkStickies: => | ||
for sticky in @stickies | ||
sticky.measure() | ||
if viewport.isScrollingUp() | ||
if sticky.isStuckBottom | ||
sticky.pin() | ||
else | ||
sticky.stick() if viewport.isAbove(sticky.el) | ||
if sticky.tooHigh() | ||
sticky.unstick() if sticky.isStuckTop | ||
else | ||
if viewport.fits(sticky.el) | ||
sticky.stick() unless viewport.isAbove(sticky.el) | ||
else | ||
sticky.stick(true) if viewport.isBelow(sticky.el) | ||
if sticky.tooLow() | ||
sticky.pin(true) | ||
|
||
@stickies: [] | ||
|
||
constructor: (@el) -> | ||
@container = @el.parent() | ||
@placeholder = $('<div>').insertBefore(@el) | ||
@measure() | ||
if @container.css('position') == 'static' | ||
@container.css('position', 'relative') | ||
Sticky.stickies.push(this) | ||
|
||
measure: => | ||
@maxTop = @placeholder.offset().top | ||
@maxBottom = @container.offsetBottom() | ||
|
||
tooLow: => | ||
@el.offsetBottom() >= @maxBottom | ||
|
||
tooHigh: => | ||
@el.offset().top <= @maxTop | ||
|
||
stick: (bottom) => | ||
if bottom | ||
return if @isStuckBottom | ||
@isStuckBottom = true | ||
else | ||
return if @isStuckTop | ||
@isStuckTop = true | ||
width = @el.width() | ||
@el.addClass(if bottom then 'sticky-bottom' else 'sticky-top') | ||
.css | ||
position: 'fixed' | ||
top: '' | ||
width: width | ||
|
||
unstick: => | ||
return unless @isStuckBottom || @isStuckTop | ||
@isStuckBottom = @isStuckTop = false | ||
@el.removeClass('sticky-bottom sticky-top') | ||
@el.css('position', '') | ||
|
||
pin: (bottom) => | ||
top = @el.offset().top - @container.offset().top | ||
@unstick() | ||
@el.css('position', 'absolute') | ||
if bottom | ||
@el.css('bottom', 0) | ||
else | ||
@el.css('top', Math.max(top, @maxTop)) | ||
|
||
$(window).on | ||
scroll: @checkStickies | ||
|
||
$.fn.sticky = -> | ||
$(this).each -> | ||
new Sticky($(this)) | ||
|
||
$ -> | ||
$('[data-sticky]').sticky() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<div class="admin-nav"> | ||
<%= render partial: 'spree/admin/shared/header' %> | ||
<%= render partial: 'spree/admin/shared/menu' %> | ||
<%= render partial: 'spree/admin/shared/navigation_footer' %> | ||
<div class="admin-nav-sticky" data-sticky> | ||
<%= render partial: 'spree/admin/shared/menu' %> | ||
<%= render partial: 'spree/admin/shared/navigation_footer' %> | ||
</div> | ||
</div> |