BigW Consortium Gitlab

pager.js.coffee 995 Bytes
Newer Older
Dmitriy Zaporozhets committed
1
@Pager =
2
  init: (@limit = 0, preload, @disable = false, @callback = $.noop) ->
3 4
    @loading = $('.loading').first()

Dmitriy Zaporozhets committed
5 6 7 8
    if preload
      @offset = 0
      @getOld()
    else
9
      @offset = @limit
Dmitriy Zaporozhets committed
10 11 12
    @initLoadMore()

  getOld: ->
13
    @loading.show()
Dmitriy Zaporozhets committed
14 15
    $.ajax
      type: "GET"
16
      url: $(".content_list").data('href') || location.href
Dmitriy Zaporozhets committed
17
      data: "limit=" + @limit + "&offset=" + @offset
18 19
      complete: =>
        @loading.hide()
20 21
      success: (data) ->
        Pager.append(data.count, data.html)
22
        Pager.callback()
23
      dataType: "json"
Dmitriy Zaporozhets committed
24 25 26 27 28 29 30 31 32

  append: (count, html) ->
    $(".content_list").append html
    if count > 0
      @offset += count
    else
      @disable = true

  initLoadMore: ->
33
    $(document).unbind('scroll')
Dmitriy Zaporozhets committed
34 35 36 37 38 39 40
    $(document).endlessScroll
      bottomPixels: 400
      fireDelay: 1000
      fireOnce: true
      ceaseFire: ->
        Pager.disable

41 42 43 44
      callback: (i) =>
        unless @loading.is(':visible')
          @loading.show()
          Pager.getOld()