From f7d14abd12375c90293eb3fe66871fe3e90ff0fc Mon Sep 17 00:00:00 2001 From: Michael Große Date: Fri, 8 Jun 2018 09:50:46 +0200 Subject: ✨(dw_locktimer) plugins may reuse to add fields and callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Plugins may want to add further fields to be present when saving drafts. Plugins may want to execute some js functionality that should be timed to the saved draft/refreshed lock. If a plugin does another init() to attach the dw_locktimer to its own editor, then the default callback would be added a second time, causing unexpected and undesired behavior. This includes the changes from the following commits: 6ef45cc1c69591eb7facf381ef4bcf88e3aaa1c0 6ca947f3ad455df4fca1a3076b174b7b2688bd89 0fff419cc95b9783dd33ab02ffb3bd7806d5fcde 87bed8b672a166d073948bcb4ca49aaa81dc880c --- lib/scripts/locktimer.js | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'lib/scripts') diff --git a/lib/scripts/locktimer.js b/lib/scripts/locktimer.js index f83b6334e..7f1c7fb23 100644 --- a/lib/scripts/locktimer.js +++ b/lib/scripts/locktimer.js @@ -8,6 +8,13 @@ var dw_locktimer = { lasttime: null, msg: LANG.willexpire, pageid: '', + fieldsToSaveAsDraft: [ + 'input[name=prefix]', + 'textarea[name=wikitext]', + 'input[name=suffix]', + 'input[name=date]', + ], + callbacks: [], /** * Initialize the lock timer @@ -42,6 +49,26 @@ var dw_locktimer = { dw_locktimer.reset(); }, + /** + * Add another field of the editform to be posted to the server when a draft is saved + */ + addField: function(selector) { + dw_locktimer.fieldsToSaveAsDraft.push(selector); + }, + + /** + * Add a callback that is executed when the post request to renew the lock and save the draft returns successfully + * + * If the user types into the edit-area, then dw_locktimer will regularly send a post request to the DokuWiki server + * to extend the page's lock and update the draft. When this request returns successfully, then the draft__status + * is updated. This method can be used to add further callbacks to be executed at that moment. + * + * @param {function} callback the only param is the data returned by the server + */ + addRefreshCallback: function(callback) { + dw_locktimer.callbacks.push(callback); + }, + /** * (Re)start the warning timer */ @@ -84,18 +111,21 @@ var dw_locktimer = { // POST everything necessary for draft saving if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){ - params += jQuery('#dw__editform').find('input[name=prefix], ' + - 'textarea[name=wikitext], ' + - 'input[name=suffix], ' + - 'input[name=date]').serialize(); + params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize(); } jQuery.post( DOKU_BASE + 'lib/exe/ajax.php', params, - dw_locktimer.refreshed, + null, 'html' - ); + ).done(function dwLocktimerRefreshDoneHandler(data) { + dw_locktimer.callbacks.forEach( + function (callback) { + callback(data); + } + ); + }); dw_locktimer.lasttime = now; }, @@ -113,3 +143,4 @@ var dw_locktimer = { dw_locktimer.reset(); } }; +dw_locktimer.callbacks.push(dw_locktimer.refreshed); -- cgit v1.2.3