-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery.perpetualposts.js
More file actions
80 lines (70 loc) · 1.99 KB
/
jquery.perpetualposts.js
File metadata and controls
80 lines (70 loc) · 1.99 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
/* jshint browser: true */
/* global console: true, jQuery: true */
(function ($) {
'use strict';
$.fn.perpetualPosts = function (options) {
if (!options) {
options = {};
}
var selector = options.selector || this.selector;
var callback = options.callback;
return this.each(function (index, anchor) {
var $anchor = $(anchor);
$anchor.click(function (event) {
$.get($anchor[0].href, function (data) {
// Create new content container.
var $content = $('<div class="amp-eternal-article-content"></div>');
var $data;
$content.hide();
// Process callback for developer manipulation of data.
if (callback) {
$data = callback(data);
} else {
$data = $(data);
}
var $script = $data.find('script');
if ($script.length) {
window.console && console.log('jQuery.perpetualPosts warning: <script> tags in HTML may cause you pain.');
}
// Insert new content after anchor.
$content.append($data);
$anchor.after($content);
$content.fadeIn();
// Rig up sonar to update push state.
if (options.sonar) {
$content.on('scrollin', function () {
history.pushState({}, $anchor.text(), $anchor[0].href);
});
}
// Find next URL and update anchor.
$anchor = $data.find(selector);
if ($anchor.length) {
$anchor.perpetualPosts(options);
$anchor.insertAfter($content);
} else {
throw 'Next jQuery.perpetualPosts anchor (' + selector + ') not found in data.';
}
});
event.preventDefault();
});
if (options.sonar) {
// Hide the anchor but ensure it still has layout.
$anchor.css({
height: 0,
display: 'block',
overflow: 'hidden'
});
// jQuery.sonar plugin
// https://github.com/artzstudio/jQuery-Sonar
if ($.sonar) {
$anchor.on('scrollin', function () {
$anchor.off('scrollin');
$anchor.click();
});
} else {
throw 'jQuery Sonar plugin not found.';
}
}
});
};
}(jQuery));