1/** 2 * Class managing the timer to display a warning on a expiring lock 3 */ 4var dw_locktimer = { 5 timeout: 0, 6 draft: false, 7 timerID: null, 8 lasttime: null, 9 msg: LANG.willexpire, 10 pageid: '', 11 fieldsToSaveAsDraft: [ 12 'input[name=prefix]', 13 'textarea[name=wikitext]', 14 'input[name=suffix]', 15 'input[name=date]', 16 ], 17 callbacks: [], 18 19 /** 20 * Initialize the lock timer 21 * 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 25 */ 26 init: function(timeout,draft,edid){ 27 var $edit; 28 29 edid = edid || 'wiki__text'; 30 31 $edit = jQuery('#' + edid); 32 if($edit.length === 0 || $edit.attr('readonly')) { 33 return; 34 } 35 36 // init values 37 dw_locktimer.timeout = timeout*1000; 38 dw_locktimer.draft = draft; 39 dw_locktimer.lasttime = new Date(); 40 41 dw_locktimer.pageid = jQuery('#dw__editform').find('input[name=id]').val(); 42 if(!dw_locktimer.pageid) { 43 return; 44 } 45 46 // register refresh event 47 // 'input' fires on any value change (typing, paste, cut, drag-drop), unlike 'keypress' 48 // which misses mouse-driven paste — see issue #3714 49 $edit.on('input', dw_locktimer.refresh); 50 // start timer 51 dw_locktimer.reset(); 52 }, 53 54 /** 55 * Add another field of the editform to be posted to the server when a draft is saved 56 */ 57 addField: function(selector) { 58 dw_locktimer.fieldsToSaveAsDraft.push(selector); 59 }, 60 61 /** 62 * Add a callback that is executed when the post request to renew the lock and save the draft returns successfully 63 * 64 * If the user types into the edit-area, then dw_locktimer will regularly send a post request to the DokuWiki server 65 * to extend the page's lock and update the draft. When this request returns successfully, then the draft__status 66 * is updated. This method can be used to add further callbacks to be executed at that moment. 67 * 68 * @param {function} callback the only param is the data returned by the server 69 */ 70 addRefreshCallback: function(callback) { 71 dw_locktimer.callbacks.push(callback); 72 }, 73 74 /** 75 * (Re)start the warning timer 76 */ 77 reset: function(){ 78 dw_locktimer.clear(); 79 dw_locktimer.timerID = window.setTimeout(dw_locktimer.warning, dw_locktimer.timeout); 80 }, 81 82 /** 83 * Display the warning about the expiring lock 84 */ 85 warning: function(){ 86 dw_locktimer.clear(); 87 alert(fixtxt(dw_locktimer.msg)); 88 }, 89 90 /** 91 * Remove the current warning timer 92 */ 93 clear: function(){ 94 if(dw_locktimer.timerID !== null){ 95 window.clearTimeout(dw_locktimer.timerID); 96 dw_locktimer.timerID = null; 97 } 98 }, 99 100 /** 101 * Refresh the lock via AJAX 102 * 103 * Called on input events (typing, paste, cut, drag-drop) in the edit area 104 */ 105 refresh: function(){ 106 var now = new Date(), 107 params = 'call=lock&id=' + dw_locktimer.pageid + '&'; 108 109 // refresh every half minute only 110 if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) { 111 return; 112 } 113 114 // POST everything necessary for draft saving 115 if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){ 116 params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize(); 117 } 118 119 jQuery.post( 120 DOKU_BASE + 'lib/exe/ajax.php', 121 params, 122 null, 123 'json' 124 ).done(function dwLocktimerRefreshDoneHandler(data) { 125 dw_locktimer.callbacks.forEach( 126 function (callback) { 127 callback(data); 128 } 129 ); 130 }); 131 dw_locktimer.lasttime = now; 132 }, 133 134 /** 135 * Callback. Resets the warning timer 136 */ 137 refreshed: function(data){ 138 if (data.errors.length) { 139 data.errors.forEach(function(error) { 140 jQuery('#draft__status').after( 141 jQuery('<div class="error"></div>').text(error) 142 ); 143 }) 144 } 145 146 jQuery('#draft__status').html(data.draft); 147 if(data.lock !== '1') { 148 return; // locking failed 149 } 150 dw_locktimer.reset(); 151 } 152}; 153dw_locktimer.callbacks.push(dw_locktimer.refreshed); 154