xref: /dokuwiki/lib/scripts/locktimer.js (revision 80997d21e0aee9b8f27153000742ecd781f429f8)
1/**
2 * Class managing the timer to display a warning on a expiring lock
3 */
4
5// must be global variables, they are called from outside too
6var initLocktimer, expWarning;
7
8(function ($) {
9    var reset, clear, refresh, refreshed;
10
11    var locktimer = {
12        timeout: 0,
13        timerID: null,
14        lasttime: null,
15        msg: '',
16        pageid: '',
17    };
18
19    initLocktimer = function(timeout, msg, draft){
20
21        // init values
22        locktimer.timeout  = timeout*1000;
23        locktimer.msg      = msg;
24        locktimer.draft    = draft;
25        locktimer.lasttime = new Date();
26
27        if($('#dw__editform').length == 0) return;
28        locktimer.pageid = $('#dw__editform input[name=id]').val();
29
30        if(!locktimer.pageid) return;
31        if($('#wiki__text').attr('readonly')) return;
32
33        // register refresh event
34        $('#dw__editform').keypress(
35            function() {
36                refresh();
37            }
38        );
39
40        // start timer
41        reset();
42    };
43
44    /**
45     * (Re)start the warning timer
46     */
47    reset = function(){
48        clear();
49        locktimer.timerID = window.setTimeout("expWarning()", locktimer.timeout);
50    };
51
52    /**
53     * Display the warning about the expiring lock
54     */
55    expWarning = function(){
56        clear();
57        alert(locktimer.msg);
58    };
59
60    /**
61     * Remove the current warning timer
62     */
63    clear = function(){
64        if(locktimer.timerID !== null){
65            window.clearTimeout(locktimer.timerID);
66            locktimer.timerID = null;
67        }
68    };
69
70    /**
71     * Refresh the lock via AJAX
72     *
73     * Called on keypresses in the edit area
74     */
75    refresh = function(){
76
77        var now = new Date();
78        var params = {};
79
80        // refresh every minute only
81        if(now.getTime() - locktimer.lasttime.getTime() > 30*1000){
82
83            params['call'] = 'lock';
84            params['id'] = locktimer.pageid;
85
86            if(locktimer.draft && $('#dw__editform textarea[name=wikitext]').length > 0){
87                params['prefix'] = $('#dw__editform input[name=prefix]').val();
88                params['wikitext'] = $('#dw__editform textarea[name=wikitext]').val();
89                params['suffix'] = $('#dw__editform input[name=suffix]').val();
90
91                if($('#dw__editform input[name=date]').length > 0){
92                    params['date'] = $('#dw__editform input[name=id]').val();
93                }
94            }
95
96            $.post(
97                DOKU_BASE + 'lib/exe/ajax.php',
98                params,
99                function (data) {
100                    refreshed(data);
101                },
102                'html'
103            );
104
105            locktimer.lasttime = now;
106        }
107    };
108
109    /**
110     * Callback. Resets the warning timer
111     */
112    refreshed = function(data){
113        var error = data.charAt(0);
114        data  = data.substring(1);
115
116        $('#draft__status').html(data);
117        if(error != '1') return; // locking failed
118        reset();
119    };
120
121}(jQuery));
122