PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/webapp/public/js/ie.paging.js

http://thoughtsite.googlecode.com/
JavaScript | 104 lines | 60 code | 13 blank | 31 comment | 19 complexity | ee7655d2e6f341e05b2102ef594fd830 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16/**
 17 * Library for ideaexchange js Paging
 18 * 
 19 * @author Abhishek
 20 */
 21ie.Paging = {
 22    previousCss : 'previous',
 23    nextCss : 'next',
 24    selectedPageCss : 'page_sel',
 25    pageCss : 'page_no',
 26    recordPerPage : '20',
 27    pageRange : 10
 28};
 29
 30
 31/**
 32 * get HTML by providing offset
 33 */
 34ie.Paging.getHTML = function(jsonData, callback) {
 35    var outputHtml = '';
 36    if(undefined != jsonData.previous && jsonData.previous >= 0) {
 37        outputHtml += '<a title="Previous" onclick="' + callback + '(' + jsonData.previous + ')" href="javascript:void(0);" class="' + this.previousCss + '">&laquo; Previous</a>';
 38    }
 39    if(undefined != jsonData.next && jsonData.next >= 0) {
 40        outputHtml += '<a title="Next" onclick="' + callback + '(' + jsonData.next + ')" href="javascript:void(0);" class="' + this.previousCss + '">Next &raquo;</a>';
 41    }
 42    return outputHtml;
 43}
 44
 45/**
 46 * get HTML by providing offset
 47 */
 48ie.Paging.getHTMLByOffset = function(totalRecords, startOffset) {
 49    if(0 == totalRecords % this.recordPerPage) {
 50        var totalPageCount = totalRecords/this.recordPerPage + 1;
 51    }
 52    else {
 53        var totalPageCount = totalRecords/this.recordPerPage;
 54    }
 55        selectedPage = (startOffset / this.recordPerPage) + 1;
 56    this.getHTMLByPage(totalPageCount, startOffset);
 57}
 58
 59/**
 60 * get HTML by providing offset
 61 */
 62ie.Paging.getHTMLByPage = function(totalPageCount, selectedPage) {
 63    var minRange = (this.pageRange % 2 == 0) ? (this.pageRange / 2) - 1 : (this.pageRange - 1) / 2;
 64    var maxRange = (this.pageRange % 2 == 0) ? minRange + 1 : minRange;
 65    // selectedPage will be current page
 66    var minPage = selectedPage - minRange;
 67    var maxPage = selectedPage + maxRange;
 68    minPage = (minPage < 1) ? 1 : minPage;
 69    maxPage = (maxPage < (minPage + this.pageRange - 1)) ? minPage + this.pageRange - 1 : maxPage;
 70    
 71    // totalPageCount is total pages for records
 72    if (maxPage > totalPageCount) {
 73        minPage = (minPage > 1) ? totalPageCount - this.pageRange + 1 : 1;
 74        maxPage = totalPageCount;
 75    }
 76    
 77    minPage = (minPage < 1) ? 1 : minPage;
 78
 79    
 80    
 81    
 82    var outputHtml = '';
 83    // Add previous page link
 84    if (selectedPage != 1) {
 85        outputHtml += '<a title="Previous" href="#" class="' + this.previousCss + '">&laquo;</a>';
 86    }
 87    
 88    var pageCount = 1;
 89    for ( var counter = minPage; counter < maxPage; counter++) {
 90        if (selectedPage == counter) {
 91            outputHtml += '<a href="#" class="' + this.selectedPageCss + '">'
 92                    + counter + '</a>';
 93        } else {
 94            outputHtml += '<a href="#" class="' + this.pageCss + ' '
 95                    + this.pageCss + '">' + counter + '</a>';
 96        }
 97        pageCount++;
 98    }
 99
100    if (selectedPage < totalPageCount) {
101        outputHtml += '<a title="Next" href="#" class="' + this.nextCss + '">&raquo;</a>';
102    }
103    return outputHtml;
104}