Blame view
resources/js/common/services/frInfiniteScrollCtrl.js
3.04 KB
e77200db5 Initial commit |
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 |
angular.module('fr.infiniteScrollCtrl', []) .service('infiniteScrollCtrl', ['$window', '$http', '$compile', function ($window, $http, $compile) { return function (scope, element) { try { scope.currentPage = parseInt($window.location.href.match(/\/page\/(\d+)/)[1], 10); } catch (e) { scope.currentPage = 1; } scope.loadMoreEnabled = true; scope.infiniteScrollDisabled = true; scope.pagesScrolled = 0; scope.enableInfiniteScroll = function () { scope.infiniteScrollDisabled = false; }; scope.infiniteScroll = function () { // Disable infinite scroll until we load the next page. scope.infiniteScrollDisabled = true; // Let others know that posts are loading. scope.postsLoading = true; scope.infiniteScrollError = null; // Load the page. $http.get(DataStore.postsEndpointUrl + '?page=' + (scope.currentPage + 1) + ((DataStore.currentStage) ? '&stageTree=' + DataStore.currentStage.tree : '') + ((DataStore.legendary) ? '&legendary=1' : '') + ((DataStore.search) ? '&q=' + encodeURIComponent(DataStore.search) : '') ) .then(function (response) { // Increment current page. scope.currentPage++; // Increment number of pages scrolled. scope.pagesScrolled++; // Append the newly loaded content. element.append($compile(response.data)(scope)); // If we did not scroll more than 15 pages. if (scope.pagesScrolled < 15) { // Re-enabled infinite scroll scope.infiniteScrollDisabled = false; // Disable "Load more" button. Assumes that the button is no longer needed because infinite scroll is now enabled. scope.loadMoreEnabled = false; } // If we scrolled more than 15 pages. else { // Re-enable "Load more" button. scope.loadMoreEnabled = true; // Reset pages scrolled count. scope.pagesScrolled = 0; } // Let everyone know that posts finished loading. scope.postsLoading = false; // Track the page view in Google analytics. ga('send', 'event', 'InfiniteScroll', 'Loaded', $window.location.href, scope.currentPage); // history.replaceState({}, '', '/page/' + scope.currentPage); }) .catch(function (response) { scope.postsLoading = false; if (response.status === 404) { scope.loadMoreEnabled = false; } if (response.status === 429) { // eslint-disable-next-line prefer-destructuring scope.infiniteScrollError = response.data.errors[0]; } }); }; }; }]); |