source: extensions/GrumPluginClasses/js/pagesNavigator.js @ 19961

Last change on this file since 19961 was 16458, checked in by grum, 12 years ago

feature:2634- compatibility with Piwigo 2.4
Update URI + small changes

File size: 8.7 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: pagesNavigator.js
4 * file version: 1.0.0
5 * date: 2010-05-02
6 *
7 * JS file provided by the piwigo's plugin "GrumPluginClasses"
8 *
9 * -----------------------------------------------------------------------------
10 * Author     : Grum
11 *   email    : grum@piwigo.com
12 *   website  : http://www.grum.fr
13 *
14 *   << May the Little SpaceFrog be with you ! >>
15 * -----------------------------------------------------------------------------
16 *
17 * constructor : myPN = new pagesNavigator(containerId [, options]);
18 *
19 *  - containerId : the Id of the DOM element where the criteria will be added
20 *  - options : an object with this properties :
21 *          . numberItem:0,
22 *          . itemPerPage:25,
23 *          . defaultPage:0,
24 *          . displayNumPage:7,
25 *          . displayFirst:true,
26 *          . displayLast:true,
27 *          . displayPrevious:true,
28 *          . displayNext:true,
29 *          . textFirst:'&lt;&lt;',
30 *          . textLast:'&gt;&gt;',
31 *          . textPrevious:'&lt;',
32 *          . textNext:'&gt;',
33 *          . textMore:'...',
34 *          . onPageChange:null,
35 *          . classActive:'',
36 *          . classInactive:'',
37 *
38 *
39 * :: HISTORY ::
40 *
41 * | release | date       |
42 * | 1.0.0   | 2010/05/02 | start to coding
43 * |         |            |
44 * |         |            |
45 * |         |            |
46 * |         |            |
47 * |         |            |
48 * |         |            |
49 *
50 */
51
52
53
54function pagesNavigator(container)
55{
56  var itemsId = {
57          first:'iNavFirst',
58          last:'iNavLast',
59          previous:'iNavPrevious',
60          next:'iNavNext',
61          pageNumber:'iNavPage',
62          morePrevious:'iNavPrevMore',
63          moreNext:'iNavNextMore',
64          container:container,
65        },
66      pages = {
67          num:0,
68          current:1,
69        },
70      options = {
71        numberItem:0,
72        itemPerPage:25,
73        defaultPage:0,
74        displayNumPage:7,
75        displayFirst:true,
76        displayLast:true,
77        displayPrevious:true,
78        displayNext:true,
79        hideMode:'1,<', // "1": hide nav if only 1 page to display, "<": hide nav First, Previous, Next & Last if num of page < displayNumPage
80        textFirst:'&lt;&lt;',
81        textLast:'&gt;&gt;',
82        textPrevious:'&lt;',
83        textNext:'&gt;',
84        textMore:'...',
85        onPageChange:null,
86        classActive:'',
87        classInactive:'',
88        classDisabled:'',
89      };
90
91
92  /**
93   *
94   */
95  this.doAction = function(fct)
96  {
97    switch(fct)
98    {
99      case 'setOptions':
100        /* function 'setOptions' : allows to set options after the object was
101         * created
102         */
103        if(arguments.length==2)
104        {
105          setOptions(arguments[1]);
106        }
107        break;
108    }
109  };
110
111  /**
112   * calculate the number of page
113   *
114   * @param Integer numItems : number of item to manage
115   * @param Integer numPerPage : number of item to display per page
116   * @return Integer : number of page
117   */
118  var calcNumPages=function(numItems, numPerPage)
119  {
120    return(Math.ceil(numItems/numPerPage));
121  };
122
123  /**
124   *
125   * @param Object optionsToSet : set the given options
126   */
127  var setOptions = function(optionsToSet)
128  {
129    if(typeof optionsToSet=='object')
130    {
131      options = jQuery.extend(options, optionsToSet);
132
133      if(options.numberItem<=0) options.numberItem=0;
134      if(options.itemPerPage<=0) options.itemPerPage=25;
135      if(options.displayNumPage<=2) options.displayNumPage=8;
136
137      pages.num=calcNumPages(options.numberItem, options.itemPerPage);
138
139      if(options.defaultPage>0 && options.defaultPage<=pages.num) pages.current=options.defaultPage;
140      build();
141    }
142  };
143
144  /**
145   * build the page navigator and affect it in the container
146   *
147   */
148  var build = function()
149  {
150    re=/1/;
151    if( (re.exec(options.hideMode)==null)==false && pages.num==1)
152    {
153      $('#'+itemsId.container).html('');
154      return('');
155    }
156
157    var content="<ul id='"+container+"PageNavigator'>";
158
159    styleLI=" list-style:none;float:left; ";
160    re=/</;
161    if( (re.exec(options.hideMode)==null)==false && pages.num<=options.displayNumPage)
162    {
163      hideFPNL='display:none;';
164    }
165    else
166    {
167      hideFPNL='';
168    }
169
170
171    pnClass="class='cPnInactive "+options.classInactive+"'";
172
173    if(options.displayFirst) content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.first+"' "+pnClass+">"+options.textFirst+"</li>";
174    if(options.displayPrevious) content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.previous+"' "+pnClass+">"+options.textPrevious+"</li>";
175    content+="<li style='"+styleLI+"display:none;' id='"+itemsId.morePrevious+"' class='cPnDisabled "+options.classDisabled+"'>"+options.textMore+"</li>";
176
177    for(i=1;i<=pages.num;i++)
178    {
179      content+="<li style='"+styleLI+"display:none;' id='"+itemsId.pageNumber+i+"' "+pnClass+">"+i+"</li>";
180    }
181
182    content+="<li style='"+styleLI+";display:none;' id='"+itemsId.moreNext+"' class='cPnDisabled "+options.classDisabled+"'>"+options.textMore+"</li>";
183    if(options.displayNext) content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.next+"' "+pnClass+">"+options.textNext+"</li>";
184    if(options.displayLast) content+="<li style='"+styleLI+hideFPNL+"' id='"+itemsId.last+"' "+pnClass+">"+options.textLast+"</li>";
185
186    content+="</ul>";
187
188    $('#'+itemsId.container).css('visibility', 'hidden').html(content);
189    $('.cPnInactive').bind('click', onChangePage);
190
191    displayNav();
192
193    $('#'+itemsId.container).css('visibility', 'visible');
194  };
195
196
197  var displayNav = function()
198  {
199    // -1 for the current page
200    prev=Math.ceil((options.displayNumPage-1)/2);
201    if(pages.current-prev<=0)
202    {
203      prev=pages.current-1;
204    }
205    next=options.displayNumPage-1-prev;
206
207    if(pages.current+next>=pages.num)
208    {
209      prev+=(pages.current+next-pages.num);
210      next=pages.num-pages.current;
211    }
212
213    prev=pages.current-prev;
214    next=pages.current+next;
215
216    if(prev>1)
217    {
218      $('#'+itemsId.morePrevious).css('display', 'block');
219    }
220    else
221    {
222      $('#'+itemsId.morePrevious).css('display', 'none');
223    }
224
225    if(next<pages.num)
226    {
227      $('#'+itemsId.moreNext).css('display', 'block');
228    }
229    else
230    {
231      $('#'+itemsId.moreNext).css('display', 'none');
232    }
233
234    $('#'+itemsId.container+' ul li').each(
235      function ()
236      {
237        id=-1;
238        if(!(this.id==itemsId.first ||
239             this.id==itemsId.previous ||
240             this.id==itemsId.next ||
241             this.id==itemsId.last ||
242             this.id==itemsId.morePrevious ||
243             this.id==itemsId.moreNext))
244        {
245          re=/[0-9]*$/i;
246          id=re.exec(this.id)[0];
247
248
249          if(id>=prev && id <=next)
250          {
251            $(this).css('display', 'block');
252          }
253          else
254          {
255            $(this).css('display', 'none');
256          }
257
258          if(id==pages.current)
259          {
260            $(this).addClass('cPnActive '+options.classActive).removeClass('cPnInactive '+options.classInactive);
261          }
262          else
263          {
264            $(this).addClass('cPnInactive '+options.classInactive).removeClass('cPnActive '+options.classActive);
265          }
266        }
267
268
269
270        if( ((this.id==itemsId.first || this.id==itemsId.previous) && pages.current==1) ||
271            ((this.id==itemsId.last || this.id==itemsId.next) && pages.current==pages.num) ||
272            (this.id==itemsId.morePrevious || this.id==itemsId.moreNext ) )
273        {
274          $(this).addClass('cPnDisabled '+options.classDisabled).removeClass('cPnInactive '+options.classInactive);
275        }
276        else
277        {
278          $(this).addClass('cPnInactive '+options.classInactive).removeClass('cPnDisabled '+options.classDisabled);
279        }
280      }
281    );
282
283  };
284
285
286  /**
287   * this function is called every time a page is changed
288   *  - manage the navigation bar
289   *  - if defined, execute the callback function
290   */
291  var onChangePage = function (event)
292  {
293    //event.target.id : the clicked item
294    if(event.target.id==itemsId.first)
295    {
296      pages.current=1;
297    }
298    else if(event.target.id==itemsId.previous)
299    {
300      pages.current--;
301    }
302    else if(event.target.id==itemsId.next)
303    {
304      pages.current++;
305    }
306    else if(event.target.id==itemsId.last)
307    {
308      pages.current=pages.num;
309    }
310    else
311    {
312      re=/[0-9]*$/i;
313      page=re.exec(event.target.id)[0];
314
315      if(page==pages.current) return(false);
316
317      pages.current=eval(page);
318    }
319
320    if(pages.current<=0) pages.current=1;
321    if(pages.current>=pages.num) pages.current=pages.num;
322
323    displayNav();
324    if(options.onPageChange!=null && jQuery.isFunction(options.onPageChange)) options.onPageChange(pages.current);
325  };
326
327  if(arguments.length==2)
328  {
329    setOptions(arguments[1]);
330  }
331
332}
Note: See TracBrowser for help on using the repository browser.