support for F-Spot photo versioning, refs #67
[phpfspot.git] / phpfspot.js
index b5f6e98f33d0c8e34f628ede50720ce7eebf8624..54395e2a8aa27497f62d5232302129a952b7c2ac 100644 (file)
@@ -1,7 +1,8 @@
 /***************************************************************************
  *
- * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
- * All rights reserved
+ * phpfspot, presents your F-Spot photo collection in Web browsers.
+ *
+ * Copyright (c) by Andreas Unterkircher
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  ***************************************************************************/
 
 /**
- * display image
+ * show photo
  *
  * this function will be called by client and fetches
  * the single-photo view via AJAX from the server.
  * Furthermore it will scrollup the browser to the top
  * position so the image become visibile immediatley.
  */
-function showImage(id, scrollup)
+function showPhoto(id, scrollup)
 {
    /* is phpfspot skeleton really displayed? */
    if(!document.getElementById('content'))
@@ -41,7 +42,7 @@ function showImage(id, scrollup)
    }
 
    /* fetch single-photo view from server */
-   HTML_AJAX.replace(content, encodeURI('rpc.php?action=showphoto&id=' + id));
+   HTML_AJAX.replace(content, encodeURI(web_path + '/rpc.php?action=showphoto&id=' + id));
 
    /* scroll the window up to the top */
    if(scrollup != undefined) {
@@ -53,7 +54,7 @@ function showImage(id, scrollup)
    delete(origWidth); origWidth = undefined;
    delete(photo_details_pos); photo_details_pos = undefined;
 
-} // showImage()
+} // showPhoto()
 
 /**
  * scroll browser to the last shown photo
@@ -100,7 +101,7 @@ function findPos(obj, direction) {
 function showCredits()
 {
    var credits = document.getElementById("content");
-   credits.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=showcredits'));
+   credits.innerHTML = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=showcredits'));
 
 } // showCredits()
 
@@ -135,7 +136,7 @@ function Tags(mode, id)
       objTemp['mode'] = id.value;
    }
 
-   var retr = HTML_AJAX.post('rpc.php', objTemp);
+   var retr = HTML_AJAX.post(web_path + '/rpc.php', objTemp);
    if(retr == "ok") {
       refreshAvailableTags();
       refreshSelectedTags();
@@ -158,8 +159,10 @@ function refreshAvailableTags()
 {
    // update available tags
    var avail_tags = document.getElementById('available_tags');
-   avail_tags.innerHTML = "Loading...";
-   avail_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_available_tags'));
+   if(avail_tags != undefined) {
+      avail_tags.innerHTML = "Loading...";
+      avail_tags.innerHTML = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=show_available_tags'));
+   }
 
 } // refreshAvailableTags()
 
@@ -173,8 +176,10 @@ function refreshSelectedTags()
 {
    // update selected tags
    var selected_tags = document.getElementById("selected_tags");
-   selected_tags.innerHTML = "Loading...";
-   selected_tags.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=show_selected_tags'));
+   if(selected_tags != undefined) {
+      selected_tags.innerHTML = "Loading...";
+      selected_tags.innerHTML = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=show_selected_tags'));
+   }
 
 } // refreshSelectedTags()
 
@@ -187,7 +192,7 @@ function refreshSelectedTags()
  */
 function showPhotoIndex(begin_with, last_photo)
 {
-   var url = "rpc.php?action=show_photo_index";
+   var url = web_path + "/rpc.php?action=show_photo_index";
    if(begin_with != undefined)
       url = url + '&begin_with=' + begin_with;
    if(last_photo != undefined)
@@ -260,30 +265,36 @@ function setCheckedValue(condition, value) {
  */
 function startSearch()
 {
-   from_year = document.getElementById('fromyear').value;
-   from_month = document.getElementById('frommonth').value;
-   from_day = document.getElementById('fromday').value;
-   from = from_year +"-"+ from_month +"-"+ from_day;
-   to_year = document.getElementById('toyear').value;
-   to_month = document.getElementById('tomonth').value;
-   to_day = document.getElementById('today').value;
-   to = to_year +"-"+ to_month +"-"+ to_day;
-
+   if(document.getElementById('date_from').value != undefined) {
+      date_from = document.getElementById('date_from').value;
+   }
+   if(document.getElementById('date_to').value != undefined) {
+      date_to = document.getElementById('date_to').value;
+   }
+      
    var objTemp = new Object();
    objTemp['action'] = 'search';
 
-   if(document.getElementsByName('searchfor_tag')[0].value != "") {
+   if(document.getElementsByName('searchfor_tag')[0] != undefined &&
+      document.getElementsByName('searchfor_tag')[0].value != "") {
       objTemp['for_tag'] = document.getElementsByName('searchfor_tag')[0].value;
    }
-   if(document.getElementsByName('searchfor_name')[0].value != "") {
+   if(document.getElementsByName('searchfor_name')[0] != undefined &&
+      document.getElementsByName('searchfor_name')[0].value != "") {
       objTemp['for_name'] = document.getElementsByName('searchfor_name')[0].value;
    }
-   if(document.getElementsByName('consider_date')[0].checked == true) {
-      objTemp['from'] = from;
-      objTemp['to'] = to;
+   if(document.getElementsByName('consider_date')[0] != undefined &&
+      document.getElementsByName('consider_date')[0].checked == true) {
+      objTemp['date_from'] = date_from;
+      objTemp['date_to'] = date_to;
+   }
+   if(document.getElementsByName('consider_rate')[0] != undefined &&
+      document.getElementsByName('consider_rate')[0].checked == true) {
+      objTemp['rate_from'] = rate_search['from'];
+      objTemp['rate_to'] = rate_search['to'];
    }
 
-   var retr = HTML_AJAX.post('rpc.php', objTemp);
+   var retr = HTML_AJAX.post(web_path + '/rpc.php', objTemp);
    if(retr == "ok") {
       refreshAvailableTags();
       refreshSelectedTags();
@@ -309,12 +320,8 @@ function datesearch()
       mode = false;
    }
       
-   document.getElementById('fromyear').disabled = mode;
-   document.getElementById('frommonth').disabled = mode;
-   document.getElementById('fromday').disabled = mode;
-   document.getElementById('toyear').disabled = mode;
-   document.getElementById('tomonth').disabled = mode;
-   document.getElementById('today').disabled = mode;
+   document.getElementById('date_from').disabled = mode;
+   document.getElementById('date_to').disabled = mode;
  
 } // datesearch()
 
@@ -324,11 +331,16 @@ function datesearch()
  * called for photo-index export. will return the
  * selected mode via AJAX from the server.
  */
-function setViewMode(mode)
+function setViewMode(srv_webpath, mode)
 {
+   if(srv_webpath != undefined)
+      web_path = srv_webpath;
+   else
+      web_path = '';
+
    var exprt = document.getElementById('output');
    exprt.innerHTML = "Loading...";
-   exprt.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_export&mode=' + mode));
+   exprt.innerHTML = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=get_export&mode=' + mode));
 
 } // setViewMode()
 
@@ -337,13 +349,20 @@ function setViewMode(mode)
  */
 function clearSearch()
 {
-   document.getElementsByName('searchfor_tag')[0].value = '';
-   document.getElementsByName('searchfor_name')[0].value = '';
+   if(document.getElementsByName('searchfor_tag')[0] != undefined)
+      document.getElementsByName('searchfor_tag')[0].value = '';
+   if(document.getElementsByName('searchfor_name')[0] != undefined)
+      document.getElementsByName('searchfor_name')[0].value = '';
 
-   if(document.getElementsByName('consider_date')[0].checked == true) {
+   if(document.getElementsByName('consider_date')[0] != undefined &&
+      document.getElementsByName('consider_date')[0].checked == true) {
       document.getElementsByName('consider_date')[0].checked = false;
       datesearch();
-   }  
+   }
+   if(document.getElementsByName('consider_rate')[0] != undefined &&
+      document.getElementsByName('consider_rate')[0].checked == true) {
+      document.getElementsByName('consider_rate')[0].checked = false;
+   }
 
 } // clearSearch()
 
@@ -353,7 +372,8 @@ function clearSearch()
  */
 function AskServerWhatToDo()
 {
-   return HTML_AJAX.grab(encodeURI('rpc.php?action=what_to_do'));
+   return HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=what_to_do'));
+
 } // AskServerWhatToDo()
 
 /**
@@ -363,11 +383,22 @@ function AskServerWhatToDo()
  * the first time. It will fetch the tag-lists and will then
  * switch to the right view, which the browser got told from
  * the server (maybe someone hit the refresh button...).
+ *
+ * as parameter the server can set the correct webpath.
+ * espacialley when using user-friendly url's, the browser
+ * does not know the correct URLs to address images, stylesheets,
+ * ... then.
  */
-function init_phpfspot(mode)
+function init_phpfspot(srv_webpath)
 {
+   if(srv_webpath != undefined)
+      web_path = srv_webpath;
+   else
+      web_path = '';
+
    /* always load list of available tags */
-   refreshAvailableTags();
+   //this should not be more necessary since 4.5.08
+   //refreshAvailableTags();
 
    /* ask the server what we are currently displaying */
    whattodo = AskServerWhatToDo();
@@ -381,7 +412,7 @@ function init_phpfspot(mode)
    }
    if(whattodo == 'show_photo') {
       if(photo = getPhotoToShow()) {
-         showImage(photo);
+         showPhoto(photo);
          refreshSelectedTags();
       }
    }
@@ -412,7 +443,7 @@ function setBackGrdColor(item, color)
  */
 function getPhotoToShow()
 {
-   var photo_to_show = HTML_AJAX.grab(encodeURI('rpc.php?action=get_photo_to_show'));
+   var photo_to_show = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=get_photo_to_show'));
 
    // if no image needs to be shown, return false from here
    if(photo_to_show == "")
@@ -522,7 +553,7 @@ function showCalendar(date_box, click_obj)
    if(calendar.style.visibility == "" || calendar.style.visibility == 'hidden') {
       calendar.style.visibility = 'visible';
       calendar.innerHTML = "Loading...";
-      calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year=' + year + '&month=' + month));
+      calendar.innerHTML = HTML_AJAX.grab(encodeURI(web_path +'/rpc.php?action=get_calendar_matrix&year=' + year + '&month=' + month));
       calendar_shown = 1;
    }
    else {
@@ -550,7 +581,7 @@ function setMonth(year, month, day)
 {
    var calendar = document.getElementById('calendar');
    calendar.innerHTML = "Loading...";
-   calendar.innerHTML = HTML_AJAX.grab(encodeURI('rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
+   calendar.innerHTML = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=get_calendar_matrix&year='+ year +'&month='+ month +'&day='+ day));
 }
 
 /**
@@ -571,7 +602,7 @@ function setCalendarDate(year, month, day)
  */
 function resetAll()
 {
-   HTML_AJAX.grab(encodeURI('rpc.php?action=reset'));
+   HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=reset'));
    clearSearch();
    refreshAvailableTags();
    refreshSelectedTags();
@@ -623,17 +654,19 @@ function preloadPhotos(image_url) {
    var waiting = 100;
    var counting;
 
-   lbImg=WSR_getElementsByClassName(document,"img","thumb");
-   for(i=0;i<lbImg.length;i++){
-      lbImg[i].src=image_url[i];
-      // to not bomb the server with requests, give the page some time
-      // to load the images one by one. if a image exceeds the timeout,
-      // the next image will be loaded.
-      if(lbImg[i].complete != undefined && lbImg[i].complete != true) {
-         counting = 0;
-         while(lbImg[i].complete != true && counting < timeout) {
-            window.setTimeout("noop()", waiting);
-            counting+=waiting;
+   lbImg = WSR_getElementsByClassName(document,"img","thumb");
+   if(lbImg != undefined) {
+      for(i=0;i<lbImg.length;i++){
+         lbImg[i].src=image_url[i];
+         // to not bomb the server with requests, give the page some time
+         // to load the images one by one. if a image exceeds the timeout,
+         // the next image will be loaded.
+         if(lbImg[i].complete != undefined && lbImg[i].complete != true) {
+            counting = 0;
+            while(lbImg[i].complete != true && counting < timeout) {
+               window.setTimeout("noop()", waiting);
+               counting+=waiting;
+            }
          }
       }
    }
@@ -646,18 +679,23 @@ function noop() {}
 /**
  * start slideshow
  */
-function startSlideShow()
+function startSlideShow(srv_webpath)
 {
+   if(srv_webpath != undefined)
+      web_path = srv_webpath;
+   else
+      web_path = '';
+
    if(!sliding) {
-      HTML_AJAX.grab(encodeURI('rpc.php?action=reset_slideshow'));
+      HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=reset_slideshow'));
       nextSlide();
       sliding = setInterval("nextSlide()", sliding_time*1000);
-      document.getElementById('stop_ico').src = "resources/32_stop.png";
+      document.getElementById('stop_ico').src = web_path + "/resources/32_stop.png";
    }
    else {
       clearInterval(sliding);
       sliding = 0;
-      document.getElementById('stop_ico').src = "resources/32_play.png";
+      document.getElementById('stop_ico').src = web_path + "/resources/32_play.png";
    }
 
 } // startSlideShow()
@@ -667,7 +705,7 @@ function startSlideShow()
  */
 function nextSlide()
 {
-   var next_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_next_slideshow_img'));
+   var next_img = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=get_next_slideshow_img'));
    document.getElementById('slide_img').src = next_img;
 
 } // nextSlide()
@@ -677,7 +715,7 @@ function nextSlide()
  */
 function prevSlide()
 {
-   var prev_img = HTML_AJAX.grab(encodeURI('rpc.php?action=get_prev_slideshow_img'));
+   var prev_img = HTML_AJAX.grab(encodeURI(web_path + '/rpc.php?action=get_prev_slideshow_img'));
    document.getElementById('slide_img').src = prev_img;
 
 } // prevSlide()
@@ -690,12 +728,12 @@ function pauseSlideShow()
    if(!sliding_paused) {
       sliding_paused = 1;
       clearInterval(sliding);
-      document.getElementById('pause_ico').src = "resources/32_play.png";
+      document.getElementById('pause_ico').src = web_path + "/resources/32_play.png";
    }
    else {
       sliding_paused = 0;
       sliding = setInterval("nextSlide()", sliding_time*1000);
-      document.getElementById('pause_ico').src = "resources/32_pause.png";
+      document.getElementById('pause_ico').src = web_path + "/resources/32_pause.png";
    }
 
 } // pauseSlideShow()
@@ -712,7 +750,7 @@ function startAutoBrowse()
    else {
       clearInterval(autobrowse);
       autobrowse = 0;
-      document.getElementById('autobrowse_ico').src = "resources/32_play.png";
+      document.getElementById('autobrowse_ico').src = web_path + "/resources/16_play.png";
    }
 
 } // startAutoBrowser()
@@ -725,12 +763,12 @@ function autoBrowse()
    if(document.getElementById('next_link')) {
       var next_link = document.getElementById('next_link').href;
       window.location.href = next_link;
-      document.getElementById('autobrowse_ico').src = "resources/32_pause.png";
+      document.getElementById('autobrowse_ico').src = web_path + "/resources/16_pause.png";
    }
    /* we have reached the last photo */
    else {
       if(ab_ico = document.getElementById('autobrowse_ico'))
-         ab_ico.src = "resources/32_play.png";
+         ab_ico.src = web_path + "/resources/16_play.png";
       clearInterval(autobrowse);
    }
 
@@ -771,7 +809,7 @@ function update_sort_order(obj)
    var objTemp = new Object();
    objTemp['value'] = obj.options[obj.selectedIndex].value;
 
-   var retr = HTML_AJAX.post('rpc.php?action=update_sort_order', objTemp);
+   var retr = HTML_AJAX.post(web_path + '/rpc.php?action=update_sort_order', objTemp);
 
    if(retr == "ok") {
       showPhotoIndex();
@@ -782,6 +820,85 @@ function update_sort_order(obj)
 
 } // update_sort_order()
 
+/**
+ * if the photo-version Ń•elect-box has changed, set the newly
+ * choosen photo version as the to-be-displayed photo version
+ */
+function update_photo_version(obj, current_photo)
+{
+   var objTemp = new Object();
+   objTemp['photo_version'] = obj.options[obj.selectedIndex].value;
+   objTemp['photo_idx'] = current_photo;
+
+   var retr = HTML_AJAX.post(web_path + '/rpc.php?action=update_photo_version', objTemp);
+
+   if(retr == "ok") {
+      showPhoto(current_photo);
+   }
+   else {
+      window.alert("Server message: "+ retr);
+   }
+
+} // update_photo_version()
+
+/**
+ * show rate stars
+ *
+ * this function will show the requested amount of
+ * rate-stars.
+ *
+ * @param string mode
+ * @param int level
+ */
+function show_rate(mode, level)
+{
+   var i;
+
+   for(i = 1; i <= 5; i++) {
+      if(i <= level) {
+         document.getElementById('rate_' + mode + '_' + i).src = web_path + '/resources/star.png';
+      }
+      else {
+         document.getElementById('rate_' + mode + '_' + i).src = web_path + '/resources/empty_rate.png';
+      }
+   }
+
+} // show_rate()
+
+/**
+ * set rate stars
+ *
+ *
+ * this function will set the requested rate-stars-amount into a global
+ * variable (which will then later be used on form-submit) and will also
+ * update the display.
+ *
+ * @param string mode
+ * @param int level
+ */
+function set_rate(mode, level)
+{
+   rate_search[mode] = level;
+   show_rate(mode, level);
+
+} // set_rate()
+
+/**
+ * reset rate stars
+ *
+ * this function will reset the rate-star to their initial value.
+ *
+ * @param string mode
+ */
+function reset_rate(mode)
+{
+   if(rate_search[mode] == undefined)
+      rate_search[mode] = 0;
+
+   show_rate(mode, rate_search[mode]);
+
+} // reset_rate()
+
 /**
  * handle key events
  */
@@ -840,3 +957,5 @@ var origHeight;
 var origWidth;
 // position of the last shown photo in photo-index
 var photo_details_pos;
+var web_path;
+var rate_search = new Array();