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