encapsulate jQuery object usage, MKWS-24
[mkws-moved-to-github.git] / tools / htdocs / mkws.js
index c5f7d48..75f9f4f 100644 (file)
@@ -25,8 +25,21 @@ var service_proxy_url = mkws_config.service_proxy_url ? mkws_config.service_prox
 var pazpar2path = mkws_config.use_service_proxy ? service_proxy_url : pazpar2_url;
 var usesessions = mkws_config.use_service_proxy ? false : true;
 
-
-var mkws_locale_lang = {
+// exported symobols
+var debug;
+var my_paz;
+var limitTarget;
+var delimitTarget;
+var limitQuery;
+var showDetails;
+var pagerNext;
+var switchView;
+var showPage;
+var mkws_locale_lang;
+
+(function ($) {
+
+mkws_locale_lang = {
     "de": {
        "Authors": "Autoren",
        "Subjects": "Schlagwörter",
@@ -76,6 +89,22 @@ var mkws_locale_lang = {
     }
 };
 
+debug = function (string) {
+    if (!mkws_debug)
+       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_debug >= 3) {
+       console.log(arguments.callee.caller);
+    } else if (mkws_debug >= 2) {
+       console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
+    }
+    console.log(string);
+}
 
 for (var key in mkws_config) {
     if (mkws_config.hasOwnProperty(key)) {
@@ -91,7 +120,7 @@ for (var key in mkws_config) {
 // create a parameters array and pass it to the pz2's constructor
 // then register the form submit event with the pz2.search function
 // autoInit is set to true on default
-var my_paz = new pz2( { "onshow": my_onshow,
+my_paz = new pz2( { "onshow": my_onshow,
                     "showtime": 500,            //each timer (show, stat, term, bytarget) can be specified this way
                     "pazpar2path": pazpar2path,
                     "oninit": my_oninit,
@@ -201,7 +230,7 @@ function my_onterm(data) {
 }
 
 function add_single_facet(acc, caption, data, max, cclIndex) {
-    acc.push('<div class="facet">');
+    acc.push('<div class="facet" id="mkwsFacet' + caption + '">');
     acc.push('<div class="termtitle">' + M(caption) + '</div>');
     for (var i = 0; i < data.length && i < max; i++ ) {
        acc.push('<div class="term">');
@@ -235,6 +264,11 @@ function my_onrecord(data) {
 
 function my_onbytarget(data) {
     var targetDiv = document.getElementById("mkwsBytarget");
+    if (!targetDiv) {
+       // No mkwsTargets div.
+       return;
+    }
+
     var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
         +'<td>Records</td><td>State</td></tr></thead><tbody>';
 
@@ -308,14 +342,14 @@ function loadSelect ()
 }
 
 // limit the query after clicking the facet
-function limitQuery (field, value)
+limitQuery = function (field, value)
 {
-    document.mkwsSearchForm.query.value += ' and ' + field + '="' + value + '"';
+    document.mkwsSearchForm.mkwsQuery.value += ' and ' + field + '="' + value + '"';
     onFormSubmitEventHandler();
 }
 
 // limit by target functions
-function limitTarget (id, name)
+limitTarget  = function (id, name)
 {
     var navi = document.getElementById('mkwsNavi');
     navi.innerHTML =
@@ -328,7 +362,7 @@ function limitTarget (id, name)
     return false;
 }
 
-function delimitTarget ()
+delimitTarget = function ()
 {
     var navi = document.getElementById('mkwsNavi');
     navi.innerHTML = '';
@@ -385,7 +419,7 @@ function drawPager (pagerDiv)
         + prev + predots + middle + postdots + next + '</div>';
 }
 
-function showPage (pageNum)
+showPage = function (pageNum)
 {
     curPage = pageNum;
     my_paz.showPage( curPage - 1 );
@@ -393,7 +427,7 @@ function showPage (pageNum)
 
 // simple paging functions
 
-function pagerNext() {
+pagerNext = function () {
     if ( totalRec - recPerPage*curPage > 0) {
         my_paz.showNext();
         curPage++;
@@ -407,7 +441,7 @@ function pagerPrev() {
 
 // switching view between targets and records
 
-function switchView(view) {
+switchView = function(view) {
     var targets = document.getElementById('mkwsTargets');
     var results = document.getElementById('mkwsResults') ||
                  document.getElementById('mkwsRecords');
@@ -439,7 +473,7 @@ function switchView(view) {
 }
 
 // detailed record drawing
-function showDetails (prefixRecId) {
+showDetails = function (prefixRecId) {
     var recId = prefixRecId.replace('mkwsRec_', '');
     var oldRecId = curDetRecId;
     curDetRecId = recId;
@@ -524,7 +558,6 @@ function mkws_html_all(config) {
        sort_default: "relevance",
        perpage_default: 20,
        query_width: 50,
-       switch_menu: false,     /* show/hide Records|Targets menu */
        lang_menu: true,        /* show/hide language menu */
        sort_menu: true,        /* show/hide sort menu */
        perpage_menu: true,     /* show/hide perpage menu */
@@ -640,7 +673,7 @@ function mkws_html_all(config) {
 }
 
 function mkws_set_lang(mkws_config)  {
-    var lang = jQuery.parseQuerystring().lang || mkws_config.lang || "";
+    var lang = $.parseQuerystring().lang || mkws_config.lang || "";
     if (!lang || !mkws_locale_lang[lang]) {
        mkws_config.lang = ""
     } else {
@@ -672,11 +705,6 @@ function mkws_html_switch(config) {
        No information available yet.\
       </div>');
     $("#mkwsTargets").css("display", "none");
-
-    if (!config.switch_menu) {
-       debug("disable switch menu");
-        $("#mkwsSwitch").css("display", "none");
-    }
 }
 
 function mkws_html_sort(config) {
@@ -727,9 +755,12 @@ function mkws_service_proxy_auth(auth_url) {
 
     debug("Run service proxy auth URL: " + auth_url);
 
-    var request = new pzHttpRequest(auth_url);
+    var request = new pzHttpRequest(auth_url, function(err) {
+       alert("HTTP call for authentication failed: " + err)
+       return;
+    });
     request.get(null, function(data) {
-       if (!jQuery.isXMLDoc(data)) {
+       if (!$.isXMLDoc(data)) {
            alert("service proxy auth response document is not valid XML document, give up!");
            return;
        }
@@ -823,8 +854,8 @@ function M(word) {
 /*
  * implement jQuery plugins
  */
-jQuery.extend({
-    // implement jQuery.parseQuerystring() for parsing URL parameters
+$.extend({
+    // implement $.parseQuerystring() for parsing URL parameters
     parseQuerystring: function() {
        var nvpair = {};
        var qs = window.location.search.replace('?', '');
@@ -842,6 +873,9 @@ jQuery.extend({
 
     // service-proxy or pazpar2
     pazpar2: function(config) {
+       var id_popup = config.id_popup || "#mkwsPopup";
+       id_popup = id_popup.replace(/^#/, "");
+
        // simple layout
        var div = '<div id="mkwsSwitch"></div>\
        <div id="mkwsLang"></div>\
@@ -898,7 +932,7 @@ jQuery.extend({
 
        var popup = '\
          <div id="mkwsSearch"></div>\
-         <div id="mkwsPopup">\
+         <div id="' + id_popup + '">\
            <div id="mkwsSwitch"></div>\
            <div id="mkwsLang"></div>\
            <div id="mkwsResults"></div>\
@@ -910,7 +944,7 @@ jQuery.extend({
            this.debug2("jquery plugin layout: div");
            document.write(div);
        } else if (config && config.layout == 'popup') {
-           this.debug2("jquery plugin layout: popup");
+           this.debug2("jquery plugin layout: popup with id: " + id_popup);
            document.write(popup);
            $(document).ready( function() { init_popup(config); } );
        } else {
@@ -925,11 +959,18 @@ function init_popup(obj) {
 
     var height = config.height || 760;
     var width = config.width || 880;
-    var id_button = config.button || "input#mkwsButton";
-    var id_popup = config.popup || "#mkwsPopup";
+    var id_button = config.id_button || "input#mkwsButton";
+    var id_popup = config.id_popup || "#mkwsPopup";
 
     debug("popup height: " + height + ", width: " + width);
 
+    // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
+    // <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
+    if (!$.ui) {
+       debug("Error: jquery-ui.js is missing, did you included it after jquery core in the HTML file?");
+       return;
+    }
+
     $(id_popup).dialog({
       closeOnEscape: true,
       autoOpen: false,
@@ -952,23 +993,21 @@ function init_popup(obj) {
       });
 };
 
-function debug(string) {
-    if (!mkws_debug)
-       return;
 
-    if (typeof console === "undefined" || typeof console.log === "undefined") { /* ARGH!!! old IE */
-       return;
+
+
+/* magic */
+$(document).ready(function() {
+    try {
+       mkws_html_all(mkws_config)
     }
 
-    // you need to disable use strict at the top of the file!!!
-    if (mkws_debug >= 3) {
-       console.log(arguments.callee.caller);
-    } else if (mkws_debug >= 2) {
-       console.log(">>> called from function " + arguments.callee.caller.name + ' <<<');
+    catch (e) {
+       mkws_config.error = e.message;
+       // alert(e.message);
     }
-    console.log(string);
-}
+});
 
+})(jQuery);
 
-/* magic */
-$(document).ready(function() { mkws_html_all(mkws_config) });
+jQuery = null;
\ No newline at end of file