spine_paginator 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +0 -1
- data/component.json +1 -1
- data/dist/spine.pagination_controller.js +45 -45
- data/dist/spine.pagination_controller.min.js +1 -1
- data/dist/spine.paginator.js +2 -0
- data/dist/spine.paginator.min.js +1 -1
- data/examples/spine_pagination.coffee +48 -9
- data/examples/spine_pagination.html +1 -1
- data/examples/spine_pagination.js +17 -19
- data/examples/spine_pagination.slim +1 -1
- data/lib/spine_paginator/version.rb +1 -1
- data/src/spine.pagination_controller.coffee +46 -59
- data/src/spine.paginator.coffee +2 -4
- metadata +2 -2
data/README.md
CHANGED
data/component.json
CHANGED
@@ -21,56 +21,39 @@
|
|
21
21
|
Spine.PaginationController = (function(_super) {
|
22
22
|
__extends(PaginationController, _super);
|
23
23
|
|
24
|
-
PaginationController.MODEL = null;
|
25
|
-
|
26
|
-
PaginationController.PER_PAGES = [10, 20, 30, 40];
|
27
|
-
|
28
|
-
PaginationController.PAGINATE_EVENT = "paginate";
|
29
|
-
|
30
|
-
PaginationController.PAGE = 1;
|
31
|
-
|
32
|
-
PaginationController.PER_PAGE = null;
|
33
|
-
|
34
|
-
PaginationController.PAGINATION = null;
|
35
|
-
|
36
24
|
function PaginationController() {
|
37
25
|
this.templateHtmlDataEmpty = __bind(this.templateHtmlDataEmpty, this);
|
38
|
-
this.render = __bind(this.render, this);
|
39
|
-
|
40
|
-
|
26
|
+
this.render = __bind(this.render, this); this.page || (this.page = 1);
|
27
|
+
this.pagination || (this.pagination = null);
|
28
|
+
this.model || (this.model = null);
|
29
|
+
this.perPage || (this.perPage = 10);
|
30
|
+
this.paginateEvent || (this.paginateEvent = "paginate");
|
31
|
+
PaginationController.__super__.constructor.apply(this, arguments);
|
32
|
+
if (typeof this.model.page !== 'function') {
|
33
|
+
throw new Error("not found page function for model");
|
41
34
|
}
|
42
|
-
this.
|
43
|
-
if (this.constructor.PER_PAGE == null) {
|
44
|
-
throw new Error("please defined class variable PER_PAGES");
|
45
|
-
}
|
46
|
-
this.constructor.MODEL.bind(this.constructor.PAGINATE_EVENT, this.render);
|
35
|
+
this.model.bind(this.paginateEvent, this.render);
|
47
36
|
}
|
48
37
|
|
49
|
-
PaginationController.refresh = function() {
|
50
|
-
this.
|
38
|
+
PaginationController.prototype.refresh = function() {
|
39
|
+
this.page = 1;
|
51
40
|
return this.load();
|
52
41
|
};
|
53
42
|
|
54
|
-
PaginationController.toPage = function(page) {
|
55
|
-
this.
|
43
|
+
PaginationController.prototype.toPage = function(page) {
|
44
|
+
this.page = page;
|
56
45
|
return this.load();
|
57
46
|
};
|
58
47
|
|
59
|
-
PaginationController.load = function() {
|
60
|
-
|
61
|
-
|
62
|
-
pagination = this.MODEL.page(this.PAGE, {
|
63
|
-
perPage: this.PER_PAGE
|
48
|
+
PaginationController.prototype.load = function() {
|
49
|
+
this.pagination = this.model.page(this.page, {
|
50
|
+
perPage: this.perPage
|
64
51
|
});
|
65
|
-
this.
|
66
|
-
return this.MODEL.trigger(this.PAGINATE_EVENT);
|
52
|
+
return this.model.trigger(this.paginateEvent);
|
67
53
|
};
|
68
54
|
|
69
55
|
PaginationController.prototype.render = function() {
|
70
|
-
|
71
|
-
|
72
|
-
pagination = this.constructor.PAGINATION;
|
73
|
-
if (pagination.records.length > 0) {
|
56
|
+
if (this.pagination.records.length > 0) {
|
74
57
|
return this.html(this.templateHtml());
|
75
58
|
} else {
|
76
59
|
return this.html(this.templateHtmlDataEmpty());
|
@@ -89,7 +72,7 @@
|
|
89
72
|
if (page == null) {
|
90
73
|
return;
|
91
74
|
}
|
92
|
-
return this.
|
75
|
+
return this.toPage(page);
|
93
76
|
};
|
94
77
|
|
95
78
|
PaginationController.prototype.getPageFromE = function(e) {
|
@@ -103,16 +86,16 @@
|
|
103
86
|
page = null;
|
104
87
|
switch (_page) {
|
105
88
|
case 'first':
|
106
|
-
page = this.
|
89
|
+
page = this.pagination.firstPage();
|
107
90
|
break;
|
108
91
|
case 'prev':
|
109
|
-
page = this.
|
92
|
+
page = this.pagination.currentPage() - 1;
|
110
93
|
break;
|
111
94
|
case 'next':
|
112
|
-
page = this.
|
95
|
+
page = this.pagination.currentPage() + 1;
|
113
96
|
break;
|
114
97
|
case 'last':
|
115
|
-
page = this.
|
98
|
+
page = this.pagination.lastPage();
|
116
99
|
break;
|
117
100
|
case 'gap':
|
118
101
|
page = null;
|
@@ -128,11 +111,28 @@
|
|
128
111
|
};
|
129
112
|
|
130
113
|
PaginationController.prototype.templateHtml = function() {
|
131
|
-
var
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
114
|
+
var div, firstLi, lastLi, locals, nextLi, page, pageLi, prevLi, ul, _i, _len, _ref;
|
115
|
+
|
116
|
+
locals = this.pagination.locals;
|
117
|
+
div = $("<div class='pagination pagination-small pull-right'></div>");
|
118
|
+
ul = $("<ul></ul>");
|
119
|
+
firstLi = $("<li><a href='javascript:void(0);' data-page='first'>first</a></li>").addClass(locals.hasFirst ? '' : 'disabled');
|
120
|
+
prevLi = $("<li><a href='javascript:void(0);' data-page='prev'>prev</a></li>").addClass(locals.hasPrev ? '' : 'disabled');
|
121
|
+
nextLi = $("<li><a href='javascript:void(0);' data-page='next'>next</a></li>").addClass(locals.hasNext ? '' : 'disabled');
|
122
|
+
lastLi = $("<li><a href='javascript:void(0);' data-page='last'>last</a></li>").addClass(locals.hasLast ? '' : 'disabled');
|
123
|
+
ul.append(firstLi).append(prevLi);
|
124
|
+
_ref = locals.pages;
|
125
|
+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
126
|
+
page = _ref[_i];
|
127
|
+
if (page.gap) {
|
128
|
+
pageLi = $("<li class='disabled'><a href='javascript:void(0);' data-page='gap'>...</a></li>");
|
129
|
+
} else {
|
130
|
+
pageLi = $("<li><a href='javascript:void(0);'' data-page='" + page.number + "'>" + page.number + "</a></li>").addClass(page.current ? 'active' : '');
|
131
|
+
}
|
132
|
+
ul.append(pageLi);
|
133
|
+
}
|
134
|
+
ul.append(nextLi).append(lastLi);
|
135
|
+
return div.append(ul);
|
136
136
|
};
|
137
137
|
|
138
138
|
return PaginationController;
|
@@ -2,4 +2,4 @@
|
|
2
2
|
// version: 0.1.1
|
3
3
|
// author: vkill
|
4
4
|
// license: MIT
|
5
|
-
(function(){var t,
|
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);
|
data/dist/spine.paginator.js
CHANGED
data/dist/spine.paginator.min.js
CHANGED
@@ -2,4 +2,4 @@
|
|
2
2
|
// version: 0.1.1
|
3
3
|
// author: vkill
|
4
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
|
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);
|
@@ -12,20 +12,54 @@ App.UsersCtrl ||= {}
|
|
12
12
|
Ctrl = App.UsersCtrl
|
13
13
|
|
14
14
|
class Ctrl.IndexPagination extends Spine.PaginationController
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
constructor: ->
|
16
|
+
@model = User
|
17
|
+
@perPage = 5
|
18
|
+
@paginateEvent = "paginate"
|
19
|
+
super
|
20
|
+
|
21
|
+
# templateHtml: ->
|
22
|
+
# htmlSource = """
|
23
|
+
# <div class='pagination pagination-small pull-right'>
|
24
|
+
# <ul>
|
25
|
+
# <li {{#unless hasFirst}}class='disabled'{{/unless}}>
|
26
|
+
# <a href='javascript:void(0);' data-page='first'>first</a>
|
27
|
+
# </li>
|
28
|
+
# <li {{#unless hasPrev}}class='disabled'{{/unless}}>
|
29
|
+
# <a href='javascript:void(0);' data-page='prev'>prev</a>
|
30
|
+
# </li>
|
31
|
+
# {{#each pages}}
|
32
|
+
# {{#if this.gap}}
|
33
|
+
# <li class='disabled'>
|
34
|
+
# <a href='javascript:void(0);' data-page='gap'>...</a>
|
35
|
+
# </li>
|
36
|
+
# {{else}}
|
37
|
+
# <li {{#if this.current}}class='active'{{/if}}>
|
38
|
+
# <a href='javascript:void(0);' data-page='{{this.number}}'>{{this.number}}</a>
|
39
|
+
# </li>
|
40
|
+
# {{/if}}
|
41
|
+
# {{/each}}
|
42
|
+
# <li {{#unless hasNext}}class='disabled'{{/unless}}>
|
43
|
+
# <a href='javascript:void(0);' data-page='next'>next</a>
|
44
|
+
# </li>
|
45
|
+
# <li {{#unless hasLast}}class='disabled'{{/unless}}>
|
46
|
+
# <a href='javascript:void(0);' data-page='last'>last</a>
|
47
|
+
# </li>
|
48
|
+
# </ul>
|
49
|
+
# </div>
|
50
|
+
# """
|
51
|
+
# Handlebars.compile(htmlSource)(@pagination.locals)
|
18
52
|
|
19
53
|
class Ctrl.IndexTbody extends Spine.Controller
|
20
54
|
constructor: ->
|
21
55
|
super
|
22
|
-
User.bind
|
56
|
+
User.bind @owner.pagination.paginateEvent, @render
|
23
57
|
|
24
58
|
render: =>
|
25
59
|
@html @templateHtml()
|
26
60
|
|
27
61
|
templateHtml: =>
|
28
|
-
collection =
|
62
|
+
collection = @owner.pagination.pagination.records
|
29
63
|
source = """
|
30
64
|
{{#each collection}}
|
31
65
|
<tr data-id={{this.id}}>
|
@@ -39,14 +73,19 @@ class Ctrl.IndexTbody extends Spine.Controller
|
|
39
73
|
class Ctrl.Index extends Spine.Controller
|
40
74
|
constructor: ->
|
41
75
|
super
|
42
|
-
|
43
|
-
el: @$('tbody')
|
76
|
+
|
44
77
|
@pagination = new Ctrl.IndexPagination
|
45
78
|
el: @$('#pagination')
|
79
|
+
owner: @
|
80
|
+
|
81
|
+
@tbody = new Ctrl.IndexTbody
|
82
|
+
el: @$('tbody')
|
83
|
+
owner: @
|
84
|
+
|
46
85
|
User.bind 'refresh', @refreshPagination
|
47
86
|
|
48
|
-
refreshPagination:
|
49
|
-
|
87
|
+
refreshPagination: =>
|
88
|
+
@pagination.refresh()
|
50
89
|
|
51
90
|
|
52
91
|
$(document).ready ->
|
@@ -1 +1 @@
|
|
1
|
-
<!DOCTYPE html><html><head><title>Spine Pagination Demo</title><link href="http://
|
1
|
+
<!DOCTYPE html><html><head><title>Spine Pagination Demo</title><link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet" /><link href="./spine_pagination.css" rel="stylesheet" /><script type="text/javascript">window.MyPaginatorName = "Paginator"</script><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script><script src="../spec/lib/spine.js"></script><script src="../spec/lib/handlebars.js"></script><script src="../dist/spine.paginator.js"></script><script src="../dist/spine.pagination_controller.js"></script><script src="./spine_pagination.js"></script></head><body><h1>Spine Pagination Demo</h1><div id="users"><table class="table table-hover"><thead><tr><td>ID</td><td>Name</td></tr></thead><tbody></tbody></table><div id="pagination"></div></div></body></html>
|
@@ -1,5 +1,5 @@
|
|
1
1
|
(function() {
|
2
|
-
var App, Ctrl, Spine, User, _ref,
|
2
|
+
var App, Ctrl, Spine, User, _ref,
|
3
3
|
__hasProp = {}.hasOwnProperty,
|
4
4
|
__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; },
|
5
5
|
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
@@ -34,16 +34,12 @@
|
|
34
34
|
__extends(IndexPagination, _super);
|
35
35
|
|
36
36
|
function IndexPagination() {
|
37
|
-
|
38
|
-
|
37
|
+
this.model = User;
|
38
|
+
this.perPage = 5;
|
39
|
+
this.paginateEvent = "paginate";
|
40
|
+
IndexPagination.__super__.constructor.apply(this, arguments);
|
39
41
|
}
|
40
42
|
|
41
|
-
IndexPagination.MODEL = User;
|
42
|
-
|
43
|
-
IndexPagination.PER_PAGES = [5, 10, 20, 30];
|
44
|
-
|
45
|
-
IndexPagination.PAGINATE_EVENT = "paginate";
|
46
|
-
|
47
43
|
return IndexPagination;
|
48
44
|
|
49
45
|
})(Spine.PaginationController);
|
@@ -54,7 +50,7 @@
|
|
54
50
|
function IndexTbody() {
|
55
51
|
this.templateHtml = __bind(this.templateHtml, this);
|
56
52
|
this.render = __bind(this.render, this); IndexTbody.__super__.constructor.apply(this, arguments);
|
57
|
-
User.bind(
|
53
|
+
User.bind(this.owner.pagination.paginateEvent, this.render);
|
58
54
|
}
|
59
55
|
|
60
56
|
IndexTbody.prototype.render = function() {
|
@@ -64,7 +60,7 @@
|
|
64
60
|
IndexTbody.prototype.templateHtml = function() {
|
65
61
|
var collection, source;
|
66
62
|
|
67
|
-
collection =
|
63
|
+
collection = this.owner.pagination.pagination.records;
|
68
64
|
source = "{{#each collection}}\n <tr data-id={{this.id}}>\n <td>{{this.id}}</td>\n <td>{{this.name}}</td>\n </tr>\n{{/each}}";
|
69
65
|
return Handlebars.compile(source)({
|
70
66
|
collection: collection
|
@@ -79,18 +75,20 @@
|
|
79
75
|
__extends(Index, _super);
|
80
76
|
|
81
77
|
function Index() {
|
82
|
-
Index.__super__.constructor.apply(this, arguments);
|
83
|
-
this.tbody = new Ctrl.IndexTbody({
|
84
|
-
el: this.$('tbody')
|
85
|
-
});
|
78
|
+
this.refreshPagination = __bind(this.refreshPagination, this); Index.__super__.constructor.apply(this, arguments);
|
86
79
|
this.pagination = new Ctrl.IndexPagination({
|
87
|
-
el: this.$('#pagination')
|
80
|
+
el: this.$('#pagination'),
|
81
|
+
owner: this
|
82
|
+
});
|
83
|
+
this.tbody = new Ctrl.IndexTbody({
|
84
|
+
el: this.$('tbody'),
|
85
|
+
owner: this
|
88
86
|
});
|
89
87
|
User.bind('refresh', this.refreshPagination);
|
90
88
|
}
|
91
89
|
|
92
90
|
Index.prototype.refreshPagination = function() {
|
93
|
-
return
|
91
|
+
return this.pagination.refresh();
|
94
92
|
};
|
95
93
|
|
96
94
|
return Index;
|
@@ -104,10 +102,10 @@
|
|
104
102
|
el: '#users'
|
105
103
|
});
|
106
104
|
data = (function() {
|
107
|
-
var _i,
|
105
|
+
var _i, _ref1, _ref2, _results;
|
108
106
|
|
109
107
|
_results = [];
|
110
|
-
for (num = _i =
|
108
|
+
for (num = _i = _ref1 = 'A'.charCodeAt(0), _ref2 = 'z'.charCodeAt(0); _ref1 <= _ref2 ? _i <= _ref2 : _i >= _ref2; num = _ref1 <= _ref2 ? ++_i : --_i) {
|
111
109
|
_results.push({
|
112
110
|
name: String.fromCharCode(num)
|
113
111
|
});
|
@@ -2,7 +2,7 @@ doctype html
|
|
2
2
|
html
|
3
3
|
head
|
4
4
|
title Spine Pagination Demo
|
5
|
-
link href="http://
|
5
|
+
link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet"
|
6
6
|
link href="./spine_pagination.css" rel="stylesheet"
|
7
7
|
javascript:
|
8
8
|
window.MyPaginatorName = "Paginator"
|
@@ -11,38 +11,35 @@ see https://github.com/vkill/spine_paginator/blob/master/examples/spine_paginati
|
|
11
11
|
|
12
12
|
class Spine.PaginationController extends Spine.Controller
|
13
13
|
|
14
|
-
@MODEL = null
|
15
|
-
@PER_PAGES = [10, 20, 30, 40]
|
16
|
-
@PAGINATE_EVENT = "paginate"
|
17
|
-
|
18
|
-
@PAGE = 1
|
19
|
-
@PER_PAGE = null
|
20
|
-
@PAGINATION = null
|
21
|
-
|
22
14
|
constructor: ->
|
15
|
+
@page ||= 1
|
16
|
+
@pagination ||= null
|
17
|
+
|
18
|
+
@model ||= null
|
19
|
+
@perPage ||= 10
|
20
|
+
@paginateEvent ||= "paginate"
|
21
|
+
|
23
22
|
super
|
24
|
-
|
25
|
-
@
|
26
|
-
|
27
|
-
|
23
|
+
|
24
|
+
if typeof(@model.page) isnt 'function'
|
25
|
+
throw new Error("not found page function for model")
|
26
|
+
|
27
|
+
@model.bind @paginateEvent, @render
|
28
28
|
|
29
|
-
|
30
|
-
@
|
29
|
+
refresh: ->
|
30
|
+
@page = 1
|
31
31
|
@load()
|
32
32
|
|
33
|
-
|
34
|
-
@
|
33
|
+
toPage: (page)->
|
34
|
+
@page = page
|
35
35
|
@load()
|
36
36
|
|
37
|
-
|
38
|
-
pagination = @
|
39
|
-
@
|
40
|
-
@MODEL.trigger(@PAGINATE_EVENT)
|
41
|
-
|
37
|
+
load: ->
|
38
|
+
@pagination = @model.page(@page, {perPage: @perPage})
|
39
|
+
@model.trigger(@paginateEvent)
|
42
40
|
|
43
41
|
render: =>
|
44
|
-
pagination
|
45
|
-
if pagination.records.length > 0
|
42
|
+
if @pagination.records.length > 0
|
46
43
|
@html @templateHtml()
|
47
44
|
else
|
48
45
|
@html @templateHtmlDataEmpty()
|
@@ -59,7 +56,7 @@ class Spine.PaginationController extends Spine.Controller
|
|
59
56
|
|
60
57
|
page = @getPageFromE(e)
|
61
58
|
return unless page?
|
62
|
-
@
|
59
|
+
@toPage(page)
|
63
60
|
|
64
61
|
getPageFromE: (e) ->
|
65
62
|
$node = $(e.target)
|
@@ -69,13 +66,13 @@ class Spine.PaginationController extends Spine.Controller
|
|
69
66
|
page = null
|
70
67
|
switch _page
|
71
68
|
when 'first'
|
72
|
-
page = @
|
69
|
+
page = @pagination.firstPage()
|
73
70
|
when 'prev'
|
74
|
-
page = @
|
71
|
+
page = @pagination.currentPage() - 1
|
75
72
|
when 'next'
|
76
|
-
page = @
|
73
|
+
page = @pagination.currentPage() + 1
|
77
74
|
when 'last'
|
78
|
-
page = @
|
75
|
+
page = @pagination.lastPage()
|
79
76
|
when 'gap'
|
80
77
|
page = null
|
81
78
|
else
|
@@ -86,34 +83,24 @@ class Spine.PaginationController extends Spine.Controller
|
|
86
83
|
""
|
87
84
|
|
88
85
|
templateHtml: ->
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
<li {{#unless hasNext}}class="disabled"{{/unless}}>
|
111
|
-
<a href='javascript:void(0);' data-page="next">next</a>
|
112
|
-
</li>
|
113
|
-
<li {{#unless hasLast}}class="disabled"{{/unless}}>
|
114
|
-
<a href='javascript:void(0);' data-page="last">last</a>
|
115
|
-
</li>
|
116
|
-
</ul>
|
117
|
-
</div>
|
118
|
-
"""
|
119
|
-
Handlebars.compile(source)(pagination.locals)
|
86
|
+
locals = @pagination.locals
|
87
|
+
|
88
|
+
div = $("<div class='pagination pagination-small pull-right'></div>")
|
89
|
+
ul = $("<ul></ul>")
|
90
|
+
|
91
|
+
firstLi = $("<li><a href='javascript:void(0);' data-page='first'>first</a></li>").addClass(if locals.hasFirst then '' else 'disabled')
|
92
|
+
prevLi = $("<li><a href='javascript:void(0);' data-page='prev'>prev</a></li>").addClass(if locals.hasPrev then '' else 'disabled')
|
93
|
+
nextLi = $("<li><a href='javascript:void(0);' data-page='next'>next</a></li>").addClass(if locals.hasNext then '' else 'disabled')
|
94
|
+
lastLi = $("<li><a href='javascript:void(0);' data-page='last'>last</a></li>").addClass(if locals.hasLast then '' else 'disabled')
|
95
|
+
|
96
|
+
ul.append(firstLi).append(prevLi)
|
97
|
+
for page in locals.pages
|
98
|
+
if page.gap
|
99
|
+
pageLi = $("<li class='disabled'><a href='javascript:void(0);' data-page='gap'>...</a></li>")
|
100
|
+
else
|
101
|
+
pageLi = $("<li><a href='javascript:void(0);'' data-page='#{page.number}'>#{page.number}</a></li>").addClass(if page.current then 'active' else '')
|
102
|
+
ul.append(pageLi)
|
103
|
+
ul.append(nextLi).append(lastLi)
|
104
|
+
|
105
|
+
div.append(ul)
|
106
|
+
|
data/src/spine.paginator.coffee
CHANGED
@@ -194,12 +194,10 @@ isArray = (value) ->
|
|
194
194
|
|
195
195
|
Paginator.isArray = isArray
|
196
196
|
|
197
|
-
#
|
198
|
-
# if your want use window.Paginator, please set `window.MyPaginatorName = 'Paginator' ` before require this.
|
199
|
-
#
|
200
197
|
if @MyPaginatorName?
|
201
198
|
@[@MyPaginatorName] = Paginator
|
202
|
-
|
199
|
+
else
|
200
|
+
@['Paginator'] = Paginator
|
203
201
|
|
204
202
|
|
205
203
|
if @Spine?
|
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: 0.
|
4
|
+
version: 1.0.0
|
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-
|
12
|
+
date: 2013-08-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Paginator for Spine
|
15
15
|
email:
|