From: Jason Skomorowski Date: Wed, 2 Mar 2011 08:31:10 +0000 (+0100) Subject: Towards generalised facet limits. X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=49f8894f932cb93c328d13260fb9e773b39a1a6c;p=mkdru-moved-to-drupal.org.git Towards generalised facet limits. --- diff --git a/mkdru.client.js b/mkdru.client.js index 03f9507..c73e875 100644 --- a/mkdru.client.js +++ b/mkdru.client.js @@ -2,7 +2,6 @@ var mkdru = { // Settings to pass to pz2.js useSessions: Drupal.settings.mkdru.use_sessions === '1', - // showResponseType: 'json', // Variables active: false, pz2: null, @@ -17,11 +16,15 @@ var mkdru = { }, subject: { pz2Name: 'subject', - max: Drupal.settings.mkdru.subject_max + max: Drupal.settings.mkdru.subject_max, + multiLimit: true, + limiter: 'su' }, author: { pz2Name: 'author', - max: Drupal.settings.mkdru.author_max + max: Drupal.settings.mkdru.author_max, + multiLimit: false, + limiter: 'au' } }, // State @@ -207,9 +210,43 @@ mkdru.hashChange = function () { } }; +// return link to limit facet +mkdru.addLimit = function (facet, limit) { + var newHash = $.deparam.fragment(); + delete newHash['page']; + if ((typeof(newHash['limit_' + facet]) === 'undefined') + || !mkdru.facets[facet].multiLimit) { + newHash['limit_' + facet] = limit; + } + else { + newHash['limit_' + facet] += ';' + limit; + } + return $.param.fragment("#", newHash); +}; +// return link to remove limit from facet +mkdru.removeLimit = function (facet, limit) { + var newHash = $.deparam.fragment(); + delete newHash['page']; + if (!newHash['limit_' + facet].indexOf(';') || !mkdru.facets[facet].multiLimit) { + delete newHash['limit_' + facet]; + } + else { + var limits = newHash['limit_' + facet].split(';'); + for (var i = 0; i < limits.length; i++) { + if (limits[i] == limit) { + limits.splice(i, 1); + newHash['limit_' + facet] = limits.join(';'); + break; + } + } + } + return $.param.fragment("#", newHash); +}; -//form submit handler + + +// form submit handler mkdru.submitQuery = function () { // new query, back to defaults (shallow copy) mkdru.state = $.extend({}, mkdru.defaultState); @@ -221,7 +258,7 @@ mkdru.submitQuery = function () { return false; }; -//criteria drop-down (perpage, sort) handler +// criteria drop-down (perpage, sort) handler mkdru.submitCriteria = function () { mkdru.pollDropDowns(); //search is not ON, do nothing @@ -238,17 +275,24 @@ mkdru.search = function () { 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 + '"'; + for (var facet in mkdru.facets) { + // facet is limited + if (mkdru.state['limit_' + facet]) { + if (facet == "source") { + filter = 'pz:id=' + mkdru.state.limit_source; + } + else { + var limits = mkdru.state['limit_' + facet].split(';'); + for (var i = 0; i < limits.length; i++) { + // ex. query + and au="{limit_author}" + query += ' and ' + mkdru.facets[facet]['limiter'] + '="' + + mkdru.state['limit_' + facet] + '"'; + } + } + } } + mkdru.pz2.search(query, mkdru.state.perpage, mkdru.state.sort, filter); - //inform others that the search is ON mkdru.active = true; }; @@ -258,7 +302,7 @@ mkdru.pollDropDowns = function () { }; mkdru.generatePager = function () { - // make sure page param is a number, otherwise pageing frwd will be broken + // cast page parameter to numeric so we can add to it if (typeof mkdru.state.page == "string") { mkdru.state.page = Number(mkdru.state.page); } @@ -266,7 +310,7 @@ mkdru.generatePager = function () { 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; + ? first + 2 * mkdru.pagerRange : total; var prev = null; var next = null; var pages = []; @@ -329,7 +373,7 @@ $(document).ready(function () { if (typeof(Drupal.settings.mkdru.query) !== "undefined") { mkdru.state.query = Drupal.settings.mkdru.query } - + //not running against SP? init, otherwise authenticate if (mkdru.useSessions) { mkdru.pz2.init();