merged cont.
[opensuse:yast-rest-service.git] / webyast / public / javascripts / jquery.activity.js
1 /*!
2  * jQuery Activity
3  * December 20, 2009
4  * Corey Hart @ http://www.codenothing.com
5
6 The MIT License
7
8 Copyright (c) 2009 Corey Hart
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27
28  */
29
30 (function($, undefined){
31         // Current Timestamp
32         function now(){
33                 return (new Date).getTime();
34         }
35
36         // Attach activity object to jQuery base object
37         $.activity = {
38                 // Variable Storage
39                 defaults: {
40                         // Users last active timestamp
41                         lastActive: now(),
42
43                         // Interval times in milliseconds
44                         inactive: 1000*60*30, // Default inactive at 30 minutes
45                         interval: 1000*60*5, // Default interval check every 5 minutes
46
47                         // Callback functions when intervals are reached
48                         inactiveFn: function(){},
49                         intervalFn: function(){}
50                 },
51
52                 // Timerid
53                 timer: undefined,
54
55                 // Expose the current time fn
56                 now: now,
57
58                 // Starts the interval
59                 init: function(options){
60                         // Update vars with options
61                         var self = this;
62                         self.defaults = $.extend(self.defaults, options||{});
63                         self.defaults.lastActive = now();
64
65                         // Check activity on a continuous interval
66                         self.bind();
67                         return self.timer = setInterval(function(){ self.check(); }, self.defaults.interval);
68                 },
69
70                 // Updates lastActive to current/provided timestamp
71                 update: function(time){
72                         return (this.defaults.lastActive = time === undefined ? now() : time);
73                 },
74
75                 // Returns boolean indicator of activity
76                 isActive: function(){
77                         return (this.timer !== undefined);
78                 },
79
80                 // Getter for current activity
81                 getActivity: function(){
82                         var self = this, time = now();
83                         return {diff: time-self.defaults.lastActive, time: time, lastActive: self.defaults.lastActive};
84                 },
85
86                 // Re-activates the interval (options only required if change to defaults wanted)
87                 reActivate: function(options){
88                         var self = this;
89                         // Clear interval if still running
90                         if (self.timer)
91                                 self.timer = clearInterval(self.timer);
92                         // Reactivation
93                         return self.init(options||{});
94                 },
95
96                 // Interval check function
97                 check: function(){
98                         var self = this,
99                                 time = now(),
100                                 diff = time - self.defaults.lastActive;
101
102                         // Trigger appropriate callback
103                         if (diff > self.defaults.inactive){
104                                 self.defaults.inactiveFn.call(self, {diff: diff, time: time, lastActive: self.defaults.lastActive});
105                                 return self.timer = clearInterval(self.timer);
106                         }
107                         else if (diff > self.defaults.interval){
108                                 self.defaults.intervalFn.call(self, {diff: diff, time: time, lastActive: self.defaults.lastActive});
109                         }
110
111                         // Rebind mouse tracking
112                         return self.bind();
113                 },
114
115                 // Rebinds activity events 
116                 bind: function(){
117                         var self = this;
118                         $(window).unbind('.activity').one('mousemove.activity keyup.activity', function(event){
119                                 self.defaults.lastActive = event.timeStamp;
120                         });
121                         return self;
122                 }
123         };
124 })(jQuery);