Rename '#select' to '#mkwsSelect'
[mkws-moved-to-github.git] / experiments / spclient / mkws.js
index b29be6d..477da10 100644 (file)
@@ -3,10 +3,10 @@
 
 "use strict"; // HTML5: disable for debug >= 2
 
-/* 
- * global config object: mkws_config 
- * 
- * needs to be defined in the HTML header before 
+/*
+ * global config object: mkws_config
+ *
+ * needs to be defined in the HTML header before
  * including this JS file
  */
 
@@ -113,7 +113,7 @@ function my_oninit() {
 function my_onshow(data) {
     totalRec = data.merged;
     // move it out
-    var pager = document.getElementById("pager");
+    var pager = document.getElementById("mkwsPager");
     pager.innerHTML = "";
     pager.innerHTML +='<hr/><div style="float: right">' + M('Displaying') + ': '
                     + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
@@ -121,14 +121,14 @@ function my_onshow(data) {
                      + data.total + ')</div>';
     drawPager(pager);
     // navi
-    var results = document.getElementById("results");
+    var results = document.getElementById("mkwsResults");
 
     var html = [];
     for (var i = 0; i < data.hits.length; i++) {
         var hit = data.hits[i];
-             html.push('<div class="record" id="recdiv_'+hit.recid+'" >'
+             html.push('<div class="record" id="mkwsRecdiv_'+hit.recid+'" >'
             +'<span>'+ (i + 1 + recPerPage * (curPage - 1)) +'. </span>'
-            +'<a href="#" id="rec_'+hit.recid
+            +'<a href="#" id="mkwsRec_'+hit.recid
             +'" onclick="showDetails(this.id);return false;"><b>'
             + hit["md-title"] +' </b></a>');
              if (hit["md-title-remainder"] !== undefined) {
@@ -158,58 +158,68 @@ function my_onstat(data) {
 }
 
 function my_onterm(data) {
-    if (!mkws_config.termlist_menu)
+    // no facets
+    if (!mkws_config.facets || mkws_config.facets.length == 0) {
+       $("#mkwsTermlists").parent().hide();
        return;
-    
-    var termlists = [];
-    termlists.push('<div class="title">' + M('Termlists') + '</div>');
-
-    termlists.push('<div class="facet">');
-    termlists.push('<div class="termtitle">' + M('Sources') + '</div>');
-    for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
-        termlists.push('<a href="#" target_id='+data.xtargets[i].id
-            + ' onclick="limitTarget(this.getAttribute(\'target_id\'), this.firstChild.nodeValue);return false;">' + data.xtargets[i].name
-        + ' </a><span> (' + data.xtargets[i].freq + ')</span><br/>');
     }
-    termlists.push('</div>');
 
-    termlists.push('<div class="facet">');
-    termlists.push('<div class="termtitle">' + M('Subjects') + '</div>');
-    for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
-        termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">' + data.subject[i].name + '</a><span>  ('
-              + data.subject[i].freq + ')</span><br/>');
-    }
-    termlists.push('</div>');
-
-    termlists.push('<div class="facet">');
-    termlists.push('<div class="termtitle">' + M('Authors') + '</div>');
-    for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
-        termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
-                            + data.author[i].name
-                            + ' </a><span> ('
-                            + data.author[i].freq
-                            + ')</span><br/>');
+    // display if we first got results
+    $("#mkwsTermlists").parent().show();
+
+    var acc = [];
+    acc.push('<div class="title">' + M('Termlists') + '</div>');
+    var facets = mkws_config.facets;
+
+    for(var i = 0; i < facets.length; i++) {
+       if (facets[i] == "sources") {
+           add_single_facet(acc, "Sources",  data.xtargets, SourceMax, null);
+       } else if (facets[i] == "subjects") {
+           add_single_facet(acc, "Subjects", data.subject,  SubjectMax, "su");
+       } else if (facets[i] == "authors") {
+           add_single_facet(acc, "Authors",  data.author,   AuthorMax, "au");
+       } else {
+           alert("bad facet configuration: '" + facets[i] + "'");
+       }
     }
-    termlists.push('</div>');
 
-    var termlist = document.getElementById("termlist");
-    replaceHtml(termlist, termlists.join(''));
+    var termlist = document.getElementById("mkwsTermlists");
+    replaceHtml(termlist, acc.join(''));
+}
+
+function add_single_facet(acc, caption, data, max, cclIndex) {
+    acc.push('<div class="facet">');
+    acc.push('<div class="termtitle">' + M(caption) + '</div>');
+    for (var i = 0; i < data.length && i < max; i++ ) {
+        acc.push('<a href="#" ');
+       var action;
+       if (!cclIndex) {
+           // Special case: target selection
+           acc.push('target_id='+data[i].id+' ');
+           action = 'limitTarget(this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
+       } else {
+           action = 'limitQuery(\'' + cclIndex + '\', this.firstChild.nodeValue)';
+       }
+       acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
+                + '<span> (' + data[i].freq + ')</span><br/>');
+    }
+    acc.push('</div>');
 }
 
 function my_onrecord(data) {
     // FIXME: record is async!!
     clearTimeout(my_paz.recordTimer);
     // in case on_show was faster to redraw element
-    var detRecordDiv = document.getElementById('det_'+data.recid);
+    var detRecordDiv = document.getElementById('mkwsDet_'+data.recid);
     if (detRecordDiv) return;
     curDetRecData = data;
-    var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
+    var recordDiv = document.getElementById('mkwsRecdiv_'+curDetRecData.recid);
     var html = renderDetails(curDetRecData);
     recordDiv.innerHTML += html;
 }
 
 function my_onbytarget(data) {
-    var targetDiv = document.getElementById("bytarget");
+    var targetDiv = document.getElementById("mkwsBytarget");
     var table ='<table><thead><tr><td>Target ID</td><td>Hits</td><td>Diags</td>'
         +'<td>Records</td><td>State</td></tr></thead><tbody>';
 
@@ -232,9 +242,9 @@ function my_onbytarget(data) {
 function domReady ()
 {
     document.search.onsubmit = onFormSubmitEventHandler;
-    document.search.query.value = '';
-    document.select.sort.onchange = onSelectDdChange;
-    document.select.perpage.onchange = onSelectDdChange;
+    document.search.mkwsQuery.value = '';
+    document.mkwsSelect.sort.onchange = onSelectDdChange;
+    document.mkwsSelect.perpage.onchange = onSelectDdChange;
 }
 
 // when search button pressed
@@ -265,13 +275,13 @@ function resetPage()
 
 function triggerSearch ()
 {
-    my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
+    my_paz.search(document.search.mkwsQuery.value, recPerPage, curSort, curFilter);
 }
 
 function loadSelect ()
 {
-    curSort = document.select.sort.value;
-    recPerPage = document.select.perpage.value;
+    curSort = document.mkwsSelect.sort.value;
+    recPerPage = document.mkwsSelect.perpage.value;
 }
 
 // limit the query after clicking the facet
@@ -284,7 +294,7 @@ function limitQuery (field, value)
 // limit by target functions
 function limitTarget (id, name)
 {
-    var navi = document.getElementById('navi');
+    var navi = document.getElementById('mkwsNavi');
     navi.innerHTML =
         'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
         + name + '</a>';
@@ -298,7 +308,7 @@ function limitTarget (id, name)
 
 function delimitTarget ()
 {
-    var navi = document.getElementById('navi');
+    var navi = document.getElementById('mkwsNavi');
     navi.innerHTML = '';
     curFilter = null;
     resetPage();
@@ -404,12 +414,12 @@ function switchView(view) {
 
 // detailed record drawing
 function showDetails (prefixRecId) {
-    var recId = prefixRecId.replace('rec_', '');
+    var recId = prefixRecId.replace('mkwsRec_', '');
     var oldRecId = curDetRecId;
     curDetRecId = recId;
 
     // remove current detailed view if any
-    var detRecordDiv = document.getElementById('det_'+oldRecId);
+    var detRecordDiv = document.getElementById('mkwsDet_'+oldRecId);
     // lovin DOM!
     if (detRecordDiv)
       detRecordDiv.parentNode.removeChild(detRecordDiv);
@@ -440,7 +450,7 @@ function replaceHtml(el, html) {
 
 function renderDetails(data, marker)
 {
-    var details = '<div class="details" id="det_'+data.recid+'"><table>';
+    var details = '<div class="details" id="mkwsDet_'+data.recid+'"><table>';
     if (marker) details += '<tr><td>'+ marker + '</td></tr>';
 
     details += renderField("Title", data["md-title"], data["md-title-remainder"], data["md-title-responsibility"]);
@@ -491,7 +501,8 @@ function mkws_html_all(config) {
        switch_menu: true,      /* show/hide Records|Targets menu */
        lang_menu: true,        /* show/hide language menu */
        lang_display: [],       /* display languages links for given languages, [] for all */
-       termlist_menu: true,    /* show/hide termlist */
+       facets: ["sources", "subjects", "authors"], /* display facets, in this order, [] for none */
+
        debug: 0,     /* debug level for development: 0..2 */
 
        dummy: "dummy"
@@ -503,7 +514,7 @@ function mkws_html_all(config) {
     } else if (mkws_config_default.debug !== 'undefined') {
        mkws_debug = mkws_config_default.debug;
     }
-    
+
     /* override standard config values by function parameters */
     for (var k in mkws_config_default) {
        if (typeof config[k] === 'undefined')
@@ -515,18 +526,18 @@ function mkws_html_all(config) {
        debug("Reset query width: " + mkws_config.query_width);
        mkws_config.query_width = 50;
     }
-   
-    mkws_set_lang(mkws_config); 
+
+    mkws_set_lang(mkws_config);
     if (mkws_config.lang_menu)
-       mkws_html_lang(mkws_config); 
+       mkws_html_lang(mkws_config);
 
     // For some reason, doing this programmatically results in
-    // document.search.query being undefined, hence the raw HTML.
+    // document.search.mkwsQuery being undefined, hence the raw HTML.
     debug("HTML search form");
     $("#mkwsSearch").html('\
-    <form id="searchForm" name="search" action="" >\
-      <input id="query" type="text" size="' + mkws_config.query_width + '" />\
-      <input id="button" type="submit" value="' + M('Search') + '" />\
+    <form name="search" action="" >\
+      <input id="mkwsQuery" type="text" size="' + mkws_config.query_width + '" />\
+      <input id="mkwsButton" type="submit" value="' + M('Search') + '" />\
     </form>');
 
     debug("HTML records");
@@ -534,28 +545,51 @@ function mkws_html_all(config) {
       <table width="100%" border="0" cellpadding="6" cellspacing="0">\
         <tr>\
           <td width="250" valign="top">\
-            <div id="termlist"></div>\
+            <div id="mkwsTermlists"></div>\
           </td>\
           <td valign="top">\
-            <div id="ranking">\
-              <form name="select" id="select" action="" >\
+            <div id="mkwsRanking">\
+              <form name="mkwsSelect" id="mkwsSelect" action="" >\
         ' + M('Sort by') + ' ' + mkws_html_sort(config) + '\
         ' + M('and show') + ' ' + mkws_html_perpage(config) + '\
         ' + M('per page') + '.\
        </form>\
             </div>\
-            <div id="pager"></div>\
-            <div id="navi"></div>\
-            <div id="results"></div>\
+            <div id="mkwsPager"></div>\
+            <div id="mkwsNavi"></div>\
+            <div id="mkwsResults"></div>\
           </td>\
         </tr>\
       </table>');
 
     mkws_html_switch(config);
+
     if (mkws_config.use_service_proxy)
        mkws_service_proxy_auth(config.service_proxy_auth);
 
+    if (mkws_config.responsive_design)
+       mkws_responsive_design();
+
     domReady();
+
+    // on first page, hide the termlist
+    $(document).ready(function() { $("#mkwsTermlists").parent().hide(); } );
+}
+
+/* Responsive web design - change layout on the fly depending on
+ * the current screen size width/height. Required for mobile devices.
+ */
+function mkws_responsive_design () {
+    var timeout = null;
+
+    $(window).resize( function(e) {
+       if (timeout)
+           clearTimeout(timeout);
+       timeout = setTimeout(function () { mkws_mobile_resize() }, 100);
+    });
+
+    // initial check after page load
+    $(document).ready(function() { mkws_mobile_resize() });
 }
 
 function mkws_set_lang(mkws_config)  {
@@ -565,14 +599,14 @@ function mkws_set_lang(mkws_config)  {
     } else {
        mkws_config.lang = lang;
     }
-    
+
     debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
     return mkws_config.lang;
 }
 
 function mkws_html_switch(config) {
     debug("HTML switch");
-    
+
     $("#mkwsSwitch").html($("<a/>", {
        href: '#',
        onclick: "switchView(\'records\')",
@@ -587,7 +621,7 @@ function mkws_html_switch(config) {
 
     debug("HTML targets");
     $("#mkwsTargets").html('\
-      <div id="bytarget">\
+      <div id="mkwsBytarget">\
        No information available yet.\
       </div>');
     $("#mkwsTargets").css("display", "none");
@@ -643,7 +677,7 @@ function mkws_html_perpage(config) {
 function mkws_service_proxy_auth(auth_url) {
     if (!auth_url)
        auth_url = "http://mkws.indexdata.com/service-proxy-auth";
-       
+
     debug("Run service proxy auth URL: " + auth_url);
 
     var request = new pzHttpRequest(auth_url);
@@ -672,7 +706,7 @@ function mkws_html_lang(mkws_config) {
     for (var i = 0; i < lang_display.length; i++) {
        hash[lang_display[i]] = 1;
     }
-    
+
     for (var k in mkws_locale_lang) {
        if (hash[k] == 1 || lang_display.length == 0)
            list.push(k);
@@ -685,23 +719,46 @@ function mkws_html_lang(mkws_config) {
     debug("Language menu for: " + list.join(", "));
 
     /* the HTML part */
-    var data = "";    
+    var data = "";
     for(var i = 0; i < list.length; i++) {
        var l = list[i];
-       
+
        if (data)
            data += ' | ';
-           
+
        if (lang == l) {
            data += ' <span>' + l + '</span> ';
        } else {
            data += ' <a href="?lang=' + l + '">' + l + '</a> '
        }
     }
-    
+
     $("#mkwsLang").html(data);
 }
 
+function mkws_mobile_resize () {
+    debug("resize width: " + $(window).height() + ", width: " + $(window).width());
+    var list = ["mkwsSwitch"];
+    var obj;
+    // alert($(window).width());
+
+    if ($(window).width() <= 980) {
+       for(var i = 0; i < list.length; i++) {
+           $("#" + list[i]).hide();
+       }
+
+       $("#mkwsTermlists").parent().hide();
+       obj = $("#mkwsTermlists").parent().html();
+        $("#mkwsShiftedTermlists").html("<hr/>" + obj);
+    } else {
+       for(var i = 0; i < list.length; i++) {
+           $("#" + list[i]).show();
+       }
+       $("#mkwsTermlists").parent().show();
+       $("#mkwsShiftedTermlists").html("");
+    }
+};
+
 /* locale */
 function M(word) {
     var lang = mkws_config.lang;
@@ -723,7 +780,7 @@ jQuery.extend({
        nvpair[pair[0]] = pair[1];
     });
     return nvpair;
-  }, 
+  },
   pazpar2: function(data) {
        document.write('<div id="mkwsSwitch"></div>\
     <div id="mkwsLang"></div>\