aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/scripts/locktimer.js
blob: 6681438d7ae4bb9a1b360da9740cac325e0fc229 (plain) (blame)
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
/**
 * Class managing the timer to display a warning on a expiring lock
 */

// must be global variables, they are called from outside too
var initLocktimer, expWarning;

(function ($) {
    var reset, clear, refresh, refreshed;

    var locktimer = {
        timeout: 0,
        timerID: null,
        lasttime: null,
        msg: '',
        pageid: '',
    };

    initLocktimer = function(timeout, msg, draft){

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

        if($('#dw__editform').length == 0) return;
        locktimer.pageid = $('#dw__editform input[name=id]').val();
        
        if(!locktimer.pageid) return;
        if($('#wiki__text').attr('readonly')) return;

        // register refresh event
        $('#dw__editform').keypress(
            function() {
                refresh();
            }
        );
        
        // start timer
        reset();
    };

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

    /**
     * Display the warning about the expiring lock
     */
    expWarning = function(){
        clear();
        alert(locktimer.msg);
    };

    /**
     * Remove the current warning timer
     */
    clear = function(){
        if(locktimer.timerID !== null){
            window.clearTimeout(locktimer.timerID);
            locktimer.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() - locktimer.lasttime.getTime() > 30*1000){

            params['call'] = 'lock';
            params['id'] = locktimer.pageid;
            
            if(locktimer.draft && $('#dw__editform textarea[name=wikitext]').length > 0){
                params['prefix'] = $('#dw__editform input[name=prefix]').val(); 
                params['wikitext'] = $('#dw__editform textarea[name=wikitext]').val();
                params['suffix'] = $('#dw__editform input[name=suffix]').val();
                
                if($('#dw__editform input[name=date]').length > 0){
                    params['date'] = $('#dw__editform input[name=id]').val();
                }
            }
            
            $.post(
                DOKU_BASE + 'lib/exe/ajax.php',
                params,
                function (data) {
                    refreshed(data);
                },
                'html'
            );
            
            locktimer.lasttime = now;
        }
    };

    /**
     * Callback. Resets the warning timer
     */
    refreshed = function(data){
        var error = data.charAt(0);
        data  = data.substring(1);
            
        $('#draft__status').html(data);
        if(error != '1') return; // locking failed
        reset();
    };
    
}(jQuery));