From 26900b02cc656a600d67454547cf1699174c713b Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Tue, 18 Jan 2011 06:22:59 +0100 Subject: [PATCH] Pager rewrite for bookmark/history support. --- mkdru.client.js | 76 ++++++++++++++++++++++++++++++----------------------- mkdru.theme.js | 78 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 85 insertions(+), 69 deletions(-) diff --git a/mkdru.client.js b/mkdru.client.js index b8e7f4c..a672f21 100644 --- a/mkdru.client.js +++ b/mkdru.client.js @@ -7,6 +7,7 @@ var mkdru = { submitted: false, pz2: null, totalRec: 0, + pagerRange: 6, sourceMax: Drupal.settings.mkdru.source_max, subjectMax: Drupal.settings.mkdru.subject_max, authorMax: Drupal.settings.mkdru.author_max, @@ -17,16 +18,16 @@ var mkdru = { perpage: 20, sort: 'relevance', filter: null, - query:null, + query:null }, - state: {}, + state: {} }; // So we can use jQuery BBQ with Drupal 6 and its 1.2.6 jQuery -if (!$.isArray) $.isArray = function( obj ) { - return toString.call(obj) === "[object Array]"; +if (!$.isArray) $.isArray = function(obj) { + return Object.prototype.toString.call(obj) === "[object Array]"; }, @@ -40,15 +41,10 @@ mkdru.pz2Init = function () { mkdru.pz2Show = function (data) { mkdru.totalRec = data.merged; - - var pagerHtml = Drupal.theme('mkdruPager', data, mkdru.state.page, - Math.ceil(mkdru.totalRec / mkdru.state.perpage)); - $('.mkdru-pager').html(pagerHtml); - $('.mkdru-next').bind('click', mkdru.nextPage); - $('.mkdru-prev').bind('click', mkdru.prevPage); - - var countsHtml = 'Displaying: ' + (data.start + 1) + ' to ' - + (data.start + data.num) + ' of ' + data.merged + ' (found: ' + data.total + ')'; + $('.mkdru-pager').html(mkdru.generatePager()); + var countsHtml = Drupal.t('Displaying: ') + (data.start + 1) + + Drupal.t(' to ') + (data.start + data.num) + Drupal.t(' of ') + + data.merged + Drupal.t(' (found: ') + data.total + ')'; $('.mkdru-counts').html(countsHtml); var html = ""; @@ -125,7 +121,6 @@ mkdru.pz2Record = function (data) { mkdru.stateFromObject = function (obj) { for (var key in mkdru.defaultState) { if (typeof(obj[key]) != "undefined") { - dump (key + " :: " + obj[key] + "\n"); mkdru.state[key] = obj[key]; } else { @@ -169,12 +164,15 @@ mkdru.uiFromState = function () { }; mkdru.hashChange = function () { - dump("Submidded? " + mkdru.submitted + "\n"); var hash = $.deparam.fragment(); + // Request for details if (typeof(hash.recid) !== "undefined") { mkdru.pz2.record(hash.recid); } + // Other internal link else { + mkdru.stateFromObject($.deparam.fragment()); + mkdru.pz2.showPage(mkdru.state.page-1); $('.mkdru-detail').hide(); $('.mkdru-results').show(); } @@ -208,6 +206,7 @@ mkdru.pollDropDowns = function () { mkdru.state.sort = $('.mkdru-sort').attr('value'); if (!mkdru.submitted) return false; mkdru.resetPage(); + mkdru.hashFromState(); mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort); return false; }; @@ -232,6 +231,34 @@ mkdru.limitTarget = function (id, name) { return false; }; +mkdru.generatePager = function () { + var total = Math.ceil(mkdru.totalRec / mkdru.state.perpage); + var first = (mkdru.state.page - mkdru.pagerRange > 0) + ? mkdru.state.page - mkdru.pagerRange : 1; + var last = first + 2 * mkdru.pagerRange < total + ? first + 2 * mkdru.pagerRange : total; + var prev = null; + var next = null; + var pages = []; + + if ((mkdru.state.page - 1) >= first) { + prev = "#" + $.param.fragment($.param.fragment( + window.location.href, {page: mkdru.state.page - 1})) + } + if ((mkdru.state.page + 1) <= total) { + next = "#" + $.param.fragment($.param.fragment( + window.location.href, {page: mkdru.state.page + 1})) + } + + for (var i = first; i <= last; i++) { + pages.push("#" + $.param.fragment($.param.fragment( + window.location.href, {page: i}))); + } + + return Drupal.theme('mkdruPager', pages, first, mkdru.state.page, + total, prev, next); +}; + mkdru.resetPage = function () { mkdru.state.page = 1; mkdru.totalRec = 0; @@ -242,21 +269,6 @@ mkdru.showPage = function (pageNum) { mkdru.pz2.showPage(pageNum-1); }; -mkdru.nextPage = function () { - if (mkdru.totalRec - mkdru.state.perpage * mkdru.state.page > 0) { - mkdru.pz2.showNext(); - mkdru.state.page++; - mkdru.hashFromState(); - } -}; - -mkdru.prevPage = function () { - if (mkdru.pz2.showPrev() != false) { - mkdru.state.page--; - mkdru.hashFromState(); - } -}; - // wait until the DOM is ready, bind events @@ -268,7 +280,6 @@ $(document).ready(function () { $('.mkdru-perpage').bind('change', function () { mkdru.pollDropDowns() }); $('.mkdru-sort').bind('change', function () { mkdru.pollDropDowns() }); - dump("NEW PZ2!\n"); mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show, "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way "pazpar2path": mkdru.pazpar2path, @@ -286,8 +297,7 @@ $(document).ready(function () { mkdru.stateFromHash(); // and update UI to match mkdru.uiFromState(); - dump("QUERY:" + mkdru.state.query + "\n"); - + // ting thing if (typeof(Drupal.settings.mkdru.query) !== "undefined") { mkdru.state.query = Drupal.settings.mkdru.query diff --git a/mkdru.theme.js b/mkdru.theme.js index da581ff..7e660f6 100644 --- a/mkdru.theme.js +++ b/mkdru.theme.js @@ -18,7 +18,7 @@ Drupal.theme.prototype.mkdruResult = function(hit, num, detailLink) { }; Drupal.theme.prototype.mkdruDetail = function(data) { - var html = '
'; + var html = '
'; if (data["md-title"] != undefined) { html += '
Title: '+data["md-title"]; if (data["md-title-remainder"] !== undefined) { @@ -44,46 +44,52 @@ Drupal.theme.prototype.mkdruDetail = function(data) { return html; }; -Drupal.theme.prototype.mkdruPager = function (data, curPage, pages) { - var onsides = 6; - - var firstClkbl = ( curPage - onsides > 0 ) - ? curPage - onsides - : 1; - - var lastClkbl = firstClkbl + 2*onsides < pages - ? firstClkbl + 2*onsides - : pages; - - var prev = '<< Prev | '; - if (curPage > 1) - var prev = '' - +'<< Prev | '; - - var middle = ''; - for(var i = firstClkbl; i <= lastClkbl; i++) { - var numLabel = i; - if(i == curPage) - numLabel = '' + i + ''; +/** + * Pager theme + * + * @param pages + * Array of hrefs for page links. + * @param start + * Number of first page. + * @param current + * Number of current page. + * @param total + * Total number of pages. + * @param prev + * Href for previous page. + * @param next + * Href for next page. + */ +Drupal.theme.prototype.mkdruPager = function (pages, start, current, total, prev, next) { + var html = ""; + if (prev) + html += '<< ' + + Drupal.t("Prev") + ' | '; + else + html += '<< ' + Drupal.t("Prev") + + ' | '; - middle += ' ' - + numLabel + ' '; - } + if (start > 1) + html += '...'; - var next = ' | Next >>'; - if (pages - curPage > 0) - var next = ' | ' - +'Next >>'; + for (var i = 0; i < pages.length; i++) { + if (i + start == current) + html += ' ' + (i + start) + ''; + else + html += ' ' + (i + start) + ''; + } - predots = ''; - if (firstClkbl > 1) - predots = '...'; + if (total > i) + html += ' ...'; - postdots = ''; - if (lastClkbl < pages) - postdots = '...'; + if (next) + html += ' | ' + + Drupal.t("Next") + ' >>'; + else + html += ' | ' + Drupal.t("Next") + + ' >>'; - return (prev + predots + middle + postdots + next); + return html; }; Drupal.theme.prototype.mkdruTerm = function (term, freq, linkClass, id) { -- 1.7.10.4