Fixed race condition on canvas init (caused boxes to appear off by ~320px); fixed JSON response of null when row[img_tags] is NULL
authorDan
Sat, 23 Feb 2008 16:02:15 -0500
changeset 25 d9f37d2ea2cf
parent 24 7f3bd3030ae9
child 26 023a21c8f47c
Fixed race condition on canvas init (caused boxes to appear off by ~320px); fixed JSON response of null when row[img_tags] is NULL
plugins/gallery/fetcher.php
plugins/gallery/tagging.js
plugins/gallery/viewimage.php
--- a/plugins/gallery/fetcher.php	Sun Feb 17 15:21:47 2008 -0500
+++ b/plugins/gallery/fetcher.php	Sat Feb 23 16:02:15 2008 -0500
@@ -30,6 +30,9 @@
 {
   global $db, $session, $paths, $template, $plugins; // Common objects
   
+  // artificial race condition for debug
+  // sleep(5);
+  
   $type = $paths->getParam(0);
   if ( !in_array($type, array('thumb', 'preview', 'full')) )
   {
--- a/plugins/gallery/tagging.js	Sun Feb 17 15:21:47 2008 -0500
+++ b/plugins/gallery/tagging.js	Sat Feb 23 16:02:15 2008 -0500
@@ -361,8 +361,17 @@
   }
 }
 
-var snapr_tags_onload = function()
+// Don't fire the tag init until JS init *and* image are finished.
+// Thanks to pmccullough for pointing out this race condition.
+var snapr_lock_onload_js = true;
+var snapr_lock_onload_img = true;
+
+var snapr_tags_onload_real = function()
 {
+  // make sure we aren't waiting...
+  if ( snapr_lock_onload_img || snapr_lock_onload_js )
+    return false;
+  
   // add the new box
   var parent_obj = document.getElementById('snapr_preview_img').parentNode;
   var id = parent_obj.getAttribute('snapr:imgid');
@@ -371,5 +380,11 @@
   ajaxPost(makeUrlNS('Gallery', id), 'ajax=true&act=get_tags', snapr_process_ajax_tag_packet);
 }
 
+var snapr_tags_onload = function()
+{
+  snapr_lock_onload_js = false;
+  snapr_tags_onload_real();
+}
+
 addOnloadHook(snapr_tags_onload);
 
--- a/plugins/gallery/viewimage.php	Sun Feb 17 15:21:47 2008 -0500
+++ b/plugins/gallery/viewimage.php	Sat Feb 23 16:02:15 2008 -0500
@@ -281,6 +281,10 @@
         
         break;
       case 'get_tags':
+        if ( empty($row['img_tags']) )
+        {
+          $row['img_tags'] = '[]';
+        }
         $response = snapr_json_decode($row['img_tags']);
         foreach ( $response as $key => $_ )
         {
@@ -338,8 +342,9 @@
   $img_url  = makeUrlNS('Special', 'GalleryFetcher/preview/' . $img_id);
   $img_href = makeUrlNS('Special', 'GalleryFetcher/full/' . $img_id);
   
+  // The actual image
   $iehack = ( strstr(@$_SERVER['HTTP_USER_AGENT'], 'MSIE') ) ? ' style="width: 1px;"' : '';
-  echo '<div snapr:imgid="' . $img_id . '"' . $iehack . '><img alt="Image preview (640px max width)" src="' . $img_url . '" id="snapr_preview_img" style="border-width: 0; margin-bottom: 5px; display: block;" /></div>';
+  echo '<div snapr:imgid="' . $img_id . '"' . $iehack . '><img onload="snapr_lock_onload_img = false; snapr_tags_onload_real();" alt="Image preview (640px max width)" src="' . $img_url . '" id="snapr_preview_img" style="border-width: 0; margin-bottom: 5px; display: block;" /></div>';
   
   echo '<table border="0" width="100%"><tr><td style="text-align: left; width: 24px;">';