From 6035eb334bae7ec2081fe3328d4aa08e2d920593 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sat, 28 Jan 2006 16:21:39 +0100
Subject: [PATCH] Pagelocks are now refreshed in the background

An AJAX call is used to refresh the pagelock when keypresses in the textarea
are detected (With a minimum wait of one minute between calls).

darcs-hash:20060128152139-7ad00-66d64326bcf04c9b8d49285ac736137e9dd48249.gz
---
 lib/exe/ajax.php    |  15 +++++++
 lib/exe/js.php      |   2 +-
 lib/scripts/edit.js | 103 +++++++++++++++++++++++++++++++++++---------
 3 files changed, 98 insertions(+), 22 deletions(-)

diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php
index 8c93bcdab..e52d5d378 100644
--- a/lib/exe/ajax.php
+++ b/lib/exe/ajax.php
@@ -60,5 +60,20 @@ function ajax_qsearch(){
   print '</ul>';
 }
 
+/**
+ * Refresh a page lock
+ *
+ * Andreas Gohr <andi@splitbrain.org>
+ */
+function ajax_lock(){
+  $id = cleanID($_POST['id']);
+  if(empty($id)) return;
+
+  if(!checklock($id)){
+    lock($id);
+    print 1;
+  }
+}
+
 //Setup VIM: ex: et ts=2 enc=utf-8 :
 ?>
diff --git a/lib/exe/js.php b/lib/exe/js.php
index 79d846ed8..223917fc8 100644
--- a/lib/exe/js.php
+++ b/lib/exe/js.php
@@ -93,7 +93,7 @@ function js_out(){
             js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')");
 
             // add lock timer
-            js_runonstart("init_locktimer(".($conf['locktime']-60).",'".js_escape($lang['willexpire'])."')");
+            js_runonstart("locktimer.init(".($conf['locktime'] - 60).",'".js_escape($lang['willexpire'])."')");
 
             // load spell checker
             if($conf['spellchecker']){
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js
index 84cd067cc..9a317a85c 100644
--- a/lib/scripts/edit.js
+++ b/lib/scripts/edit.js
@@ -401,26 +401,87 @@ function summaryCheck(){
 
 
 /**
- * global variable for the locktimer
+ * Class managing the timer to display a warning on a expiring lock
  */
-var locktimerID;
-
-/**
- * This starts a timer to remind the user of an expiring lock
- * Accepts the delay in seconds and a text to display.
- */
-function init_locktimer(delay,txt){
-  txt = escapeQuotes(txt);
-  locktimerID = self.setTimeout("locktimer('"+txt+"')", delay*1000);
-}
-
-/**
- * This stops the timer and displays a message about the expiring lock
- */
-function locktimer(txt){
-  clearTimeout(locktimerID);
-  alert(txt);
-}
-
-
+function locktimer_class(){
+		this.sack     = null;
+		this.timeout  = 0;
+		this.timerID  = null;
+		this.lasttime = null;
+    this.msg      = '';
+		this.pageid   = '';
+};
+var locktimer = new locktimer_class();
+		locktimer.init = function(timeout,msg){
+				// init values
+				locktimer.timeout  = timeout*1000;
+				locktimer.msg      = msg;
+				locktimer.lasttime = new Date();
+
+				if(!$('dw__editform')) return;
+				locktimer.pageid = $('dw__editform').elements.id.value;
+				if(!locktimer.pageid) return;
+
+				// init ajax component
+				locktimer.sack = new sack(DOKU_BASE + 'lib/exe/ajax.php');
+		    locktimer.sack.AjaxFailedAlert = '';
+    		locktimer.sack.encodeURIString = false;
+				locktimer.sack.onCompletion = locktimer.refreshed;
+
+				// register refresh event
+				addEvent($('dw__editform').elements.wikitext,'keyup',function(){locktimer.refresh();});
+
+				// start timer
+				locktimer.reset();
+		};
+
+		/**
+     * (Re)start the warning timer
+     */
+		locktimer.reset = function(){
+				locktimer.clear();
+				locktimer.timerID = window.setTimeout("locktimer.warning()", locktimer.timeout);
+		};
+
+		/**
+		 * Display the warning about the expiring lock
+		 */
+		locktimer.warning = function(){
+				locktimer.clear();
+				alert(locktimer.msg);
+		};
+
+		/**
+		 * Remove the current warning timer
+     */
+		locktimer.clear = function(){
+				if(locktimer.timerID !== null){
+						window.clearTimeout(locktimer.timerID);
+						locktimer.timerID = null;
+				}
+		};
+
+		/**
+     * Refresh the lock via AJAX
+     *
+     * Called on keypresses in the edit area
+     */
+		locktimer.refresh = function(){
+				var now = new Date();
+				// refresh every minute only
+				if(now.getTime() - locktimer.lasttime.getTime() > 60*1000){
+						locktimer.sack.runAJAX('call=lock&id='+encodeURI(locktimer.pageid));
+						locktimer.lasttime = now;
+				}
+		};
+
+
+		/**
+     * Callback. Resets the warning timer
+     */
+		locktimer.refreshed = function(){
+	  		if(this.response != '1') return; // locking failed
+				locktimer.reset();
+		};
+// end of locktimer class functions
 
-- 
GitLab