-
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.
Merge pull request #30 from it3s/stick-component
Stick component
- Loading branch information
Showing
13 changed files
with
259 additions
and
17 deletions.
There are no files selected for viewing
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,17 @@ | ||
#= require jquery.sticky | ||
# | ||
App.components.stick = (container) -> | ||
{ | ||
container: container, | ||
|
||
init: -> | ||
data = @container.data 'stick' | ||
|
||
@container.sticky | ||
className: 'stick' | ||
wrapperClassName: 'stick-wrapper' | ||
topSpacing: data?.top_spacing | ||
bottomSpacing: data?.bottom_spacing | ||
getWidthFrom: data?.get_width_from | ||
|
||
} |
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
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 |
---|---|---|
|
@@ -54,7 +54,7 @@ form { | |
} | ||
|
||
.form-actions { | ||
margin-bottom: 20px; | ||
padding: 15px 0; | ||
|
||
.edit-save { | ||
display: inline-block; | ||
|
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,8 @@ | ||
.stick > * { | ||
background-color: $stick-color-bg; | ||
z-index: 2000; | ||
} | ||
|
||
.page .stick > * { | ||
box-shadow: $stick-color-shadow 0px 5px 3px -3px; | ||
} |
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,9 +1,11 @@ | ||
<ul class="toolbar" role="toolbar"> | ||
<% tools_list(object, local_assigns[:only] || :all).each do |tool| %> | ||
<li title="<%= tool[:title] %>"> | ||
<a class="<%= 'active' if request.path == tool[:url] %>" href="<%= tool[:url] %>" role="button"> | ||
<%= icon tool[:icon], '', :class => "fa-lg fa-fw" %> | ||
</a> | ||
</li> | ||
<% end %> | ||
</ul> | ||
<div class="toolbar"> | ||
<ul class="toolbar-buttons" role="toolbar" data-components="stick"> | ||
<% tools_list(object, local_assigns[:only] || :all).each do |tool| %> | ||
<li title="<%= tool[:title] %>"> | ||
<a class="<%= 'active' if request.path == tool[:url] %>" href="<%= tool[:url] %>" role="button"> | ||
<%= icon tool[:icon], '', :class => "fa-lg fa-fw" %> | ||
</a> | ||
</li> | ||
<% end %> | ||
</ul> | ||
</div> |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
## Stick | ||
|
||
"stick" content | ||
|
||
### markup | ||
|
||
``` | ||
<div data-components="stick" data-stick='{"options": "goes here"}'> | ||
Always visible | ||
</div> | ||
``` | ||
|
||
### Options | ||
``` | ||
:top_spacing => default 0 | ||
:bottom_spacing => default 0 | ||
:get_width_from => default undefined | ||
``` |
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,20 @@ | ||
#= require spec_helper | ||
|
||
describe "stick", -> | ||
beforeEach -> | ||
@container = $ JST['templates/stick']() | ||
|
||
it 'initializes component', -> | ||
spy App.components, 'stick', => | ||
App.mediator.publish 'components:start', @container | ||
expect(App.components.stick).to.be.called | ||
|
||
|
||
describe 'component', -> | ||
beforeEach -> | ||
@component = App.components.stick @container | ||
|
||
it 'calls jquery-sticky plugin', -> | ||
spy @component.container, 'sticky', => | ||
@component.init() | ||
expect(@component.container.sticky).to.be.called |
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,5 @@ | ||
<div id="container"> | ||
<div id="test" data-components="stick"> | ||
content | ||
</div> | ||
</div> |
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,160 @@ | ||
// Sticky Plugin v1.0.0 for jQuery | ||
// ============= | ||
// Author: Anthony Garand | ||
// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk) | ||
// Improvements by Leonardo C. Daronco (daronco) | ||
// Created: 2/14/2011 | ||
// Date: 2/12/2012 | ||
// Website: http://labs.anthonygarand.com/sticky | ||
// Description: Makes an element on the page stick on the screen as you scroll | ||
// It will only set the 'top' and 'position' of your element, you | ||
// might need to adjust the width in some cases. | ||
|
||
(function($) { | ||
var defaults = { | ||
topSpacing: 0, | ||
bottomSpacing: 0, | ||
className: 'is-sticky', | ||
wrapperClassName: 'sticky-wrapper', | ||
center: false, | ||
getWidthFrom: '' | ||
}, | ||
$window = $(window), | ||
$document = $(document), | ||
sticked = [], | ||
windowHeight = $window.height(), | ||
scroller = function() { | ||
var scrollTop = $window.scrollTop(), | ||
documentHeight = $document.height(), | ||
dwh = documentHeight - windowHeight, | ||
extra = (scrollTop > dwh) ? dwh - scrollTop : 0; | ||
|
||
for (var i = 0; i < sticked.length; i++) { | ||
var s = sticked[i], | ||
elementTop = s.stickyWrapper.offset().top, | ||
etse = elementTop - s.topSpacing - extra; | ||
|
||
if (scrollTop <= etse) { | ||
if (s.currentTop !== null) { | ||
s.stickyElement | ||
.css('position', '') | ||
.css('top', ''); | ||
s.stickyElement.parent().removeClass(s.className); | ||
s.currentTop = null; | ||
} | ||
} | ||
else { | ||
var newTop = documentHeight - s.stickyElement.outerHeight() | ||
- s.topSpacing - s.bottomSpacing - scrollTop - extra; | ||
if (newTop < 0) { | ||
newTop = newTop + s.topSpacing; | ||
} else { | ||
newTop = s.topSpacing; | ||
} | ||
if (s.currentTop != newTop) { | ||
s.stickyElement | ||
.css('position', 'fixed') | ||
.css('top', newTop); | ||
|
||
if (typeof s.getWidthFrom !== 'undefined') { | ||
s.stickyElement.css('width', $(s.getWidthFrom).width()); | ||
} | ||
|
||
s.stickyElement.parent().addClass(s.className); | ||
s.currentTop = newTop; | ||
} | ||
} | ||
} | ||
}, | ||
resizer = function() { | ||
windowHeight = $window.height(); | ||
}, | ||
methods = { | ||
init: function(options) { | ||
var o = $.extend(defaults, options); | ||
return this.each(function() { | ||
var stickyElement = $(this); | ||
|
||
var stickyId = stickyElement.attr('id'); | ||
var wrapper = $('<div></div>') | ||
.attr('id', stickyId + '-sticky-wrapper') | ||
.addClass(o.wrapperClassName); | ||
stickyElement.wrapAll(wrapper); | ||
|
||
if (o.center) { | ||
stickyElement.parent().css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"}); | ||
} | ||
|
||
if (stickyElement.css("float") == "right") { | ||
stickyElement.css({"float":"none"}).parent().css({"float":"right"}); | ||
} | ||
|
||
var stickyWrapper = stickyElement.parent(); | ||
stickyWrapper.css('height', stickyElement.outerHeight()); | ||
sticked.push({ | ||
topSpacing: o.topSpacing, | ||
bottomSpacing: o.bottomSpacing, | ||
stickyElement: stickyElement, | ||
currentTop: null, | ||
stickyWrapper: stickyWrapper, | ||
className: o.className, | ||
getWidthFrom: o.getWidthFrom | ||
}); | ||
}); | ||
}, | ||
update: scroller, | ||
unstick: function(options) { | ||
return this.each(function() { | ||
var unstickyElement = $(this); | ||
|
||
removeIdx = -1; | ||
for (var i = 0; i < sticked.length; i++) | ||
{ | ||
if (sticked[i].stickyElement.get(0) == unstickyElement.get(0)) | ||
{ | ||
removeIdx = i; | ||
} | ||
} | ||
if(removeIdx != -1) | ||
{ | ||
sticked.splice(removeIdx,1); | ||
unstickyElement.unwrap(); | ||
unstickyElement.removeAttr('style'); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer): | ||
if (window.addEventListener) { | ||
window.addEventListener('scroll', scroller, false); | ||
window.addEventListener('resize', resizer, false); | ||
} else if (window.attachEvent) { | ||
window.attachEvent('onscroll', scroller); | ||
window.attachEvent('onresize', resizer); | ||
} | ||
|
||
$.fn.sticky = function(method) { | ||
if (methods[method]) { | ||
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | ||
} else if (typeof method === 'object' || !method ) { | ||
return methods.init.apply( this, arguments ); | ||
} else { | ||
$.error('Method ' + method + ' does not exist on jQuery.sticky'); | ||
} | ||
}; | ||
|
||
$.fn.unstick = function(method) { | ||
if (methods[method]) { | ||
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | ||
} else if (typeof method === 'object' || !method ) { | ||
return methods.unstick.apply( this, arguments ); | ||
} else { | ||
$.error('Method ' + method + ' does not exist on jQuery.sticky'); | ||
} | ||
|
||
}; | ||
$(function() { | ||
setTimeout(scroller, 0); | ||
}); | ||
})(jQuery); |