1 /*! MKWS, the MasterKey Widget Set.
2 * Copyright (C) 2013-2014 Index Data
3 * See the file LICENSE for details
6 "use strict"; // HTML5: disable for log_level >= 2
9 // Set up global mkws object. Contains truly global state such as SP
10 // authentication, and a hash of team objects, indexed by team-name.
14 log_level: 1, // Will be overridden from mkws.config, but
15 // initial value allows jQuery popup to use logging.
17 widgetType2function: {},
22 "Subjects": "Schlagwörter",
23 "Sources": "Daten und Quellen",
24 "source": "datenquelle",
25 "Termlists": "Termlisten",
27 "Prev": "Zurück",
29 "Sort by": "Sortieren nach",
30 "and show": "und zeige",
31 "per page": "pro Seite",
32 "Displaying": "Zeige",
40 "Subject": "Schlagwort",
41 "subject": "schlagwort",
43 "Records": "Datensätze",
44 "Targets": "Datenbanken",
50 "Authors": "Forfattere",
54 "Termlists": "Termlists",
55 "Next": "Næste",
57 "Search": "Søg",
58 "Sort by": "Sorter efter",
60 "per page": "per side",
61 "Displaying": "Viser",
66 "Author": "Forfatter",
67 "author": "forfatter",
71 "Location": "Lokation",
81 mkws.log = function(string) {
85 if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
89 // you need to disable use strict at the top of the file!!!
90 if (mkws.log_level >= 3) {
91 // Works in Chrome; not sure about elsewhere
93 } else if (mkws.log_level >= 2) {
94 console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
100 // This function is taken from a StackOverflow answer
101 // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
102 mkws.getParameterByName = function(name) {
103 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
104 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
105 results = regex.exec(location.search);
106 return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
110 mkws.registerWidgetType = function(name, fn) {
111 mkws.widgetType2function[name] = fn;
112 mkws.log("registered widget-type '" + name + "'");
115 mkws.promotionFunction = function(name) {
116 return mkws.widgetType2function[name];
120 mkws.setMkwsConfig = function(overrides) {
121 // Set global log_level flag early so that mkws.log() works
122 // Fall back to old "debug_level" setting for backwards compatibility
123 var tmp = overrides.log_level;
124 if (typeof(tmp) === 'undefined') tmp = overrides.debug_level;
125 if (typeof(tmp) !== 'undefined') mkws.log_level = tmp;
127 var config_default = {
128 use_service_proxy: true,
129 pazpar2_url: "//mkws.indexdata.com/service-proxy/",
130 service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
132 sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
133 perpage_options: [10, 20, 30, 50],
134 sort_default: "relevance",
137 show_lang: true, /* show/hide language menu */
138 show_sort: true, /* show/hide sort menu */
139 show_perpage: true, /* show/hide perpage menu */
140 lang_options: [], /* display languages links for given languages, [] for all */
141 facets: ["xtargets", "subject", "author"], /* display facets, in this order, [] for none */
142 responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
143 log_level: 1, /* log level for development: 0..2 */
148 mkws.config = mkws.objectInheritingFrom(config_default);
149 for (var k in overrides) {
150 mkws.config[k] = overrides[k];
155 // This code is from Douglas Crockford's article "Prototypal Inheritance in JavaScript"
156 // http://javascript.crockford.com/prototypal.html
157 // mkws.objectInheritingFrom behaves the same as Object.create,
158 // but since the latter is not available in IE8 we can't use it.
160 mkws.objectInheritingFrom = function(o) {
167 mkws.defaultTemplate = function(name) {
168 if (name === 'Record') {
172 <th>{{translate "Title"}}</th>\
175 {{#if md-title-remainder}}\
176 ({{md-title-remainder}})\
178 {{#if md-title-responsibility}}\
179 <i>{{md-title-responsibility}}</i>\
185 <th>{{translate "Date"}}</th>\
186 <td>{{md-date}}</td>\
191 <th>{{translate "Author"}}</th>\
192 <td>{{md-author}}</td>\
195 {{#if md-electronic-url}}\
197 <th>{{translate "Links"}}</th>\
199 {{#each md-electronic-url}}\
200 <a href="{{this}}">Link{{index1}}</a>\
205 {{#if-any location having="md-subject"}}\
207 <th>{{translate "Subject"}}</th>\
209 {{#first location having="md-subject"}}\
211 {{#commaList md-subject}}\
212 {{this}}{{/commaList}}\
219 <th>{{translate "Locations"}}</th>\
221 {{#commaList location}}\
222 {{attr "@name"}}{{/commaList}}\
227 } else if (name === "Summary") {
229 <a href="#" id="{{_id}}" onclick="{{_onclick}}">\
232 {{#if md-title-remainder}}\
233 <span>{{md-title-remainder}}</span>\
235 {{#if md-title-responsibility}}\
236 <span><i>{{md-title-responsibility}}</i></span>\
239 } else if (name === "Image") {
241 <a href="#" id="{{_id}}" onclick="{{_onclick}}">\
242 {{#first md-thumburl}}\
243 <img src="{{this}}" alt="{{../md-title}}"/>\
250 var s = "There is no default '" + name +"' template!";
256 // The following functions are dispatchers for team methods that
257 // are called from the UI using a team-name rather than implicit
259 mkws.switchView = function(tname, view) {
260 mkws.teams[tname].switchView(view);
263 mkws.showDetails = function(tname, prefixRecId) {
264 mkws.teams[tname].showDetails(prefixRecId);
267 mkws.limitTarget = function(tname, id, name) {
268 mkws.teams[tname].limitTarget(id, name);
271 mkws.limitQuery = function(tname, field, value) {
272 mkws.teams[tname].limitQuery(field, value);
275 mkws.limitCategory = function(tname, id) {
276 mkws.teams[tname].limitCategory(id);
279 mkws.delimitTarget = function(tname, id) {
280 mkws.teams[tname].delimitTarget(id);
283 mkws.delimitQuery = function(tname, field, value) {
284 mkws.teams[tname].delimitQuery(field, value);
287 mkws.showPage = function(tname, pageNum) {
288 mkws.teams[tname].showPage(pageNum);
291 mkws.pagerPrev = function(tname) {
292 mkws.teams[tname].pagerPrev();
295 mkws.pagerNext = function(tname) {
296 mkws.teams[tname].pagerNext();
300 // wrapper to call team() after page load
304 function handleNodeWithTeam(node, callback) {
305 // First branch for DOM objects; second branch for jQuery objects
306 var classes = node.className || node.attr('class');
308 // For some reason, if we try to proceed when classes is
309 // undefined, we don't get an error message, but this
310 // function and its callers, up several stack level,
311 // silently return. What a crock.
312 log("handleNodeWithTeam() called on node with no classes");
315 var list = classes.split(/\s+/)
318 for (var i = 0; i < list.length; i++) {
320 if (cname.match(/^mkwsTeam_/)) {
321 teamName = cname.replace(/^mkwsTeam_/, '');
322 } else if (cname.match(/^mkws/)) {
323 type = cname.replace(/^mkws/, '');
327 if (!teamName) teamName = "AUTO";
328 callback.call(node, teamName, type);
332 function resizePage() {
333 var list = ["Switch", "Lang"];
335 var threshhold = mkws.config.responsive_design_width;
336 var width = $(window).width();
337 var from, to, method;
339 if ((mkws.width === undefined || mkws.width > threshhold) &&
340 width <= threshhold) {
341 from = "wide"; to = "narrow"; method = "hide";
342 } else if ((mkws.width === undefined || mkws.width <= threshhold) &&
343 width > threshhold) {
344 from = "narrow"; to = "wide"; method = "show";
349 log("changing from " + from + " to " + to + ": " + width);
350 for (var tname in mkws.teams) {
351 var team = mkws.teams[tname];
352 team.visitWidgets(function (t, w) {
353 var w1 = team.widget(t + "-Container-" + from);
354 var w2 = team.widget(t + "-Container-" + to);
360 $(w.node).appendTo($(w2.node));
363 team.queue("resize" + to).publish();
364 for(var i = 0; i < list.length; i++) {
365 var widget = team.widget(list[i]);
366 if (widget) $(widget.node)[method]();
374 * Run service-proxy authentication in background (after page load).
375 * The username/password is configured in the apache config file
378 function authenticateSession(auth_url, auth_domain, pp2_url) {
379 log("Run service proxy auth URL: " + auth_url);
382 auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
383 log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
386 var request = new pzHttpRequest(auth_url, function(err) {
387 alert("HTTP call for authentication failed: " + err)
391 request.get(null, function(data) {
392 if (!$.isXMLDoc(data)) {
393 alert("service proxy auth response document is not valid XML document, give up!");
396 var status = $(data).find("status");
397 if (status.text() != "OK") {
398 alert("service proxy auth response status: " + status.text() + ", give up!");
402 log("Service proxy auth successfully done");
403 mkws.authenticated = true;
404 var authName = $(data).find("displayName").text();
405 // You'd think there would be a better way to do this:
406 var realm = $(data).find("realm:not(realmAttributes realm)").text();
407 for (var teamName in mkws.teams) {
408 mkws.teams[teamName].queue("authenticated").publish(authName, realm);
416 function runAutoSearches() {
417 log("running auto searches");
419 for (var teamName in mkws.teams) {
420 mkws.teams[teamName].queue("ready").publish();
425 function makeWidgetsWithin(level, node) {
426 node.find('[class^="mkws"],[class*=" mkws"]').each(function() {
427 handleNodeWithTeam(this, function(tname, type) {
428 var oldHTML = this.innerHTML;
429 var myTeam = mkws.teams[tname];
430 var myWidget = widget(j, myTeam, type, this);
431 myTeam.addWidget(myWidget);
432 var newHTML = this.innerHTML;
433 if (newHTML !== oldHTML) {
434 log("widget " + tname + ":" + type + " HTML changed: reparsing");
435 makeWidgetsWithin(level+1, $(this));
442 $(document).ready(function() {
444 if (typeof mkws_config === 'undefined') {
445 log("setting empty config");
448 log("using config: " + $.toJSON(mkws_config));
449 saved_config = mkws_config;
451 mkws.setMkwsConfig(saved_config);
453 for (var key in mkws.config) {
454 if (mkws.config.hasOwnProperty(key)) {
455 if (key.match(/^language_/)) {
456 var lang = key.replace(/^language_/, "");
457 // Copy custom languages into list
458 mkws.locale_lang[lang] = mkws.config[key];
459 log("Added locally configured language '" + lang + "'");
464 if (mkws.config.query_width < 5 || mkws.config.query_width > 150) {
465 log("Reset query width: " + mkws.config.query_width);
466 mkws.config.query_width = 50;
469 // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
470 if (mkws.config.pazpar2_url.match(/^\/\//)) {
471 mkws.config.pazpar2_url = document.location.protocol + mkws.config.pazpar2_url;
472 log("adjust protocol independent links: " + mkws.config.pazpar2_url);
475 if (mkws.config.responsive_design_width) {
476 // Responsive web design - change layout on the fly based on
477 // current screen width. Required for mobile devices.
478 $(window).resize(resizePage);
479 // initial check after page load
480 $(document).ready(resizePage);
483 // Backwards compatibility: set new magic class names on any
484 // elements that have the old magic IDs.
485 var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
486 "Results", "Records", "Targets", "Ranking",
487 "Termlists", "Stat", "MOTD" ];
488 for (var i = 0; i < ids.length; i++) {
489 var id = 'mkws' + ids[i];
490 var node = $('#' + id);
491 if (node.attr('id')) {
493 log("added magic class to '" + node.attr('id') + "'");
497 // Find all nodes with an MKWS class, and determine their team from
498 // the mkwsTeam_* class. Make all team objects.
500 $('[class^="mkws"],[class*=" mkws"]').each(function() {
501 handleNodeWithTeam(this, function(tname, type) {
502 if (!mkws.teams[tname]) {
503 mkws.teams[tname] = team(j, tname);
504 log("Made MKWS team '" + tname + "'");
509 makeWidgetsWithin(1, $(':root'));
512 log("Walking MKWS nodes took " + (now-then) + " ms");
515 for (var tName in mkws.teams) {
516 var myTeam = mkws.teams[tName]
517 log("TEAM '" + tName + "' = " + myTeam + " ...");
518 myTeam.visitWidgets(function(t, w) {
519 log(" has widget of type '" + t + "': " + w);
524 if (mkws.config.use_service_proxy) {
525 authenticateSession(mkws.config.service_proxy_auth,
526 mkws.config.service_proxy_auth_domain,
527 mkws.config.pazpar2_url);