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