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.
20 "Subjects": "Schlagwörter",
21 "Sources": "Daten und Quellen",
22 "source": "datenquelle",
23 "Termlists": "Termlisten",
25 "Prev": "Zurück",
27 "Sort by": "Sortieren nach",
28 "and show": "und zeige",
29 "per page": "pro Seite",
30 "Displaying": "Zeige",
38 "Subject": "Schlagwort",
39 "subject": "schlagwort",
41 "Records": "Datensätze",
42 "Targets": "Datenbanken",
48 "Authors": "Forfattere",
52 "Termlists": "Termlists",
53 "Next": "Næste",
55 "Search": "Søg",
56 "Sort by": "Sorter efter",
58 "per page": "per side",
59 "Displaying": "Viser",
64 "Author": "Forfatter",
65 "author": "forfatter",
69 "Location": "Lokation",
79 // Define empty mkws_config for simple applications that don't define it.
80 if (mkws_config == null || typeof mkws_config != 'object') {
85 // wrapper to call team() after page load
87 function log(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 + ' <<<');
106 function handleNodeWithTeam(node, callback) {
107 // First branch for DOM objects; second branch for jQuery objects
108 var classes = node.className || node.attr('class');
110 // For some reason, if we try to proceed when classes is
111 // undefined, we don't get an error message, but this
112 // function and its callers, up several stack level,
113 // silently return. What a crock.
114 mkws.log("handleNodeWithTeam() called on node with no classes");
117 var list = classes.split(/\s+/)
120 for (var i = 0; i < list.length; i++) {
122 if (cname.match(/^mkwsTeam_/)) {
123 teamName = cname.replace(/^mkwsTeam_/, '');
124 } else if (cname.match(/^mkws/)) {
125 type = cname.replace(/^mkws/, '');
128 callback.call(node, teamName, type);
132 function resizePage() {
133 var list = ["mkwsSwitch", "mkwsLang"];
135 var width = mkws_config.responsive_design_width;
136 var parent = $(".mkwsTermlists").parent();
138 if ($(window).width() <= width &&
139 parent.hasClass("mkwsTermlistContainer1")) {
140 log("changing from wide to narrow: " + $(window).width());
141 $(".mkwsTermlistContainer1").hide();
142 $(".mkwsTermlistContainer2").show();
143 for (var tname in mkws.teams) {
144 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer2.mkwsTeam_" + tname));
145 for(var i = 0; i < list.length; i++) {
146 $("." + list[i] + ".mkwsTeam_" + tname).hide();
149 } else if ($(window).width() > width &&
150 parent.hasClass("mkwsTermlistContainer2")) {
151 log("changing from narrow to wide: " + $(window).width());
152 $(".mkwsTermlistContainer1").show();
153 $(".mkwsTermlistContainer2").hide();
154 for (var tname in mkws.teams) {
155 $(".mkwsTermlists.mkwsTeam_" + tname).appendTo($(".mkwsTermlistContainer1.mkwsTeam_" + tname));
156 for(var i = 0; i < list.length; i++) {
157 $("." + list[i] + ".mkwsTeam_" + tname).show();
164 // The following functions are dispatchers for team methods that
165 // are called from the UI using a te3am-name rather than implicit
167 mkws.switchView = function(tname, view) {
168 mkws.teams[tname].switchView(view);
171 mkws.showDetails = function (tname, prefixRecId) {
172 mkws.teams[tname].showDetails(prefixRecId);
175 mkws.limitTarget = function (tname, id, name) {
176 mkws.teams[tname].limitTarget(id, name);
179 mkws.limitQuery = function (tname, field, value) {
180 mkws.teams[tname].limitQuery(field, value);
183 mkws.delimitTarget = function (tname, id) {
184 mkws.teams[tname].delimitTarget(id);
187 mkws.delimitQuery = function (tname, field, value) {
188 mkws.teams[tname].delimitQuery(field, value);
191 mkws.showPage = function (tname, pageNum) {
192 mkws.teams[tname].showPage(pageNum);
195 mkws.pagerPrev = function (tname) {
196 mkws.teams[tname].pagerPrev();
199 mkws.pagerNext = function (tname) {
200 mkws.teams[tname].pagerNext();
204 function defaultMkwsConfig() {
205 /* default mkws config */
206 var config_default = {
207 use_service_proxy: true,
208 pazpar2_url: "//mkws.indexdata.com/service-proxy/",
209 service_proxy_auth: "//mkws.indexdata.com/service-proxy-auth",
211 sort_options: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
212 perpage_options: [10, 20, 30, 50],
213 sort_default: "relevance",
216 show_lang: true, /* show/hide language menu */
217 show_sort: true, /* show/hide sort menu */
218 show_perpage: true, /* show/hide perpage menu */
219 lang_options: [], /* display languages links for given languages, [] for all */
220 facets: ["xtargets", "subject", "author"], /* display facets, in this order, [] for none */
221 responsive_design_width: undefined, /* a page with less pixel width considered as narrow */
222 log_level: 1, /* log level for development: 0..2 */
227 // Set global log_level flag early so that log() works
228 // Fall back to old "debug_level" setting for backwards compatibility
229 var tmp = mkws_config.log_level;
230 if (typeof(tmp) === 'undefined') tmp = mkws_config.debug_level;
232 if (typeof(tmp) !== 'undefined') {
233 mkws.log_level = tmp;
234 } else if (typeof(config_default.log_level) !== 'undefined') {
235 mkws.log_level = config_default.log_level;
238 // make sure the mkws_config is a valid hash
239 if (!$.isPlainObject(mkws_config)) {
240 log("ERROR: mkws_config is not an JS object, ignore it....");
244 /* override standard config values by function parameters */
245 for (var k in config_default) {
246 if (typeof mkws_config[k] === 'undefined')
247 mkws_config[k] = config_default[k];
248 //log("Set config: " + k + ' => ' + mkws_config[k]);
254 * Run service-proxy authentication in background (after page load).
255 * The username/password is configured in the apache config file
258 function authenticateSession(auth_url, auth_domain, pp2_url) {
259 log("Run service proxy auth URL: " + auth_url);
262 auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
263 log("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
266 var request = new pzHttpRequest(auth_url, function(err) {
267 alert("HTTP call for authentication failed: " + err)
271 request.get(null, function(data) {
272 if (!$.isXMLDoc(data)) {
273 alert("service proxy auth response document is not valid XML document, give up!");
276 var status = $(data).find("status");
277 if (status.text() != "OK") {
278 alert("service proxy auth repsonse status: " + status.text() + ", give up!");
282 log("Service proxy auth successfully done");
283 mkws.authenticated = true;
289 function runAutoSearches() {
290 log("running auto searches");
292 for (var teamName in mkws.teams) {
293 mkws.teams[teamName].runAutoSearch();
298 $(document).ready(function() {
299 log("on load ready");
302 if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
303 log("Reset query width: " + mkws_config.query_width);
304 mkws_config.query_width = 50;
307 for (var key in mkws_config) {
308 if (mkws_config.hasOwnProperty(key)) {
309 if (key.match(/^language_/)) {
310 var lang = key.replace(/^language_/, "");
311 // Copy custom languages into list
312 mkws.locale_lang[lang] = mkws_config[key];
313 log("Added locally configured language '" + lang + "'");
318 if (mkws_config.responsive_design_width) {
319 // Responsive web design - change layout on the fly based on
320 // current screen width. Required for mobile devices.
321 $(window).resize(resizePage);
322 // initial check after page load
323 $(document).ready(resizePage);
326 // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
327 if (mkws_config.pazpar2_url.match(/^\/\//)) {
328 mkws_config.pazpar2_url = document.location.protocol + mkws_config.pazpar2_url;
329 log("adjust protocol independent links: " + mkws_config.pazpar2_url);
332 // Backwards compatibility: set new magic class names on any
333 // elements that have the old magic IDs.
334 var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
335 "Results", "Records", "Targets", "Ranking",
336 "Termlists", "Stat", "MOTD" ];
337 for (var i = 0; i < ids.length; i++) {
338 var id = 'mkws' + ids[i];
339 var node = $('#' + id);
340 if (node.attr('id')) {
342 log("added magic class to '" + node.attr('id') + "'");
346 // For all MKWS-classed nodes that don't have a team
347 // specified, set the team to AUTO.
348 $('[class^="mkws"],[class*=" mkws"]').each(function () {
349 if (!this.className.match(/mkwsTeam_/)) {
350 log("adding AUTO team to node with class '" + this.className + "'");
351 $(this).addClass('mkwsTeam_AUTO');
355 // Find all nodes with an MKWS class, and determine their team from
356 // the mkwsTeam_* class. Make all team objects.
358 $('[class^="mkws"],[class*=" mkws"]').each(function () {
359 handleNodeWithTeam(this, function(tname, type) {
360 if (!mkws.teams[tname]) {
361 mkws.teams[tname] = team(j, tname);
362 log("Made MKWS team '" + tname + "'");
366 // Second pass: make the individual widget objects. This has
367 // to be done separately, and after the team-creation, since
368 // that sometimes makes new widget nodes (e.g. creating
369 // mkwsTermlists inside mkwsResults.
370 $('[class^="mkws"],[class*=" mkws"]').each(function () {
371 handleNodeWithTeam(this, function(tname, type) {
372 var myTeam = mkws.teams[tname];
373 var myWidget = widget(j, myTeam, type, this);
377 log("Walking MKWS nodes took " + (now-then) + " ms");
379 if (mkws_config.use_service_proxy) {
380 authenticateSession(mkws_config.service_proxy_auth,
381 mkws_config.service_proxy_auth_domain,
382 mkws_config.pazpar2_url);