-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfinite.js
More file actions
156 lines (139 loc) · 4.76 KB
/
Infinite.js
File metadata and controls
156 lines (139 loc) · 4.76 KB
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
(function ($) {
$.fn.InfiniteJS = function (args) {
var counter = 0;
var selectorHeight = {
top: 0,
bottom: 0
};
var options = $.extend({
debug: false,
limit: {
start: 0,
total: 10
},
repeat: {
finish: 10,
loadMore: 5,
showLoadMore: true
},
navSelector: null,
nextSelector: null,
loadSelector: null
}, args);
var objects = {
loadSelector: null
};
var isLoading = false;
var isLoadedMoreButton = false;
this.init(function () {
_hideLoadMoreButton();
if (options.limit.items < options.limit.total) {
_log.warn("Infinite.js doesn't init!");
_log.error('REASON: The number of items is not enough.');
return;
}
_initScroll();
});
_getSelector = function () {
return $(options.navSelector);
};
_getSelectorButton = function () {
return _getSelector().find('a');
};
_getLoadSelector = function () {
if (objects.loadSelector === null) {
objects.loadSelector = $(options.loadSelector);
}
return objects.loadSelector;
};
_initScroll = function () {
_calculateSelector();
$(window).scroll(function () {
if (counter === options.repeat.finish) {
_log.warn('Number of repetitions maximum.');
return;
}
var windowScroll = $(window).scrollTop();
var windowHeight = $(window).height();
var targetHeight = (_getSelector().offset().top - windowHeight) - 100;
if (! isLoadedMoreButton && windowScroll >= targetHeight && isLoading === false) {
isLoading = true;
if (options.repeat.showLoadMore && counter === options.repeat.loadMore) {
_log.log('Display block load more button');
_showLoadMoreButton();
} else {
_doRequest(null, '_appendTarget');
}
}
});
};
_doRequest = function (beforeCallback, afterCallback, extra) {
counter++;
$.get(_getRequestUrl(), function () {
if (counter === options.repeat.finish) {
_hideLoadMoreButton();
}
if (typeof window[beforeCallback] === 'function') {
window[beforeCallback](extra);
}
}).done(function (response) {
if (typeof window[afterCallback] === 'function') {
window[afterCallback](response, extra);
}
_finalProcess();
});
};
_finalProcess = function () {
_calculateSelector();
isLoading = false;
};
_getRequestUrl = function () {
return _getSelectorButton().data('url')
+ '?limit=' + (counter * options.limit.total)
+ '&' + _getSelectorButton().data('url-append');
};
_calculateSelector = function () {
_setSelectorHeight(_getSelector().offset().top);
};
_appendTarget = function (response) {
_getLoadSelector().before(response);
};
_showLoadMoreButton = function () {
isLoadedMoreButton = true;
_getSelectorButton().show();
_onClickButton();
};
_hideLoadMoreButton = function () {
_getSelectorButton().hide();
};
_onClickButton = function () {
_getSelectorButton().on('click', function () {
if (counter < options.repeat.finish) {
_doRequest(null, '_appendTarget');
}
});
};
_setSelectorHeight = function (top) {
selectorHeight.top = top;
};
_log = {
info: function (message) {
this.logPrint('info', message);
},
log: function (message) {
this.logPrint('log', message);
},
warn: function (message) {
this.logPrint('warn', message);
},
error: function (message) {
this.logPrint('error', message);
},
logPrint: function (type, message) {
if (options.debug) {
console[type](message);
}
}
};
};
}(jQuery));