spine_paginator 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gruntfile.coffee CHANGED
@@ -32,6 +32,12 @@ module.exports = (grunt) ->
32
32
  files:
33
33
  'dist/spine.paginator.min.js': 'dist/spine.paginator.js'
34
34
  'dist/spine.pagination_controller.min.js': 'dist/spine.pagination_controller.js'
35
+
36
+ copy:
37
+ main:
38
+ files: [
39
+ {expand: true, flatten: true, src: ['dist/**'], dest: 'vendor/assets/javascripts/'}
40
+ ]
35
41
 
36
42
  jasmine:
37
43
  all:
@@ -59,9 +65,10 @@ module.exports = (grunt) ->
59
65
  grunt.loadNpmTasks 'grunt-contrib-uglify'
60
66
  grunt.loadNpmTasks 'grunt-contrib-jasmine'
61
67
  grunt.loadNpmTasks 'grunt-contrib-watch'
68
+ grunt.loadNpmTasks 'grunt-contrib-copy'
62
69
  grunt.loadNpmTasks 'grunt-slim'
63
70
  grunt.loadNpmTasks 'grunt-contrib-sass'
64
71
 
65
72
  grunt.registerTask 'default', ['watch']
66
73
  grunt.registerTask 'spec', ['jasmine']
67
- grunt.registerTask 'build', ['coffee', 'concat', 'uglify', 'slim', 'sass']
74
+ grunt.registerTask 'build', ['coffee', 'concat', 'uglify', 'copy', 'slim', 'sass']
@@ -1,5 +1,5 @@
1
1
  // spine.paginator.js
2
- // version: 0.1.1
2
+ // version: 1.0.0
3
3
  // author: vkill
4
4
  // license: MIT
5
5
  (function() {
@@ -11,7 +11,6 @@
11
11
  Spine = this.Spine || require('spine');
12
12
 
13
13
  /*
14
-
15
14
  Usage
16
15
 
17
16
  see https://github.com/vkill/spine_paginator/blob/master/examples/spine_pagination.coffee
@@ -1,5 +1,5 @@
1
1
  // spine.paginator.js
2
- // version: 0.1.1
2
+ // version: 1.0.0
3
3
  // author: vkill
4
4
  // license: MIT
5
5
  (function(){var t,e=function(t,e){return function(){return t.apply(e,arguments)}},i={}.hasOwnProperty,s=function(t,e){function s(){this.constructor=t}for(var a in e)i.call(e,a)&&(t[a]=e[a]);return s.prototype=e.prototype,t.prototype=new s,t.__super__=e.prototype,t};t=this.Spine||require("spine"),t.PaginationController=function(t){function i(){if(this.templateHtmlDataEmpty=e(this.templateHtmlDataEmpty,this),this.render=e(this.render,this),this.page||(this.page=1),this.pagination||(this.pagination=null),this.model||(this.model=null),this.perPage||(this.perPage=10),this.paginateEvent||(this.paginateEvent="paginate"),i.__super__.constructor.apply(this,arguments),"function"!=typeof this.model.page)throw Error("not found page function for model");this.model.bind(this.paginateEvent,this.render)}return s(i,t),i.prototype.refresh=function(){return this.page=1,this.load()},i.prototype.toPage=function(t){return this.page=t,this.load()},i.prototype.load=function(){return this.pagination=this.model.page(this.page,{perPage:this.perPage}),this.model.trigger(this.paginateEvent)},i.prototype.render=function(){return this.pagination.records.length>0?this.html(this.templateHtml()):this.html(this.templateHtmlDataEmpty())},i.prototype.events={"click a[data-page]":"clickPage"},i.prototype.clickPage=function(t){var e;return t.preventDefault(),e=this.getPageFromE(t),null!=e?this.toPage(e):void 0},i.prototype.getPageFromE=function(t){var e,i,s;if(e=$(t.target),e.parent(".disabled, .active").length>0)return null;switch(s=e.data("page"),i=null,s){case"first":i=this.pagination.firstPage();break;case"prev":i=this.pagination.currentPage()-1;break;case"next":i=this.pagination.currentPage()+1;break;case"last":i=this.pagination.lastPage();break;case"gap":i=null;break;default:i=s}return i},i.prototype.templateHtmlDataEmpty=function(){return""},i.prototype.templateHtml=function(){var t,e,i,s,a,r,n,o,h,p,u,l;for(s=this.pagination.locals,t=$("<div class='pagination pagination-small pull-right'></div>"),h=$("<ul></ul>"),e=$("<li><a href='javascript:void(0);' data-page='first'>first</a></li>").addClass(s.hasFirst?"":"disabled"),o=$("<li><a href='javascript:void(0);' data-page='prev'>prev</a></li>").addClass(s.hasPrev?"":"disabled"),a=$("<li><a href='javascript:void(0);' data-page='next'>next</a></li>").addClass(s.hasNext?"":"disabled"),i=$("<li><a href='javascript:void(0);' data-page='last'>last</a></li>").addClass(s.hasLast?"":"disabled"),h.append(e).append(o),l=s.pages,p=0,u=l.length;u>p;p++)r=l[p],n=r.gap?$("<li class='disabled'><a href='javascript:void(0);' data-page='gap'>...</a></li>"):$("<li><a href='javascript:void(0);'' data-page='"+r.number+"'>"+r.number+"</a></li>").addClass(r.current?"active":""),h.append(n);return h.append(a).append(i),t.append(h)},i}(t.Controller)}).call(this);
@@ -1,5 +1,5 @@
1
1
  // spine.paginator.js
2
- // version: 0.1.1
2
+ // version: 1.0.0
3
3
  // author: vkill
4
4
  // license: MIT
5
5
  /*
@@ -1,5 +1,5 @@
1
1
  // spine.paginator.js
2
- // version: 0.1.1
2
+ // version: 1.0.0
3
3
  // author: vkill
4
4
  // license: MIT
5
5
  (function(){var t,s,i;t=function(){function t(t,s,e){var r;this._page=s,null==e&&(e={}),i(t)||(t=[t]),this.originalRecords=t,this.totalCount=this.originalRecords.length,this._page=parseInt(this._page),(isNaN(this._page)||0>=this._page)&&(this._page=1),this._originalPage=this._page,this.perPage=e.perPage||this.constructor.DEFAULT_PER_PAGE,this.perPage=parseInt(this.perPage),(isNaN(this.perPage)||0>=this.perPage)&&(this.perPage=this.constructor.DEFAULT_PER_PAGE),this.maxPerPage=e.maxPerPage||this.constructor.MAX_PER_PAGE,this.window=e.window||e.inner_window||this.constructor.WINDOW,r=e.outer_window||this.constructor.OUTER_WINDOW,this.left=e.left||this.constructor.LEFT,0===this.left&&(this.left=r),this.right=e.right||this.constructor.RIGHT,0===this.right&&(this.right=r),this.skipbuildButtonsAndLocals=e.skipbuildButtonsAndLocals,this.records=[],this.buttons=[],this.locals={},this.per()}return t.DEFAULT_PER_PAGE=25,t.MAX_PER_PAGE=null,t.WINDOW=4,t.OUTER_WINDOW=0,t.LEFT=0,t.RIGHT=0,t.PAGE_TEXTS={first:"first",prev:"prev",current:"current",next:"next",last:"last",gap:"gap"},t.prototype.per=function(t){var s,i,e;return i=parseInt(t),!isNaN(i)&&i>0&&(this.perPage=i,this._page=this._originalPage),s=this.offsetValue(),e=s+this.limitValue(),this.records=this.originalRecords.slice(s,e),null==this.skipbuildButtonsAndLocals&&this.buildButtonsAndLocals(),this},t.prototype.totalPages=function(){return Math.ceil(this.totalCount/this.limitValue())},t.prototype.currentPage=function(){return null==this.limitValue()?this.firstPage():this.offsetValue()/this.limitValue()+1},t.prototype.firstPage=function(){return 1},t.prototype.isFirstPage=function(){return this.currentPage()===this.firstPage()},t.prototype.lastPage=function(){return this.totalPages()},t.prototype.isLastPage=function(){return this.currentPage()>=this.lastPage()},t.prototype.limitValue=function(){return this.perPage>this.totalCount&&(this.perPage=this.totalCount),null!=this.maxPerPage&&this.perPage>this.maxPerPage&&(this.perPage=this.maxPerPage),this.perPage},t.prototype.offsetValue=function(){var t;return t=this.totalPages(),this._page>t&&(this._page=t),(this._page-1)*this.limitValue()},t.prototype.buildPage=function(t,s,i,e,r){return{number:t,isCurrent:t===i,isFirst:t===e,isLast:t===r,isPrev:t===i-1,isNext:t===i+1,isLeftOuter:this.left>=t,isRightOuter:this.right>r-t,isInsideWindow:Math.abs(i-t)<=this.window,isWasTruncated:s===this.constructor.PAGE_TEXTS.gap}},t.prototype.curPage=function(){var t,s,i;return t=this.currentPage(),s=this.firstPage(),i=this.lastPage(),this.buildPage(t,null,t,s,i)},t.prototype.pages=function(){var t,s,i,e,r,n,a,h;for(t=this.currentPage(),s=this.firstPage(),e=this.lastPage(),h=[],i=null,r=a=s;e>=s?e>=a:a>=e;r=e>=s?++a:--a)n=this.buildPage(r,i,t,s,e),i=n.isLeftOuter||n.isRightOuter||n.isInsideWindow?null:this.constructor.PAGE_TEXTS.gap,h.push(n);return h},t.prototype.buildButtonsAndLocals=function(){var t,s,i,e,r,n,a;for(e=[],a={},t=this.curPage(),i=this.pages(),t.isFirst?a.hasFirst=!1:(e.push(this.constructor.PAGE_TEXTS.first),a.hasFirst=!0),t.isFirst?a.hasPrev=!1:(e.push(this.constructor.PAGE_TEXTS.prev),a.hasPrev=!0),a.pages=[],r=0,n=i.length;n>r;r++)s=i[r],s.isLeftOuter||s.isRightOuter||s.isInsideWindow?s.isCurrent?(e.push(this.constructor.PAGE_TEXTS.current),a.pages.push({number:s.number,current:!0})):(e.push(s.number),a.pages.push({number:s.number,current:!1})):s.isWasTruncated||(e.push(this.constructor.PAGE_TEXTS.gap),a.pages.push({number:s.number,gap:!0}));return t.isLast?a.hasNext=!1:(e.push(this.constructor.PAGE_TEXTS.next),a.hasNext=!0),t.isLast?a.hasLast=!1:(e.push(this.constructor.PAGE_TEXTS.last),a.hasLast=!0),a.first=this.firstPage(),a.current=this.currentPage(),a.last=this.lastPage(),a.numStart=0===this.records.length?0:this.offsetValue()+1,a.numEnd=this.offsetValue()+this.records.length,a.numTotal=this.totalCount,this.buttons=e,this.locals=a},t}(),i=function(t){return"[object Array]"===Object.prototype.toString.call(t)},t.isArray=i,null!=this.MyPaginatorName?this[this.MyPaginatorName]=t:this.Paginator=t,null!=this.Spine&&(t.SpineModelExtend={page:function(s,i){return null==i&&(i={}),new t(this._perPaginateRecords(),s,i)},_perPaginateRecords:function(){return this.all()}},s=this.Spine,s.Paginator=t,s.Model.Paginator={extended:function(){return this.extend(t.SpineModelExtend)}})}).call(this);
@@ -0,0 +1,4 @@
1
+ module SpinePaginator
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module SpinePaginator
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "spine_paginator/version"
2
2
 
3
3
  if defined?(::Rails)
4
- require "spine_paginator/railtie"
4
+ require "spine_paginator/engine"
5
5
  end
6
6
 
7
7
  module SpinePaginator
data/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "spine",
7
7
  "pagination"
8
8
  ],
9
- "version": "0.1.1",
9
+ "version": "1.0.0",
10
10
  "author": "vkill",
11
11
  "url": "https://github.com/vkill/spine_paginator",
12
12
  "main": "./dist/spine.paginator.js",
@@ -30,6 +30,7 @@
30
30
  "grunt-contrib-uglify": "~0.2.0",
31
31
  "grunt-contrib-jasmine": "~0.4.2",
32
32
  "grunt-contrib-watch": "~0.3.1",
33
+ "grunt-contrib-copy": "~0.4.1",
33
34
  "grunt-slim": "~0.1.0",
34
35
  "grunt-contrib-sass": "~0.3.0"
35
36
  },
@@ -1,14 +1,12 @@
1
1
  Spine = @Spine or require('spine')
2
2
 
3
3
  ###
4
-
5
4
  Usage
6
5
 
7
6
  see https://github.com/vkill/spine_paginator/blob/master/examples/spine_pagination.coffee
8
7
 
9
8
  ###
10
9
 
11
-
12
10
  class Spine.PaginationController extends Spine.Controller
13
11
 
14
12
  constructor: ->
@@ -0,0 +1,141 @@
1
+ // spine.paginator.js
2
+ // version: 1.0.0
3
+ // author: vkill
4
+ // license: MIT
5
+ (function() {
6
+ var Spine,
7
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
8
+ __hasProp = {}.hasOwnProperty,
9
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
10
+
11
+ Spine = this.Spine || require('spine');
12
+
13
+ /*
14
+ Usage
15
+
16
+ see https://github.com/vkill/spine_paginator/blob/master/examples/spine_pagination.coffee
17
+ */
18
+
19
+
20
+ Spine.PaginationController = (function(_super) {
21
+ __extends(PaginationController, _super);
22
+
23
+ function PaginationController() {
24
+ this.templateHtmlDataEmpty = __bind(this.templateHtmlDataEmpty, this);
25
+ this.render = __bind(this.render, this); this.page || (this.page = 1);
26
+ this.pagination || (this.pagination = null);
27
+ this.model || (this.model = null);
28
+ this.perPage || (this.perPage = 10);
29
+ this.paginateEvent || (this.paginateEvent = "paginate");
30
+ PaginationController.__super__.constructor.apply(this, arguments);
31
+ if (typeof this.model.page !== 'function') {
32
+ throw new Error("not found page function for model");
33
+ }
34
+ this.model.bind(this.paginateEvent, this.render);
35
+ }
36
+
37
+ PaginationController.prototype.refresh = function() {
38
+ this.page = 1;
39
+ return this.load();
40
+ };
41
+
42
+ PaginationController.prototype.toPage = function(page) {
43
+ this.page = page;
44
+ return this.load();
45
+ };
46
+
47
+ PaginationController.prototype.load = function() {
48
+ this.pagination = this.model.page(this.page, {
49
+ perPage: this.perPage
50
+ });
51
+ return this.model.trigger(this.paginateEvent);
52
+ };
53
+
54
+ PaginationController.prototype.render = function() {
55
+ if (this.pagination.records.length > 0) {
56
+ return this.html(this.templateHtml());
57
+ } else {
58
+ return this.html(this.templateHtmlDataEmpty());
59
+ }
60
+ };
61
+
62
+ PaginationController.prototype.events = {
63
+ 'click a[data-page]': 'clickPage'
64
+ };
65
+
66
+ PaginationController.prototype.clickPage = function(e) {
67
+ var page;
68
+
69
+ e.preventDefault();
70
+ page = this.getPageFromE(e);
71
+ if (page == null) {
72
+ return;
73
+ }
74
+ return this.toPage(page);
75
+ };
76
+
77
+ PaginationController.prototype.getPageFromE = function(e) {
78
+ var $node, page, _page;
79
+
80
+ $node = $(e.target);
81
+ if ($node.parent('.disabled, .active').length > 0) {
82
+ return null;
83
+ }
84
+ _page = $node.data('page');
85
+ page = null;
86
+ switch (_page) {
87
+ case 'first':
88
+ page = this.pagination.firstPage();
89
+ break;
90
+ case 'prev':
91
+ page = this.pagination.currentPage() - 1;
92
+ break;
93
+ case 'next':
94
+ page = this.pagination.currentPage() + 1;
95
+ break;
96
+ case 'last':
97
+ page = this.pagination.lastPage();
98
+ break;
99
+ case 'gap':
100
+ page = null;
101
+ break;
102
+ default:
103
+ page = _page;
104
+ }
105
+ return page;
106
+ };
107
+
108
+ PaginationController.prototype.templateHtmlDataEmpty = function() {
109
+ return "";
110
+ };
111
+
112
+ PaginationController.prototype.templateHtml = function() {
113
+ var div, firstLi, lastLi, locals, nextLi, page, pageLi, prevLi, ul, _i, _len, _ref;
114
+
115
+ locals = this.pagination.locals;
116
+ div = $("<div class='pagination pagination-small pull-right'></div>");
117
+ ul = $("<ul></ul>");
118
+ firstLi = $("<li><a href='javascript:void(0);' data-page='first'>first</a></li>").addClass(locals.hasFirst ? '' : 'disabled');
119
+ prevLi = $("<li><a href='javascript:void(0);' data-page='prev'>prev</a></li>").addClass(locals.hasPrev ? '' : 'disabled');
120
+ nextLi = $("<li><a href='javascript:void(0);' data-page='next'>next</a></li>").addClass(locals.hasNext ? '' : 'disabled');
121
+ lastLi = $("<li><a href='javascript:void(0);' data-page='last'>last</a></li>").addClass(locals.hasLast ? '' : 'disabled');
122
+ ul.append(firstLi).append(prevLi);
123
+ _ref = locals.pages;
124
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
125
+ page = _ref[_i];
126
+ if (page.gap) {
127
+ pageLi = $("<li class='disabled'><a href='javascript:void(0);' data-page='gap'>...</a></li>");
128
+ } else {
129
+ pageLi = $("<li><a href='javascript:void(0);'' data-page='" + page.number + "'>" + page.number + "</a></li>").addClass(page.current ? 'active' : '');
130
+ }
131
+ ul.append(pageLi);
132
+ }
133
+ ul.append(nextLi).append(lastLi);
134
+ return div.append(ul);
135
+ };
136
+
137
+ return PaginationController;
138
+
139
+ })(Spine.Controller);
140
+
141
+ }).call(this);
@@ -0,0 +1,5 @@
1
+ // spine.paginator.js
2
+ // version: 1.0.0
3
+ // author: vkill
4
+ // license: MIT
5
+ (function(){var t,e=function(t,e){return function(){return t.apply(e,arguments)}},i={}.hasOwnProperty,s=function(t,e){function s(){this.constructor=t}for(var a in e)i.call(e,a)&&(t[a]=e[a]);return s.prototype=e.prototype,t.prototype=new s,t.__super__=e.prototype,t};t=this.Spine||require("spine"),t.PaginationController=function(t){function i(){if(this.templateHtmlDataEmpty=e(this.templateHtmlDataEmpty,this),this.render=e(this.render,this),this.page||(this.page=1),this.pagination||(this.pagination=null),this.model||(this.model=null),this.perPage||(this.perPage=10),this.paginateEvent||(this.paginateEvent="paginate"),i.__super__.constructor.apply(this,arguments),"function"!=typeof this.model.page)throw Error("not found page function for model");this.model.bind(this.paginateEvent,this.render)}return s(i,t),i.prototype.refresh=function(){return this.page=1,this.load()},i.prototype.toPage=function(t){return this.page=t,this.load()},i.prototype.load=function(){return this.pagination=this.model.page(this.page,{perPage:this.perPage}),this.model.trigger(this.paginateEvent)},i.prototype.render=function(){return this.pagination.records.length>0?this.html(this.templateHtml()):this.html(this.templateHtmlDataEmpty())},i.prototype.events={"click a[data-page]":"clickPage"},i.prototype.clickPage=function(t){var e;return t.preventDefault(),e=this.getPageFromE(t),null!=e?this.toPage(e):void 0},i.prototype.getPageFromE=function(t){var e,i,s;if(e=$(t.target),e.parent(".disabled, .active").length>0)return null;switch(s=e.data("page"),i=null,s){case"first":i=this.pagination.firstPage();break;case"prev":i=this.pagination.currentPage()-1;break;case"next":i=this.pagination.currentPage()+1;break;case"last":i=this.pagination.lastPage();break;case"gap":i=null;break;default:i=s}return i},i.prototype.templateHtmlDataEmpty=function(){return""},i.prototype.templateHtml=function(){var t,e,i,s,a,r,n,o,h,p,u,l;for(s=this.pagination.locals,t=$("<div class='pagination pagination-small pull-right'></div>"),h=$("<ul></ul>"),e=$("<li><a href='javascript:void(0);' data-page='first'>first</a></li>").addClass(s.hasFirst?"":"disabled"),o=$("<li><a href='javascript:void(0);' data-page='prev'>prev</a></li>").addClass(s.hasPrev?"":"disabled"),a=$("<li><a href='javascript:void(0);' data-page='next'>next</a></li>").addClass(s.hasNext?"":"disabled"),i=$("<li><a href='javascript:void(0);' data-page='last'>last</a></li>").addClass(s.hasLast?"":"disabled"),h.append(e).append(o),l=s.pages,p=0,u=l.length;u>p;p++)r=l[p],n=r.gap?$("<li class='disabled'><a href='javascript:void(0);' data-page='gap'>...</a></li>"):$("<li><a href='javascript:void(0);'' data-page='"+r.number+"'>"+r.number+"</a></li>").addClass(r.current?"active":""),h.append(n);return h.append(a).append(i),t.append(h)},i}(t.Controller)}).call(this);
@@ -0,0 +1,311 @@
1
+ // spine.paginator.js
2
+ // version: 1.0.0
3
+ // author: vkill
4
+ // license: MIT
5
+ /*
6
+ Usage
7
+
8
+ data = ({name: String.fromCharCode(num)} for num in ['a'.charCodeAt(0)..'z'.charCodeAt(0)])
9
+ pagination = new Paginator(data, 2, {perPage: 3})
10
+ pagination.records
11
+ pagination.locals
12
+ pagination.buttons
13
+ */
14
+
15
+
16
+ (function() {
17
+ var Paginator, Spine, isArray;
18
+
19
+ Paginator = (function() {
20
+ Paginator.DEFAULT_PER_PAGE = 25;
21
+
22
+ Paginator.MAX_PER_PAGE = null;
23
+
24
+ Paginator.WINDOW = 4;
25
+
26
+ Paginator.OUTER_WINDOW = 0;
27
+
28
+ Paginator.LEFT = 0;
29
+
30
+ Paginator.RIGHT = 0;
31
+
32
+ Paginator.PAGE_TEXTS = {
33
+ first: 'first',
34
+ prev: 'prev',
35
+ current: 'current',
36
+ next: 'next',
37
+ last: 'last',
38
+ gap: 'gap'
39
+ };
40
+
41
+ function Paginator(records, _page, options) {
42
+ var outer_window;
43
+
44
+ this._page = _page;
45
+ if (options == null) {
46
+ options = {};
47
+ }
48
+ if (!isArray(records)) {
49
+ records = [records];
50
+ }
51
+ this.originalRecords = records;
52
+ this.totalCount = this.originalRecords.length;
53
+ this._page = parseInt(this._page);
54
+ if (isNaN(this._page) || this._page <= 0) {
55
+ this._page = 1;
56
+ }
57
+ this._originalPage = this._page;
58
+ this.perPage = options.perPage || this.constructor.DEFAULT_PER_PAGE;
59
+ this.perPage = parseInt(this.perPage);
60
+ if (isNaN(this.perPage) || this.perPage <= 0) {
61
+ this.perPage = this.constructor.DEFAULT_PER_PAGE;
62
+ }
63
+ this.maxPerPage = options.maxPerPage || this.constructor.MAX_PER_PAGE;
64
+ this.window = options.window || options.inner_window || this.constructor.WINDOW;
65
+ outer_window = options.outer_window || this.constructor.OUTER_WINDOW;
66
+ this.left = options.left || this.constructor.LEFT;
67
+ if (this.left === 0) {
68
+ this.left = outer_window;
69
+ }
70
+ this.right = options.right || this.constructor.RIGHT;
71
+ if (this.right === 0) {
72
+ this.right = outer_window;
73
+ }
74
+ this.skipbuildButtonsAndLocals = options.skipbuildButtonsAndLocals;
75
+ this.records = [];
76
+ this.buttons = [];
77
+ this.locals = {};
78
+ this.per();
79
+ }
80
+
81
+ Paginator.prototype.per = function(num) {
82
+ var fromN, n, toN;
83
+
84
+ n = parseInt(num);
85
+ if (!isNaN(n) && n > 0) {
86
+ this.perPage = n;
87
+ this._page = this._originalPage;
88
+ }
89
+ fromN = this.offsetValue();
90
+ toN = fromN + this.limitValue();
91
+ this.records = this.originalRecords.slice(fromN, toN);
92
+ if (this.skipbuildButtonsAndLocals == null) {
93
+ this.buildButtonsAndLocals();
94
+ }
95
+ return this;
96
+ };
97
+
98
+ Paginator.prototype.totalPages = function() {
99
+ return Math.ceil(this.totalCount / this.limitValue());
100
+ };
101
+
102
+ Paginator.prototype.currentPage = function() {
103
+ if (this.limitValue() == null) {
104
+ return this.firstPage();
105
+ }
106
+ return (this.offsetValue() / this.limitValue()) + 1;
107
+ };
108
+
109
+ Paginator.prototype.firstPage = function() {
110
+ return 1;
111
+ };
112
+
113
+ Paginator.prototype.isFirstPage = function() {
114
+ return this.currentPage() === this.firstPage();
115
+ };
116
+
117
+ Paginator.prototype.lastPage = function() {
118
+ return this.totalPages();
119
+ };
120
+
121
+ Paginator.prototype.isLastPage = function() {
122
+ return this.currentPage() >= this.lastPage();
123
+ };
124
+
125
+ Paginator.prototype.limitValue = function() {
126
+ if (this.perPage > this.totalCount) {
127
+ this.perPage = this.totalCount;
128
+ }
129
+ if ((this.maxPerPage != null) && this.perPage > this.maxPerPage) {
130
+ this.perPage = this.maxPerPage;
131
+ }
132
+ return this.perPage;
133
+ };
134
+
135
+ Paginator.prototype.offsetValue = function() {
136
+ var totalPages;
137
+
138
+ totalPages = this.totalPages();
139
+ if (this._page > totalPages) {
140
+ this._page = totalPages;
141
+ }
142
+ return (this._page - 1) * this.limitValue();
143
+ };
144
+
145
+ Paginator.prototype.buildPage = function(page, last, currentPage, firstPage, lastPage) {
146
+ return {
147
+ number: page,
148
+ isCurrent: page === currentPage,
149
+ isFirst: page === firstPage,
150
+ isLast: page === lastPage,
151
+ isPrev: page === (currentPage - 1),
152
+ isNext: page === (currentPage + 1),
153
+ isLeftOuter: page <= this.left,
154
+ isRightOuter: (lastPage - page) < this.right,
155
+ isInsideWindow: Math.abs(currentPage - page) <= this.window,
156
+ isWasTruncated: last === this.constructor.PAGE_TEXTS['gap']
157
+ };
158
+ };
159
+
160
+ Paginator.prototype.curPage = function() {
161
+ var currentPage, firstPage, lastPage;
162
+
163
+ currentPage = this.currentPage();
164
+ firstPage = this.firstPage();
165
+ lastPage = this.lastPage();
166
+ return this.buildPage(currentPage, null, currentPage, firstPage, lastPage);
167
+ };
168
+
169
+ Paginator.prototype.pages = function() {
170
+ var currentPage, firstPage, last, lastPage, page, result, _i, _pages;
171
+
172
+ currentPage = this.currentPage();
173
+ firstPage = this.firstPage();
174
+ lastPage = this.lastPage();
175
+ _pages = [];
176
+ last = null;
177
+ for (page = _i = firstPage; firstPage <= lastPage ? _i <= lastPage : _i >= lastPage; page = firstPage <= lastPage ? ++_i : --_i) {
178
+ result = this.buildPage(page, last, currentPage, firstPage, lastPage);
179
+ if (result.isLeftOuter || result.isRightOuter || result.isInsideWindow) {
180
+ last = null;
181
+ } else {
182
+ last = this.constructor.PAGE_TEXTS['gap'];
183
+ }
184
+ _pages.push(result);
185
+ }
186
+ return _pages;
187
+ };
188
+
189
+ Paginator.prototype.buildButtonsAndLocals = function() {
190
+ var curPage, page, pages, _buttons, _i, _len, _locals;
191
+
192
+ _buttons = [];
193
+ _locals = {};
194
+ curPage = this.curPage();
195
+ pages = this.pages();
196
+ if (!curPage.isFirst) {
197
+ _buttons.push(this.constructor.PAGE_TEXTS['first']);
198
+ _locals.hasFirst = true;
199
+ } else {
200
+ _locals.hasFirst = false;
201
+ }
202
+ if (!curPage.isFirst) {
203
+ _buttons.push(this.constructor.PAGE_TEXTS['prev']);
204
+ _locals.hasPrev = true;
205
+ } else {
206
+ _locals.hasPrev = false;
207
+ }
208
+ _locals.pages = [];
209
+ for (_i = 0, _len = pages.length; _i < _len; _i++) {
210
+ page = pages[_i];
211
+ if (page.isLeftOuter || page.isRightOuter || page.isInsideWindow) {
212
+ if (page.isCurrent) {
213
+ _buttons.push(this.constructor.PAGE_TEXTS['current']);
214
+ _locals.pages.push({
215
+ number: page.number,
216
+ current: true
217
+ });
218
+ } else {
219
+ _buttons.push(page.number);
220
+ _locals.pages.push({
221
+ number: page.number,
222
+ current: false
223
+ });
224
+ }
225
+ } else if (!page.isWasTruncated) {
226
+ _buttons.push(this.constructor.PAGE_TEXTS['gap']);
227
+ _locals.pages.push({
228
+ number: page.number,
229
+ gap: true
230
+ });
231
+ }
232
+ }
233
+ if (!curPage.isLast) {
234
+ _buttons.push(this.constructor.PAGE_TEXTS['next']);
235
+ _locals.hasNext = true;
236
+ } else {
237
+ _locals.hasNext = false;
238
+ }
239
+ if (!curPage.isLast) {
240
+ _buttons.push(this.constructor.PAGE_TEXTS['last']);
241
+ _locals.hasLast = true;
242
+ } else {
243
+ _locals.hasLast = false;
244
+ }
245
+ _locals.first = this.firstPage();
246
+ _locals.current = this.currentPage();
247
+ _locals.last = this.lastPage();
248
+ _locals.numStart = this.records.length === 0 ? 0 : this.offsetValue() + 1;
249
+ _locals.numEnd = this.offsetValue() + this.records.length;
250
+ _locals.numTotal = this.totalCount;
251
+ this.buttons = _buttons;
252
+ return this.locals = _locals;
253
+ };
254
+
255
+ return Paginator;
256
+
257
+ })();
258
+
259
+ isArray = function(value) {
260
+ return Object.prototype.toString.call(value) === '[object Array]';
261
+ };
262
+
263
+ Paginator.isArray = isArray;
264
+
265
+ if (this.MyPaginatorName != null) {
266
+ this[this.MyPaginatorName] = Paginator;
267
+ } else {
268
+ this['Paginator'] = Paginator;
269
+ }
270
+
271
+ if (this.Spine != null) {
272
+ /*
273
+ # Spine Usage
274
+
275
+ App = {}
276
+
277
+ class App.User extends Spine.Model
278
+ @configure 'User', 'name'
279
+ @extend Spine.Model.Paginator
280
+
281
+ data = ({name: String.fromCharCode(num)} for num in ['a'.charCodeAt(0)..'z'.charCodeAt(0)])
282
+
283
+ App.User.refresh(data)
284
+ pagination = App.User.page(2).per(5) #or App.User.page(2, {perPage: 5})
285
+ pagination.records
286
+ pagination.locals
287
+ pagination.buttons
288
+ App.User.PAGINATION = pagination
289
+ */
290
+
291
+ Paginator.SpineModelExtend = {
292
+ page: function(n, options) {
293
+ if (options == null) {
294
+ options = {};
295
+ }
296
+ return new Paginator(this._perPaginateRecords(), n, options);
297
+ },
298
+ _perPaginateRecords: function() {
299
+ return this.all();
300
+ }
301
+ };
302
+ Spine = this.Spine;
303
+ Spine.Paginator = Paginator;
304
+ Spine.Model.Paginator = {
305
+ extended: function() {
306
+ return this.extend(Paginator.SpineModelExtend);
307
+ }
308
+ };
309
+ }
310
+
311
+ }).call(this);
@@ -0,0 +1,5 @@
1
+ // spine.paginator.js
2
+ // version: 1.0.0
3
+ // author: vkill
4
+ // license: MIT
5
+ (function(){var t,s,i;t=function(){function t(t,s,e){var r;this._page=s,null==e&&(e={}),i(t)||(t=[t]),this.originalRecords=t,this.totalCount=this.originalRecords.length,this._page=parseInt(this._page),(isNaN(this._page)||0>=this._page)&&(this._page=1),this._originalPage=this._page,this.perPage=e.perPage||this.constructor.DEFAULT_PER_PAGE,this.perPage=parseInt(this.perPage),(isNaN(this.perPage)||0>=this.perPage)&&(this.perPage=this.constructor.DEFAULT_PER_PAGE),this.maxPerPage=e.maxPerPage||this.constructor.MAX_PER_PAGE,this.window=e.window||e.inner_window||this.constructor.WINDOW,r=e.outer_window||this.constructor.OUTER_WINDOW,this.left=e.left||this.constructor.LEFT,0===this.left&&(this.left=r),this.right=e.right||this.constructor.RIGHT,0===this.right&&(this.right=r),this.skipbuildButtonsAndLocals=e.skipbuildButtonsAndLocals,this.records=[],this.buttons=[],this.locals={},this.per()}return t.DEFAULT_PER_PAGE=25,t.MAX_PER_PAGE=null,t.WINDOW=4,t.OUTER_WINDOW=0,t.LEFT=0,t.RIGHT=0,t.PAGE_TEXTS={first:"first",prev:"prev",current:"current",next:"next",last:"last",gap:"gap"},t.prototype.per=function(t){var s,i,e;return i=parseInt(t),!isNaN(i)&&i>0&&(this.perPage=i,this._page=this._originalPage),s=this.offsetValue(),e=s+this.limitValue(),this.records=this.originalRecords.slice(s,e),null==this.skipbuildButtonsAndLocals&&this.buildButtonsAndLocals(),this},t.prototype.totalPages=function(){return Math.ceil(this.totalCount/this.limitValue())},t.prototype.currentPage=function(){return null==this.limitValue()?this.firstPage():this.offsetValue()/this.limitValue()+1},t.prototype.firstPage=function(){return 1},t.prototype.isFirstPage=function(){return this.currentPage()===this.firstPage()},t.prototype.lastPage=function(){return this.totalPages()},t.prototype.isLastPage=function(){return this.currentPage()>=this.lastPage()},t.prototype.limitValue=function(){return this.perPage>this.totalCount&&(this.perPage=this.totalCount),null!=this.maxPerPage&&this.perPage>this.maxPerPage&&(this.perPage=this.maxPerPage),this.perPage},t.prototype.offsetValue=function(){var t;return t=this.totalPages(),this._page>t&&(this._page=t),(this._page-1)*this.limitValue()},t.prototype.buildPage=function(t,s,i,e,r){return{number:t,isCurrent:t===i,isFirst:t===e,isLast:t===r,isPrev:t===i-1,isNext:t===i+1,isLeftOuter:this.left>=t,isRightOuter:this.right>r-t,isInsideWindow:Math.abs(i-t)<=this.window,isWasTruncated:s===this.constructor.PAGE_TEXTS.gap}},t.prototype.curPage=function(){var t,s,i;return t=this.currentPage(),s=this.firstPage(),i=this.lastPage(),this.buildPage(t,null,t,s,i)},t.prototype.pages=function(){var t,s,i,e,r,n,a,h;for(t=this.currentPage(),s=this.firstPage(),e=this.lastPage(),h=[],i=null,r=a=s;e>=s?e>=a:a>=e;r=e>=s?++a:--a)n=this.buildPage(r,i,t,s,e),i=n.isLeftOuter||n.isRightOuter||n.isInsideWindow?null:this.constructor.PAGE_TEXTS.gap,h.push(n);return h},t.prototype.buildButtonsAndLocals=function(){var t,s,i,e,r,n,a;for(e=[],a={},t=this.curPage(),i=this.pages(),t.isFirst?a.hasFirst=!1:(e.push(this.constructor.PAGE_TEXTS.first),a.hasFirst=!0),t.isFirst?a.hasPrev=!1:(e.push(this.constructor.PAGE_TEXTS.prev),a.hasPrev=!0),a.pages=[],r=0,n=i.length;n>r;r++)s=i[r],s.isLeftOuter||s.isRightOuter||s.isInsideWindow?s.isCurrent?(e.push(this.constructor.PAGE_TEXTS.current),a.pages.push({number:s.number,current:!0})):(e.push(s.number),a.pages.push({number:s.number,current:!1})):s.isWasTruncated||(e.push(this.constructor.PAGE_TEXTS.gap),a.pages.push({number:s.number,gap:!0}));return t.isLast?a.hasNext=!1:(e.push(this.constructor.PAGE_TEXTS.next),a.hasNext=!0),t.isLast?a.hasLast=!1:(e.push(this.constructor.PAGE_TEXTS.last),a.hasLast=!0),a.first=this.firstPage(),a.current=this.currentPage(),a.last=this.lastPage(),a.numStart=0===this.records.length?0:this.offsetValue()+1,a.numEnd=this.offsetValue()+this.records.length,a.numTotal=this.totalCount,this.buttons=e,this.locals=a},t}(),i=function(t){return"[object Array]"===Object.prototype.toString.call(t)},t.isArray=i,null!=this.MyPaginatorName?this[this.MyPaginatorName]=t:this.Paginator=t,null!=this.Spine&&(t.SpineModelExtend={page:function(s,i){return null==i&&(i={}),new t(this._perPaginateRecords(),s,i)},_perPaginateRecords:function(){return this.all()}},s=this.Spine,s.Paginator=t,s.Model.Paginator={extended:function(){return this.extend(t.SpineModelExtend)}})}).call(this);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spine_paginator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Paginator for Spine
15
15
  email:
@@ -37,7 +37,7 @@ files:
37
37
  - examples/spine_pagination.scss
38
38
  - examples/spine_pagination.slim
39
39
  - lib/spine_paginator.rb
40
- - lib/spine_paginator/railtie.rb
40
+ - lib/spine_paginator/engine.rb
41
41
  - lib/spine_paginator/version.rb
42
42
  - package.json
43
43
  - spec/lib/.gitkeep
@@ -47,6 +47,10 @@ files:
47
47
  - spine_paginator.gemspec
48
48
  - src/spine.pagination_controller.coffee
49
49
  - src/spine.paginator.coffee
50
+ - vendor/assets/javascripts/spine.pagination_controller.js
51
+ - vendor/assets/javascripts/spine.pagination_controller.min.js
52
+ - vendor/assets/javascripts/spine.paginator.js
53
+ - vendor/assets/javascripts/spine.paginator.min.js
50
54
  homepage: https://github.com/vkill/spine_paginator
51
55
  licenses: []
52
56
  post_install_message:
@@ -1,12 +0,0 @@
1
-
2
- module SpinePaginator
3
- class Railtie < ::Rails::Railtie
4
-
5
- initializer "sprockets.spine_paginator", after: "append_asset_paths", group: :all do |app|
6
- next unless app.config.assets.enabled
7
- path = File.expand_path("../../../dist", __FILE__)
8
- app.config.assets.paths << path unless app.config.assets.paths.include?(path)
9
- end
10
-
11
- end
12
- end