1 // Set up namespace and some state.
3 // Settings to pass to pz2.js
4 useSessions: Drupal.settings.mkdru.use_sessions === '1',
10 pazpar2Path: Drupal.settings.mkdru.pz2_path,
11 facetContainerSelector: Drupal.settings.mkdru.facetContainerSelector,
15 displayName: Drupal.settings.mkdru.source_dname || "Source",
16 orderWeight: Drupal.settings.mkdru.source_weight || 1,
18 max: Drupal.settings.mkdru.source_max || 10
21 displayName: Drupal.settings.mkdru.subject_dname || "Subject",
22 orderWeight: Drupal.settings.mkdru.source_weight || 2,
24 max: Drupal.settings.mkdru.subject_max || 10,
29 displayName: Drupal.settings.mkdru.author_dname || "Author",
30 orderWeight: Drupal.settings.mkdru.source_weight || 3,
32 max: Drupal.settings.mkdru.author_max || 10,
37 // State; Keys limit_{facet key} will also be initialised
54 // So we can use jQuery BBQ with Drupal 6 and its 1.2.6 jQuery
55 if (!$.isArray) $.isArray = function(obj) {
56 return Object.prototype.toString.call(obj) === "[object Array]";
59 // BBQ has no handy way to remove params without changing the hash.
60 // This takes an object to add and an array of keys to delete.
61 mkdru.hashAddDelMany = function (add, del) {
62 var newHash = $.deparam.fragment();
63 if (typeof(add) === 'object')
64 $.extend(newHash, add);
66 for (var i=0; i < del.length; i++)
67 if (newHash[del[i]] !== 'undefined')
68 delete newHash[del[i]];
69 return $.param.fragment("#", newHash);
72 // It's sometimes cumbersome that object literals can't take variable keys.
73 mkdru.hashAddDelOne = function (key, value, del) {
84 return mkdru.hashAddDelMany(toAdd, toDel);
89 // pz2.js event handlers:
90 mkdru.pz2Init = function () {
91 if (mkdru.state.query) {
95 //mkdru.pz2.bytarget();
98 mkdru.pz2Show = function (data) {
99 mkdru.totalRec = data.merged;
100 $('.mkdru-pager').html(mkdru.generatePager());
101 $('.mkdru-counts').html(Drupal.theme('mkdruCounts', data.start + 1,
102 data.num, data.merged, data.total));
104 for (var i = 0; i < data.hits.length; i++) {
105 html += Drupal.theme('mkdruResult', data.hits[i],
106 i + 1 + mkdru.state.perpage * (mkdru.state.page - 1),
107 "#" + $.param.fragment($.param.fragment(
108 window.location.href, {recid: data.hits[i].recid})) + "\n"
111 $('.mkdru-result-list').html(html);
112 if (mkdru.state.recid) {
113 mkdru.pz2.record(mkdru.state.recid);
116 $('.mkdru-results').show();
120 mkdru.pz2Status = function (data) {
121 $('.mkdru-status').html(Drupal.theme('mkdruStatus', data.activeclients, data.clients));
124 mkdru.contains = function (hash, key, value) {
126 if (hash[key].length) {
127 for (var i=0; i<hash[key].length; i++) {
128 if (hash[key][i] === value) {
133 return hash[key] === value;
139 mkdru.pz2Term = function (data) {
140 // map all facets against selected, for simple rendering
141 var hash = $.deparam.fragment();
142 for (var key in hash) {
143 if (key.indexOf('limit') == 0 && hash[key])
144 //always wrap in array
145 hash[key.substr(6)] = hash[key].split(/;+/);
148 for (var facet in mkdru.facets) {
149 var terms = data[mkdru.facets[facet].pz2Name];
150 for (var i=0; i<terms.length; i++) {
152 var value = facet == "source" ? term.id : term.name;
153 if (mkdru.contains(hash, facet, value)) { //enabled
154 term.toggleLink = mkdru.removeLimit(facet, value);
155 term.selected = true;
157 term.toggleLink = mkdru.addLimit(facet, value);
158 term.selected = false;
161 $('.mkdru-facet-' + facet).html(
162 Drupal.theme('mkdruFacet', terms, facet, mkdru.facets[facet].max,
167 mkdru.pz2ByTarget = function (data) {
171 mkdru.pz2Record = function (data) {
172 clearTimeout(mkdru.pz2.showTimer);
173 $('.mkdru-results').hide();
174 $('.mkdru-detail').html(Drupal.theme('mkdruDetail', data,
175 mkdru.hashAddDelOne(null, null, 'recid')));
176 $('.mkdru-detail').show();
177 clearTimeout(mkdru.pz2.recordTimer);
182 // State and URL handling
184 // populate state from an object and fill in the blanks with defaults
185 mkdru.stateFromObject = function (obj) {
186 mkdru.state = $.extend({}, mkdru.defaultState);
187 for (var key in mkdru.defaultState)
188 if (typeof(obj[key]) != "undefined")
189 mkdru.state[key] = obj[key];
192 // populate state from current window's hash string
193 mkdru.stateFromHash = function () {
194 mkdru.stateFromObject($.deparam.fragment());
197 // set current window's hash string from state
198 mkdru.hashFromState = function () {
199 // only include non-default settings in the URL
200 var alteredState = {};
201 for (var key in mkdru.defaultState) {
202 if (mkdru.state[key] != mkdru.defaultState[key]) {
203 alteredState[key] = mkdru.state[key];
206 $.bbq.pushState(alteredState, 2);
209 // update mkdru_form theme's ui to match state
210 mkdru.uiFromState = function () {
211 for (var key in mkdru.state) {
214 $('.mkdru-search input:text').attr('value', mkdru.state[key]);
217 $('.mkdru-perpage').attr('value', mkdru.state[key]);
220 $('.mkdru-sort').attr('value', mkdru.state[key]);
226 mkdru.hashChange = function () {
227 // do we need to restart the search?
228 var searchTrigger = false;
229 // shallow copy of state so we can see what changed.
230 var oldState = $.extend({}, mkdru.state);
231 mkdru.stateFromHash();
232 // only have to compare values since all keys are initialised
233 for (key in mkdru.state) {
234 var changed = (mkdru.state[key] != oldState[key]);
235 if (key.substring(0,5) === 'limit' && changed)
236 searchTrigger = true;
237 if (key === 'page' && changed)
238 mkdru.pz2.showPage(mkdru.state.page-1);
239 if (key === 'query' && changed)
240 searchTrigger = true;
244 // request for record detail
245 if (mkdru.state.recid && (mkdru.state.recid != oldState.recid)) {
246 mkdru.pz2.record(mkdru.state.recid);
249 $('.mkdru-detail').hide();
250 $('.mkdru-results').show();
254 // return link to limit facet
255 mkdru.addLimit = function (facet, limit) {
256 var newHash = $.deparam.fragment();
257 delete newHash['page'];
258 if ((typeof(newHash['limit_' + facet]) === 'undefined')
259 || !mkdru.facets[facet].multiLimit) {
260 newHash['limit_' + facet] = limit;
263 newHash['limit_' + facet] += ';' + limit;
265 return $.param.fragment("#", newHash);
268 // return link to remove limit from facet
269 mkdru.removeLimit = function (facet, limit) {
270 var newHash = $.deparam.fragment();
271 delete newHash['page'];
272 if (!newHash['limit_' + facet].indexOf(';')
273 || !mkdru.facets[facet].multiLimit) {
274 delete newHash['limit_' + facet];
277 var limits = newHash['limit_' + facet].split(';');
278 for (var i = 0; i < limits.length; i++) {
279 if (limits[i] == limit) {
281 if (limits.length < 1)
282 delete newHash['limit_' + facet];
284 newHash['limit_' + facet] = limits.join(';');
289 return $.param.fragment("#", newHash);
294 // form submit handler
295 mkdru.submitQuery = function () {
296 // new query, back to defaults (shallow copy)
297 mkdru.state = $.extend({}, mkdru.defaultState);
298 mkdru.state.query = $('.mkdru-search input:text').attr('value');
299 mkdru.pollDropDowns();
300 mkdru.hashFromState();
306 // criteria drop-down (perpage, sort) handler
307 mkdru.submitCriteria = function () {
308 mkdru.pollDropDowns();
309 //search is not ON, do nothing
310 if (!mkdru.active) return false;
311 // pages mean different things now
312 mkdru.state.page = 1;
313 mkdru.hashFromState();
314 mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort);
318 mkdru.search = function () {
320 var query = mkdru.state.query;
322 // facet limit implementation
323 for (var facet in mkdru.facets) {
325 if (mkdru.state['limit_' + facet]) {
326 if (facet == "source") {
327 filter = 'pz:id=' + mkdru.state.limit_source;
330 var limits = mkdru.state['limit_' + facet].split(/;+/);
331 for (var i = 0; i < limits.length; i++) {
332 // ex. query + and au="{limit_author}"
334 query += ' and ' + mkdru.facets[facet]['limiter'] + '="'
341 mkdru.pz2.search(query, mkdru.state.perpage, mkdru.state.sort, filter);
345 mkdru.pollDropDowns = function () {
346 mkdru.state.perpage = $('.mkdru-perpage').attr('value');
347 mkdru.state.sort = $('.mkdru-sort').attr('value');
350 mkdru.generatePager = function () {
351 // cast page parameter to numeric so we can add to it
352 if (typeof mkdru.state.page == "string") {
353 mkdru.state.page = Number(mkdru.state.page);
355 var total = Math.ceil(mkdru.totalRec / mkdru.state.perpage);
356 var first = (mkdru.state.page - mkdru.pagerRange > 0)
357 ? mkdru.state.page - mkdru.pagerRange : 1;
358 var last = first + 2 * mkdru.pagerRange < total
359 ? first + 2 * mkdru.pagerRange : total;
364 if ((mkdru.state.page - 1) >= first) {
365 prev = "#" + $.param.fragment($.param.fragment(
366 window.location.href, {page: mkdru.state.page - 1}))
368 if ((mkdru.state.page + 1) <= total) {
369 next = "#" + $.param.fragment($.param.fragment(
370 window.location.href, {page: mkdru.state.page + 1}))
373 for (var i = first; i <= last; i++) {
374 pages.push("#" + $.param.fragment($.param.fragment(
375 window.location.href, {page: i})));
378 return Drupal.theme('mkdruPager', pages, first, mkdru.state.page,
384 // wait until the DOM is ready, bind events
385 // and instantiate pz2 library
386 $(document).ready(function () {
387 $(window).bind( 'hashchange', mkdru.hashChange);
388 $('.mkdru-search').bind('submit', mkdru.submitQuery);
389 $('.mkdru-search input:text').attr('value', '');
390 $('.mkdru-perpage').bind('change', mkdru.submitCriteria);
391 $('.mkdru-sort').bind('change', mkdru.submitCriteria);
393 // generate termlist for pz2.js and populate facet limit state
395 for (var key in mkdru.facets) {
396 termlist.push(mkdru.facets[key].pz2Name);
397 mkdru.defaultState['limit_' + key] = null;
400 // if signaled, prepare facet container client-side
401 if (mkdru.facetContainerSelector) {
402 $(mkdru.facetContainerSelector).html(Drupal.theme('mkdruFacetContainer',
406 mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show,
407 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
408 "pazpar2path": mkdru.pazpar2Path,
409 "oninit": mkdru.pz2Init,
410 "onstat": mkdru.pz2Status,
411 "onterm": mkdru.pz2Term,
412 "termlist": termlist.join(','),
413 "onbytarget": mkdru.pz2ByTarget,
414 "usesessions" : mkdru.useSessions,
415 "showResponseType": mkdru.showResponseType,
416 "onrecord": mkdru.pz2Record,
417 "autoInit": false } );
418 mkdru.pz2.showFastCount = 1;
420 // initialise state to hash string or defaults
421 mkdru.stateFromHash();
422 // and update UI to match
426 if (typeof(Drupal.settings.mkdru.query) !== "undefined") {
427 mkdru.state.query = Drupal.settings.mkdru.query
428 //force the hash to contain query
429 mkdru.hashFromState();
432 //not running against SP? init, otherwise authenticate
433 if (mkdru.useSessions) {
437 var user = Drupal.settings.mkdru.sp_user;
438 var pass = Drupal.settings.mkdru.sp_pass;
440 params['command'] = 'auth';
442 params['action'] = 'login';
443 params['username'] = user;
444 params['password'] = pass;
446 params['action'] = 'ipauth';
448 var authReq = new pzHttpRequest(mkdru.pazpar2Path,
450 alert("Authentication against metasearch gateway failed: " +err);
455 var s = data.getElementsByTagName('status');
456 if (s.length && Element_getTextContent(s[0]) == "OK") {
457 mkdru.realm = data.getElementsByTagName('realm');
460 alert("Malformed response when authenticating against the metasearch"