xref: /dokuwiki/lib/scripts/locktimer.js (revision 5d8c9d422c83ef31e1acbe6e37664185196c3016)
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        $edit.keypress(dw_locktimer.refresh);
48        // start timer
49        dw_locktimer.reset();
50    },
51
52    /**
53     * Add another field of the editform to be posted to the server when a draft is saved
54     */
55    addField: function(selector) {
56        dw_locktimer.fieldsToSaveAsDraft.push(selector);
57    },
58
59    /**
60     * Add a callback that is executed when the post request to renew the lock and save the draft returns successfully
61     *
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.
65     *
66     * @param {function} callback the only param is the data returned by the server
67     */
68    addRefreshCallback: function(callback) {
69        dw_locktimer.callbacks.push(callback);
70    },
71
72    /**
73     * (Re)start the warning timer
74     */
75    reset: function(){
76        dw_locktimer.clear();
77        dw_locktimer.timerID = window.setTimeout(dw_locktimer.warning, dw_locktimer.timeout);
78    },
79
80    /**
81     * Display the warning about the expiring lock
82     */
83    warning: function(){
84        dw_locktimer.clear();
85        alert(fixtxt(dw_locktimer.msg));
86    },
87
88    /**
89     * Remove the current warning timer
90     */
91    clear: function(){
92        if(dw_locktimer.timerID !== null){
93            window.clearTimeout(dw_locktimer.timerID);
94            dw_locktimer.timerID = null;
95        }
96    },
97
98    /**
99     * Refresh the lock via AJAX
100     *
101     * Called on keypresses in the edit area
102     */
103    refresh: function(){
104        var now = new Date(),
105            params = 'call=lock&id=' + dw_locktimer.pageid;
106
107        // refresh every half minute only
108        if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) {
109            return;
110        }
111
112        // always send the security token to authorize the lock and draft write
113        params += '&sectok=' + encodeURIComponent(
114            jQuery('#dw__editform').find('input[name=sectok]').val() || ''
115        );
116
117        // POST everything necessary for draft saving
118        if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
119            params += '&' + jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
120        }
121
122        jQuery.post(
123            DOKU_BASE + 'lib/exe/ajax.php',
124            params,
125            null,
126            'json'
127        ).done(function dwLocktimerRefreshDoneHandler(data) {
128            dw_locktimer.callbacks.forEach(
129                function (callback) {
130                    callback(data);
131                }
132            );
133        });
134        dw_locktimer.lasttime = now;
135    },
136
137    /**
138     * Callback. Resets the warning timer
139     */
140    refreshed: function(data){
141        if (data.errors.length) {
142            data.errors.forEach(function(error) {
143                jQuery('#draft__status').after(
144                    jQuery('<div class="error"></div>').text(error)
145                );
146            })
147        }
148
149        jQuery('#draft__status').html(data.draft);
150        if(data.lock !== '1') {
151            return; // locking failed
152        }
153        dw_locktimer.reset();
154    }
155};
156dw_locktimer.callbacks.push(dw_locktimer.refreshed);
157