Skip to content
Snippets Groups Projects
Commit 55f92d7e authored by Andreas Gohr's avatar Andreas Gohr
Browse files

moved locktimer class to its own file

I also adjusted the coding style to match our other JS classes
parent 1b885c58
No related branches found
No related tags found
No related merge requests found
......@@ -48,6 +48,7 @@ function js_out(){
DOKU_INC.'lib/scripts/textselection.js',
DOKU_INC.'lib/scripts/toolbar.js',
DOKU_INC.'lib/scripts/edit.js',
DOKU_INC.'lib/scripts/locktimer.js',
DOKU_INC.'lib/scripts/linkwiz.js',
DOKU_INC.'lib/scripts/media.js',
DOKU_INC.'lib/scripts/subscriptions.js',
......
......@@ -341,104 +341,3 @@ function summaryCheck(){
}
}
/**
* Class managing the timer to display a warning on a expiring lock
*/
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,draft){
// init values
locktimer.timeout = timeout*1000;
locktimer.msg = msg;
locktimer.draft = draft;
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'),'keypress',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() > 30*1000){ //FIXME decide on time
var params = 'call=lock&id='+encodeURIComponent(locktimer.pageid);
var dwform = $('dw__editform');
if(locktimer.draft && dwform.elements.wikitext){
params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value);
params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value);
params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value);
if(dwform.elements.date){
params += '&date='+encodeURIComponent(dwform.elements.date.value);
}
}
locktimer.sack.runAJAX(params);
locktimer.lasttime = now;
}
};
/**
* Callback. Resets the warning timer
*/
locktimer.refreshed = function(){
var data = this.response;
var error = data.charAt(0);
data = data.substring(1);
$('draft__status').innerHTML=data;
if(error != '1') return; // locking failed
locktimer.reset();
};
// end of locktimer class functions
/**
* Class managing the timer to display a warning on a expiring lock
*/
var locktimer = {
sack: null,
timeout: 0,
timerID: null,
lasttime: null,
msg: '',
pageid: '',
init: function(timeout,msg,draft){
// init values
locktimer.timeout = timeout*1000;
locktimer.msg = msg;
locktimer.draft = draft;
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'),'keypress',function(){locktimer.refresh();});
// start timer
locktimer.reset();
},
/**
* (Re)start the warning timer
*/
reset: function(){
locktimer.clear();
locktimer.timerID = window.setTimeout("locktimer.warning()", locktimer.timeout);
},
/**
* Display the warning about the expiring lock
*/
warning: function(){
locktimer.clear();
alert(locktimer.msg);
},
/**
* Remove the current warning timer
*/
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
*/
refresh: function(){
var now = new Date();
// refresh every minute only
if(now.getTime() - locktimer.lasttime.getTime() > 30*1000){
var params = 'call=lock&id='+encodeURIComponent(locktimer.pageid);
var dwform = $('dw__editform');
if(locktimer.draft && dwform.elements.wikitext){
params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value);
params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value);
params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value);
if(dwform.elements.date){
params += '&date='+encodeURIComponent(dwform.elements.date.value);
}
}
locktimer.sack.runAJAX(params);
locktimer.lasttime = now;
}
},
/**
* Callback. Resets the warning timer
*/
refreshed: function(){
var data = this.response;
var error = data.charAt(0);
data = data.substring(1);
$('draft__status').innerHTML=data;
if(error != '1') return; // locking failed
locktimer.reset();
},
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment