This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LazyVerticalLayoutConnector.java
262 lines (217 loc) · 8.55 KB
/
LazyVerticalLayoutConnector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package org.vaadin.alump.lazylayouts.client;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.ConnectorHierarchyChangeEvent;
import com.vaadin.client.ServerConnector;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.VWindow;
import com.vaadin.client.ui.layout.MayScrollChildren;
import com.vaadin.client.ui.orderedlayout.VerticalLayoutConnector;
import com.vaadin.shared.ui.Connect;
import org.vaadin.alump.lazylayouts.LazyVerticalLayout;
import java.util.logging.Logger;
/**
* Connector for LazyVerticalLayout
*/
@Connect(LazyVerticalLayout.class)
public class LazyVerticalLayoutConnector extends VerticalLayoutConnector implements LazyScrollListener {
private final static Logger LOGGER = Logger.getLogger(LazyVerticalLayoutConnector.class.getName());
private ComponentConnector scrollerFollowed;
private Element scrollingElement;
protected boolean waitingResponse = false;
private HandlerRegistration handlerRegistration;
/**
* How long is waited after connector hierarchy change until scroller position is checked automatically
*/
private final static int DELAYED_CHECK_AFTER_CHANGE_MS = 250;
@Override
public LazyVerticalLayoutState getState() {
return (LazyVerticalLayoutState)super.getState();
}
@Override
public void init() {
super.init();
}
@Override
public void onUnregister() {
requestTimer.cancel();
removeScrollingHandlers();
super.onUnregister();
}
private void removeScrollingHandlers() {
if(handlerRegistration != null) {
handlerRegistration.removeHandler();
handlerRegistration = null;
}
if(scrollerFollowed != null) {
Widget widget = scrollerFollowed.getWidget();
if(widget instanceof LazyScrollNotifier) {
((LazyScrollNotifier) scrollerFollowed.getWidget()).removeLazyScrollListener(this);
}
scrollerFollowed = null;
}
}
@Override
public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
if(getParent() != null) {
if (scrollerFollowed == null) {
scrollerFollowed = resolveScrollingParent();
if (scrollerFollowed != null) {
attachScrollingEvents(scrollerFollowed);
} else {
LOGGER.severe("Failed to resolve scrolling parent!");
}
}
} else {
removeScrollingHandlers();
}
waitingResponse = false;
super.onConnectorHierarchyChange(event);
if(getParent() != null) {
Widget indicator = getLazyLoadingIndicator();
if(indicator != null) {
indicator.getElement().getStyle().setOpacity(0.5);
}
// Verify that we do not need to continue loading after hierarchy change
if(scrollingElement != null) {
Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
@Override
public boolean execute() {
if (!waitingResponse) {
if (checkIfLazyRequestRequired(scrollingElement)) {
sendLazyLoadRequest();
}
}
return false;
}
}, DELAYED_CHECK_AFTER_CHANGE_MS);
}
}
}
@Override
public void onStateChanged(StateChangeEvent event) {
super.onStateChanged(event);
if(event.hasPropertyChanged("lazyLoading")) {
waitingResponse = false;
}
}
protected void attachScrollingEvents(ComponentConnector connector) {
Widget widget = connector.getWidget();
if(widget instanceof LazyScrollNotifier) {
LazyScrollNotifier not = (LazyScrollNotifier) widget;
scrollingElement = not.getLazyScrollingElement();
not.addLazyScrollListener(this);
} else if (widget instanceof VWindow) {
final VWindow window = (VWindow)widget;
scrollingElement = window.contentPanel.getElement();
handlerRegistration = ((VWindow)widget).contentPanel.addScrollHandler(new ScrollHandler() {
@Override
public void onScroll(ScrollEvent event) {
onLazyScroll(window.contentPanel.getElement());
}
});
} else {
LOGGER.severe("Failed to connect to scrolling events of " + connector.getClass().getName());
}
}
protected ComponentConnector resolveScrollingParent() {
if(getState().scrollingParent != null) {
return (ComponentConnector)getState().scrollingParent;
}
ServerConnector connector = getParent();
if(connector == null) {
LOGGER.severe("LazyVerticalLayout not in connector hierarchy.");
return null;
}
while(connector != null) {
if(connector instanceof MayScrollChildren) {
return (ComponentConnector)connector;
}
connector = connector.getParent();
}
LOGGER.severe("Failed to resolve scrolling parent for LazyVerticalLayout");
return null;
}
@Override
public void onLazyScroll(Element scrollingElement) {
// Can be ignored, if lazy loading not enabled
if(!getState().lazyLoading) {
return;
}
// Remember element so it can be used to check current position
this.scrollingElement = scrollingElement;
// Send load request if no pending request and position requires new content
if(!waitingResponse && checkIfLazyRequestRequired(scrollingElement)) {
sendLazyLoadRequest();
}
}
/**
* Check if given scrolling element is at position that requires to perform lazy load
* @param scrollingElement Element that is scrollable
* @return true if lazy loading is required, false if not
*/
protected boolean checkIfLazyRequestRequired(Element scrollingElement) {
if(scrollingElement == null) {
LOGGER.severe("Can not check scroll position of undefined element");
return false;
}
Widget indicator = getLazyLoadingIndicator();
if(indicator != null) {
int indicatorY = indicator.getElement().getAbsoluteTop() - scrollingElement.getAbsoluteTop();
return (indicatorY < scrollingElement.getClientHeight());
} else {
int maxScroll = scrollingElement.getScrollHeight() - scrollingElement.getClientHeight();
return (scrollingElement.getScrollTop() >= maxScroll);
}
}
/**
* Sends lazy component loading request to server
*/
protected void sendLazyLoadRequest() {
if(!waitingResponse) {
waitingResponse = true;
// Delay so other signals will be sent before
requestTimer.schedule();
}
}
/**
* Timer used to delay request until scrolling stops
*/
protected class LazyRequestTimer extends Timer {
public final static int REQUEST_DELAY_TIMER_MS = 50;
public LazyRequestTimer() {
}
public void schedule() {
schedule(REQUEST_DELAY_TIMER_MS);
}
@Override
public void run() {
if(LazyVerticalLayoutConnector.this.isEnabled()) {
LOGGER.fine("Sending lazy loading request...");
Widget indicator = getLazyLoadingIndicator();
if (indicator != null) {
indicator.getElement().getStyle().setOpacity(1.0);
}
getRpcProxy(LazyLayoutServerRpc.class).onLazyLoadRequest();
}
}
};
protected LazyRequestTimer requestTimer = new LazyRequestTimer();
/**
* Gets lazy loading indicator
* @return Loading indicator widget instance if defined
*/
private Widget getLazyLoadingIndicator() {
ComponentConnector connector = (ComponentConnector)getState().lazyLoadingIndicator;
if(connector == null) {
return null;
}
return connector.getWidget();
}
}