-
Notifications
You must be signed in to change notification settings - Fork 1
/
infinitescroll.js
290 lines (223 loc) · 7.37 KB
/
infinitescroll.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* TODO:
proper comments
make loading animation prettier
update location bar URL
update history
change location URL when scrolling past posts from an offset
auto scroll to first new loaded post title?
logic if there is no loadingAnimationClass
DONE:
determine when scrolled to bottom
Get data from offset URL
Append items to contentWrapper
update URL of the previous post link for next get request
initialize squarespace blocks
add logic for when there are no more prev URLs
add loading animation
add logic for when there are no more next URLs
Option to choose between load more button and scroll to load
Load posts on item view as well? Or disable plugin on item? -- disabled on item view.
debounce ajax loading or make it smarter so it doesnt accidentally load the same request twice (simple flag)
*/
YUI.add('infinite-scroll', function (Y) {
Y.namespace('Squarespace').InfiniteScroll = Y.Base.create('infiniteScroll', Y.Base, [], {
initializer: function (config) {
this.bindUI();
},
destructor: function () {
},
/* Add Event listeners here */
bindUI: function () {
// this.scrolledToBottom();
// Defaults to load on scroll, unless config is set to load on prev post button click
if(!Y.one(this.get('contentWrapper'))){
console.log('not a blog page');
return false;
} else {
if(this.get('loadOnClick')){
Y.one(this.get('olderPostsLink')).on('click', Y.bind(function (e){
e.preventDefault();
// this.showLoadingAnimation();
this.getData();
}, this));
} else {
Y.on('scroll', Y.bind(this.scrolledToBottom, this));
}
}
},
/* Gate to stop getData() when there are no more offset pages */
morePagesToLoad: true,
/* Gate to stop ajax from firing too many times */
ajaxFired: false,
/* This function checks how far down the user has scrolled */
getScrollPosition: function (e) {
if(document.documentElement.scrollTop) {
return document.documentElement.scrollTop;
}
else {
return document.body.scrollTop;
}
},
/* This function checks the height of the visible portion of the document */
getVisibleDocHeight: function (e) {
if(document.documentElement.clientHeight) {
return document.documentElement.clientHeight;
}
else {
return document.body.clientHeight;
}
},
/* This function checks the total height of the document */
getTotalDocHeight: function (e) {
if (document.documentElement.offsetHeight) {
return document.documentElement.offsetHeight;
}
else {
return document.body.offsetHeight;
}
},
/* This function returns true if the user is at the very bottom of the document */
scrolledToBottom: function (e) {
if(this.getTotalDocHeight() == this.getScrollPosition() + this.getVisibleDocHeight()){
this.getData();
}
},
/* This function makes the AJAX call to the next blog page */
getData: function () {
var location = Y.config.win.location;
var request = Y.config.win.location.href.replace(location.href, Y.one(this.get('olderPostsLink')).getAttribute('href') + this.get('queryString'));
if(!this.ajaxFired){
this.ajaxFired = true;
// this is wrapped in Y.later so the user can set a delay on the animation (or none at all)
if(this.morePagesToLoad){
this.showLoadingAnimation();
Y.later( 1000 * this.get('ajaxDelay'), this, function(){
Y.Data.get({
url: request,
responseFormat: 'raw',
success: function(data) {
this.appendItems(data);
this.initSquarespaceBlocks();
this.updatePaginationUrl(data);
this.hideLoadingAnimation();
this.ajaxFired = false;
},
failure: function(error) {
console.log(error);
this.hideLoadingAnimation();
this.ajaxFired = false;
}
}, this);
}, [], false);
} else {
console.log('no more pages to load!');
this.ajaxFired = false;
return;
}
}
},
/* This function adds the data from the AJAX call to the DOM */
appendItems: function (data) {
var items = this.createDOM(data);
var wrapper = Y.one(this.get('contentWrapper'));
items.each(function (post){
wrapper.appendChild(post);
});
},
/* Grab articles from the document fragment */
createDOM: function (data) {
var dom = Y.DOM.create(data);
console.log(dom);
var blog = dom.querySelector(this.get('contentWrapper'));
blog.setAttribute('class', this.get('ajaxClass').replace(/\./g, ''));
var ajaxItems = dom.querySelector(this.get('ajaxClass'));
return Y.one(ajaxItems).get('children');
},
updatePaginationUrl: function (data) {
var dom = Y.DOM.create(data);
var olderPostsLink = dom.querySelector(this.get('olderPostsLink')).getAttribute('href');
if(!olderPostsLink) {
this.morePagesToLoad = false;
Y.one(this.get('olderPostsLink')).hide();
} else {
Y.one(this.get('olderPostsLink')).setAttribute('href', olderPostsLink);
}
},
initSquarespaceBlocks: function () {
Squarespace.afterBodyLoad();
Y.one(this.get('contentWrapper')).all('img[data-src]').each(function (el) {
ImageLoader.load(el, {
load: true
});
});
Y.one(this.get('contentWrapper')).all('.sqs-search-ui-text-input').each(function (search) {
if (!search.one('.yui3-widget')) {
new Y.Squarespace.Widgets.SearchPreview({
render: search
});
}
}, this);
},
showLoadingAnimation: function () {
if(this.morePagesToLoad) {
var loading = Y.Node.create('<div class="loading-container"><div class="' + this.get('loadingAnimationClass').replace(/\./g, '') + '"></div></div>');
if(Y.one(this.get('loadingAnimationClass'))){
Y.one(this.get('loadingAnimationClass')).remove(true);
// Y.one(this.get('contentWrapper')).append(loading);
Y.one('body').append(loading);
} else {
// Y.one(this.get('contentWrapper')).append(loading);
Y.one('body').append(loading);
}
}
},
hideLoadingAnimation: function () {
if(Y.one(this.get('loadingAnimationClass'))){
Y.one(this.get('loadingAnimationClass')).remove(true);
}
}
}, {
NS: 'InfiniteScroll',
ATTRS: {
contentWrapper: {
value: '#blogWrapper'
},
ajaxClass: {
value: '.ajax-items'
},
newerPostsLink: {
value: '#newer'
},
olderPostsLink: {
value: '#older'
},
queryString: {
value: '&format=main-content'
},
loadingAnimationClass: {
value: '.pulses'
},
ajaxDelay: {
value: 0
},
loadOnClick: {
value: false
}
}
},
'1.0',
{
requires: [
'base',
'node-base',
'plugin',
'event',
'node',
'node-event-delegate',
'anim',
'transition',
'io-base'
]
}
);
});