class Lazypager constructor: (options) -> @options = debug: true container: "#catalog" navSelector: "a.infinite-loader" nextSelector: "a.infinite-loader:last" blockSelector: ".item" img: "" imgClass: 'infinite-loader-img' callback: (elements) -> $.extend(@options, options) @setObjects() setObjects: -> @url = $(@options.nextSelector).attr('href') @container = $(@options.container) @paginator = $(@options.navSelector) @image = $(@options.img).addClass(@options.imgClass) return if @paginator.size() < 1 @visible = false self = @ $(window).scroll -> if self.paginatorVisible() self.visibleHandler() unless self.visible visibleHandler: -> @visible = true @paginator.replaceWith(@image) @loadElements() paginatorVisible: -> return false if @paginator.size() < 1 docViewTop = $(window).scrollTop() docViewBottom = docViewTop + $(window).height() elemTop = @paginator.offset().top elemBottom = elemTop + @paginator.height() (elemBottom <= docViewBottom) and (elemTop >= docViewTop) loadElements: -> self = @ $.ajax( dataType: "text" url: self.url ).done (data) -> $("img." + self.options.imgClass).remove() elements = $(data).find(self.options.blockSelector) paginator = $(data).find(self.options.navSelector) self.options.callback(elements) self.container.append elements self.container.parent().append paginator self.setObjects() window.Lazypager ||= Lazypager