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

Last change on this file since 7175 was 7175, checked in by grum, 14 years ago

Packing js files + add categorySelector functionnalities

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