summaryrefslogtreecommitdiff
path: root/lib/scripts/locktimer.js
blob: 60508a8e718e6b94128b5f64cc8a080eba1401d1 (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
/**
* Class managing the timer to display a warning on a expiring lock
*/
var locktimer = {
    sack: null,
    timeout: 0,
    timerID: null,
    lasttime: null,
    msg: '',
    pageid: '',

    init: function(timeout,msg,draft,edid){
        var edit = $(edid);
        if(!edit) return;
        if(edit.readOnly) return;

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

        if(jQuery('#dw__editform').length == 0) return;
        this.pageid = jQuery('#dw__editform input[name=id]').val();
        if(!this.pageid) return;

        if(jQuery('#wiki__text').attr('readonly')) return;

        // register refresh event
        jQuery('#dw__editform').keypress(
            function() {
                locktimer.refresh();
            }
        );
        // start timer
        this.reset();
    },

    /**
     * (Re)start the warning timer
     */
    reset: function(){
        this.clear();
        this.timerID = window.setTimeout("locktimer.warning()", this.timeout);
    },

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

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

    /**
     * Refresh the lock via AJAX
     *
     * Called on keypresses in the edit area
     */
    refresh: function(){
        var now = new Date();
        var params = {};
        // refresh every minute only
        if(now.getTime() - this.lasttime.getTime() > 30*1000){
            params['call'] = 'lock';
            params['id'] = locktimer.pageid;

            if(locktimer.draft && jQuery('#dw__editform textarea[name=wikitext]').length > 0){
                params['prefix'] = jQuery('#dw__editform input[name=prefix]').val();
                params['wikitext'] = jQuery('#dw__editform textarea[name=wikitext]').val();
                params['suffix'] = jQuery('#dw__editform input[name=suffix]').val();
                if(jQuery('#dw__editform input[name=date]').length > 0) {
                    params['date'] = jQuery('#dw__editform input[name=id]').val();
                }
            }

            jQuery.post(
                DOKU_BASE + 'lib/exe/ajax.php',
                params,
                function (data) {
                    locktimer.refreshed(data);
                },
                'html'
            );
            this.lasttime = now;
        }
    },

    /**
     * Callback. Resets the warning timer
     */
    refreshed: function(data){
        var error = data.charAt(0);
        data = data.substring(1);

        jQuery('#draft__status').html(data);
        if(error != '1') return; // locking failed
        this.reset();
    }
};