1/* 2 * jQuery.autopager v1.0.0 3 * 4 * Copyright (c) 2009 lagos 5 * Dual licensed under the MIT and GPL licenses. 6 * 7 * http://lagoscript.org 8 */ 9(function($) { 10 var window = this, options = {}, 11 content, currentUrl, nextUrl, 12 active = false, 13 defaults = { 14 autoLoad: true, 15 page: 1, 16 content: '.content', 17 link: 'a[rel=next]', 18 insertBefore: null, 19 appendTo: null, 20 start: function() {}, 21 load: function() {}, 22 disabled: false 23 }; 24 25 $.autopager = function(_options) { 26 var autopager = this.autopager; 27 28 if (typeof _options === 'string' && $.isFunction(autopager[_options])) { 29 var args = Array.prototype.slice.call(arguments, 1), 30 value = autopager[_options].apply(autopager, args); 31 32 return value === autopager || value === undefined ? this : value; 33 } 34 35 _options = $.extend({}, defaults, _options); 36 autopager.option(_options); 37 38 content = $(_options.content).filter(':last'); 39 if (content.length) { 40 if (!_options.insertBefore && !_options.appendTo) { 41 var insertBefore = content.next(); 42 if (insertBefore.length) { 43 set('insertBefore', insertBefore); 44 } else { 45 set('appendTo', content.parent()); 46 } 47 } 48 } 49 50 setUrl(); 51 52 return this; 53 }; 54 55 $.extend($.autopager, { 56 option: function(key, value) { 57 var _options = key; 58 59 if (typeof key === "string") { 60 if (value === undefined) { 61 return options[key]; 62 } 63 _options = {}; 64 _options[key] = value; 65 } 66 67 $.each(_options, function(key, value) { 68 set(key, value); 69 }); 70 return this; 71 }, 72 73 enable: function() { 74 set('disabled', false); 75 return this; 76 }, 77 78 disable: function() { 79 set('disabled', true); 80 return this; 81 }, 82 83 destroy: function() { 84 this.autoLoad(false); 85 options = {}; 86 content = currentUrl = nextUrl = undefined; 87 return this; 88 }, 89 90 autoLoad: function(value) { 91 return this.option('autoLoad', value); 92 }, 93 94 load: function() { 95 if (active || !nextUrl || options.disabled) { 96 return; 97 } 98 99 active = true; 100 options.start(currentHash(), nextHash()); 101 $.get(nextUrl, insertContent); 102 return this; 103 } 104 105 }); 106 107 function set(key, value) { 108 switch (key) { 109 case 'autoLoad': 110 if (value && !options.autoLoad) { 111 $(window).scroll(loadOnScroll); 112 } else if (!value && options.autoLoad) { 113 $(window).unbind('scroll', loadOnScroll); 114 } 115 break; 116 case 'insertBefore': 117 if (value) { 118 options.appendTo = null; 119 } 120 break 121 case 'appendTo': 122 if (value) { 123 options.insertBefore = null; 124 } 125 break 126 } 127 options[key] = value; 128 } 129 130 function setUrl(context) { 131 currentUrl = nextUrl || window.location.href; 132 nextUrl = $(options.link, context).attr('href'); 133 } 134 135 function loadOnScroll() { 136 if (content.offset().top + content.height() < $(document).scrollTop() + $(window).height()) { 137 $.autopager.load(); 138 } 139 } 140 141 function insertContent(res) { 142 var _options = options, 143 nextPage = $('<div/>').append(res.replace(/<script(.|\s)*?\/script>/g, "")), 144 nextContent = nextPage.find(_options.content); 145 146 set('page', _options.page + 1); 147 setUrl(nextPage); 148 if (nextContent.length) { 149 if (_options.insertBefore) { 150 nextContent.insertBefore(_options.insertBefore); 151 } else { 152 nextContent.appendTo(_options.appendTo); 153 } 154 _options.load.call(nextContent.get(), currentHash(), nextHash()); 155 content = nextContent.filter(':last'); 156 } 157 active = false; 158 } 159 160 function currentHash() { 161 return { 162 page: options.page, 163 url: currentUrl 164 }; 165 } 166 167 function nextHash() { 168 return { 169 page: options.page + 1, 170 url: nextUrl 171 }; 172 } 173})(jQuery);
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.