From 2b5e7db83479b99d46023a9e832d374f66fad453 Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Thu, 3 Feb 2011 05:07:52 +0100 Subject: [PATCH] Faceting rewrite w/BBQ. --- mkdru.client.js | 189 +++++++++++++++++++++++++++++-------------------------- mkdru.module | 6 +- mkdru.theme.js | 32 ++++++++-- 3 files changed, 131 insertions(+), 96 deletions(-) diff --git a/mkdru.client.js b/mkdru.client.js index 371818e..add8bd1 100644 --- a/mkdru.client.js +++ b/mkdru.client.js @@ -1,17 +1,29 @@ // Set up namespace and some state. var mkdru = { // Settings to pass to pz2.js - usesessions: Drupal.settings.mkdru.use_sessions === '1', + useSessions: Drupal.settings.mkdru.use_sessions === '1', // showResponseType: 'json', // Variables - submitted: false, + active: 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, pazpar2Path: Drupal.settings.mkdru.pz2_path, + // Facets + facets: { + source: { + pz2Name: 'xtargets', + max: Drupal.settings.mkdru.source_max + }, + subject: { + pz2Name: 'subject', + max: Drupal.settings.mkdru.subject_max + }, + author: { + pz2Name: 'author', + max: Drupal.settings.mkdru.author_max + } + }, // State defaultState: { page: 1, @@ -29,7 +41,35 @@ var mkdru = { // So we can use jQuery BBQ with Drupal 6 and its 1.2.6 jQuery if (!$.isArray) $.isArray = function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; -}, +}; + +// BBQ has no handy way to remove params without changing the hash. +// This takes an object to add and an array of keys to delete. +mkdru.hashAddDelMany = function (add, del) { + var newHash = $.deparam.fragment(); + if (typeof(add) === 'object') + $.extend(newHash, add); + if ($.isArray(del)) + for (var i=0; i < del.length; i++) + if (newHash[del[i]] !== 'undefined') + delete newHash[del[i]]; + return $.param.fragment("#", newHash); +} + +// It's sometimes cumbersome that object literals can't take variable keys. +mkdru.hashAddDelOne = function (key, value, del) { + var toAdd; + var toDel; + if (key && value) { + var toAdd = {}; + toAdd[key] = value; + } + if (del) { + var toDel = []; + toDel.push(del); + } + return mkdru.hashAddDelMany(toAdd, toDel); +} @@ -71,41 +111,18 @@ mkdru.pz2Status = function (data) { }; mkdru.pz2Term = function (data) { - var html = ""; - for (var i = 0; i < data.xtargets.length && i < mkdru.sourceMax; i++ ) { - html += Drupal.theme('mkdruTerm', data.xtargets[i].name, data.xtargets[i].freq, - 'mkdru-facet-source', data.xtargets[i].id); - } - $('.mkdru-facet-sources').html(html); - - html = ""; - for (var i = 0; i < data.subject.length && i < mkdru.subjectMax; i++ ) { - html += Drupal.theme('mkdruTerm', data.subject[i].name, data.subject[i].freq, - 'mkdru-facet-subject', data.subject[i].id); - } - $('.mkdru-facet-subjects').html(html); - - html = ""; - for (var i = 0; i < data.author.length && i < mkdru.authorMax; i++ ) { - html += Drupal.theme('mkdruTerm', data.author[i].name, data.author[i].freq, - 'mkdru-facet-author', data.author[i].id); + for (var facet in mkdru.facets) { + // facet is limited + if (mkdru.state['limit_' + facet]) { + $('.mkdru-facet-' + facet).html(Drupal.theme('mkdruFacetLimit', + data[mkdru.facets[facet].pz2Name][0], + mkdru.hashAddDelOne(null, null, 'limit_' + facet))); + } else { + $('.mkdru-facet-' + facet).html(Drupal.theme('mkdruFacet', + data[mkdru.facets[facet].pz2Name], + facet, mkdru.facets[facet].max)); + } } - $('.mkdru-facet-authors').html(html); - - $('.mkdru-facet-source').bind('click', function (e) { - mkdru.limitTarget(this.getAttribute('target_id'), this.firstChild.nodeValue); - return false; - }); - - $('.mkdru-facet-subject').bind('click', function (e) { - mkdru.limitQuery('su', this.firstChild.nodeValue); - return false; - }); - - $('.mkdru-facet-author').bind('click', function (e) { - mkdru.limitQuery('au', this.firstChild.nodeValue); - return false; - }); }; mkdru.pz2ByTarget = function (data) { @@ -127,14 +144,10 @@ mkdru.pz2Record = function (data) { // populate state from an object and fill in the blanks with defaults mkdru.stateFromObject = function (obj) { - for (var key in mkdru.defaultState) { - if (typeof(obj[key]) != "undefined") { + mkdru.state = $.extend({}, mkdru.defaultState); + for (var key in mkdru.defaultState) + if (typeof(obj[key]) != "undefined") mkdru.state[key] = obj[key]; - } - else { - mkdru.state[key] = mkdru.defaultState[key]; - } - } }; // populate state from current window's hash string @@ -151,7 +164,7 @@ mkdru.hashFromState = function () { alteredState[key] = mkdru.state[key]; } } - $.bbq.pushState(alteredState); + $.bbq.pushState(alteredState, 2); }; // update mkdru_form theme's ui to match state @@ -179,6 +192,13 @@ mkdru.hashChange = function () { } // Other internal link else { + // need to run search again to limit targets + for (key in mkdru.state) { + if (key.substring(0,5) === 'limit') { + mkdru.search(); + break; + } + } mkdru.pz2.showPage(mkdru.state.page-1); $('.mkdru-detail').hide(); $('.mkdru-results').show(); @@ -187,33 +207,47 @@ mkdru.hashChange = function () { -//search bo handler ONLY +//form submit handler mkdru.submitQuery = function () { + // new query, back to defaults (shallow copy) + mkdru.state = $.extend({}, mkdru.defaultState); mkdru.state.query = $('.mkdru-search input:text').attr('value'); mkdru.pollDropDowns(); - mkdru.resetPage(); mkdru.hashFromState(); mkdru.search(); - mkdru.submitted = true; + mkdru.active = true; return false; }; -//criteria drop-downs (perpage,sort) handler ONLY +//criteria drop-down (perpage, sort) handler mkdru.submitCriteria = function () { mkdru.pollDropDowns(); //search is not ON, do nothing - if (!mkdru.submitted) return false; - mkdru.resetPage(); + if (!mkdru.active) return false; + // pages mean different things now + mkdru.state.page = 1; mkdru.hashFromState(); mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort); return false; } mkdru.search = function () { - mkdru.pz2.search(mkdru.state.query, mkdru.state.perpage, mkdru.state.sort, - mkdru.state.filter); + var filter = null; + var query = mkdru.state.query; + + // facet limit implementation + if (mkdru.state.limit_source) { + filter = 'pz:id=' + mkdru.state.limit_source; + } + if (mkdru.state.limit_subject) { + query += ' and su="' + mkdru.state.limit_subject + '"'; + } + if (mkdru.state.limit_author) { + query += ' and au="' + mkdru.state.limit_author + '"'; + } + mkdru.pz2.search(query, mkdru.state.perpage, mkdru.state.sort, filter); //inform others that the search is ON - mkdru.submitted = true; + mkdru.active = true; }; mkdru.pollDropDowns = function () { @@ -221,29 +255,6 @@ mkdru.pollDropDowns = function () { mkdru.state.sort = $('.mkdru-sort').attr('value'); }; -//TODO it's rude to mess with user's query, we should have a breadcrumb -mkdru.limitQuery = function (field, value) { - $('.mkdru-search input:text').attr('value', function () { - return this.value += ' and ' + field + '="' + value + '"'; - }); - mkdru.submitQuery(); -}; - -mkdru.limitTarget = function (id, name) { - mkdru.state.filter = 'pz:id=' + id; - mkdru.pollDropDowns(); - mkdru.resetPage(); - mkdru.hashFromState(); - mkdru.search(); - var navi = document.getElementById('mkdru-navi'); - if (!navi) return false; - navi.innerHTML = - 'Source: ' - + name + ''; - navi.innerHTML += '
'; - return false; -}; - mkdru.generatePager = function () { // make sure page param is a number, otherwise pageing frwd will be broken if (typeof mkdru.state.page == "string") { @@ -276,11 +287,6 @@ mkdru.generatePager = function () { total, prev, next); }; -mkdru.resetPage = function () { - mkdru.state.page = 1; - mkdru.totalRec = 0; -}; - // wait until the DOM is ready, bind events @@ -292,15 +298,22 @@ $(document).ready(function () { $('.mkdru-perpage').bind('change', mkdru.submitCriteria); $('.mkdru-sort').bind('change', mkdru.submitCriteria); + // generate termlist for pz2.js and populate facet limit state + var termlist = []; + for (var key in mkdru.facets) { + termlist.push(mkdru.facets[key].pz2Name); + mkdru.defaultState['limit_' + key] = null; + } + mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show, "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way "pazpar2path": mkdru.pazpar2path, "oninit": mkdru.pz2Init, "onstat": mkdru.pz2Status, "onterm": mkdru.pz2Term, - "termlist": "xtargets,subject,author", + "termlist": termlist.join(','), "onbytarget": mkdru.pz2ByTarget, - "usesessions" : mkdru.usesessions, + "usesessions" : mkdru.useSessions, "showResponseType": mkdru.showResponseType, "onrecord": mkdru.pz2Record, "autoInit": false } ); @@ -315,7 +328,7 @@ $(document).ready(function () { mkdru.state.query = Drupal.settings.mkdru.query } - if (mkdru.usesessions) { + if (mkdru.useSessions) { mkdru.pz2.init(); } else if (mkdru.state.recid) { diff --git a/mkdru.module b/mkdru.module index 9d55303..d554ee3 100644 --- a/mkdru.module +++ b/mkdru.module @@ -329,15 +329,15 @@ function mkdru_block($op='list', $delta='sources', $edit=array()) { switch ($delta) { case 'mkdru_sources': $block['subject'] = t('Source'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-sources'); + $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source'); return $block; case 'mkdru_subjects': $block['subject'] = t('Subject'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subjects'); + $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject'); return $block; case 'mkdru_authors': $block['subject'] = t('Author'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-authors'); + $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author'); return $block; } if (substr($delta, 0, 13) == 'mkdru_search_') { diff --git a/mkdru.theme.js b/mkdru.theme.js index 6f77dda..a3a0c0e 100644 --- a/mkdru.theme.js +++ b/mkdru.theme.js @@ -100,10 +100,32 @@ Drupal.theme.prototype.mkdruPager = function (pages, start, current, total, prev return html; }; -Drupal.theme.prototype.mkdruTerm = function (term, freq, linkClass, id) { - var html = '' + term + ' (' + freq + ')
'; +Drupal.theme.prototype.mkdruFacet = function (terms, facet, max) { + var html = ""; + for (var i = 0; i < terms.length && i < max; i++ ) { + html += Drupal.theme('mkdruFacetTerm', terms[i], facet); + } return html; +}; + +Drupal.theme.prototype.mkdruFacetTerm = function (term, facet) { + var html = '' + term.name + ' (' + term.freq + ')
'; + return html; +}; + +Drupal.theme.prototype.mkdruFacetLimit = function (term, link) { + if (term) { + return '

' + Drupal.t('Only displaying ') + term.name + '

' + + '

' + Drupal.t('Show all...') + '

'; + } }; \ No newline at end of file -- 1.7.10.4