Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/*
2
 * jQuery Idle Timeout 1.2
3
 * Copyright (c) 2011 Eric Hynds
4
 *
5
 * http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
6
 *
7
 * Depends:
8
 *  - jQuery 1.4.2+
9
 *  - jQuery Idle Timer (by Paul Irish, http://paulirish.com/2009/jquery-idletimer-plugin/)
10
 *
11
 * Dual licensed under the MIT and GPL licenses:
12
 *   http://www.opensource.org/licenses/mit-license.php
13
 *   http://www.gnu.org/licenses/gpl.html
14
 *
15
*/
16
 
17
(function($, win){
18
 
19
	var idleTimeout = {
20
		init: function( element, resume, options ){
21
			var self = this, elem;
22
 
23
			this.warning = elem = $(element);
24
			this.resume = $(resume);
25
			this.options = options;
26
			this.countdownOpen = false;
27
			this.failedRequests = options.failedRequests;
28
			this._startTimer();
29
      		this.title = document.title;
30
 
31
			// expose obj to data cache so peeps can call internal methods
32
			$.data( elem[0], 'idletimeout', this );
33
 
34
			// start the idle timer
35
			$.idleTimer(options.idleAfter * 1000);
36
 
37
			// once the user becomes idle
38
			$(document).bind("idle.idleTimer", function(){
39
 
40
				// if the user is idle and a countdown isn't already running
41
				if( $.data(document, 'idleTimer') === 'idle' && !self.countdownOpen ){
42
					self._stopTimer();
43
					self.countdownOpen = true;
44
					self._idle();
45
				}
46
			});
47
 
48
			// bind continue link
49
			this.resume.bind("click", function(e){
50
				e.preventDefault();
51
 
52
				win.clearInterval(self.countdown); // stop the countdown
53
				self.countdownOpen = false; // stop countdown
54
				self._startTimer(); // start up the timer again
55
				self._keepAlive( false ); // ping server
56
				options.onResume.call( self.warning ); // call the resume callback
57
			});
58
		},
59
 
60
		_idle: function(){
61
			var self = this,
62
				options = this.options,
63
				warning = this.warning[0],
64
				counter = options.warningLength;
65
 
66
			// fire the onIdle function
67
			options.onIdle.call(warning);
68
 
69
			// set inital value in the countdown placeholder
70
			options.onCountdown.call(warning, counter);
71
 
72
			// create a timer that runs every second
73
			this.countdown = win.setInterval(function(){
74
				if(--counter === 0){
75
					window.clearInterval(self.countdown);
76
					options.onTimeout.call(warning);
77
				} else {
78
					options.onCountdown.call(warning, counter);
79
          document.title = options.titleMessage.replace('%s', counter) + self.title;
80
				}
81
			}, 1000);
82
		},
83
 
84
		_startTimer: function(){
85
			var self = this;
86
 
87
			this.timer = win.setTimeout(function(){
88
				self._keepAlive();
89
			}, this.options.pollingInterval * 1000);
90
		},
91
 
92
		_stopTimer: function(){
93
			// reset the failed requests counter
94
			this.failedRequests = this.options.failedRequests;
95
			win.clearTimeout(this.timer);
96
		},
97
 
98
		_keepAlive: function( recurse ){
99
			var self = this,
100
				options = this.options;
101
 
102
			//Reset the title to what it was.
103
			document.title = self.title;
104
 
105
			// assume a startTimer/keepAlive loop unless told otherwise
106
			if( typeof recurse === "undefined" ){
107
				recurse = true;
108
			}
109
 
110
			// if too many requests failed, abort
111
			if( !this.failedRequests ){
112
				this._stopTimer();
113
				options.onAbort.call( this.warning[0] );
114
				return;
115
			}
116
 
117
			$.ajax({
118
				timeout: options.AJAXTimeout,
119
				url: options.keepAliveURL,
120
				error: function(){
121
					self.failedRequests--;
122
				},
123
				success: function(response){
124
					if($.trim(response) !== options.serverResponseEquals){
125
						self.failedRequests--;
126
					}
127
				},
128
				complete: function(){
129
					if( recurse ){
130
						self._startTimer();
131
					}
132
				}
133
			});
134
		}
135
	};
136
 
137
	// expose
138
	$.idleTimeout = function(element, resume, options){
139
		idleTimeout.init( element, resume, $.extend($.idleTimeout.options, options) );
140
		return this;
141
	};
142
 
143
	// options
144
	$.idleTimeout.options = {
145
		// number of seconds after user is idle to show the warning
146
		warningLength: 30,
147
 
148
		// url to call to keep the session alive while the user is active
149
		keepAliveURL: "",
150
 
151
		// the response from keepAliveURL must equal this text:
152
		serverResponseEquals: "OK",
153
 
154
		// user is considered idle after this many seconds.  10 minutes default
155
		idleAfter: 600,
156
 
157
		// a polling request will be sent to the server every X seconds
158
		pollingInterval: 60,
159
 
160
		// number of failed polling requests until we abort this script
161
		failedRequests: 5,
162
 
163
		// the $.ajax timeout in MILLISECONDS!
164
		AJAXTimeout: 250,
165
 
166
		// %s will be replaced by the counter value
167
    	titleMessage: 'Warning: %s seconds until log out | ',
168
 
169
		/*
170
			Callbacks
171
			"this" refers to the element found by the first selector passed to $.idleTimeout.
172
		*/
173
		// callback to fire when the session times out
174
		onTimeout: $.noop,
175
 
176
		// fires when the user becomes idle
177
		onIdle: $.noop,
178
 
179
		// fires during each second of warningLength
180
		onCountdown: $.noop,
181
 
182
		// fires when the user resumes the session
183
		onResume: $.noop,
184
 
185
		// callback to fire when the script is aborted due to too many failed requests
186
		onAbort: $.noop
187
	};
188
 
189
})(jQuery, window);