diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-22 17:07:50 +0200 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-22 17:07:50 +0200 |
commit | 7bcdc3d062996918c8cb59bf827a3db3229b8544 (patch) | |
tree | b5cc78bffab0993d0778a1977da4b233ece556f7 /js/directive/NewsScroll.js | |
parent | 3108dba268295461354a5756dbce58d8b6fb7121 (diff) |
make code more readable
Diffstat (limited to 'js/directive/NewsScroll.js')
-rw-r--r-- | js/directive/NewsScroll.js | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/js/directive/NewsScroll.js b/js/directive/NewsScroll.js index 6638c36b0..043235bbf 100644 --- a/js/directive/NewsScroll.js +++ b/js/directive/NewsScroll.js @@ -11,10 +11,10 @@ app.directive('newsScroll', ($timeout) => { 'use strict'; // autopaging - let autoPage = (enabled, limit, items, callback) => { + let autoPage = (enabled, limit, callback, elem) => { if (enabled) { let counter = 0; - for (let item of reverse(items.find('.feed_item'))) { + for (let item of reverse(elem.find('.feed_item'))) { item = $(item); @@ -39,16 +39,15 @@ app.directive('newsScroll', ($timeout) => { }; // mark read - let markRead = (enabled, items, callback) => { + let markRead = (enabled, callback, elem) => { if (enabled) { let ids = []; - let unreadItems = items.find('.feed_item:not(.read)'); - for (let item of unreadItems) { + for (let item of elem.find('.feed_item:not(.read)')) { item = $(item); if (item.position().top <= -50) { - ids.push(parseInt($(item).data('id'), 10)); + ids.push(parseInt(item.data('id'), 10)); } else { break; } @@ -63,57 +62,50 @@ app.directive('newsScroll', ($timeout) => { scope: { 'newsScrollAutoPage': '&', 'newsScrollMarkRead': '&', - 'newsScrollDisabledMarkRead': '=', - 'newsScrollDisabledAutoPage': '=', + 'newsScrollEnabledMarkRead': '=', + 'newsScrollEnableAutoPage': '=', 'newsScrollMarkReadTimeout': '@', // optional, defaults to 1 second 'newsScrollTimeout': '@', // optional, defaults to 1 second - 'newsScrollAutoPageWhenLeft': '@', // optional, defaults to 50 - 'newsScrollItemsSelector': '@' // optional, defaults to .items + 'newsScrollAutoPageWhenLeft': '@' // optional, defaults to 50 }, link: (scope, elem) => { - let scrolling = false; + let allowScroll = true; scope.newsScrollTimeout = scope.newsScrollTimeout || 1; scope.newsScrollMarkReadTimeout = scope.newsScrollMarkReadTimeout || 1; scope.newsScrollAutoPageWhenLeft = scope.newsScrollAutoPageWhenLeft || 50; - scope.newsScrollItemsSelector = - scope.newsScrollItemsSelector || '.items'; - - elem.on('scroll', () => { - - // only allow one scrolling event to trigger - if (!scrolling) { - scrolling = true; + let scrollHandler = () => { + // allow only one scroll event to trigger at once + if (allowScroll) { + allowScroll = false; $timeout(() => { - scrolling = false; + allowScroll = true; }, scope.newsScrollTimeout*1000); - - let items = $(scope.newsScrollItemsSelector); - - // autopaging - autoPage(!scope.newsScrollDisabledAutoPage, + autoPage(scope.newsScrollEnableAutoPage, scope.newsScrollAutoPageWhenLeft, - items, scope.newsScrollAutoPage); - - + scope.newsScrollAutoPage, + elem); // allow user to undo accidental scroll $timeout(() => { - markRead(!scope.newsScrollDisabledMarkRead, items, - scope.newsScrollMarkRead); + markRead(scope.newsScrollEnabledMarkRead, + scope.newsScrollMarkRead, + elem); }, scope.newsScrollMarkReadTimeout*1000); } }); + elem.on('scroll', scrollHandler); + // remove scroll handler if element is destroyed scope.$on('$destroy', () => { - elem.off('scroll'); + elem.off('scroll', scrollHandler); }); } }; |