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 // Define empty mkws_config for simple applications that don't define it.
82 if (mkws_config == null || typeof mkws_config != 'object') {
87 mkws.log = function(string) {
91 if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
95 // you need to disable use strict at the top of the file!!!
96 if (mkws.log_level >= 3) {
97 console.log(arguments.callee.caller);
98 } else if (mkws.log_level >= 2) {
99 console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
105 mkws.registerWidgetType = function(name, fn) {
106 mkws.widgetType2function[name] = fn;
107 mkws.log("registered widget-type '" + name + "'");
110 mkws.promotionFunction = function(name) {
111 return mkws.widgetType2function[name];
115 // wrapper to call team() after page load
119 function handleNodeWithTeam(node, callback) {
120 // First branch for DOM objects; second branch for jQuery objects
121 var classes = node.className || node.attr('class');
123 // For some reason, if we try to proceed when classes is
124 // undefined, we don't get an error message, but this
125 // function and its callers, up several stack level,
126 // silently return. What a crock.
127 log("handleNodeWithTeam() called on node with no classes");
130 var list = classes.split(/\s+/)
133 for (var i = 0; i < list.length; i++) {
135 if (cname.match(/^mkwsTeam_/)) {
136 teamName = cname.replace(/^mkwsTeam_/, '');
137 } else if (cname.match(/^mkws/)) {
138 type = cname.replace(/^mkws/, '');
141 callback.call(node, teamName, type);
145 function resizePage() {
146 var list = ["mkwsSwitch", "mkwsLang"];
148 var width = mkws_config.responsive_design_width;
149 var parent = $(".mkwsTermlists").parent();
151 if ($(window).width() <= width &&
152 parent.hasClass("mkwsTermlistContainer1")) {
153 log("changing from wide to narrow: " + $(window).width());
154 $(".mkwsTermlistContainer1").hide();
155 $(".mkwsTermlistContainer2").show();
156 for (var tname in mkws.teams) {
157 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer2.mkwsTeam_" + tname));
158 for(var i = 0; i < list.length; i++) {
159 $("." + list[i] + ".mkwsTeam_" + tname).hide();
162 } else if ($(window).width() > width &&
163 parent.hasClass("mkwsTermlistContainer2")) {
164 log("changing from narrow to wide: " + $(window).width());
165 $(".mkwsTermlistContainer1").show();
166 $(".mkwsTermlistContainer2").hide();
167 for (var tname in mkws.teams) {
168 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer1.mkwsTeam_" + tname));
169 for(var i = 0; i < list.length; i++) {
170 $("." + list[i] + ".mkwsTeam_" + tname).show();
177 // The following functions are dispatchers for team methods that
178 // are called from the UI using a team-name rather than implicit
179 // context. Apart from mkws.log, they are the ONLY public UI to
181 mkws.switchView = function(tname, view) {
182 mkws.teams[tname].switchView(view);
185 mkws.showDetails = function(tname, prefixRecId) {
186 mkws.teams[tname].showDetails(prefixRecId);
189 mkws.limitTarget = function(tname, id, name) {
190 mkws.teams[tname].limitTarget(id, name);
193 mkws.limitQuery = function(tname, field, value) {
194 mkws.teams[tname].limitQuery(field, value);
197 mkws.delimitTarget = function(tname, id) {
198 mkws.teams[tname].delimitTarget(id);
201 mkws.delimitQuery = function(tname, field, value) {
202 mkws.teams[tname].delimitQuery(field, value);
205 mkws.showPage = function(tname, pageNum) {
206 mkws.teams[tname].showPage(pageNum);
209 mkws.pagerPrev = function(tname) {
210 mkws.teams[tname].pagerPrev();
213 mkws.pagerNext = function(tname) {
214 mkws.teams[tname].pagerNext();
218 function defaultMkwsConfig() {
219 /* default mkws config */
220 var config_default = {
221 use_service_proxy: true,
222 pazpar2_url: "//mkws.indexdata.com/service-proxy/",
223 service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
225 sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
226 perpage_options: [10, 20, 30, 50],
227 sort_default: "relevance",
230 show_lang: true, /* show/hide language menu */
231 show_sort: true, /* show/hide sort menu */
232 show_perpage: true, /* show/hide perpage menu */
233 lang_options: [], /* display languages links for given languages, [] for all */
234 facets: ["xtargets", "subject", "author"], /* display facets, in this order, [] for none */
235 responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
236 log_level: 1, /* log level for development: 0..2 */
241 // Set global log_level flag early so that log() works
242 // Fall back to old "debug_level" setting for backwards compatibility
243 var tmp = mkws_config.log_level;
244 if (typeof(tmp) === 'undefined') tmp = mkws_config.debug_level;
246 if (typeof(tmp) !== 'undefined') {
247 mkws.log_level = tmp;
248 } else if (typeof(config_default.log_level) !== 'undefined') {
249 mkws.log_level = config_default.log_level;
252 // make sure the mkws_config is a valid hash
253 if (!$.isPlainObject(mkws_config)) {
254 log("ERROR: mkws_config is not an JS object, ignore it....");
258 /* override standard config values by function parameters */
259 for (var k in config_default) {
260 if (typeof mkws_config[k] === 'undefined')
261 mkws_config[k] = config_default[k];
262 //log("Set config: " + k + ' => ' + mkws_config[k]);
268 * Run service-proxy authentication in background (after page load).
269 * The username/password is configured in the apache config file
272 function authenticateSession(auth_url, auth_domain, pp2_url) {
273 log("Run service proxy auth URL: " + auth_url);
276 auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
277 log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
280 var request = new pzHttpRequest(auth_url, function(err) {
281 alert("HTTP call for authentication failed: " + err)
285 request.get(null, function(data) {
286 if (!$.isXMLDoc(data)) {
287 alert("service proxy auth response document is not valid XML document, give up!");
290 var status = $(data).find("status");
291 if (status.text() != "OK") {
292 alert("service proxy auth response status: " + status.text() + ", give up!");
296 log("Service proxy auth successfully done");
297 mkws.authenticated = true;
298 var authName = $(data).find("displayName").text();
299 for (var teamName in mkws.teams) {
300 mkws.teams[teamName].queue("authenticated").publish(authName);
308 function runAutoSearches() {
309 log("running auto searches");
311 for (var teamName in mkws.teams) {
312 mkws.teams[teamName].runAutoSearch();
317 $(document).ready(function() {
320 if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
321 log("Reset query width: " + mkws_config.query_width);
322 mkws_config.query_width = 50;
325 for (var key in mkws_config) {
326 if (mkws_config.hasOwnProperty(key)) {
327 if (key.match(/^language_/)) {
328 var lang = key.replace(/^language_/, "");
329 // Copy custom languages into list
330 mkws.locale_lang[lang] = mkws_config[key];
331 log("Added locally configured language '" + lang + "'");
336 if (mkws_config.responsive_design_width) {
337 // Responsive web design - change layout on the fly based on
338 // current screen width. Required for mobile devices.
339 $(window).resize(resizePage);
340 // initial check after page load
341 $(document).ready(resizePage);
344 // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
345 if (mkws_config.pazpar2_url.match(/^\/\//)) {
346 mkws_config.pazpar2_url = document.location.protocol + mkws_config.pazpar2_url;
347 log("adjust protocol independent links: " + mkws_config.pazpar2_url);
350 // Backwards compatibility: set new magic class names on any
351 // elements that have the old magic IDs.
352 var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
353 "Results", "Records", "Targets", "Ranking",
354 "Termlists", "Stat", "MOTD" ];
355 for (var i = 0; i < ids.length; i++) {
356 var id = 'mkws' + ids[i];
357 var node = $('#' + id);
358 if (node.attr('id')) {
360 log("added magic class to '" + node.attr('id') + "'");
364 // For all MKWS-classed nodes that don't have a team
365 // specified, set the team to AUTO.
366 $('[class^="mkws"],[class*=" mkws"]').each(function() {
367 if (!this.className.match(/mkwsTeam_/)) {
368 log("adding AUTO team to node with class '" + this.className + "'");
369 $(this).addClass('mkwsTeam_AUTO');
373 // Find all nodes with an MKWS class, and determine their team from
374 // the mkwsTeam_* class. Make all team objects.
376 $('[class^="mkws"],[class*=" mkws"]').each(function() {
377 handleNodeWithTeam(this, function(tname, type) {
378 if (!mkws.teams[tname]) {
379 mkws.teams[tname] = team(j, tname);
380 log("Made MKWS team '" + tname + "'");
384 // Second pass: make the individual widget objects. This has
385 // to be done separately, and after the team-creation, since
386 // that sometimes makes new widget nodes (e.g. creating
387 // mkwsTermlists inside mkwsResults.
388 $('[class^="mkws"],[class*=" mkws"]').each(function() {
389 handleNodeWithTeam(this, function(tname, type) {
390 var myTeam = mkws.teams[tname];
391 var myWidget = widget(j, myTeam, type, this);
395 log("Walking MKWS nodes took " + (now-then) + " ms");
397 if (mkws_config.use_service_proxy) {
398 authenticateSession(mkws_config.service_proxy_auth,
399 mkws_config.service_proxy_auth_domain,
400 mkws_config.pazpar2_url);