summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pager.js
blob: 2e4dc62273e9c3bdb6c5309f5b17842511f7cc32 (plain)
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
/* eslint-disable */
(function() {
  this.Pager = {
    init: function(limit, preload, disable, callback) {
      this.limit = limit != null ? limit : 0;
      this.disable = disable != null ? disable : false;
      this.callback = callback != null ? callback : $.noop;
      this.loading = $('.loading').first();
      if (preload) {
        this.offset = 0;
        this.getOld();
      } else {
        this.offset = this.limit;
      }
      return this.initLoadMore();
    },
    getOld: function() {
      this.loading.show();
      return $.ajax({
        type: "GET",
        url: $(".content_list").data('href') || location.href,
        data: "limit=" + this.limit + "&offset=" + this.offset,
        complete: (function(_this) {
          return function() {
            return _this.loading.hide();
          };
        })(this),
        success: function(data) {
          Pager.append(data.count, data.html);
          return Pager.callback();
        },
        dataType: "json"
      });
    },
    append: function(count, html) {
      $(".content_list").append(html);
      if (count > 0) {
        return this.offset += count;
      } else {
        return this.disable = true;
      }
    },
    initLoadMore: function() {
      $(document).unbind('scroll');
      return $(document).endlessScroll({
        bottomPixels: 400,
        fireDelay: 1000,
        fireOnce: true,
        ceaseFire: function() {
          return Pager.disable;
        },
        callback: (function(_this) {
          return function(i) {
            if (!_this.loading.is(':visible')) {
              _this.loading.show();
              return Pager.getOld();
            }
          };
        })(this)
      });
    }
  };

}).call(this);