spine_paginator 0.1.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -19,7 +19,6 @@ Add this line to your application's Gemfile:
19
19
  Add the following to your `app/assets/javascripts/application.js` file.
20
20
 
21
21
  //= require spine
22
- //= require handlebars
23
22
  //= require spine.paginator
24
23
  //= require spine.pagination_controller
25
24
 
data/component.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "spine.paginator",
3
3
  "repo": "vkill/spine_paginator",
4
4
  "description": "Paginator/Pagination for Spine.",
5
- "version": "0.1.1",
5
+ "version": "1.0.0",
6
6
  "keywords": [
7
7
  "paginator",
8
8
  "spine",
@@ -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); PaginationController.__super__.constructor.apply(this, arguments);
39
- if (this.constructor.MODEL == null) {
40
- throw new Error("please defined class variable MODEL");
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.constructor.PER_PAGE = this.constructor.PER_PAGES[0];
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.PAGE = 1;
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.PAGE = page;
43
+ PaginationController.prototype.toPage = function(page) {
44
+ this.page = page;
56
45
  return this.load();
57
46
  };
58
47
 
59
- PaginationController.load = function() {
60
- var pagination;
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.PAGINATION = pagination;
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
- var pagination;
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.constructor.toPage(page);
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.constructor.PAGINATION.firstPage();
89
+ page = this.pagination.firstPage();
107
90
  break;
108
91
  case 'prev':
109
- page = this.constructor.PAGINATION.currentPage() - 1;
92
+ page = this.pagination.currentPage() - 1;
110
93
  break;
111
94
  case 'next':
112
- page = this.constructor.PAGINATION.currentPage() + 1;
95
+ page = this.pagination.currentPage() + 1;
113
96
  break;
114
97
  case 'last':
115
- page = this.constructor.PAGINATION.lastPage();
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 pagination, source;
132
-
133
- pagination = this.constructor.PAGINATION;
134
- source = "<div class=\"pagination pagination-small pull-right\">\n <ul>\n <li {{#unless hasFirst}}class=\"disabled\"{{/unless}}>\n <a href=\"javascript:void(0);\" data-page=\"first\">first</a>\n </li>\n <li {{#unless hasPrev}}class=\"disabled\"{{/unless}}>\n <a href=\"javascript:void(0);\" data-page=\"prev\">prev</a>\n </li>\n {{#each pages}}\n {{#if this.gap}}\n <li class=\"disabled\">\n <a href=\"javascript:void(0);\" data-page='gap'>...</a>\n </li>\n {{else}}\n <li {{#if this.current}}class=\"active\"{{/if}}>\n <a href=\"javascript:void(0);\" data-page={{this.number}}>{{this.number}}</a>\n </li>\n {{/if}}\n {{/each}}\n <li {{#unless hasNext}}class=\"disabled\"{{/unless}}>\n <a href='javascript:void(0);' data-page=\"next\">next</a>\n </li>\n <li {{#unless hasLast}}class=\"disabled\"{{/unless}}>\n <a href='javascript:void(0);' data-page=\"last\">last</a>\n </li>\n </ul>\n</div>";
135
- return Handlebars.compile(source)(pagination.locals);
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,s=function(t,s){return function(){return t.apply(s,arguments)}},e={}.hasOwnProperty,i=function(t,s){function i(){this.constructor=t}for(var r in s)e.call(s,r)&&(t[r]=s[r]);return i.prototype=s.prototype,t.prototype=new i,t.__super__=s.prototype,t};t=this.Spine||require("spine"),t.PaginationController=function(t){function e(){if(this.templateHtmlDataEmpty=s(this.templateHtmlDataEmpty,this),this.render=s(this.render,this),e.__super__.constructor.apply(this,arguments),null==this.constructor.MODEL)throw Error("please defined class variable MODEL");if(this.constructor.PER_PAGE=this.constructor.PER_PAGES[0],null==this.constructor.PER_PAGE)throw Error("please defined class variable PER_PAGES");this.constructor.MODEL.bind(this.constructor.PAGINATE_EVENT,this.render)}return i(e,t),e.MODEL=null,e.PER_PAGES=[10,20,30,40],e.PAGINATE_EVENT="paginate",e.PAGE=1,e.PER_PAGE=null,e.PAGINATION=null,e.refresh=function(){return this.PAGE=1,this.load()},e.toPage=function(t){return this.PAGE=t,this.load()},e.load=function(){var t;return t=this.MODEL.page(this.PAGE,{perPage:this.PER_PAGE}),this.PAGINATION=t,this.MODEL.trigger(this.PAGINATE_EVENT)},e.prototype.render=function(){var t;return t=this.constructor.PAGINATION,t.records.length>0?this.html(this.templateHtml()):this.html(this.templateHtmlDataEmpty())},e.prototype.events={"click a[data-page]":"clickPage"},e.prototype.clickPage=function(t){var s;return t.preventDefault(),s=this.getPageFromE(t),null!=s?this.constructor.toPage(s):void 0},e.prototype.getPageFromE=function(t){var s,e,i;if(s=$(t.target),s.parent(".disabled, .active").length>0)return null;switch(i=s.data("page"),e=null,i){case"first":e=this.constructor.PAGINATION.firstPage();break;case"prev":e=this.constructor.PAGINATION.currentPage()-1;break;case"next":e=this.constructor.PAGINATION.currentPage()+1;break;case"last":e=this.constructor.PAGINATION.lastPage();break;case"gap":e=null;break;default:e=i}return e},e.prototype.templateHtmlDataEmpty=function(){return""},e.prototype.templateHtml=function(){var t,s;return t=this.constructor.PAGINATION,s='<div class="pagination pagination-small pull-right">\n <ul>\n <li {{#unless hasFirst}}class="disabled"{{/unless}}>\n <a href="javascript:void(0);" data-page="first">first</a>\n </li>\n <li {{#unless hasPrev}}class="disabled"{{/unless}}>\n <a href="javascript:void(0);" data-page="prev">prev</a>\n </li>\n {{#each pages}}\n {{#if this.gap}}\n <li class="disabled">\n <a href="javascript:void(0);" data-page=\'gap\'>...</a>\n </li>\n {{else}}\n <li {{#if this.current}}class="active"{{/if}}>\n <a href="javascript:void(0);" data-page={{this.number}}>{{this.number}}</a>\n </li>\n {{/if}}\n {{/each}}\n <li {{#unless hasNext}}class="disabled"{{/unless}}>\n <a href=\'javascript:void(0);\' data-page="next">next</a>\n </li>\n <li {{#unless hasLast}}class="disabled"{{/unless}}>\n <a href=\'javascript:void(0);\' data-page="last">last</a>\n </li>\n </ul>\n</div>',Handlebars.compile(s)(t.locals)},e}(t.Controller)}).call(this);
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);
@@ -264,6 +264,8 @@
264
264
 
265
265
  if (this.MyPaginatorName != null) {
266
266
  this[this.MyPaginatorName] = Paginator;
267
+ } else {
268
+ this['Paginator'] = Paginator;
267
269
  }
268
270
 
269
271
  if (this.Spine != null) {
@@ -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&&(this[this.MyPaginatorName]=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);
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
- @MODEL = User
16
- @PER_PAGES = [5, 10, 20, 30]
17
- @PAGINATE_EVENT = "paginate"
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 Ctrl.IndexPagination.PAGINATE_EVENT, @render
56
+ User.bind @owner.pagination.paginateEvent, @render
23
57
 
24
58
  render: =>
25
59
  @html @templateHtml()
26
60
 
27
61
  templateHtml: =>
28
- collection = Ctrl.IndexPagination.PAGINATION.records
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
- @tbody = new Ctrl.IndexTbody
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
- Ctrl.IndexPagination.refresh()
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://twitter.github.io/bootstrap/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
+ <!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, _ref1,
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
- _ref1 = IndexPagination.__super__.constructor.apply(this, arguments);
38
- return _ref1;
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(Ctrl.IndexPagination.PAGINATE_EVENT, this.render);
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 = Ctrl.IndexPagination.PAGINATION.records;
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 Ctrl.IndexPagination.refresh();
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, _ref2, _ref3, _results;
105
+ var _i, _ref1, _ref2, _results;
108
106
 
109
107
  _results = [];
110
- for (num = _i = _ref2 = 'A'.charCodeAt(0), _ref3 = 'z'.charCodeAt(0); _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; num = _ref2 <= _ref3 ? ++_i : --_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://twitter.github.io/bootstrap/assets/css/bootstrap.css" rel="stylesheet"
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"
@@ -1,3 +1,3 @@
1
1
  module SpinePaginator
2
- VERSION = "0.1.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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
- throw new Error("please defined class variable MODEL") unless @constructor.MODEL?
25
- @constructor.PER_PAGE = @constructor.PER_PAGES[0]
26
- throw new Error("please defined class variable PER_PAGES") unless @constructor.PER_PAGE?
27
- @constructor.MODEL.bind @constructor.PAGINATE_EVENT, @render
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
- @refresh: ->
30
- @PAGE = 1
29
+ refresh: ->
30
+ @page = 1
31
31
  @load()
32
32
 
33
- @toPage: (page)->
34
- @PAGE = page
33
+ toPage: (page)->
34
+ @page = page
35
35
  @load()
36
36
 
37
- @load: ->
38
- pagination = @MODEL.page(@PAGE, {perPage: @PER_PAGE})
39
- @PAGINATION = pagination
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 = @constructor.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
- @constructor.toPage(page)
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 = @constructor.PAGINATION.firstPage()
69
+ page = @pagination.firstPage()
73
70
  when 'prev'
74
- page = @constructor.PAGINATION.currentPage() - 1
71
+ page = @pagination.currentPage() - 1
75
72
  when 'next'
76
- page = @constructor.PAGINATION.currentPage() + 1
73
+ page = @pagination.currentPage() + 1
77
74
  when 'last'
78
- page = @constructor.PAGINATION.lastPage()
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
- pagination = @constructor.PAGINATION
90
- source = """
91
- <div class="pagination pagination-small pull-right">
92
- <ul>
93
- <li {{#unless hasFirst}}class="disabled"{{/unless}}>
94
- <a href="javascript:void(0);" data-page="first">first</a>
95
- </li>
96
- <li {{#unless hasPrev}}class="disabled"{{/unless}}>
97
- <a href="javascript:void(0);" data-page="prev">prev</a>
98
- </li>
99
- {{#each pages}}
100
- {{#if this.gap}}
101
- <li class="disabled">
102
- <a href="javascript:void(0);" data-page='gap'>...</a>
103
- </li>
104
- {{else}}
105
- <li {{#if this.current}}class="active"{{/if}}>
106
- <a href="javascript:void(0);" data-page={{this.number}}>{{this.number}}</a>
107
- </li>
108
- {{/if}}
109
- {{/each}}
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
+
@@ -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.1.2
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-05-25 00:00:00.000000000 Z
12
+ date: 2013-08-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Paginator for Spine
15
15
  email: