Use md-author if present instead of md-title-responsibility. Swap title-remainder...
[pazpar2-moved-to-github.git] / www / iphone / example_client.js
index 1292d8b..ea74e67 100644 (file)
@@ -5,16 +5,17 @@
 // then register the form submit event with the pz2.search function
 // autoInit is set to true on default
 var usesessions = true;
-var pazpar2path = '/pazpar2/search.pz2';
+var pazpar2path = '/service-proxy/';
 var showResponseType = '';
-var queryBeforeLimit = null;
-
-if (document.location.hash == '#useproxy') {
+var querys = {'su': '', 'au': '', 'xt': ''};
+var showResponseType = 'json';
+if (document.location.hash == '#pazpar2' || document.location.search.match("useproxy=false")) {
     usesessions = false;
-    pazpar2path = '/service-proxy/';
-    showResponseType = 'json';
+    pazpar2path = '/pazpar2/search.pz2';
+    showResponseType = 'xml';
 }
 
+
 my_paz = new pz2( { "onshow": my_onshow,
                     "showtime": 500,            //each timer (show, stat, term, bytarget) can be specified this way
                     "pazpar2path": pazpar2path,
@@ -23,7 +24,7 @@ my_paz = new pz2( { "onshow": my_onshow,
                     "onterm": my_onterm_iphone,
                     "termlist": "xtargets,subject,author",
                     "onbytarget": my_onbytarget,
-                           "usesessions" : usesessions,
+                   "usesessions" : usesessions,
                     "showResponseType": showResponseType,
                     "onrecord": my_onrecord } );
 // some state vars
@@ -33,16 +34,96 @@ var totalRec = 0;
 var curDetRecId = '';
 var curDetRecData = null;
 var curSort = 'relevance';
-var curFilter = null;
+var curFilter = 'ALL';
 var submitted = false;
 var SourceMax = 16;
 var SubjectMax = 10;
 var AuthorMax = 10;
 var tab = "recordview"; 
 
+var triedPass = "";
+var triedUser = "";
+
+function loginFormSubmit() {
+    triedUser = document.loginForm.username.value;
+    triedPass = document.loginForm.password.value;
+    auth.login( {"username": triedUser,
+               "password": triedPass},
+       authCb, authCb);
+}
+
+function handleKeyPress(e, formId, focusId)  
+{  
+  var key;  
+  if(window.event)  
+    key = window.event.keyCode;  
+  else  
+    key = e.which;  
+
+  if(key == 13 || key == 10)  
+  {  
+    document.getElementById(formId).submit();  
+    focusElement = document.getElementById(focusId);
+    if (focusElement)
+      focusElement.focus();  
+    return false;  
+  }  
+  else  
+    return true;  
+}  
+
+function authCb(authData) {
+    if (!authData.loginFailed) {
+       triedUser = "";
+       triedPass = "";
+    }
+
+    if (authData.loggedIn == true) {        
+       showhide("recordview");
+    }
+}
+
+function logOutClick() {
+    auth.logOut(authCb, authCb);
+}
+
+function loggedOut() {
+    var login = document.getElementById("login");
+    login.innerHTML = 'Login';
+}
+
+function loggingOutFailed() {
+    alert("Logging out failed");
+}
+
+function login() {
+    showhide("login");
+}
+
+function logout() {
+    auth.logOut(loggedOut, loggingOutFailed, true);
+}
+
+function logInOrOut() {
+    var loginElement = document.getElementById("login");
+    if (loginElement.innerHTML == 'Login')
+       login();
+    else
+       logout();
+}
+function loggedIn() {
+    var login = document.getElementById("login");
+    login.innerHTML = 'Logout(' + auth.displayName + ')';
+    document.getElementById("log").innerHTML = login.innerHTML;
+}
+
+function auth_check() {
+    auth.check(loggedIn, login);
+    domReady();
+}
 
 //
-// pz2.js event handlers:
+// Pz2.js event handlers:
 //
 function my_oninit() {
     my_paz.stat();
@@ -59,10 +140,12 @@ function my_onshow(data) {
                      ' of ' + data.merged + ' (found: ' 
                      + data.total + ')</div>';
     drawPager(pager);
-    // navi
+
     var results = document.getElementById("results");
   
     var html = [];
+    if (data.hits == undefined) 
+       return ; 
     for (var i = 0; i < data.hits.length; i++) {
         var hit = data.hits[i];
              html.push('<li id="recdiv_'+hit.recid+'" >'
@@ -70,14 +153,17 @@ function my_onshow(data) {
             +'<a href="#" id="rec_'+hit.recid
             +'" onclick="showDetails(this.id);return false;">' 
             + hit["md-title"] +'</a> '); 
-             if (hit["md-title-responsibility"] !== undefined) {
-           html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
              if (hit["md-title-remainder"] !== undefined) {
-               html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
+                 html.push('<a href="#">' + hit["md-title-remainder"] + ' </a> ');
+             if (hit["md-author"] != undefined) {
+                 html.push('<a href="#">'+hit["md-author"]+'</a> ');
+             }
+             else if (hit["md-title-responsibility"] !== undefined) {
+                 html.push('<a href="#">'+hit["md-title-responsibility"]+'</a> ');
              }
        }
         if (hit.recid == curDetRecId) {
-            html.push(renderDetails(curDetRecData));
+            html.push(renderDetails_iphone(curDetRecData));
         }
        html.push('</div>');
     }
@@ -97,39 +183,49 @@ function my_onstat(data) {
 }
 
 function showhide(newtab) {
-       var showtermlist = false;
-       if (newtab != null)
-               tab = newtab;
+    var showtermlist = false;
+    if (newtab != null)
+       tab = newtab;
+    
+    if (tab == "recordview") {
+       document.getElementById("recordview").style.display = '';
+    }
+    else 
+       document.getElementById("recordview").style.display = 'none';
 
-       if (tab == "recordview") {
-               document.getElementById("recordview").style.display = '';
-       }
-       else 
-               document.getElementById("recordview").style.display = 'none';
+    if (tab == "xtargets") {
+       document.getElementById("term_xtargets").style.display = '';
+       showtermlist = true;
+    }
+    else
+       document.getElementById("term_xtargets").style.display = 'none';
 
-       if (tab == "xtargets") {
-               document.getElementById("term_xtargets").style.display = '';
-               showtermlist = true;
-       }
-       else
-               document.getElementById("term_xtargets").style.display = 'none';
-       if (tab == "subjects") {
-               document.getElementById("term_subjects").style.display = '';
-               showtermlist = true;
-       }
-       else
-               document.getElementById("term_subjects").style.display = 'none';
-       if (tab == "authors") {
-               document.getElementById("term_authors").style.display = '';
-               showtermlist = true;
-       }
-       else
-               document.getElementById("term_authors").style.display = 'none';
+    if (tab == "subjects") {
+       document.getElementById("term_subjects").style.display = '';
+       showtermlist = true;
+    }
+    else
+       document.getElementById("term_subjects").style.display = 'none';
 
-       if (showtermlist == false) 
-               document.getElementById("termlist").style.display = 'none';
-       else 
-               document.getElementById("termlist").style.display = '';
+    if (tab == "authors") {
+       document.getElementById("term_authors").style.display = '';
+       showtermlist = true;
+    }
+    else
+       document.getElementById("term_authors").style.display = 'none';
+
+    if (showtermlist == false) 
+       document.getElementById("termlist").style.display = 'none';
+    else 
+       document.getElementById("termlist").style.display = '';
+
+    var tabDiv = document.getElementById("loginDiv");
+    if (tab == "login") {
+       tabDiv.style.display = '';
+    }
+    else {
+       tabDiv.style.display = 'none';
+    }
 }
 
 function my_onterm(data) {
@@ -138,19 +234,21 @@ function my_onterm(data) {
     termlists.push('<div id="term_xtargets" >');
     termlists.push('<h4 class="termtitle">Sources</h4>');
     termlists.push('<ul>');
+    termlists.push('<li><a href="#" target_id="reset_xt" onclick="limitOrResetTarget(\'reset_xt\',\'All\');return false;">All</a></li>');
     for (var i = 0; i < data.xtargets.length && i < SourceMax; i++ ) {
         termlists.push('<li><a href="#" target_id='+data.xtargets[i].id
-            + ' onclick="limitTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">' 
-           + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')<a></li>');
+            + ' onclick="limitOrResetTarget(this.getAttribute(\'target_id\'), \'' + data.xtargets[i].name + '\');return false;">' 
+           + data.xtargets[i].name + ' (' + data.xtargets[i].freq + ')</a></li>');
     }
     termlists.push('</ul>');
     termlists.push('</div>');
      
     termlists.push('<div id="term_subjects" >');
-    termlists.push('<h4 id="subjects" class="termtitle">Subjects</h4>');
+    termlists.push('<h4>Subjects</h4>');
     termlists.push('<ul>');
+    termlists.push('<li><a href="#" target_id="reset_su" onclick="limitOrResetQuery(\'reset_su\',\'All\');return false;">All</a></li>');
     for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
-        termlists.push('<li><a href="#" onclick="limitQuery(\'su\', \'' + data.subject[i].name + '\');return false;">' 
+        termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'su\', \'' + data.subject[i].name + '\');return false;">' 
                       + data.subject[i].name + ' (' + data.subject[i].freq + ')</a></li>');
     }
     termlists.push('</ul>');
@@ -159,8 +257,9 @@ function my_onterm(data) {
     termlists.push('<div id="term_authors" >');
     termlists.push('<h4 class="termtitle">Authors</h4>');
     termlists.push('<ul>');
+    termlists.push('<li><a href="#" onclick="limitOrResetQuery(\'reset_au\',\'All\');return false;">All<a></li>');
     for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
-        termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.subject[i].name +'\');return false;">' 
+        termlists.push('<li><a href="#" onclick="limitQuery(\'au\', \'' + data.author[i].name +'\');return false;">' 
                             + data.author[i].name 
                             + '  (' 
                             + data.author[i].freq 
@@ -170,61 +269,29 @@ function my_onterm(data) {
     termlists.push('</div>');
     var termlist = document.getElementById("termlist");
     replaceHtml(termlist, termlists.join(''));
-    var d;
-/*
-    for (d in ("xtargets", "subjects", "authors")) {
-       alert(d);
-       if (tab == d)
-               document.getElementById("term_" + d).style.display = '';
-       else 
-               document.getElementById("term_" +d ).style.display = 'none';
-    }
-*/
     showhide();
 }
 
-function serialize(array) {
-       var t = typeof (obj);
-       if (t != "object" || obj === null) {
-               // simple data type
-               return String(obj);
-       } else {
-               // recurse array or object
-               var n, v, json = [], arr = (obj && obj.constructor == Array);
-               for (n in obj) {
-                       v = obj[n];
-                       t = typeof (v);
-                       if (t == "string")
-                               v = '"' + v + '"';
-                       else if (t == "object" && v !== null)
-                               v = JSON.stringify(v);
-                       json.push((arr ? "" : '"' + n + '":') + String(v));
-               }
-               return (arr ? "" : "") + String(json) + (arr ? "]" : "}");
-       }
-}
-
 var termlist = {};
 function my_onterm_iphone(data) {
     my_onterm(data);
-    var targets = "reset_to_all|All\n";
+    var targets = "reset_xt|All\n";
     
     for (var i = 0; i < data.xtargets.length; i++ ) {
        
         targets = targets + data.xtargets[i].id + "|" + data.xtargets[i].name + "|" + data.xtargets[i].freq + "\n";
     }
     termlist["xtargets"] = targets;
-    var subjects = "reset_to_all|All\n";
+    var subjects = "reset_su|All\n";
     for (var i = 0; i < data.subject.length; i++ ) {
         subjects = subjects + "su" + "|" + data.subject[i].name + "|" + data.subject[i].freq + "\n";
     }
     termlist["subjects"] = subjects;
-    var authors = "reset_to_all|All\n";
+    var authors = "reset_au|All\n";
     for (var i = 0; i < data.author.length; i++ ) {
         authors = authors + "au" + "|" + data.author[i].name + "|" + data.author[i].freq + "\n";
     }
     termlist["authors"] = authors;
-    //document.getElementById("log").innerHTML = targets + "\n" + subjects + "\n" + authors;
     callback.send("termlist", "refresh");
 }
 
@@ -248,7 +315,7 @@ function my_onrecord(data) {
     if (detRecordDiv) return;
     curDetRecData = data;
     var recordDiv = document.getElementById('recdiv_'+curDetRecData.recid);
-    var html = renderDetails(curDetRecData);
+    var html = renderDetails_iphone(curDetRecData);
     recordDiv.innerHTML += html;
 }
 
@@ -287,31 +354,38 @@ function domReady ()
     document.select.perpage.onchange = onSelectDdChange;
     if (document.location.search.match("inApp=true")) 
        applicationMode(true);
+    else
+       applicationMode(false);
 }
  
 function applicationMode(newmode) 
 {
-       var searchdiv = document.getElementById("searchForm");
-       if (newmode)
-               inApp = newmode;
-       if (inApp) {
+    var searchdiv = document.getElementById("searchForm");
+    if (newmode)
+       inApp = newmode;
+    if (inApp) {
        document.getElementById("heading").style.display="none";
                searchdiv.style.display = 'none';
-       }
-       else { 
-               searchdiv.style.display = '';
-               document.search.onsubmit = onFormSubmit;
-       }
-       callback.init();
+    }
+    else { 
+       
+       document.getElementById("nav").style.display="";
+       document.getElementById("normal").style.display="inline";
+       document.getElementById("normal").style.visibility="";
+       searchdiv.style.display = '';
+       document.search.onsubmit = onFormSubmit;
+    }
+    callback.init();
 }
 // when search button pressed
 function onFormSubmitEventHandler() 
 {
     resetPage();
+    document.getElementById("logo").style.display = 'none';
     loadSelect();
     triggerSearch();
     submitted = true;
-    return false;
+    return true;
 }
 
 function onSelectDdChange()
@@ -332,6 +406,13 @@ function resetPage()
 function triggerSearch ()
 {
     my_paz.search(document.search.query.value, recPerPage, curSort, curFilter);
+/*
+    , startWith,
+       {
+          "limit" : getFacets() 
+       }
+       );
+*/
 }
 
 function loadSelect ()
@@ -341,41 +422,39 @@ function loadSelect ()
 }
 
 // limit the query after clicking the facet
-function limitQuery (field, value)
+function limitQuery(field, value)
 {
-       if (!queryBeforeLimit) 
-               queryBeforeLimit = document.search.query.value;
-    document.search.query.value += ' and ' + field + '="' + value + '"';
-    onFormSubmitEventHandler();
-    showhide("recordview");
+  var newQuery = ' and ' + field + '="' + value + '"';
+  querys[field] += newQuery;
+  document.search.query.value += newQuery;
+  onFormSubmitEventHandler();
+  showhide("recordview");
 }
 
-//limit the query after clicking the facet
+// limit the query after clicking the facet
 function removeQuery (field, value) {
        document.search.query.value.replace(' and ' + field + '="' + value + '"', '');
     onFormSubmitEventHandler();
     showhide("recordview");
 }
 
-//limit the query after clicking the facet
+// limit the query after clicking the facet
 function limitOrResetQuery (field, value, selected) {
-       if (field == 'reset_to_all') {
-               document.search.query.value = queryBeforeLimit;
-               queryBeforeLimit = null;
+       if (field == 'reset_su' || field == 'reset_au') {
+               var reset_field = field.substring(6);
+               document.search.query.value = document.search.query.value.replace(querys[reset_field], '');
+               querys[reset_field] = '';
+           onFormSubmitEventHandler();
+           showhide("recordview");
        }
        else 
                limitQuery(field, value);
-       alert("query: " + document.search.query.value);
+       //alert("limitOrResetQuerry: query after: " + document.search.query.value);
 }
 
 // limit by target functions
 function limitTarget (id, name)
 {
-    var navi = document.getElementById('navi');
-    navi.innerHTML = 
-        'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
-        + name + '</a>';
-    navi.innerHTML += '<hr/>';
     curFilter = 'pz:id=' + id;
     resetPage();
     loadSelect();
@@ -386,15 +465,22 @@ function limitTarget (id, name)
 
 function delimitTarget ()
 {
-    var navi = document.getElementById('navi');
-    navi.innerHTML = '';
-    curFilter = null; 
+    curFilter = 'ALL'; 
     resetPage();
     loadSelect();
     triggerSearch();
     return false;
 }
 
+function limitOrResetTarget(id, name) {
+       if (id == 'reset_xt') {
+               delimitTarget();
+       }
+       else {
+               limitTarget(id,name);
+       }
+}
+
 function drawPager (pagerDiv)
 {
     //client indexes pages from 1 but pz2 from 0
@@ -437,7 +523,7 @@ function drawPager (pagerDiv)
     if (lastClkbl < pages)
         postdots = '...';
 
-    pagerDiv.innerHTML += '<div style="float: clear">' 
+    pagerDiv.innerHTML += '<div style="float: none">' 
         + prev + predots + middle + postdots + next + '</div><hr/>';
 }
 
@@ -490,6 +576,7 @@ function showDetails (prefixRecId) {
     
     // remove current detailed view if any
     var detRecordDiv = document.getElementById('det_'+oldRecId);
+    //alert("oldRecId: " + oldRecId + " " + detRecordDiv != null); 
     // lovin DOM!
     if (detRecordDiv)
       detRecordDiv.parentNode.removeChild(detRecordDiv);
@@ -527,8 +614,8 @@ function renderDetails(data, marker)
        if (data["md-title-remainder"] !== undefined) {
              details += ' : <span>' + data["md-title-remainder"] + ' </span>';
        }
-       if (data["md-title-responsibility"] !== undefined) {
-             details += ' <span><i>'+ data["md-title-responsibility"] +'</i></span>';
+       if (data["md-author"] !== undefined) {
+             details += ' <span><i>'+ data["md-auhtor"] +'</i></span>';
        }
          details += '</td></tr>';
     }
@@ -545,4 +632,64 @@ function renderDetails(data, marker)
     details += '</table></div>';
     return details;
 }
- //EOF
+
+function renderLine(title, value) {
+    if (value != undefined)
+        return '<li><h3>' + title + '</h3> <big>' + value + '</big></li>';
+    return '';
+}
+
+function renderLineURL(title, URL, display) {
+    if (URL != undefined)
+       return '<li><h3>' + title + '</h3> <a href="' + URL + '" target="_blank">' + display + '</a></li>';
+    return '';
+}
+
+function renderLineEmail(dtitle, email, display) {
+    if (email != undefined)
+        return '<li><h3>' + title + '</h3> <a href="mailto:' + email + '" target="_blank">' + display + '</a></li>';
+    return '';
+}
+
+function renderDetails_iphone(data, marker)
+{
+       //return renderDetails(data,marker);
+
+    if (!data) 
+       return ""; 
+    var details = '<div class="details" id="det_'+data.recid+'" >'
+/*
+    details = '<div id="header" id="det_'+data.recid+'">' 
+       + '<h1>Detailed Info</h1>' 
+       + '<a id="backbutton" href="hidedetail(\'det_' + data.recid + '\')">Back</a>' 
+       + '</div>';
+*/
+    if (marker) 
+       details += '<h4>'+ marker + '</h4>'; 
+    details += '<ul class="field">';
+    if (data["md-title"] != undefined) {
+       details += '<li><h3>Title</h3> <big> ' + data["md-title"];
+        if (data["md-title-remainder"] !== undefined) {
+             details += ' ' + data["md-title-remainder"] + ' ';
+        }
+        if (data["md-author"] !== undefined) {
+             details += '<i>'+ data["md-author"] +'</i>';
+        } else if (data["md-title-responsibility"] !== undefined) {
+             details += '<i>'+ data["md-title-responsibility"] +'</i>';
+        }
+        details += '</big>'
+        details += '</li>'
+    }
+    details 
+       +=renderLine('Date',    data["md-date"])
+       + renderLine('Author',  data["md-author"])
+       + renderLineURL('URL',  data["md-electronic-url"], data["md-electronic-url"])
+       + renderLine('Subject', data["location"][0]["md-subject"]);
+    
+    if (data["location"][0]["@name"] != undefined)
+       details += renderLine('Location', data["location"][0]["@name"] + " (" +data["location"][0]["@id"] + ")");
+    details += '</ul></div>';
+    return details;
+}
+
+//EOF