From b6deb8dca5023c13b508a9c6a63817f723ee87ab Mon Sep 17 00:00:00 2001 From: Bob Lail and Matthew Kobs Date: Wed, 16 Mar 2016 16:22:43 -0500 Subject: [PATCH] [skip] Added Neat.Renderer.InfiniteReveal (48m) --- vendor/assets/javascripts/neat.js | 2 + .../neat/lib/scroll_handler.coffee | 33 +++++++++++++++++ .../neat/renderer/infinite_reveal.coffee | 37 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 vendor/assets/javascripts/neat/lib/scroll_handler.coffee create mode 100644 vendor/assets/javascripts/neat/renderer/infinite_reveal.coffee diff --git a/vendor/assets/javascripts/neat.js b/vendor/assets/javascripts/neat.js index 5dc2469..de50476 100644 --- a/vendor/assets/javascripts/neat.js +++ b/vendor/assets/javascripts/neat.js @@ -9,8 +9,10 @@ //= require ./neat/lib/observer //= require ./neat/lib/paginated_list //= require ./neat/lib/jquery_extensions +//= require ./neat/lib/scroll_handler //= require ./neat/renderer/basic //= require ./neat/renderer/paginated +//= require ./neat/renderer/infinite_reveal //= require ./neat/collection_editor //= require ./neat/model_editor diff --git a/vendor/assets/javascripts/neat/lib/scroll_handler.coffee b/vendor/assets/javascripts/neat/lib/scroll_handler.coffee new file mode 100644 index 0000000..5eb9ab6 --- /dev/null +++ b/vendor/assets/javascripts/neat/lib/scroll_handler.coffee @@ -0,0 +1,33 @@ +class @Neat.ScrollHandler + _.extend @prototype, Backbone.Events + + constructor: (options={})-> + @$viewPort = $(options.viewPort ? window) + @$document = $(document) + @$viewPort.scroll _.bind(@_windowWasScrolled, @) + + _viewportHeight: -> + @$viewPort.height() + + _documentHeight: -> + if $.isWindow @$viewPort[0] + @$document.height() + else + @$viewPort[0].scrollHeight + + _windowWasScrolled: -> + viewportHeight = @_viewportHeight() + documentHeight = @_documentHeight() + + scrollTop = @$viewPort.scrollTop() + scrollMax = documentHeight - viewportHeight + progress = scrollTop / scrollMax + distanceFromBottom = scrollTop - scrollMax + + return if scrollMax <= 0 + + @trigger "scroll", + scrollTop: scrollTop + scrollMax: scrollMax + progress: progress + distanceFromBottom: distanceFromBottom diff --git a/vendor/assets/javascripts/neat/renderer/infinite_reveal.coffee b/vendor/assets/javascripts/neat/renderer/infinite_reveal.coffee new file mode 100644 index 0000000..64aab2e --- /dev/null +++ b/vendor/assets/javascripts/neat/renderer/infinite_reveal.coffee @@ -0,0 +1,37 @@ +class window.Neat.Renderer.InfiniteReveal extends window.Neat.Renderer.Basic + + constructor: (@view, @collection, @options) -> + super + @scrollHandler = new Neat.ScrollHandler() + @scrollHandler.on "scroll", _.bind(@_windowHasBeenScrolled, @) + + @sensitivity = @options.sensitivity ? 5 + @pageSize = @options.pageSize ? 50 + + + + _render: -> + @_setMaxItems @pageSize + super + + _visibleModels: -> + @collection.models[0...@maxItems] + + _loadMore: -> + Neat.logger.log "[Neat.Renderer.InfiniteReveal] loading..." + @_setMaxItems @maxItems + @pageSize + @_renderVisibleModels() + + _thereIsMore: -> + @collection.length > @maxItems + + + + _setMaxItems: (value) -> + @maxItems = Math.min value, @collection.length + + _windowHasBeenScrolled: (e) -> + return unless @_thereIsMore() + return unless Math.abs(e.distanceFromBottom) <= @sensitivity + return unless @view.$el.is(':visible') + @_loadMore()