summaryrefslogtreecommitdiff
path: root/platform/www/lib/scripts/locktimer.js
blob: 7bc8a3969f380fb74d6e211e74b4433368e9b4c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * Class managing the timer to display a warning on a expiring lock
 */
var dw_locktimer = {
    timeout: 0,
    draft: false,
    timerID: null,
    lasttime: null,
    msg: LANG.willexpire,
    pageid: '',
    fieldsToSaveAsDraft: [
        'input[name=prefix]',
        'textarea[name=wikitext]',
        'input[name=suffix]',
        'input[name=date]',
    ],
    callbacks: [],

    /**
     * Initialize the lock timer
     *
     * @param {int}    timeout Length of timeout in seconds
     * @param {bool}   draft   Whether to save drafts
     * @param {string} edid    Optional; ID of an edit object which has to be present
     */
    init: function(timeout,draft,edid){
        var $edit;

        edid = edid || 'wiki__text';

        $edit = jQuery('#' + edid);
        if($edit.length === 0 || $edit.attr('readonly')) {
            return;
        }

        // init values
        dw_locktimer.timeout  = timeout*1000;
        dw_locktimer.draft    = draft;
        dw_locktimer.lasttime = new Date();

        dw_locktimer.pageid   = jQuery('#dw__editform').find('input[name=id]').val();
        if(!dw_locktimer.pageid) {
            return;
        }

        // register refresh event
        $edit.keypress(dw_locktimer.refresh);
        // start timer
        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
     */
    reset: function(){
        dw_locktimer.clear();
        dw_locktimer.timerID = window.setTimeout(dw_locktimer.warning, dw_locktimer.timeout);
    },

    /**
     * Display the warning about the expiring lock
     */
    warning: function(){
        dw_locktimer.clear();
        alert(fixtxt(dw_locktimer.msg));
    },

    /**
     * Remove the current warning timer
     */
    clear: function(){
        if(dw_locktimer.timerID !== null){
            window.clearTimeout(dw_locktimer.timerID);
            dw_locktimer.timerID = null;
        }
    },

    /**
     * Refresh the lock via AJAX
     *
     * Called on keypresses in the edit area
     */
    refresh: function(){
        var now = new Date(),
            params = 'call=lock&id=' + dw_locktimer.pageid + '&';

        // refresh every half minute only
        if(now.getTime() - dw_locktimer.lasttime.getTime() <= 30*1000) {
            return;
        }

        // POST everything necessary for draft saving
        if(dw_locktimer.draft && jQuery('#dw__editform').find('textarea[name=wikitext]').length > 0){
            params += jQuery('#dw__editform').find(dw_locktimer.fieldsToSaveAsDraft.join(', ')).serialize();
        }

        jQuery.post(
            DOKU_BASE + 'lib/exe/ajax.php',
            params,
            null,
            'json'
        ).done(function dwLocktimerRefreshDoneHandler(data) {
            dw_locktimer.callbacks.forEach(
                function (callback) {
                    callback(data);
                }
            );
        });
        dw_locktimer.lasttime = now;
    },

    /**
     * Callback. Resets the warning timer
     */
    refreshed: function(data){
        if (data.errors.length) {
            data.errors.forEach(function(error) {
                jQuery('#draft__status').after(
                    jQuery('<div class="error"></div>').text(error)
                );
            })
        }

        jQuery('#draft__status').html(data.draft);
        if(data.lock !== '1') {
            return; // locking failed
        }
        dw_locktimer.reset();
    }
};
dw_locktimer.callbacks.push(dw_locktimer.refreshed);