1/** 2 * jquery.clever-infinite-scroll.js 3 * Working with jQuery 2.1.4 4 * 5 * Twitter: @watarutwt 6 * GitHub: @wataruoguchi 7 */ 8/* global define, require, history, window, document, location */ 9(function(root, factory) { 10 "use strict"; 11 if (typeof define === "function" && define.amd) { 12 define(["jquery"], factory); 13 } else if (typeof exports === "object") { 14 factory(require("jquery")); 15 } else { 16 factory(root.jQuery); 17 } 18})(this, function($) { 19 "use strict"; 20 /** 21 * Elements it reffers. Each page must has those selectors. 22 * The structure must be same as article1.html 23 * #contentsWrapper, .content, #next 24 */ 25 $.fn.cleverInfiniteScroll = function(options) { 26 /** 27 * Settings 28 */ 29 var windowHeight = (typeof window.outerHeight !== "undefined") ? Math.max(window.outerHeight, $(window).height()) : $(window).height(), 30 defaults = { 31 contentsWrapperSelector: "#contentsWrapper", 32 contentSelector: ".content", 33 nextSelector: "#next", 34 loadImage: "", 35 offset: windowHeight, 36 }, 37 settings = $.extend(defaults, options); 38 39 /** 40 * Private methods 41 */ 42 var generateHiddenSpans = function(_title, _path) { 43 return "<span class='hidden-title' style='display:none'>" + _title + "</span><span class='hidden-url' style='display:none'>" + _path + "</span>"; 44 }, 45 setTitleAndHistory = function(_title, _path) { 46 // Set history 47 history.replaceState(null, _title, _path); 48 // Set title 49 $("title").html(_title); 50 }, 51 changeTitleAndURL = function(_value) { 52 // value is an element of a content user is seeing 53 // Get title and path of the article page from hidden span elements 54 var title = $(_value).children(".hidden-title:first").text(), 55 path = $(_value).children(".hidden-url:first").text(); 56 if ($("title").text() !== title) { 57 // If it has changed 58 $(settings.contentSelector).removeClass("active"); 59 $(_value).addClass("active"); 60 setTitleAndHistory(title, path); 61 $(document).trigger('clever-infinite-scroll-url-change', [title, path]); 62 } 63 }; 64 65 /** 66 * Initialize 67 */ 68 // Get current page's title and URL. 69 var title = $("title").text(), 70 path = $(location).attr("href"), 71 documentHeight = $(document).height(), 72 threshold = settings.offset, 73 $contents = $(settings.contentSelector); 74 // Set hidden span elements and history 75 $(settings.contentSelector + ":last").append(generateHiddenSpans(title, path)); 76 $(settings.contentSelector).addClass("active"); 77 setTitleAndHistory(title, path); 78 79 /** 80 * scroll 81 */ 82 var lastScroll = 0, 83 currentScroll; 84 $(window).scroll(function() { 85 // Detect where you are 86 window.clearTimeout($.data("this", "scrollTimer")); 87 $.data(this, "scrollTimer", window.setTimeout(function() { 88 // Get current scroll position 89 currentScroll = $(window).scrollTop(); 90 91 // Detect whether it's scrolling up or down by comparing current scroll location and last scroll location 92 if (currentScroll > lastScroll) { 93 // If it's scrolling down 94 $contents.each(function(key, value) { 95 if ($(value).offset().top + $(value).height() > currentScroll) { 96 // Change title and URL 97 changeTitleAndURL(value); 98 // Quit each loop 99 return false; 100 } 101 }); 102 } else if (currentScroll < lastScroll) { 103 // If it's scrolling up 104 $contents.each(function(key, value) { 105 if ($(value).offset().top + $(value).height() - windowHeight / 2 > currentScroll) { 106 // Change title and URL 107 changeTitleAndURL(value); 108 // Quit each loop 109 return false; 110 } 111 }); 112 } else { 113 // When currentScroll == lastScroll, it does not do anything because it has not been scrolled. 114 } 115 // Renew last scroll position 116 lastScroll = currentScroll; 117 }, 200)); 118 119 if ($(window).scrollTop() + windowHeight + threshold >= documentHeight) { 120 // If scrolling close to the bottom 121 122 // Getting URL from settings.nextSelector 123 var $url = [$(settings.nextSelector).attr("href")]; 124 $(settings.nextSelector).remove(); 125 if ($url[0] !== undefined) { 126 // If the page has link, call ajax 127 if (settings.loadImage !== "") { 128 $(settings.contentsWrapperSelector).append("<img src='" + settings.loadImage
128+ "' id='cis-load-img'>"); 129 } 130 $.ajax({ 131 url: $url[0], 132 dataType: "html", 133 success: function(res) { 134 // Get title and URL 135 title = $(res).filter("title").text(); 136 path = $url[0]; 137 // Set hidden span elements and history 138 $(settings.contentsWrapperSelector).append($(res).find(settings.contentSelector).append(generateHiddenSpans(title, path))); 139 if ($(res).find(settings.contentSelector).find(settings.nextSelector).length === 0) { 140 //If there is no nextSelector in the contentSelector, get next Slecter from response and append it. 141 $(settings.contentsWrapperSelector).append($(res).find(settings.nextSelector)); 142 } 143 documentHeight = $(document).height(); 144 $contents = $(settings.contentSelector); 145 $("#cis-load-img").remove(); 146 $(document).trigger('clever-infinite-scroll-content-loaded'); 147 } 148 }); 149 } 150 } 151 }); //scroll 152 153 return (this); 154 }; //$.fn.cleverInfiniteScroll 155}); 156 157jQuery(document).ready(function($) { 158 159 $('.infinite-content').cleverInfiniteScroll({ 160 contentsWrapperSelector: '.infinite-container', 161 contentSelector: '.infinite-post', 162 nextSelector: '.navigation a:first' 163 }); 164 jQuery('a[href^="http://"]').not('a[href*="thechalkboardmag"]').attr('target','_blank'); 165 jQuery('a[href^="https://"]').not('a[href*="thechalkboardmag"]').attr('target','_blank'); 166});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.