1 // Set up namespace and some state.
3 settings: JSON.parse(Drupal.settings.mkdru.settings),
9 facetContainerSelector: '#mkdru-raw-facet-container',
20 // Convenient references
21 mkdru.facets = mkdru.settings.facets;
26 // So we can use jQuery BBQ with Drupal 6 and its 1.2.6 jQuery
27 if (!$.isArray) $.isArray = function(obj) {
28 return Object.prototype.toString.call(obj) === "[object Array]";
31 // BBQ has no handy way to remove params without changing the hash.
32 // This takes an object to add and an array of keys to delete.
33 mkdru.hashAddDelMany = function (add, del) {
34 var newHash = $.deparam.fragment();
35 if (typeof(add) === 'object')
36 $.extend(newHash, add);
38 for (var i=0; i < del.length; i++)
39 if (newHash[del[i]] !== 'undefined')
40 delete newHash[del[i]];
41 return $.param.fragment("#", newHash);
44 // It's sometimes cumbersome that object literals can't take variable keys.
45 mkdru.hashAddDelOne = function (key, value, del) {
56 return mkdru.hashAddDelMany(toAdd, toDel);
61 // pz2.js event handlers:
62 mkdru.pz2Init = function () {
63 if (mkdru.state.query) {
69 mkdru.pz2Show = function (data) {
70 mkdru.totalRec = data.merged;
71 $('.mkdru-pager').html(mkdru.generatePager());
72 $('.mkdru-counts').html(Drupal.theme('mkdruCounts', data.start + 1,
73 data.num, data.merged, data.total));
75 for (var i = 0; i < data.hits.length; i++) {
76 html += Drupal.theme('mkdruResult', data.hits[i],
77 i + 1 + mkdru.state.perpage * (mkdru.state.page - 1),
78 "#" + $.param.fragment($.param.fragment(
79 window.location.href, {recid: data.hits[i].recid})) + "\n"
82 $('.mkdru-result-list').html(html);
83 if (mkdru.state.recid) {
84 mkdru.pz2.record(mkdru.state.recid);
87 $('.mkdru-results').show();
91 mkdru.pz2Status = function (data) {
92 $('.mkdru-status').html(Drupal.theme('mkdruStatus', data.activeclients, data.clients));
95 mkdru.contains = function (hash, key, value) {
97 if (hash[key].length) {
98 for (var i=0; i<hash[key].length; i++) {
99 if (hash[key][i] === value) {
104 return hash[key] === value;
110 mkdru.pz2Term = function (data) {
111 // map all facets against selected, for simple rendering
112 var hash = $.deparam.fragment();
113 for (var key in hash) {
114 if (key.indexOf('limit') == 0 && hash[key])
115 //always wrap in array
116 hash[key.substr(6)] = hash[key].split(/;+/);
119 for (var facet in mkdru.facets) {
120 var terms = data[mkdru.facets[facet].pz2Name];
121 for (var i=0; i<terms.length; i++) {
123 var value = facet == "source" ? term.id : term.name;
124 if (mkdru.contains(hash, facet, value)) { //enabled
125 term.toggleLink = mkdru.removeLimit(facet, value);
126 term.selected = true;
128 term.toggleLink = mkdru.addLimit(facet, value);
129 term.selected = false;
132 $('.mkdru-facet-' + facet).html(
133 Drupal.theme('mkdruFacet', terms, facet, mkdru.facets[facet].max,
138 mkdru.pz2Record = function (data) {
139 clearTimeout(mkdru.pz2.showTimer);
140 $('.mkdru-results').hide();
141 $('.mkdru-detail').html(Drupal.theme('mkdruDetail', data,
142 mkdru.hashAddDelOne(null, null, 'recid')));
143 $('.mkdru-detail').show();
144 clearTimeout(mkdru.pz2.recordTimer);
149 // State and URL handling
151 // populate state from an object and fill in the blanks with defaults
152 mkdru.stateFromObject = function (obj) {
153 mkdru.state = $.extend({}, mkdru.defaultState);
154 for (var key in mkdru.defaultState)
155 if (typeof(obj[key]) != "undefined")
156 mkdru.state[key] = obj[key];
159 // populate state from current window's hash string
160 mkdru.stateFromHash = function () {
161 mkdru.stateFromObject($.deparam.fragment());
164 // set current window's hash string from state
165 mkdru.hashFromState = function () {
166 // only include non-default settings in the URL
167 var alteredState = {};
168 for (var key in mkdru.defaultState) {
169 if (mkdru.state[key] != mkdru.defaultState[key]) {
170 alteredState[key] = mkdru.state[key];
173 $.bbq.pushState(alteredState, 2);
176 // update mkdru_form theme's ui to match state
177 mkdru.uiFromState = function () {
178 for (var key in mkdru.state) {
181 $('.mkdru-search input:text').attr('value', mkdru.state[key]);
184 $('.mkdru-perpage').attr('value', mkdru.state[key]);
187 $('.mkdru-sort').attr('value', mkdru.state[key]);
193 mkdru.hashChange = function () {
194 // TING hack, switching tabs resets the hash but does not re-load the page
195 // simply ignore new hash and set it to the old state
196 var hash = $.param.fragment();
197 if (hash.indexOf("-result") != -1 || hash.indexOf("facets=") != -1) {
198 mkdru.hashFromState();
201 // do we need to restart the search?
202 var searchTrigger = false;
203 // shallow copy of state so we can see what changed.
204 var oldState = $.extend({}, mkdru.state);
205 mkdru.stateFromHash();
206 // only have to compare values since all keys are initialised
207 for (key in mkdru.state) {
208 var changed = (mkdru.state[key] != oldState[key]);
209 if (key.substring(0,5) === 'limit' && changed)
210 searchTrigger = true;
211 if (key === 'page' && changed)
212 mkdru.pz2.showPage(mkdru.state.page-1);
213 if (key === 'query' && changed)
214 searchTrigger = true;
218 // request for record detail
219 if (mkdru.state.recid && (mkdru.state.recid != oldState.recid)) {
220 mkdru.pz2.record(mkdru.state.recid);
223 $('.mkdru-detail').hide();
224 $('.mkdru-results').show();
228 // return link to limit facet
229 mkdru.addLimit = function (facet, limit) {
230 var newHash = $.deparam.fragment();
231 delete newHash['page'];
232 if ((typeof(newHash['limit_' + facet]) === 'undefined')
233 || !mkdru.facets[facet].multiLimit) {
234 newHash['limit_' + facet] = limit;
237 newHash['limit_' + facet] += ';' + limit;
239 return $.param.fragment("#", newHash);
242 // return link to remove limit from facet
243 mkdru.removeLimit = function (facet, limit) {
244 var newHash = $.deparam.fragment();
245 delete newHash['page'];
246 if (!newHash['limit_' + facet].indexOf(';')
247 || !mkdru.facets[facet].multiLimit) {
248 delete newHash['limit_' + facet];
251 var limits = newHash['limit_' + facet].split(';');
252 for (var i = 0; i < limits.length; i++) {
253 if (limits[i] == limit) {
255 if (limits.length < 1)
256 delete newHash['limit_' + facet];
258 newHash['limit_' + facet] = limits.join(';');
263 return $.param.fragment("#", newHash);
268 // form submit handler
269 mkdru.submitQuery = function () {
270 // new query, back to defaults (shallow copy)
271 mkdru.state = $.extend({}, mkdru.defaultState);
272 mkdru.state.query = $('.mkdru-search input:text').attr('value');
273 mkdru.pollDropDowns();
274 mkdru.hashFromState();
280 // criteria drop-down (perpage, sort) handler
281 mkdru.submitCriteria = function () {
282 mkdru.pollDropDowns();
283 //search is not ON, do nothing
284 if (!mkdru.active) return false;
285 // pages mean different things now
286 mkdru.state.page = 1;
287 mkdru.hashFromState();
288 mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort);
292 mkdru.search = function () {
294 var query = mkdru.state.query;
296 // facet limit implementation
297 for (var facet in mkdru.facets) {
299 if (mkdru.state['limit_' + facet]) {
300 if (facet == "source") {
301 filter = 'pz:id=' + mkdru.state.limit_source;
304 var limits = mkdru.state['limit_' + facet].split(/;+/);
305 for (var i = 0; i < limits.length; i++) {
306 // ex. query + and au="{limit_author}"
308 query += ' and ' + mkdru.facets[facet]['limiter'] + '="'
315 mkdru.pz2.search(query, mkdru.state.perpage, mkdru.state.sort, filter);
319 mkdru.pollDropDowns = function () {
320 mkdru.state.perpage = $('.mkdru-perpage').attr('value');
321 mkdru.state.sort = $('.mkdru-sort').attr('value');
324 mkdru.generatePager = function () {
325 // cast page parameter to numeric so we can add to it
326 if (typeof mkdru.state.page == "string") {
327 mkdru.state.page = Number(mkdru.state.page);
329 var total = Math.ceil(mkdru.totalRec / mkdru.state.perpage);
330 var first = (mkdru.state.page - mkdru.pagerRange > 0)
331 ? mkdru.state.page - mkdru.pagerRange : 1;
332 var last = first + 2 * mkdru.pagerRange < total
333 ? first + 2 * mkdru.pagerRange : total;
338 if ((mkdru.state.page - 1) >= first) {
339 prev = "#" + $.param.fragment($.param.fragment(
340 window.location.href, {page: mkdru.state.page - 1}))
342 if ((mkdru.state.page + 1) <= total) {
343 next = "#" + $.param.fragment($.param.fragment(
344 window.location.href, {page: mkdru.state.page + 1}))
347 for (var i = first; i <= last; i++) {
348 pages.push("#" + $.param.fragment($.param.fragment(
349 window.location.href, {page: i})));
352 return Drupal.theme('mkdruPager', pages, first, mkdru.state.page,
358 // wait until the DOM is ready, bind events
359 // and instantiate pz2 library
360 $(document).ready(function () {
361 $(window).bind( 'hashchange', mkdru.hashChange);
362 $('.mkdru-search').bind('submit', mkdru.submitQuery);
363 $('.mkdru-search input:text').attr('value', '');
364 $('.mkdru-perpage').bind('change', mkdru.submitCriteria);
365 $('.mkdru-sort').bind('change', mkdru.submitCriteria);
367 // generate termlist for pz2.js and populate facet limit state
369 for (var key in mkdru.facets) {
370 termlist.push(mkdru.facets[key].pz2Name);
371 mkdru.defaultState['limit_' + key] = null;
374 // if signaled, prepare facet container client-side
375 if (mkdru.facetContainerSelector) {
376 $(mkdru.facetContainerSelector).html(Drupal.theme('mkdruFacetContainer',
380 mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show,
381 "showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
382 "pazpar2path": mkdru.settings.pz2_path,
383 "oninit": mkdru.pz2Init,
384 "onstat": mkdru.pz2Status,
385 "onterm": mkdru.pz2Term,
386 "termlist": termlist.join(','),
387 "usesessions" : mkdru.settings.useSessions,
388 "showResponseType": mkdru.showResponseType,
389 "onrecord": mkdru.pz2Record,
390 "autoInit": false } );
391 mkdru.pz2.showFastCount = 1;
393 // initialise state to hash string or defaults
394 mkdru.stateFromHash();
395 // and update UI to match
399 if (typeof(Drupal.settings.mkdru.query) !== "undefined") {
400 mkdru.state.query = Drupal.settings.mkdru.query
401 //force the hash to contain query
402 mkdru.hashFromState();
405 //not running against SP? init, otherwise authenticate
406 if (mkdru.settings.use_sessions) {
410 var user = mkdru.settings.sp.user;
411 var pass = mkdru.settings.sp.pass;
413 params['command'] = 'auth';
415 params['action'] = 'login';
416 params['username'] = user;
417 params['password'] = pass;
419 params['action'] = 'ipauth';
421 var authReq = new pzHttpRequest(mkdru.pazpar2Path,
423 alert("Authentication against metasearch gateway failed: " +err);
428 var s = data.getElementsByTagName('status');
429 if (s.length && Element_getTextContent(s[0]) == "OK") {
430 mkdru.realm = data.getElementsByTagName('realm');
433 alert("Malformed response when authenticating against the metasearch"