1+ /**
2+ * Paginate plugin for Boostrap
3+ * An example of HTML to use it:
4+ *
5+ <div id="paginate"></div>
6+
7+ * And the JavaScript:
8+ *
9+ $("#paginate").paginate({
10+ pages: 5,
11+ pageChange: function(index) {
12+ $("table tbody").load("/user/paginate/" + index);
13+ }
14+ });
15+ */
16+
17+ ! function ( $ ) {
18+ "use strict" ;
19+
20+ /* PAGINATE PUBLIC CLASS DEFINITION
21+ * ============================== */
22+
23+ var Paginate = function ( element , options ) {
24+ var self = this ;
25+
26+ // Base
27+ this . $element = $ ( element ) ;
28+ this . options = $ . extend ( { } , $ . fn . paginate . defaults , options ) ;
29+ this . index = 0 ;
30+ this . maxLink = 10 ;
31+ this . indexMin = 0 ;
32+ this . indexMax = this . indexMin + this . maxLink ;
33+
34+ // Decoration
35+ this . $element . addClass ( "pagination" ) ;
36+ this . options . align && this . $element . addClass ( "pagination-" + this . options . align ) ;
37+ this . options . size && this . $element . addClass ( "pagination-" + this . options . size ) ;
38+
39+ // Childs
40+ this . $ul = $ ( "<ul></ul>" ) ;
41+
42+ if ( this . options . previousPlacement == "detached" ) {
43+ this . $previous = $ ( "<li class='previous'><a href='#' data-index='previous'>" + this . options . detachedPreviousText + "</a></li>" ) ;
44+
45+ } else {
46+ this . $previous = $ ( "<li><a href='#' data-index='previous'>" + this . options . previousText + "</a></li>" ) ;
47+ }
48+
49+ if ( this . options . previousPlacement == "detached" ) {
50+ this . $next = $ ( "<li class='next'><a href='#' data-index='next'>" + this . options . detachedNextText + "</a></li>" ) ;
51+
52+ } else {
53+ this . $next = $ ( "<li><a href='#' data-index='next'>" + this . options . nextText + "</a></li>" ) ;
54+ }
55+
56+ this . insertIndexes ( ) ;
57+
58+ // Listeners
59+ this . $element . on ( "click" , "li:not(.disabled) a" , function ( event ) {
60+ event . preventDefault ( ) ;
61+ var $a = $ ( this ) , index = $a . data ( "index" ) ;
62+
63+ switch ( index ) {
64+ case "previous" :
65+ self . index -- ;
66+ break ;
67+
68+ case "next" :
69+ self . index ++ ;
70+ break ;
71+
72+ default :
73+ self . index = window . parseInt ( index ) ;
74+ break ;
75+ }
76+
77+ self . update ( ) ;
78+ $ . isFunction ( self . options . pageChange ) && self . options . pageChange ( self . index ) ;
79+ self . $element . trigger ( "paginate" , [ self . index ] ) ;
80+ } ) ;
81+
82+ // Display the element
83+ this . update ( ) ;
84+ this . $element . append ( this . $ul ) ;
85+ } ;
86+
87+ Paginate . prototype . insertIndexes = function ( ) {
88+ var length = Math . min ( this . options . pages , this . indexMax )
89+
90+ this . $ul . append ( this . $previous ) ;
91+
92+ for ( var i = this . indexMin ; i < length ; i ++ ) {
93+ this . $ul . append ( "<li><a href='#' data-index='" + i + "'>" + ( i + 1 ) + "</a></li>" ) ;
94+ }
95+
96+ this . $ul . append ( this . $next ) ;
97+
98+ if ( length <= 0 ) {
99+ this . $element . hide ( ) ;
100+
101+ } else {
102+ this . $element . show ( ) ;
103+ }
104+ } ;
105+
106+ Paginate . prototype . update = function ( ) {
107+ if ( this . index < this . indexMin || this . indexMax <= this . index + 1 ) {
108+ // We have to flush the elements
109+ this . $ul . find ( "li" ) . remove ( ) ;
110+
111+ this . indexMin = this . index < this . indexMin ? this . indexMin + 1 - this . maxLink : this . indexMax - 1 ;
112+ this . indexMax = this . indexMin + this . maxLink ;
113+ this . insertIndexes ( ) ;
114+
115+ } else {
116+ this . $ul . find ( "li" ) . removeClass ( "disabled" ) ;
117+ }
118+
119+ if ( this . index <= 0 ) {
120+ this . $previous . addClass ( "disabled" ) ;
121+
122+ } else {
123+ this . $previous . removeClass ( "disabled" ) ;
124+ }
125+
126+ this . $ul . find ( "a[data-index='" + this . index + "']" ) . parents ( "li:first" ) . addClass ( "disabled" ) ;
127+
128+ if ( this . index < this . options . pages - 1 ) {
129+ this . $next . removeClass ( "disabled" ) ;
130+
131+ } else {
132+ this . $next . addClass ( "disabled" ) ;
133+ }
134+ } ;
135+
136+ Paginate . prototype . index = function ( ) {
137+ return index ;
138+ } ;
139+
140+ Paginate . prototype . option = function ( options ) {
141+ if ( ! options ) {
142+ return ;
143+ }
144+
145+ var changePrevious = false , changeNext = false ;
146+
147+ for ( var i in options ) {
148+ switch ( i ) {
149+ case "previousPlacement" :
150+ case "previousText" :
151+ changePrevious = true ;
152+
153+ case "nextText" :
154+ case "detachedNextText" :
155+ changeNext = true ;
156+
157+ default :
158+ this . options [ i ] = options [ i ] ;
159+ break ;
160+ }
161+ }
162+
163+ if ( changePrevious ) {
164+ this . $previous . find ( "a" ) . text ( this . options . previousPlacement == "detached" ? this . options . detachedPreviousText : this . options . previousText ) ;
165+ }
166+
167+ if ( changeNext ) {
168+ this . $next . find ( "a" ) . text ( this . options . nextPlacement == "detached" ? this . options . detachedNextText : this . options . nextText ) ;
169+ }
170+
171+ this . update ( ) ;
172+ } ;
173+
174+ /* PAGINATE PLUGIN DEFINITION
175+ * ======================== */
176+
177+ $ . fn . paginate = function ( opts ) {
178+ return this . each ( function ( ) {
179+ var $this = $ ( this ) , data = $this . data ( "paginate" ) , options = typeof opts == "object" && opts ;
180+
181+ if ( data ) {
182+ data . option ( options )
183+
184+ } else {
185+ $this . data ( "paginate" , new $ . fn . paginate . Constructor ( this , options ) ) ;
186+ }
187+ } ) ;
188+ } ;
189+
190+ $ . fn . paginate . defaults = {
191+ align : "centered" , /* values: centered, right, left, null */
192+ size : null , /* values: large, small, mini, null */
193+ previousText : "«" ,
194+ nextText : "»" ,
195+ detachedPreviousText : "← Older" ,
196+ detachedNextText : "Newer →" ,
197+ previousPlacement : "normal" , /* values: normal, detached */
198+ nextPlacement : "normal" , /* values: normal, detached */
199+ pages : 10 , /* Nb page max */
200+ pageChange : function ( index ) { }
201+ } ;
202+
203+ $ . fn . paginate . Constructor = Paginate ;
204+
205+ /* PAGINATE DATA-API
206+ * =============== */
207+
208+ $ ( function ( ) {
209+ $ ( "body" ) . find ( "div[data-role=paginate], div.paginate" ) . paginate ( ) ;
210+ } ) ;
211+
212+ } ( window . jQuery ) ;
0 commit comments