// 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,
// 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);
+}
};
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) {
// 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
alteredState[key] = mkdru.state[key];
}
}
- $.bbq.pushState(alteredState);
+ $.bbq.pushState(alteredState, 2);
};
// update mkdru_form theme's ui to match state
}
// 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();
-//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 () {
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: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
- + name + '</a>';
- navi.innerHTML += '<hr/>';
- return false;
-};
-
mkdru.generatePager = function () {
// make sure page param is a number, otherwise pageing frwd will be broken
if (typeof mkdru.state.page == "string") {
total, prev, next);
};
-mkdru.resetPage = function () {
- mkdru.state.page = 1;
- mkdru.totalRec = 0;
-};
-
// wait until the DOM is ready, bind events
$('.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 } );
mkdru.state.query = Drupal.settings.mkdru.query
}
- if (mkdru.usesessions) {
+ if (mkdru.useSessions) {
mkdru.pz2.init();
}
else if (mkdru.state.recid) {