make the code more robust
[mkws-moved-to-github.git] / experiments / spclient / mkws.js
index f0a8f7f..30bf3b2 100644 (file)
@@ -1,18 +1,29 @@
 /* A very simple client that shows a basic usage of the pz2.js
 */
 
+"use strict"; // HTML5
+
+if (!mkws_config)
+    var mkws_config = {}; // for the guys who forgot to define mkws_config...
+
+var pazpar2_url = mkws_config.pazpar2_url ? mkws_config.pazpar2_url : "/pazpar2/search.pz2";
+var service_proxy_url = mkws_config.service_proxy_url ? mkws_config.service_proxy_url : "/service-proxy/";
+
+var pazpar2path = mkws_config.use_service_proxy ? service_proxy_url : pazpar2_url;
+var usesessions = mkws_config.use_service_proxy ? false : true;
+
 // 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,
                     "showtime": 500,            //each timer (show, stat, term, bytarget) can be specified this way
-                    "pazpar2path": '/service-proxy/',
+                    "pazpar2path": pazpar2path,
                     "oninit": my_oninit,
                     "onstat": my_onstat,
                     "onterm": my_onterm,
                     "termlist": "xtargets,subject,author",
                     "onbytarget": my_onbytarget,
-                   "usesessions" : false,
+                   "usesessions" : usesessions,
                     "showResponseType": '', // or "json" (for debugging?)
                     "onrecord": my_onrecord } );
 // some state vars
@@ -371,18 +382,28 @@ function renderDetails(data, marker)
     return details;
 }
 
-function mkws_html_all() {
-    $("#mkwsSwitch").html($("<a/>", {
-       href: '#',
-       onclick: "switchView(\'records\')",
-       text: "Records",
-    }));
-    $("#mkwsSwitch").append($("<span/>", { text: " | " }));
-    $("#mkwsSwitch").append($("<a/>", {
-       href: '#',
-       onclick: "switchView(\'targets\')",
-       text: "Targets",
-    }));
+/*
+ * All the HTML stuff to render the search forms and
+ * result pages.
+ */
+function mkws_html_all(data) {
+
+    /* default config */
+    var config = {
+       sort: [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]],
+       perpage: [10, 20, 30, 50],
+       sort_default: "relevance",
+       perpage_default: 20,
+       query_width: 50,
+       switch: true, /* show/hide Records|Targets menu */
+
+       dummy: "dummy"
+    };
+
+    /* override standard config values by function parameters */
+    for (var k in data) {
+       config[k] = data[k];
+    }
 
     // For some reason, doing this programmatically results in
     // document.search.query being undefined, hence the raw HTML.
@@ -401,10 +422,8 @@ function mkws_html_all() {
           <td valign="top">\
             <div id="ranking">\
               <form name="select" id="select">\
-        Sort by\
-        <select name="sort" id="sort"><option value="relevance" selected="selected">relevance</option><option value="title:1">title</option><option value="date:0">newest</option><option value="date:1">oldest</option></select>\
-        and show \
-        <select name="perpage" id="perpage"><option value="10">10</option><option value="20" selected="selected">20</option><option value="30">30</option><option value="50">50</option></select>\
+        Sort by' + mkws_html_sort(config) + '\
+        and show ' + mkws_html_perpage(config) + '\
         per page.\
        </form>\
             </div>\
@@ -416,22 +435,82 @@ function mkws_html_all() {
       </table>\
     </div>');
 
+    mkws_html_switch(config);
+    if (mkws_config.use_service_proxy)
+       mkws_service_proxy_auth(config.service_proxy_auth);
+
+    domReady();
+}
+
+function mkws_html_switch(config) {
+    $("#mkwsSwitch").html($("<a/>", {
+       href: '#',
+       onclick: "switchView(\'records\')",
+       text: "Records"
+    }));
+    $("#mkwsSwitch").append($("<span/>", { text: " | " }));
+    $("#mkwsSwitch").append($("<a/>", {
+       href: '#',
+       onclick: "switchView(\'targets\')",
+       text: "Targets"
+    }));
+
     $("#mkwsTargets").html('\
       <div id="bytarget">\
        No information available yet.\
       </div>');
     $("#mkwsTargets").css("display", "none");
 
-    domReady();
+    if (!config.switch) {
+        $("#mkwsSwitch").css("display", "none");
+    }
+}
+
+function mkws_html_sort(config) {
+    var sort_html = '<select name="sort" id="sort">';
+
+    for(var i = 0; i < config.sort.length; i++) {
+       var key = config.sort[i][0];
+       var val = config.sort[i].length == 1 ? config.sort[i][0] : config.sort[i][1];
+
+       sort_html += '<option value="' + key + '"';
+       if (key == config.sort_default) {
+           sort_html += ' selected="selected"';
+       }
+       sort_html += '>' + val + '</option>';
+    }
+    sort_html += '</select>';
+
+    return sort_html;
 }
 
-/* 
+function mkws_html_perpage(config) {
+    var perpage_html = '<select name="perpage" id="perpage">';
+
+    for(var i = 0; i < config.perpage.length; i++) {
+       var key = config.perpage[i];
+
+       perpage_html += '<option value="' + key + '"';
+       if (key == config.perpage_default) {
+           perpage_html += ' selected="selected"';
+       }
+       perpage_html += '>' + key + '</option>';
+    }
+    perpage_html += '</select>';
+
+    return perpage_html;
+}
+
+/*
  * Run service-proxy authentication in background (after page load).
  * The username/password is configured in the apache config file
  * for the site.
  */
-function mkws_service_proxy_auth() {
-    var jqxhr = jQuery.get("/service-proxy-auth")
+function mkws_service_proxy_auth(auth_url) {
+    if (!auth_url)
+       auth_url = "/service-proxy-auth";
+
+    var jqxhr = jQuery.get(auth_url)
        .fail(function() {
            alert("service proxy authentication failed, give up!");
        })
@@ -448,6 +527,5 @@ function mkws_service_proxy_auth() {
        });
 }
 
-$(document).ready(function() { mkws_html_all(); });
-$(document).ready(function() { mkws_service_proxy_auth(); });
-
+/* magic */
+$(document).ready(function() { mkws_html_all(mkws_config) });