Reindent.
[mkws-moved-to-github.git] / src / mkws-core.js
index 4870c7c..233a77f 100644 (file)
@@ -13,8 +13,9 @@ var mkws = {
     authenticated: false,
     log_level: 1, // Will be overridden from mkws_config, but
                   // initial value allows jQuery popup to use logging.
-    paz: undefined, // will be set up during initialisation
     teams: {},
+    widgetType2function: {},
+
     locale_lang: {
        "de": {
            "Authors": "Autoren",
@@ -83,28 +84,40 @@ if (mkws_config == null || typeof mkws_config != 'object') {
 }
 
 
-// wrapper to call team() after page load
-(function (j) {
-    mkws.log = function (string) {
-       if (!mkws.log_level)
-           return;
+mkws.log = function(string) {
+    if (!mkws.log_level)
+       return;
 
-       if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
-           return;
-       }
+    if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
+       return;
+    }
 
-       // you need to disable use strict at the top of the file!!!
-       if (mkws.log_level >= 3) {
-           console.log(arguments.callee.caller);
-       } else if (mkws.log_level >= 2) {
-           console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
-       }
-       console.log(string);
+    // you need to disable use strict at the top of the file!!!
+    if (mkws.log_level >= 3) {
+       console.log(arguments.callee.caller);
+    } else if (mkws.log_level >= 2) {
+       console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
     }
+    console.log(string);
+};
+
+
+
+// wrapper to call team() after page load
+(function(j) {
     var log = mkws.log;
 
+    mkws.registerWidgetType = function(name, fn) {
+       mkws.widgetType2function[name] = fn;
+       log("registered widget-type '" + name + "'");
+    }
+
+    mkws.promotionFunction = function(name) {
+       return mkws.widgetType2function[name];
+    }
+
 
-    mkws.handleNodeWithTeam = function(node, callback) {
+    function handleNodeWithTeam(node, callback) {
        // First branch for DOM objects; second branch for jQuery objects
        var classes = node.className || node.attr('class');
        if (!classes) {
@@ -130,7 +143,7 @@ if (mkws_config == null || typeof mkws_config != 'object') {
     }
 
 
-    mkws.resizePage = function () {
+    function resizePage() {
        var list = ["mkwsSwitch", "mkwsLang"];
 
        var width = mkws_config.responsive_design_width;
@@ -162,134 +175,47 @@ if (mkws_config == null || typeof mkws_config != 'object') {
     };
 
 
+    // The following functions are dispatchers for team methods that
+    // are called from the UI using a team-name rather than implicit
+    // context. Apart from mkws.log, they are the ONLY public UI to
+    // this module.
     mkws.switchView = function(tname, view) {
        mkws.teams[tname].switchView(view);
     }
 
-    mkws.showDetails = function (tname, prefixRecId) {
+    mkws.showDetails = function(tname, prefixRecId) {
        mkws.teams[tname].showDetails(prefixRecId);
     }
 
-    mkws.limitTarget  = function (tname, id, name) {
+    mkws.limitTarget  = function(tname, id, name) {
        mkws.teams[tname].limitTarget(id, name);
     }
 
-    mkws.limitQuery  = function (tname, field, value) {
+    mkws.limitQuery  = function(tname, field, value) {
        mkws.teams[tname].limitQuery(field, value);
     }
 
-    mkws.delimitTarget = function (tname, id) {
+    mkws.delimitTarget = function(tname, id) {
        mkws.teams[tname].delimitTarget(id);
     }
 
-    mkws.delimitQuery = function (tname, field, value) {
+    mkws.delimitQuery = function(tname, field, value) {
        mkws.teams[tname].delimitQuery(field, value);
     }
 
-    mkws.showPage = function (tname, pageNum) {
+    mkws.showPage = function(tname, pageNum) {
        mkws.teams[tname].showPage(pageNum);
     }
 
-    mkws.pagerPrev = function (tname) {
+    mkws.pagerPrev = function(tname) {
        mkws.teams[tname].pagerPrev();
     }
 
-    mkws.pagerNext = function (tname) {
+    mkws.pagerNext = function(tname) {
        mkws.teams[tname].pagerNext();
     }
 
 
-    // This function is taken from a StackOverflow answer
-    // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
-    mkws.getParameterByName = function(name) {
-       name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
-       var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
-           results = regex.exec(location.search);
-       return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
-    }
-
-
-    mkws.defaultTemplate = function(name)
-    {
-       if (name === 'Record') {
-           return '\
-<table>\
-  <tr>\
-    <th>{{translate "Title"}}</th>\
-    <td>\
-      {{md-title}}\
-      {{#if md-title-remainder}}\
-       ({{md-title-remainder}})\
-      {{/if}}\
-      {{#if md-title-responsibility}}\
-       <i>{{md-title-responsibility}}</i>\
-      {{/if}}\
-    </td>\
-  </tr>\
-  {{#if md-date}}\
-  <tr>\
-    <th>{{translate "Date"}}</th>\
-    <td>{{md-date}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-author}}\
-  <tr>\
-    <th>{{translate "Author"}}</th>\
-    <td>{{md-author}}</td>\
-  </tr>\
-  {{/if}}\
-  {{#if md-electronic-url}}\
-  <tr>\
-    <th>{{translate "Links"}}</th>\
-    <td>\
-      {{#each md-electronic-url}}\
-       <a href="{{this}}">Link{{index1}}</a>\
-      {{/each}}\
-    </td>\
-  </tr>\
-  {{/if}}\
-  {{#if-any location having="md-subject"}}\
-  <tr>\
-    <th>{{translate "Subject"}}</th>\
-    <td>\
-      {{#first location having="md-subject"}}\
-       {{#if md-subject}}\
-         {{#commaList md-subject}}\
-           {{this}}{{/commaList}}\
-       {{/if}}\
-      {{/first}}\
-    </td>\
-  </tr>\
-  {{/if-any}}\
-  <tr>\
-    <th>{{translate "Locations"}}</th>\
-    <td>\
-      {{#commaList location}}\
-       {{attr "@name"}}{{/commaList}}\
-    </td>\
-  </tr>\
-</table>\
-';
-       } else if (name === "Summary") {
-           return '\
-<a href="#" id="{{_id}}" onclick="{{_onclick}}">\
-  <b>{{md-title}}</b>\
-</a>\
-{{#if md-title-remainder}}\
-  <span>{{md-title-remainder}}</span>\
-{{/if}}\
-{{#if md-title-responsibility}}\
-  <span><i>{{md-title-responsibility}}</i></span>\
-{{/if}}\
-';
-       }
-
-       var s = "There is no default '" + name +"' template!";
-       alert(s);
-       return s;
-    }
-
-
     function defaultMkwsConfig() {
        /* default mkws config */
        var config_default = {
@@ -364,12 +290,17 @@ if (mkws_config == null || typeof mkws_config != 'object') {
            }
            var status = $(data).find("status");
            if (status.text() != "OK") {
-               alert("service proxy auth repsonse status: " + status.text() + ", give up!");
+               alert("service proxy auth response status: " + status.text() + ", give up!");
                return;
            }
 
            log("Service proxy auth successfully done");
            mkws.authenticated = true;
+           var authName = $(data).find("displayName").text();
+           for (var teamName in mkws.teams) {
+               mkws.teams[teamName].queue("authenticated").publish(authName);
+           }
+
            runAutoSearches();
        });
     }
@@ -385,7 +316,6 @@ if (mkws_config == null || typeof mkws_config != 'object') {
 
 
     $(document).ready(function() {
-       log("on load ready");
        defaultMkwsConfig();
 
        if (mkws_config.query_width < 5 || mkws_config.query_width > 150) {
@@ -407,9 +337,9 @@ if (mkws_config == null || typeof mkws_config != 'object') {
        if (mkws_config.responsive_design_width) {
            // Responsive web design - change layout on the fly based on
            // current screen width. Required for mobile devices.
-           $(window).resize(function(e) { mkws.resizePage() });
+           $(window).resize(resizePage);
            // initial check after page load
-           $(document).ready(function() { mkws.resizePage() });
+           $(document).ready(resizePage);
        }
 
        // protocol independent link for pazpar2: "//mkws/sp" -> "https://mkws/sp"
@@ -434,7 +364,7 @@ if (mkws_config == null || typeof mkws_config != 'object') {
 
        // For all MKWS-classed nodes that don't have a team
        // specified, set the team to AUTO.
-       $('[class^="mkws"],[class*=" mkws"]').each(function () {
+       $('[class^="mkws"],[class*=" mkws"]').each(function() {
            if (!this.className.match(/mkwsTeam_/)) {
                log("adding AUTO team to node with class '" + this.className + "'");
                $(this).addClass('mkwsTeam_AUTO');
@@ -444,8 +374,8 @@ if (mkws_config == null || typeof mkws_config != 'object') {
        // Find all nodes with an MKWS class, and determine their team from
        // the mkwsTeam_* class. Make all team objects.
        var then = $.now();
-       $('[class^="mkws"],[class*=" mkws"]').each(function () {
-           mkws.handleNodeWithTeam(this, function(tname, type) {
+       $('[class^="mkws"],[class*=" mkws"]').each(function() {
+           handleNodeWithTeam(this, function(tname, type) {
                if (!mkws.teams[tname]) {
                    mkws.teams[tname] = team(j, tname);
                    log("Made MKWS team '" + tname + "'");
@@ -456,8 +386,8 @@ if (mkws_config == null || typeof mkws_config != 'object') {
        // to be done separately, and after the team-creation, since
        // that sometimes makes new widget nodes (e.g. creating
        // mkwsTermlists inside mkwsResults.
-       $('[class^="mkws"],[class*=" mkws"]').each(function () {
-           mkws.handleNodeWithTeam(this, function(tname, type) {
+       $('[class^="mkws"],[class*=" mkws"]').each(function() {
+           handleNodeWithTeam(this, function(tname, type) {
                var myTeam = mkws.teams[tname];
                var myWidget = widget(j, myTeam, type, this);
            });