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 // wrapper to call team() after page load
89 function log(string) {
93 if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
97 // you need to disable use strict at the top of the file!!!
98 if (mkws.log_level >= 3) {
99 console.log(arguments.callee.caller);
100 } else if (mkws.log_level >= 2) {
101 console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
108 mkws.registerWidgetType = function(name, fn) {
109 mkws.widgetType2function[name] = fn;
110 log("registered widget-type '" + name + "'");
113 mkws.promotionFunction = function(name) {
114 return mkws.widgetType2function[name];
118 function handleNodeWithTeam(node, callback) {
119 // First branch for DOM objects; second branch for jQuery objects
120 var classes = node.className || node.attr('class');
122 // For some reason, if we try to proceed when classes is
123 // undefined, we don't get an error message, but this
124 // function and its callers, up several stack level,
125 // silently return. What a crock.
126 mkws.log("handleNodeWithTeam() called on node with no classes");
129 var list = classes.split(/\s+/)
132 for (var i = 0; i < list.length; i++) {
134 if (cname.match(/^mkwsTeam_/)) {
135 teamName = cname.replace(/^mkwsTeam_/, '');
136 } else if (cname.match(/^mkws/)) {
137 type = cname.replace(/^mkws/, '');
140 callback.call(node, teamName, type);
144 function resizePage() {
145 var list = ["mkwsSwitch", "mkwsLang"];
147 var width = mkws_config.responsive_design_width;
148 var parent = $(".mkwsTermlists").parent();
150 if ($(window).width() <= width &&
151 parent.hasClass("mkwsTermlistContainer1")) {
152 log("changing from wide to narrow: " + $(window).width());
153 $(".mkwsTermlistContainer1").hide();
154 $(".mkwsTermlistContainer2").show();
155 for (var tname in mkws.teams) {
156 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer2.mkwsTeam_" + tname));
157 for(var i = 0; i < list.length; i++) {
158 $("." + list[i] + ".mkwsTeam_" + tname).hide();
161 } else if ($(window).width() > width &&
162 parent.hasClass("mkwsTermlistContainer2")) {
163 log("changing from narrow to wide: " + $(window).width());
164 $(".mkwsTermlistContainer1").show();
165 $(".mkwsTermlistContainer2").hide();
166 for (var tname in mkws.teams) {
167 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer1.mkwsTeam_" + tname));
168 for(var i = 0; i < list.length; i++) {
169 $("." + list[i] + ".mkwsTeam_" + tname).show();
176 // The following functions are dispatchers for team methods that
177 // are called from the UI using a team-name rather than implicit
178 // context. Apart from mkws.log, they are the ONLY public UI to
180 mkws.switchView = function(tname, view) {
181 mkws.teams[tname].switchView(view);
184 mkws.showDetails = function (tname, prefixRecId) {
185 mkws.teams[tname].showDetails(prefixRecId);
188 mkws.limitTarget = function (tname, id, name) {
189 mkws.teams[tname].limitTarget(id, name);
192 mkws.limitQuery = function (tname, field, value) {
193 mkws.teams[tname].limitQuery(field, value);
196 mkws.delimitTarget = function (tname, id) {
197 mkws.teams[tname].delimitTarget(id);
200 mkws.delimitQuery = function (tname, field, value) {
201 mkws.teams[tname].delimitQuery(field, value);
204 mkws.showPage = function (tname, pageNum) {
205 mkws.teams[tname].showPage(pageNum);
208 mkws.pagerPrev = function (tname) {
209 mkws.teams[tname].pagerPrev();
212 mkws.pagerNext = function (tname) {
213 mkws.teams[tname].pagerNext();
217 function defaultMkwsConfig() {
218 /* default mkws config */
219 var config_default = {
220 use_service_proxy: true,
221 pazpar2_url: "//mkws.indexdata.com/service-proxy/",
222 service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
224 sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
225 perpage_options: [10, 20, 30, 50],
226 sort_default: "relevance",
229 show_lang: true, /* show/hide language menu */
230 show_sort: true, /* show/hide sort menu */
231 show_perpage: true, /* show/hide perpage menu */
232 lang_options: [], /* display languages links for given languages, [] for all */
233 facets: ["xtargets", "subject", "author"], /* display facets, in this order, [] for none */
234 responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
235 log_level: 1, /* log level for development: 0..2 */
240 // Set global log_level flag early so that log() works
241 // Fall back to old "debug_level" setting for backwards compatibility
242 var tmp = mkws_config.log_level;
243 if (typeof(tmp) === 'undefined') tmp = mkws_config.debug_level;
245 if (typeof(tmp) !== 'undefined') {
246 mkws.log_level = tmp;
247 } else if (typeof(config_default.log_level) !== 'undefined') {
248 mkws.log_level = config_default.log_level;
251 // make sure the mkws_config is a valid hash
252 if (!$.isPlainObject(mkws_config)) {
253 log("ERROR: mkws_config is not an JS object, ignore it....");
257 /* override standard config values by function parameters */
258 for (var k in config_default) {
259 if (typeof mkws_config[k] === 'undefined')
260 mkws_config[k] = config_default[k];
261 //log("Set config: " + k + ' => ' + mkws_config[k]);
267 * Run service-proxy authentication in background (after page load).
268 * The username/password is configured in the apache config file
271 function authenticateSession(auth_url, auth_domain, pp2_url) {
272 log("Run service proxy auth URL: " + auth_url);
275 auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
276 log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
279 var request = new pzHttpRequest(auth_url, function(err) {
280 alert("HTTP call for authentication failed: " + err)
284 request.get(null, function(data) {
285 if (!$.isXMLDoc(data)) {
286 alert("service proxy auth response document is not valid XML document, give up!");
289 var status = $(data).find("status");
290 if (status.text() != "OK") {
291 alert("service proxy auth response status: " + status.text() + ", give up!");
295 log("Service proxy auth successfully done");
296 mkws.authenticated = true;
297 mkws.authName = $(data).find("displayName").text();
298 log("authName='" + mkws.authName + "'");
304 function runAutoSearches() {
305 log("running auto searches");
307 for (var teamName in mkws.teams) {
308 mkws.teams[teamName].runAutoSearch();
313 $(document).ready(function() {
316 if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
317 log("Reset query width: " + mkws_config.query_width);
318 mkws_config.query_width = 50;
321 for (var key in mkws_config) {
322 if (mkws_config.hasOwnProperty(key)) {
323 if (key.match(/^language_/)) {
324 var lang = key.replace(/^language_/, "");
325 // Copy custom languages into list
326 mkws.locale_lang[lang] = mkws_config[key];
327 log("Added locally configured language '" + lang + "'");
332 if (mkws_config.responsive_design_width) {
333 // Responsive web design - change layout on the fly based on
334 // current screen width. Required for mobile devices.
335 $(window).resize(resizePage);
336 // initial check after page load
337 $(document).ready(resizePage);
340 // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
341 if (mkws_config.pazpar2_url.match(/^\/\//)) {
342 mkws_config.pazpar2_url = document.location.protocol + mkws_config.pazpar2_url;
343 log("adjust protocol independent links: " + mkws_config.pazpar2_url);
346 // Backwards compatibility: set new magic class names on any
347 // elements that have the old magic IDs.
348 var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
349 "Results", "Records", "Targets", "Ranking",
350 "Termlists", "Stat", "MOTD" ];
351 for (var i = 0; i < ids.length; i++) {
352 var id = 'mkws' + ids[i];
353 var node = $('#' + id);
354 if (node.attr('id')) {
356 log("added magic class to '" + node.attr('id') + "'");
360 // For all MKWS-classed nodes that don't have a team
361 // specified, set the team to AUTO.
362 $('[class^="mkws"],[class*=" mkws"]').each(function () {
363 if (!this.className.match(/mkwsTeam_/)) {
364 log("adding AUTO team to node with class '" + this.className + "'");
365 $(this).addClass('mkwsTeam_AUTO');
369 // Find all nodes with an MKWS class, and determine their team from
370 // the mkwsTeam_* class. Make all team objects.
372 $('[class^="mkws"],[class*=" mkws"]').each(function () {
373 handleNodeWithTeam(this, function(tname, type) {
374 if (!mkws.teams[tname]) {
375 mkws.teams[tname] = team(j, tname);
376 log("Made MKWS team '" + tname + "'");
380 // Second pass: make the individual widget objects. This has
381 // to be done separately, and after the team-creation, since
382 // that sometimes makes new widget nodes (e.g. creating
383 // mkwsTermlists inside mkwsResults.
384 $('[class^="mkws"],[class*=" mkws"]').each(function () {
385 handleNodeWithTeam(this, function(tname, type) {
386 var myTeam = mkws.teams[tname];
387 var myWidget = widget(j, myTeam, type, this);
391 log("Walking MKWS nodes took " + (now-then) + " ms");
393 if (mkws_config.use_service_proxy) {
394 authenticateSession(mkws_config.service_proxy_auth,
395 mkws_config.service_proxy_auth_domain,
396 mkws_config.pazpar2_url);