Skip to content

Commit 073cdeb

Browse files
committed
Completed : Material SubHeader
1 parent c4477fe commit 073cdeb

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialSubHeader.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
* #L%
2121
*/
2222

23+
import com.google.gwt.dom.client.Element;
24+
import com.google.gwt.dom.client.Style.Unit;
25+
import com.google.gwt.user.client.ui.HasText;
2326
import gwt.material.design.client.base.HasIcon;
2427
import gwt.material.design.client.constants.IconPosition;
2528
import gwt.material.design.client.constants.IconSize;
2629
import gwt.material.design.client.constants.IconType;
2730
import gwt.material.design.client.ui.html.Div;
2831
import gwt.material.design.client.ui.html.Span;
2932

30-
import com.google.gwt.dom.client.Style.Unit;
31-
import com.google.gwt.user.client.ui.HasText;
32-
3333
//@formatter:off
34+
3435
/**
3536
* Subheaders are special list tiles that delineate distinct sections of a list or grid
3637
* list and are typically related to the current filtering or sorting criteria. Subheader
@@ -48,7 +49,7 @@
4849
* @see <a href="http://gwt-material-demo.herokuapp.com/#subheaders">Material SubHeaders</a>
4950
*/
5051
//@formatter:on
51-
public class MaterialSubHeader extends Div implements HasText, HasIcon{
52+
public class MaterialSubHeader extends Div implements HasText, HasIcon {
5253

5354
private MaterialIcon icon = new MaterialIcon();
5455
private Span span = new Span();
@@ -57,6 +58,13 @@ public MaterialSubHeader() {
5758
setStyleName("subheader");
5859
}
5960

61+
@Override
62+
protected void onLoad() {
63+
super.onLoad();
64+
65+
initialize();
66+
}
67+
6068
@Override
6169
public MaterialIcon getIcon() {
6270
return icon;
@@ -108,4 +116,12 @@ public void setText(String text) {
108116
span.setText(text);
109117
add(span);
110118
}
119+
120+
private void initialize() {
121+
initialize(getElement());
122+
}
123+
124+
private native void initialize(Element e) /*-{
125+
$wnd.stickyHeaders.load($wnd.jQuery(".subheader"));
126+
}-*/;
111127
}
Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
1-
// Sticky Sub headers
2-
// TODO: Replace this script with pure GWT implementation.
3-
41
var stickyHeaders = (function() {
52

6-
var $stickies;
3+
var $window = $(window),
4+
$stickies;
5+
6+
var load = function(stickies) {
77

8-
var load = function(stickies, target) {
98
if (typeof stickies === "object" && stickies instanceof jQuery && stickies.length > 0) {
109

1110
$stickies = stickies.each(function() {
1211

13-
var $thisSticky = $(this);
12+
var $thisSticky = $(this).wrap('<div class="followWrap" />');
13+
1414
$thisSticky
1515
.data('originalPosition', $thisSticky.offset().top)
16-
.data('originalHeight', $thisSticky.outerHeight());
16+
.data('originalHeight', $thisSticky.outerHeight())
17+
.parent()
18+
.height($thisSticky.outerHeight());
1719
});
1820

19-
$(window).off("scroll.stickies").on("scroll.stickies", function(event) {
20-
_whenScrolling(event);
21+
$window.off("scroll.stickies").on("scroll.stickies", function() {
22+
_whenScrolling();
2123
});
2224
}
2325
};
2426

25-
var _whenScrolling = function(event) {
26-
27-
var $scrollTop = $(event.currentTarget).scrollTop();
27+
var _whenScrolling = function() {
2828

2929
$stickies.each(function(i) {
3030

3131
var $thisSticky = $(this),
32-
$stickyPosition = $thisSticky.data('originalPosition'),
33-
$newPosition,
34-
$nextSticky;
32+
$stickyPosition = $thisSticky.data('originalPosition');
33+
34+
if ($stickyPosition <= $window.scrollTop()) {
3535

36-
if ($stickyPosition <= $scrollTop) {
36+
var $nextSticky = $stickies.eq(i + 1),
37+
$nextStickyPosition = $nextSticky.data('originalPosition') - $thisSticky.data('originalHeight');
3738

38-
$newPosition = Math.max(0, $scrollTop - $stickyPosition);
39-
$nextSticky = $stickies.eq(i + 1);
39+
$thisSticky.addClass("fixed z-depth-1");
4040

41-
if($nextSticky.length > 0) {
41+
if ($nextSticky.length > 0 && $thisSticky.offset().top >= $nextStickyPosition) {
4242

43-
$newPosition = Math.min($newPosition,
44-
($nextSticky.data('originalPosition') - $stickyPosition) - $thisSticky.data('originalHeight'));
43+
$thisSticky.addClass("absolute").css("top", $nextStickyPosition);
4544
}
46-
$thisSticky.addClass('z-depth-1');
4745
} else {
48-
$newPosition = 0;
49-
$thisSticky.removeClass('z-depth-1');
50-
}
5146

52-
$thisSticky.css('transform', 'translateY(' + $newPosition + 'px)');
47+
var $prevSticky = $stickies.eq(i - 1);
5348

54-
//could just as easily use top instead of transform
55-
//$thisSticky.css('top', $newPosition + 'px');
49+
$thisSticky.removeClass("fixed z-depth-1");
50+
51+
if ($prevSticky.length > 0 && $window.scrollTop() <= $thisSticky.data('originalPosition') - $thisSticky.data('originalHeight')) {
52+
53+
$prevSticky.removeClass("absolute").removeAttr("style");
54+
}
55+
}
5656
});
5757
};
5858

@@ -62,6 +62,5 @@ var stickyHeaders = (function() {
6262
})();
6363

6464
$(function() {
65-
stickyHeaders.load($(".subheader"), $(window));
66-
});
67-
65+
stickyHeaders.load($(".followMeBar"));
66+
});

gwt-material/src/main/resources/gwt/material/design/public/css/overridecss.css

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,24 @@ i[class*="waves-"].material-icons {
754754
float: right;
755755
}
756756

757+
/** Sub Header */
757758
/** Sub Header */
758759
.subheader {
759-
padding: 10px 20px;
760-
position: relative;
761-
z-index: 1;
762760
background: #fff;
763-
font-weight: bold;
761+
padding: 10px 20px;
762+
position: relative;
763+
z-index: 1;
764+
color: #fff;
765+
}
766+
.subheader.fixed {
767+
position: fixed;
768+
top: 0;
769+
width: 100%;
770+
box-sizing: border-box;
771+
z-index: 999;
772+
}
773+
.subheader.fixed.absolute {
774+
position: absolute;
764775
}
765776

766777
/** Splashscreen */

0 commit comments

Comments
 (0)