-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajaxify.js
135 lines (127 loc) · 4.56 KB
/
ajaxify.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
/*==============================================================================
___ _ _ _
/ || | | | | |
\__ | | | | | | __
/ |/ |/_) |/ / \_/\/
\___/|__/| \_/|__/\__/ /\_/
|\
|/
Ajaxify v1.0
https://github.com/Elkfox/Ajaxify
Copyright (c) 2017 Elkfox Co Pty Ltd
https://elkfox.com
Project lead: George Butter
MIT License
==============================================================================*/
ajaxify = function(settings) {
settings = settings || {}
// Change the default selectors here
var linkParent = settings.linkParent || '.pagination' // Class of pagination container
var parentContainer = settings.parentContainer || '#MainContent' // All of the content selector used to detect scroll bottom distance
var endlessScrollContainer = settings.endlessScrollContainer || '.EndlessScroll' // Selector for endless scroll pages
var endlessClickContainer = settings.endlessClickContainer || '.EndlessClick' // Class of pagination container
var endlessOffset = settings.endlessOffset || 0 // Offset the distance from the bottom of the page
var ajaxinateContainer = settings.ajaxinateContainer || '.Ajaxinate' // ID Selector for ajax pagination pages
var ajaxinateLink = settings.ajaxinateLink || '.page a' // Class Selector for ajax pagination links
var fade = settings.fade || 'fast' // fade speed
var textChange = settings.textChange || 'Loading' // Text whilst loading content
var linkElem;
var contentContainer;
var pageNumber;
var pageType;
var action;
var moreURL;
$.loadMore = function() {
if (moreURL.length){
$.ajax({
type: 'GET',
dataType: 'html',
url: moreURL,
success: function(data) {
if (pageType == 'ajax') {
$(ajaxinateContainer).not('[data-page="'+pageNumber+'"]').hide();
history.pushState({}, pageNumber, moreURL);
} else {
$(linkElem).fadeOut(fade);
}
var filteredData = $(data).find(contentContainer);
$(filteredData).appendTo( $(contentContainer).parent() ).hide().fadeIn(fade);
if (pageType == 'endlessScroll') {
$.endlessScroll();
} else if (pageType == 'ajax') {
$.ajaxinationClick();
} else if (pageType == 'endlessClick') {
$.endlessClick();
}
}
});
}
}
// Check whether the page is at the bottom
$.endlessScroll = function() {
action = 'scroll load resize';
$(window).on( action, function() {
contentContainer = endlessScrollContainer;
moreURL = $(contentContainer+':last-of-type '+linkElem).attr('href');
pageType = 'endlessScroll';
$(linkElem).text(textChange);
if ($(contentContainer+':last-of-type '+linkElem).length){
var bottom = $( parentContainer ).outerHeight();
var docTop = ($(document).scrollTop() + $(window).height() + endlessOffset);
if( docTop > bottom ) {
$(window).off(action);
$.loadMore();
}
}
});
}
//Endless click function
$.endlessClick = function() {
$(linkElem).on( 'click', function(e) {
e.preventDefault();
action = 'click';
contentContainer = endlessClickContainer;
moreURL = $(this).attr('href');
pageType = 'endlessClick';
$(linkElem).text(textChange);
$(linkElem).off(action);
$.loadMore();
});
}
//Ajaxination click function
$.ajaxinationClick = function() {
$(linkElem).on( 'click', function(e) {
e.preventDefault();
action = 'click';
contentContainer = ajaxinateContainer;
moreURL = $(this).attr('href');
pageNumber = $(this).attr('data-number');
pageType = 'ajax';
if( $(contentContainer+'[data-page="'+pageNumber+'"]').length ) {
$(contentContainer).not('[data-page="'+pageNumber+'"]').hide();
$(contentContainer+'[data-page="'+pageNumber+'"]').fadeIn(fade);
history.pushState({}, pageNumber, moreURL);
} else {
$(linkElem).off(action);
$.loadMore();
}
$('html, body').animate({ scrollTop: $(parentContainer).offset().top }, 300 );
});
}
// Detect whether the pagination types exist
// Endless Click Initialize
if ( $( endlessClickContainer ).length ) {
linkElem = linkParent+' a';
$.endlessClick();
}
// Ajaxination Click Initialize
if ( $( ajaxinateContainer ).length ) {
linkElem = ajaxinateLink;
$.ajaxinationClick();
}
// Endless Scroll Initialize
if ( $( endlessScrollContainer ).length ) {
linkElem = linkParent+' a';
$.endlessScroll();
}
}