|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +InfiniteScroll = {}; |
| 4 | + |
| 5 | +var CONTAINER; |
| 6 | +var LOADING_TEMPLATE_NAME; |
| 7 | + |
| 8 | +/** |
| 9 | + * Triggers 'triggerInfiniteLoad' event when the user has scrolled |
| 10 | + * to the trigger point. |
| 11 | + */ |
| 12 | +function triggerLoadMore() { |
| 13 | + if ($('.infinite-load-more').isAlmostVisible()) { |
| 14 | + $(document).trigger('triggerInfiniteLoad'); |
| 15 | + } |
| 16 | +} |
| 17 | +InfiniteScroll.triggerLoadMore = triggerLoadMore; |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * jQuery plugin to determine whether an element is "almost visible". |
| 22 | + * @return {Boolean} |
| 23 | + */ |
| 24 | +jQuery.fn.isAlmostVisible = function jQueryIsAlmostVisible() { |
| 25 | + if (this.length === 0) { |
| 26 | + return; |
| 27 | + } |
| 28 | + var rect = this[0].getBoundingClientRect(); |
| 29 | + |
| 30 | + return ( |
| 31 | + rect.top >= 0 && |
| 32 | + rect.left >= 0 && |
| 33 | + rect.bottom <= (jQuery(CONTAINER).height() * 1.5) && |
| 34 | + rect.right <= jQuery(CONTAINER).width() |
| 35 | + ); |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Enable infinite scrolling on a template. |
| 40 | + */ |
| 41 | +Blaze.TemplateInstance.prototype.infiniteScroll = function infiniteScroll(options) { |
| 42 | + var tpl = this, _defaults, collection, subManagerCache, limit, subscriber, loadMore; |
| 43 | + |
| 44 | + /* |
| 45 | + * Create options from defaults |
| 46 | + */ |
| 47 | + _defaults = { |
| 48 | + // How many results to fetch per "page" |
| 49 | + perPage: 10, |
| 50 | + // The query to use when fetching our collection |
| 51 | + query: {}, |
| 52 | + // The sorting instructions for MongoDB |
| 53 | + sort: {}, |
| 54 | + // The subscription manager to use (optional) |
| 55 | + subManager: null, |
| 56 | + // Collection to use for counting the amount of results |
| 57 | + collection: null, |
| 58 | + // Publication to subscribe to, if null will use {collection}Infinite |
| 59 | + publication: null, |
| 60 | + // Container will use to scroll |
| 61 | + container: window |
| 62 | + }; |
| 63 | + options = _.extend({}, _defaults, options); |
| 64 | + |
| 65 | + // Validate the options |
| 66 | + check(options.perPage, Number); |
| 67 | + check(options.collection, String); |
| 68 | + check(options.publication, String); |
| 69 | + check(options.container,String); |
| 70 | + check(options.loadingTemplateName, Match.Optional(String)); |
| 71 | + |
| 72 | + CONTAINER = options.container || _defaults.container; |
| 73 | + LOADING_TEMPLATE_NAME = options.loadingTemplateName; |
| 74 | + |
| 75 | + //using collection instances package here to scan all collection and checks ours exists |
| 76 | + //may be a more elegant packageless solution but coudn't find anything |
| 77 | + let collectionExists = Meteor.Collection.getAll().find(c => c.name === options.collection); |
| 78 | + // Collection exists? |
| 79 | + if (!collectionExists) { |
| 80 | + throw new Error('Collection does not exist: ', options.collection); |
| 81 | + } else { |
| 82 | + //set collection to cursor. collectionExists.name evaluates to a string |
| 83 | + collection = collectionExists.instance; |
| 84 | + } |
| 85 | + |
| 86 | + // Generate the publication name if one hasn't been provided |
| 87 | + if(!options.publication){ |
| 88 | + options.publication = options.collection + 'Infinite'; |
| 89 | + } |
| 90 | + |
| 91 | + // If we are using a subscription manager, cache the limit variable with the subscription |
| 92 | + if(options.subManager){ |
| 93 | + // Create the cache object if it doesn't exist |
| 94 | + if(!options.subManager._infinite){ |
| 95 | + options.subManager._infinite = {}; |
| 96 | + options.subManager._infinite[options.publication] = {}; |
| 97 | + } |
| 98 | + subManagerCache = options.subManager._infinite[options.publication]; |
| 99 | + } |
| 100 | + |
| 101 | + // We use 'limit' so that Meteor can continue to use the OpLogObserve driver |
| 102 | + // See: https://github.com/meteor/meteor/wiki/Oplog-Observe-Driver |
| 103 | + // (There are a few types of queries that still use PollingObserveDriver) |
| 104 | + limit = new ReactiveVar(); |
| 105 | + |
| 106 | + // If the query changes, the limit must reset |
| 107 | + if(options.query instanceof ReactiveVar){ |
| 108 | + tpl.autorun(function(){ |
| 109 | + options.query.get(); |
| 110 | + limit.set(options.perPage); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + // Retrieve the initial page size |
| 115 | + if(subManagerCache && subManagerCache.limit){ |
| 116 | + limit.set(subManagerCache.limit); |
| 117 | + }else{ |
| 118 | + limit.set(options.perPage); |
| 119 | + } |
| 120 | + |
| 121 | + // Create subscription to the collection |
| 122 | + tpl.autorun(function() { |
| 123 | + // Rerun when the limit changes |
| 124 | + var lmt = limit.get(); |
| 125 | + |
| 126 | + // If a Subscription Manager has been supplied, use that instead to create |
| 127 | + // the subscription. This is useful if you want to keep the subscription |
| 128 | + // loaded for multiple templates. |
| 129 | + if(options.subManager){ |
| 130 | + subscriber = options.subManager; |
| 131 | + // Save the limit in the subscription manager so we can look it up later |
| 132 | + subManagerCache.limit = lmt; |
| 133 | + }else{ |
| 134 | + subscriber = tpl; |
| 135 | + } |
| 136 | + |
| 137 | + var query; |
| 138 | + if(options.query instanceof ReactiveVar){ |
| 139 | + query = options.query.get(); |
| 140 | + }else{ |
| 141 | + query = options.query; |
| 142 | + } |
| 143 | + |
| 144 | + var sort; |
| 145 | + if(options.sort instanceof ReactiveVar){ |
| 146 | + sort = options.sort.get(); |
| 147 | + }else{ |
| 148 | + sort = options.sort; |
| 149 | + } |
| 150 | + |
| 151 | + tpl.infiniteSub = subscriber.subscribe(options.publication, lmt, query, sort, null); |
| 152 | + }); |
| 153 | + |
| 154 | + // Check to see if we need to load more |
| 155 | + tpl.autorun(function(){ |
| 156 | + if(tpl.infiniteSub.ready()){ |
| 157 | + Tracker.afterFlush(triggerLoadMore); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + |
| 162 | + /** |
| 163 | + * Load more results if our limit is below the total |
| 164 | + */ |
| 165 | + loadMore = function() { |
| 166 | + |
| 167 | + var query; |
| 168 | + if(options.query instanceof ReactiveVar){ |
| 169 | + query = options.query.get(); |
| 170 | + }else{ |
| 171 | + query = options.query; |
| 172 | + } |
| 173 | + |
| 174 | + var lmt = limit.get(), results = collection.find(query).count(); |
| 175 | + if(results >= lmt){ |
| 176 | + limit.set(lmt + options.perPage); |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + // Trigger loadMore when we've scrolled/resized close to revealing .load-more |
| 181 | + $(document).off('triggerInfiniteLoad'); |
| 182 | + $(document).on('triggerInfiniteLoad', loadMore); |
| 183 | +}; |
| 184 | + |
| 185 | +/** |
| 186 | + * Attempt to trigger infinite loading when resize and scroll browser |
| 187 | + * events are fired. |
| 188 | + */ |
| 189 | +Template.infiniteScroll.onCreated(function () { |
| 190 | + $(CONTAINER).on('resize scroll', _.throttle(triggerLoadMore, 500)); |
| 191 | +}); |
| 192 | + |
| 193 | +Template.infiniteScroll.helpers({ |
| 194 | + |
| 195 | + loading: function(){ |
| 196 | + |
| 197 | + // Loop through parent templates until we find infiniteSub |
| 198 | + var tpl = Template.instance(); |
| 199 | + while(!tpl.infiniteSub){ |
| 200 | + var parent = tpl.parents(); |
| 201 | + if(!parent){ |
| 202 | + break; |
| 203 | + } |
| 204 | + tpl = parent; |
| 205 | + } |
| 206 | + |
| 207 | + return !tpl.infiniteSub.ready(); |
| 208 | + }, |
| 209 | + loadingTemplateName: function () { |
| 210 | + return LOADING_TEMPLATE_NAME; |
| 211 | + } |
| 212 | + |
| 213 | +}); |
0 commit comments