2 * Class managing the timer to display a warning on a expiring lock
11 fieldsToSaveAsDraft: [
13 'textarea[name=wikitext]',
20 * Initialize the lock timer
22 * @param {int} timeout Length of timeout in seconds
23 * @param {bool} draft Whether to save drafts
24 * @param {string} edid Optional; ID of an edit object which has to be present
26 init: function(timeout,draft,edid){
29 edid = edid || 'wiki__text';
31 $edit = jQuery('#' + edid);
32 if($edit.length === 0 || $edit.attr('readonly')) {
37 dw_locktimer.timeout = timeout*1000;
38 dw_locktimer.draft = draft;
39 dw_locktimer.lasttime = new Date();
41 dw_locktimer.pageid = jQuery('#dw__editform').find('input[name=id]').val();
42 if(!dw_locktimer.pageid) {
46 // register refresh event
47 $edit.keypress(dw_locktimer.refresh);
53 * Add another field of the editform to be posted to the server when a draft is saved
55 addField: function(selector) {
56 dw_locktimer.fieldsToSaveAsDraft.push(selector);
60 * Add a callback that is executed when the post request to renew the lock and save the draft returns successfully
62 * If the user types into the edit-area, then dw_locktimer will regularly send a post request to the DokuWiki server
63 * to extend the page's lock and update the draft. When this request returns successfully, then the draft__status
64 * is updated. This method can be used to add further callbacks to be executed at that moment.
66 * @param {function} callback the only param is the data returned by the server
68 addRefreshCallback: function(callback) {
69 dw_locktimer.callbacks.push(callback);
73 * (Re)start the warning timer
77 dw_locktimer.timerID = window.setTimeout(dw_locktimer.warning, dw_locktimer.timeout);
81 * Display the warning about the expiring lock
85 alert(fixtxt(dw_locktimer.msg));
89 * Remove the current warning timer
92 if(dw_locktimer.timerID !== null){
93 window.clearTimeout(dw_locktimer.timerID);
94 dw_locktimer.timerID = null;
99 * Refresh the lock via AJAX
101 * Called on keypresses in the edit area
104 var now = new Date(),
105 params = 'call=lock&id=' + dw_locktimer.pageid + '&';
107 // refresh every half minute only
108 if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) {
112 // POST everything necessary for draft saving
113 if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
114 params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
118 DOKU_BASE + 'lib/exe/ajax.php',
122 ).done(function dwLocktimerRefreshDoneHandler(data) {
123 dw_locktimer.callbacks.forEach(
124 function (callback) {
129 dw_locktimer.lasttime = now;
133 * Callback. Resets the warning timer
135 refreshed: function(data){
136 if (data.errors.length) {
137 data.errors.forEach(function(error) {
138 jQuery('#draft__status').after(
139 jQuery('<div class="error"></div>').text(error)
144 jQuery('#draft__status').html(data.draft);
145 if(data.lock !== '1') {
146 return; // locking failed
148 dw_locktimer.reset();
151 dw_locktimer.callbacks.push(dw_locktimer.refreshed);