Skip to content

Commit c2f1dc2

Browse files
committed
Fixed Dismissable CollectionItem Inialization
1 parent 6676f16 commit c2f1dc2

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,25 @@ public class MaterialCollectionItem extends MaterialWidget implements HasClickHa
5050
private final ToggleStyleMixin<MaterialCollectionItem> dismissableMixin = new ToggleStyleMixin<>(this, "dismissable");
5151

5252
private HandlerRegistration handlerReg;
53+
private Runnable swipeLeftCallback;
54+
private Runnable swipeRightCallback;
5355

5456
public MaterialCollectionItem() {
5557
super(Document.get().createLIElement());
5658
setStyleName("collection-item");
5759
UiHelper.addMousePressedHandlers(this);
5860
}
5961

62+
@Override
63+
protected void onLoad() {
64+
super.onLoad();
65+
initDismissableCollection(getSwipeLeftCallback(), getSwipeRightCallback());
66+
}
67+
68+
private native void initDismissableCollection(Runnable swipeLeftCallback, Runnable swipeRightCallback) /*-{
69+
$wnd.initDismissableCollection(swipeLeftCallback, swipeRightCallback);
70+
}-*/;
71+
6072
public void setType(CollectionType type) {
6173
switch (type) {
6274
case AVATAR:
@@ -123,4 +135,20 @@ public boolean isAvatar() {
123135
public HandlerRegistration addClickHandler(ClickHandler handler) {
124136
return addDomHandler(handler, ClickEvent.getType());
125137
}
138+
139+
public Runnable getSwipeLeftCallback() {
140+
return swipeLeftCallback;
141+
}
142+
143+
public void setSwipeLeftCallback(Runnable swipeLeftCallback) {
144+
this.swipeLeftCallback = swipeLeftCallback;
145+
}
146+
147+
public Runnable getSwipeRightCallback() {
148+
return swipeRightCallback;
149+
}
150+
151+
public void setSwipeRightCallback(Runnable swipeRightCallback) {
152+
this.swipeRightCallback = swipeRightCallback;
153+
}
126154
}

gwt-material/src/main/resources/gwt/material/design/client/resources/js/animation.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,86 @@ function closeGrid(id, duration){
3434
.addClass('closed');
3535
});
3636
});
37+
}
38+
39+
var swipeLeft = false;
40+
var swipeRight = false;
41+
42+
/* Hook the Dismissable Collection to includke the swipe left and swipe right
43+
callback */
44+
function initDismissableCollection(swipeLeftCallback, swipeRightCallback) {
45+
// Right On Remove callback
46+
var rightCallbacks = $.Callbacks();
47+
rightCallbacks.add(swipeRightCallback);
48+
49+
// Left on Remove callback
50+
var leftCallbacks = $.Callbacks();
51+
leftCallbacks.add(swipeLeftCallback);
52+
53+
// Dismissible Collections
54+
$('.dismissable').each(function() {
55+
$(this).hammer({
56+
prevent_default: false
57+
}).bind('pan', function(e) {
58+
if (e.gesture.pointerType === "touch") {
59+
var $this = $(this);
60+
var direction = e.gesture.direction;
61+
var x = e.gesture.deltaX;
62+
var velocityX = e.gesture.velocityX;
63+
64+
$this.velocity({ translateX: x
65+
}, {duration: 50, queue: false, easing: 'easeOutQuad'});
66+
67+
// Swipe Left
68+
if (direction === 4 && (x > ($this.innerWidth() / 2) || velocityX < -0.75)) {
69+
swipeLeft = true;
70+
}
71+
72+
// Swipe Right
73+
if (direction === 2 && (x < (-1 * $this.innerWidth() / 2) || velocityX > 0.75)) {
74+
swipeRight = true;
75+
}
76+
}
77+
}).bind('panend', function(e) {
78+
// Reset if collection is moved back into original position
79+
if (Math.abs(e.gesture.deltaX) < ($(this).innerWidth() / 2)) {
80+
swipeRight = false;
81+
swipeLeft = false;
82+
}
83+
84+
if (e.gesture.pointerType === "touch") {
85+
var $this = $(this);
86+
if (swipeLeft || swipeRight) {
87+
var fullWidth;
88+
if (swipeLeft) { fullWidth = $this.innerWidth(); }
89+
else { fullWidth = -1 * $this.innerWidth(); }
90+
91+
$this.velocity({ translateX: fullWidth,
92+
}, {duration: 100, queue: false, easing: 'easeOutQuad', complete:
93+
function() {
94+
$this.css('border', 'none');
95+
$this.velocity({ height: 0, padding: 0,
96+
}, {duration: 200, queue: false, easing: 'easeOutQuad', complete:
97+
function() {
98+
if(swipeRight){
99+
rightCallbacks.fire();
100+
}else{
101+
leftCallbacks.fire();
102+
}
103+
$this.remove();
104+
}
105+
});
106+
}
107+
});
108+
}
109+
else {
110+
$this.velocity({ translateX: 0,
111+
}, {duration: 100, queue: false, easing: 'easeOutQuad'});
112+
}
113+
swipeLeft = false;
114+
swipeRight = false;
115+
}
116+
});
117+
118+
});
37119
}

gwt-material/src/main/resources/gwt/material/design/client/resources/js/animation.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)