xref: /dokuwiki/lib/scripts/locktimer.js (revision fe58309edafb067d90f3f40ef3d416100d558a04)
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        // always send the security token to authorize the lock and draft write
115        params += '&sectok=' + encodeURIComponent(
116            jQuery('#dw__editform').find('input[name=sectok]').val() || ''
117        );
118
119        // POST everything necessary for draft saving
120        if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
121            params += '&' + jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
122        }
123
124        jQuery.post(
125            DOKU_BASE + 'lib/exe/ajax.php',
126            params,
127            null,
128            'json'
129        ).done(function dwLocktimerRefreshDoneHandler(data) {
130            dw_locktimer.callbacks.forEach(
131                function (callback) {
132                    callback(data);
133                }
134            );
135        });
136        dw_locktimer.lasttime = now;
137    },
138
139    /**
140     * Callback. Resets the warning timer
141     */
142    refreshed: function(data){
143        if (data.errors.length) {
144            data.errors.forEach(function(error) {
145                jQuery('#draft__status').after(
146                    jQuery('<div class="error"></div>').text(error)
147                );
148            })
149        }
150
151        jQuery('#draft__status').html(data.draft);
152        if(data.lock !== '1') {
153            return; // locking failed
154        }
155        dw_locktimer.reset();
156    }
157};
158dw_locktimer.callbacks.push(dw_locktimer.refreshed);
159