Revision: 2679
          https://sourceforge.net/p/mrbs/code/2679/
Author:   cimorrison
Date:     2013-02-03 14:19:48 +0000 (Sun, 03 Feb 2013)
Log Message:
-----------
Merged in latest changes from the trunk

Modified Paths:
--------------
    mrbs/branches/linked_bookings/web/js/edit_entry.js.php
    mrbs/branches/linked_bookings/web/js/functions.js.php
    mrbs/branches/linked_bookings/web/js/refresh.js.php

Modified: mrbs/branches/linked_bookings/web/js/edit_entry.js.php
===================================================================
--- mrbs/branches/linked_bookings/web/js/edit_entry.js.php      2013-02-03 
14:11:30 UTC (rev 2678)
+++ mrbs/branches/linked_bookings/web/js/edit_entry.js.php      2013-02-03 
14:19:48 UTC (rev 2679)
@@ -16,6 +16,43 @@
 $is_admin = (authGetUserLevel($user) >= $max_level);
 
 
+// Set (if set is true) or clear (if set is false) a timer
+// to check for conflicts periodically in case someone else
+// books the slot you are looking at.  If setting the timer
+// it also performs an immediate check.
+?>
+var conflictTimer = function conflictTimer(set) {
+    <?php
+    if (function_exists('json_encode') &&
+        !empty($ajax_refresh_rate))
+    {
+      ?>
+      if (set)
+      {
+        <?php
+        // (Note the config variable is in seconds, but the setInterval() 
function
+        // uses milliseconds)
+        // Only set the timer if the page is visible
+        ?>
+        if (!isHidden())
+        {
+          checkConflicts(true);
+          conflictTimer.id = window.setInterval(function() {
+              checkConflicts(true);
+            }, <?php echo $ajax_refresh_rate * 1000 ?>);
+        }
+      }
+      else if (typeof conflictTimer.id !== 'undefined')
+      {
+        window.clearInterval(conflictTimer.id);
+      }
+      <?php
+    }
+    ?>
+  };
+
+
+<?php
 // Function to display the secondary repeat type fieldset appropriate
 // to the selected repeat type
 ?>
@@ -1069,11 +1106,20 @@
   
   adjustWidth(startSelect, endSelect);
     
-} <?php // function adjustSlotSelectors()
+} <?php // function adjustSlotSelectors() ?>
 
 
+var editEntryVisChanged = function editEntryVisChanged() {
+    <?php
+    // Clear the conflict timer and then restart it.   We want
+    // a check to be performed immediately the page becomes
+    // visible again.
+    ?>
+    conflictTimer(false);
+    conflictTimer(true);
+  };
 
-
+<?php
 // 
=================================================================================
 
 // Extend the init() function 
@@ -1233,8 +1279,6 @@
                   checkConflicts();
                 });
      
-    checkConflicts();
-
     $('#conflict_check, #policy_check').click(function manageTabs() {
         var tabId;
         var checkResults = $('#check_results');
@@ -1293,17 +1337,10 @@
     <?php
     // Finally, set a timer so that conflicts are periodically checked for,
     // in case someone else books that slot before you press Save.
-    // (Note the config variable is in seconds, but the setInterval() function
-    // uses milliseconds)
-    if (!empty($ajax_refresh_rate))
-    {
-      ?>
-      window.setInterval(function() {
-        checkConflicts(true);
-      }, <?php echo $ajax_refresh_rate * 1000 ?>);
-      <?php
-    }
-
+    ?>
+    conflictTimer(true);
+    
+    <?php
   } // if (function_exists('json_encode'))
 
   
@@ -1336,6 +1373,13 @@
     
   $('input[name="rep_type"]').change(changeRepTypeDetails);
   changeRepTypeDetails();
+  
+  var prefix = visibilityPrefix();
+  if (document.addEventListener &&
+      (prefix !== null))
+  {
+    document.addEventListener(prefix + "visibilitychange", 
editEntryVisChanged);
+  }
 
   <?php
   // selectedList:3 means that a maximum of three rooms is displayed when the 
select box is not open

Modified: mrbs/branches/linked_bookings/web/js/functions.js.php
===================================================================
--- mrbs/branches/linked_bookings/web/js/functions.js.php       2013-02-03 
14:11:30 UTC (rev 2678)
+++ mrbs/branches/linked_bookings/web/js/functions.js.php       2013-02-03 
14:19:48 UTC (rev 2679)
@@ -31,4 +31,53 @@
   }
   result.html += "<\/ul>";
   return result;
-}
\ No newline at end of file
+}
+
+
+<?php
+// Gets the correct prefix to use (if any) with the page visibility API.
+// Returns null if not supported.
+?>
+var visibilityPrefix = function visibilityPrefix() {
+    var prefixes = ['', 'webkit', 'moz', 'ms', 'o'];
+    var testProperty;
+    
+    if (typeof visibilityPrefix.prefix === 'undefined')
+    {
+      visibilityPrefix.prefix = null;
+      for (var i=0; i<prefixes.length; i++)
+      {
+        testProperty = prefixes[i];
+        testProperty += (prefixes[i] === '') ? 'hidden' : 'Hidden';
+        if (testProperty in document)
+        {
+          visibilityPrefix.prefix = prefixes[i];
+          break;
+        }
+      }
+    }
+
+    return visibilityPrefix.prefix;
+  };
+
+<?php
+// Determine if the page is hidden from the user (eg if it has been minimised
+// or the tab is not visible).    Returns true, false or null (if not known).
+?>
+var isHidden = function isHidden() {
+    var prefix;
+    prefix = visibilityPrefix();
+    switch (prefix)
+    {
+      case null:
+        return null;
+        break;
+      case '':
+        return document.hidden;
+        break;
+      default:
+        return document[prefix + 'Hidden'];
+        break;
+    }
+  };
+

Modified: mrbs/branches/linked_bookings/web/js/refresh.js.php
===================================================================
--- mrbs/branches/linked_bookings/web/js/refresh.js.php 2013-02-03 14:11:30 UTC 
(rev 2678)
+++ mrbs/branches/linked_bookings/web/js/refresh.js.php 2013-02-03 14:19:48 UTC 
(rev 2679)
@@ -2,42 +2,91 @@
 
 // $Id$
 
+// Implements Ajax refreshing of the calendar view.   Only necessary, 
obviously,
+// if $refresh_rate has been set to something non-zero
+
 require "../defaultincludes.inc";
 
 header("Content-type: application/x-javascript");
 expires_header(60*30); // 30 minute expiry
-
-if ($use_strict)
+  
+if (!empty($refresh_rate))
 {
-  echo "'use strict';\n";
-}
+  if ($use_strict)
+  {
+    echo "'use strict';\n";
+  }
 
-// 
=================================================================================
+  // refreshPage will be defined later as a function, once we know
+  // the page data, which won't be until init()
+  ?>
+  var refreshPage;
 
-// Extend the init() function 
-?>
+  <?php
+  // Set a timeout to refresh the page, but only if one isn't
+  // outstanding
+  ?>
+  var refreshTimer = function refreshTimer() {
+      <?php
+      if (!empty($refresh_rate))
+      {
+        // setTimeout not setInterval because the 'load' trigger restarts us ?>
+        if (typeof refreshTimer.id === 'undefined')
+        {
+          refreshTimer.id = window.setTimeout(function() {
+              refreshTimer.id = undefined;
+              refreshPage();
+            }, <?php echo $refresh_rate * 1000 ?>);
+        }
+        <?php
+      }
+      ?>
+    };
 
-var oldInitRefresh = init;
-init = function(args) {
-  oldInitRefresh.apply(this, [args]);
+  var refreshVisChanged = function refreshVisChanged() {
+      var hidden = isHidden();
+    
+      <?php
+      // If the page is hidden stop the timer;  if it is now visible
+      // then refresh the page, which will also start a timer;  if we
+      // don't know the status then don't do anything.
+      ?>
+      switch (hidden)
+      {
+        case true:
+          window.clearTimeout(refreshTimer.id);
+          refreshTimer.id = undefined;
+          break;
+        case false:
+          refreshPage();
+          break;
+        default:
+          break;
+      }
+    };
+  
 
   <?php
-  if (!empty($refresh_rate))
-  {
-    ?>
-    var data = {ajax: 1, 
-                day: args.day,
-                month: args.month,
-                year: args.year,
-                room: args.room,
-                area: args.area};
-    if (args.timetohighlight !== undefined)
-    {
-      data.timetohighlight = args.timetohighlight;
-    }
-    var table = $('table.dwm_main');
-    <?php // setTimeout not setInterval because the 'load' trigger restarts us 
?>
-    window.setTimeout(function() {
+  // 
=================================================================================
+
+  // Extend the init() function 
+  ?>
+  var oldInitRefresh = init;
+  init = function(args) {
+    oldInitRefresh.apply(this, [args]);
+
+    refreshPage = function refreshPage() {
+        var data = {ajax: 1, 
+                    day: args.day,
+                    month: args.month,
+                    year: args.year,
+                    room: args.room,
+                    area: args.area};
+        if (args.timetohighlight !== undefined)
+        {
+          data.timetohighlight = args.timetohighlight;
+        }
+        var table = $('table.dwm_main');
         $.post(args.page + '.php',
                data,
                function(result){
@@ -52,9 +101,25 @@
                    $(window).trigger('load');
                  },
                'html');
-      }, <?php echo $refresh_rate * 1000 ?>);
-    <?php
-  } // if (!empty($refresh_rate))
-  ?>
-};
-
+      };
+    
+    if (!isHidden())
+    {
+      <?php
+      // Set a timer if the page is visible or if we don't know the status
+      ?>
+      refreshTimer();
+    }
+    
+    var prefix = visibilityPrefix();
+    if (document.addEventListener &&
+        (prefix !== null) && 
+        !init.refreshListenerAdded)
+    {
+      document.addEventListener(prefix + "visibilitychange", 
refreshVisChanged);
+      init.refreshListenerAdded = true;
+    }
+  };
+  <?php
+}
+?>
\ No newline at end of file
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to