visibilityjs 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,11 @@
1
+ == 0.3 (Explorer 1, answer)
2
+ * Rename gem to visibilityjs.
3
+ * Fix gem integration with Asset Pipeline paths.
4
+
5
+ == 0.2 (Sputnik 2, Laika)
6
+ * Rename support() to isSupported() and notPrerender() to afterPrerendering().
7
+ * Fix IE 6 and 7 support.
8
+ * Documentation fixes by Peter Zotov.
9
+
10
+ == 0.1 (Sputnik 1, the first)
11
+ * initial release.
data/LICENSE ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
data/README.md ADDED
@@ -0,0 +1,267 @@
1
+ # Visibility.js – a wrapper for the Page Visibility API
2
+
3
+ Visibility.js allow you to determine whether your web page is visible to an
4
+ user, is hidden in background tab or is prerendering. It allows you use the page
5
+ visibility state in JavaScript logic and improve browser performance by
6
+ disabling unnecessary timers and AJAX requests, or improve user interface
7
+ experience (for example, by stopping video playback or slideshow when user
8
+ switches to another browser tab).
9
+
10
+ Moreover, you can detect if the browser is just [prerendering] the page while
11
+ the user has not still opened the link, and don’t count this as a visit in your
12
+ analytics module, or do not run heavy calculations or other actions which will
13
+ disable the prerendering.
14
+
15
+ This library is a wrapper of the [Page Visibility API]. It eases usage of the
16
+ API by hiding vendor-specific property prefixes and adding some high-level
17
+ functions.
18
+
19
+ In most cases you don’t need to check whether the Page Visibility API is
20
+ actually supported in the browser as, if it does not, the library will just
21
+ assume that the page is visible all the time, and your logic will still work
22
+ correctly, albeit less effective in some cases.
23
+
24
+ [Page Visibility API]: http://www.w3.org/TR/2011/WD-page-visibility-20110602/
25
+ [prerendering]: http://code.google.com/chrome/whitepapers/prerender.html
26
+
27
+ ## States
28
+
29
+ Currently the Page Visibility API supports three visibility states:
30
+
31
+ * `visible`: user has opened the page and works within it.
32
+ * `hidden`: user has switched to another tab or minimized browser window.
33
+ * `prerender`: browser is just prerendering a page which may possibly be opened
34
+ by the user to make the apparent loading time lesser.
35
+
36
+ ## Timers
37
+
38
+ The main use case for this library is to enable some of the times only when
39
+ content is visible to the user, i.e. the ones animating a countdown animation.
40
+
41
+ `Visibility.every(interval, callback)` is similar to
42
+ `setInterval(callback, interval)`, but calls `callback` every `interval` ms only
43
+ if the page is visible. For example, let’s create a countdown timer:
44
+
45
+ ```js
46
+ Visibility.every(1000, function () {
47
+ updateCountdownAnimation();
48
+ });
49
+ ```
50
+
51
+ You can provide an additional interval which will be used when the page
52
+ is hidden. In next example, a check for inbox updates will be run every 1 minute
53
+ for a visible page and every 5 minutes for a hidden one:
54
+
55
+ ```js
56
+ var minute = 60 * 1000;
57
+ Visibility.every(minute, 5 * minute, function () {
58
+ checkForEmail();
59
+ });
60
+ ```
61
+
62
+ Note that the callback will also be executed on every `hidden`->`visible` state
63
+ change to update old contents.
64
+
65
+ A syntactic sugar for specifying time intervals is supported when
66
+ [jQuery Chrono plugin] is included before Visibility.js. It can be used like
67
+ this:
68
+
69
+ ```js
70
+ Visibility.every('minute', '5 minutes', function () {
71
+ checkNewMails();
72
+ });
73
+ ```
74
+
75
+ `Visibility.every` returns a timer identifier, much like the `setTimeout`
76
+ function. It cannot be passed to `clearInterval`, through, and you should use
77
+ `Visibility.stop(id)` to stop the timer.
78
+
79
+ ```js
80
+ slideshow = Visibility.every(5 * 1000, function () {
81
+ nextSlide();
82
+ });
83
+
84
+ $('.stopSlideshow').click(function () {
85
+ Visibility.stop(slideshow);
86
+ });
87
+ ```
88
+
89
+ If the browser does not support the Page Visibility API, `Visibility.every` will
90
+ fall back to `setInterval`, and `callback` will be run every `interval` ms for
91
+ both the hidden and visible pages.
92
+
93
+ [jQuery Chrono plugin]: https://github.com/avk/jQuery-Chrono
94
+
95
+ ## Initializers
96
+
97
+ In another common use case you need to execute some actions upon a switch to
98
+ particular visibility state.
99
+
100
+ ### Waiting until the page becomes visible
101
+
102
+ `Visibility.onVisible(callback)` checks current state of the page. If it is
103
+ visible now, it will run `callback`, otherwise it will wait until state changes
104
+ to `visible`, and then run `callback`.
105
+
106
+ For example, let’s show an animated notification only when the page is visible,
107
+ so if an user opens a page in the background, the animation will delay until
108
+ the page becomes visible, i.e. until the user has switched to a tab with
109
+ the page:
110
+
111
+ ```js
112
+ Visibility.onVisible(function () {
113
+ Notification.animateNotice("Hello");
114
+ });
115
+ ```
116
+
117
+ If a browser doesn’t support Page Visibility API, `Visibility.onVisible` will
118
+ run the `callback` immediately.
119
+
120
+ ### Wait until the page is opened after prerendering
121
+
122
+ A web developer can hint a browser (using Prerendering API) that an user is
123
+ likely to click on some link (i.e. on a “Next” link in a multi-page article),
124
+ and the browser then may prefetch and prerender the page, so that the user will
125
+ not wait after actually going via the like.
126
+
127
+ But you may not want to count the browser prerendering a page as a visitor in
128
+ your analytics system. Moreover, the browser will disable prerendering if you
129
+ will try to do heavy computations or use audio/video tags on the page. So, you
130
+ may decide to not run parts of the code while prerendering and wait until the
131
+ user actually opens the link.
132
+
133
+ You can use `Visibility.afterPrerendering(callback)` in this cases. For example,
134
+ this code will only take real visitors (and not page prerenderings) into
135
+ account:
136
+
137
+ ```js
138
+ Visibility.afterPrerendering(function () {
139
+ Statistics.countVisitor();
140
+ });
141
+ ```
142
+
143
+ If the browser doesn’t support Page Visibility API,
144
+ `Visibility.afterPrerendering` will run `callback` immediately.
145
+
146
+ ## Low-level API
147
+
148
+ In some cases you may need more low-level methods. For example, you may want to
149
+ count the time user has viewed the page in foreground and time it has stayed in
150
+ background.
151
+
152
+ `Visibility.isSupported()` will return `true` if browser supports the
153
+ Page Visibility API:
154
+
155
+ ```js
156
+ if( Visibility.isSupported() ) {
157
+ Statistics.startTrackingVisibility();
158
+ }
159
+ ```
160
+
161
+ `Visibility.state()` will return a string with visibility state. More states
162
+ can be added in the future, so for most cases a simpler `Visibility.hidden()`
163
+ method can be used. It will return `true` if the page is hidden by any reason.
164
+ For example, while prerendering, `Visibility.state()` will return `"prerender"`,
165
+ but `Visibility.hidden()` will return `true`.
166
+
167
+ This code will aid in collecting page visibility statistics:
168
+
169
+ ```js
170
+ $(document).load(function () {
171
+
172
+ if ( 'hidden' == Visibility.state() ) {
173
+ Statistics.userOpenPageInBackgroundTab();
174
+ }
175
+ if ( 'prerender' == Visibility.state() ) {
176
+ Statistics.pageIsPrerendering();
177
+ }
178
+
179
+ });
180
+ ```
181
+
182
+ And this example will only enable auto-playing when the page is opening as a
183
+ visible tab (not a background one):
184
+
185
+ ```js
186
+ $(document).load(function () {
187
+
188
+ if ( !Visibility.hidden() ) {
189
+ VideoPlayer.play();
190
+ }
191
+
192
+ });
193
+ ```
194
+
195
+ Using `Visibility.change(callback)` you can listen to visibility state changing
196
+ events. The `callback` takes 2 arguments: an event object and a state name.
197
+
198
+ Let’s collect some statistics with this evented approach:
199
+
200
+ ```js
201
+ Visibility.change(function (e, state) {
202
+ Statistics.visibilityChange(state);
203
+ });
204
+ ```
205
+
206
+ ## Installing
207
+
208
+ ### Rails 3.1
209
+
210
+ In Rails 3.1 just add `visibilityjs` gem to `Gemfile`:
211
+
212
+ ```ruby
213
+ gem 'visibilityjs'
214
+ ```
215
+
216
+ and require it in `app/assets/javascripts/application.js.coffee`:
217
+
218
+ ```coffee
219
+ #= require visibility
220
+ ```
221
+
222
+ ### Jammit
223
+
224
+ If you use Jammit or another package manager, you’ll need to copy
225
+ `lib/visibility.js` to `public/javascripts/lib` in your project and add the
226
+ library to `config/assets.yml`:
227
+
228
+ ```yaml
229
+ javascripts:
230
+ application:
231
+ - public/javascripts/lib/visibility.js
232
+ ```
233
+
234
+ ### Other
235
+
236
+ If you don’t use Rails 3.1 or assets packaging manager you can use an already
237
+ minified version of the library, located in repository as
238
+ `lib/visibility.min.js`.
239
+
240
+ ## Contributing
241
+
242
+ To run project tests and minimize source you’ll need to have Ruby and Bundler
243
+ installed. For example, in a Debian-based (e.g. Ubuntu) environment:
244
+
245
+ ```
246
+ sudo apt-get install ruby rubygems
247
+ sudo gem install bundler
248
+ ```
249
+
250
+ Then, you will need to install Jasmine, UglifyJS and other dependencies with
251
+ Bundler. Run in root of the project repository:
252
+
253
+ ```
254
+ bundle install --path=.bundle
255
+ ```
256
+
257
+ That’s all. To run tests, start server and open <http://localhost:8888/>:
258
+
259
+ ```
260
+ bundle exec rake server
261
+ ```
262
+
263
+ Minimize the source before commiting:
264
+
265
+ ```
266
+ bundle exec rake min
267
+ ```
@@ -0,0 +1,8 @@
1
+ # Used only for Ruby on Rails gem to tell, that gem contain `vendor/assets` with
2
+ # visibility.js file.
3
+ module VisibilityJs
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,385 @@
1
+ /*
2
+ * Copyright 2011 Andrey “A.I.” Sitnik <andrey@sitnik.ru>,
3
+ * sponsored by Evil Martians.
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ ;(function() {
20
+ "use strict";
21
+
22
+ var defined = function(variable) {
23
+ return ('undefined' != typeof(variable));
24
+ };
25
+
26
+ // Visibility.js allow you to know, that your web page is in the background
27
+ // tab and thus not visible to the user. This library is wrap under
28
+ // Page Visibility API. It fix problems with different vendor prefixes and
29
+ // add high-level useful functions.
30
+ window.Visibility = {
31
+
32
+ // Link to document object to change it in tests.
33
+ _doc: window.document,
34
+
35
+ // Vendor prefixes to create event and properties names.
36
+ _prefixes: ['webkit', 'moz', 'o', 'ms'],
37
+
38
+ // Vendor prefix cached by `_prefix` function.
39
+ _chechedPrefix: null,
40
+
41
+ // Is listener for `visibilitychange` event is already added
42
+ // by `_setListener` method.
43
+ _listening: false,
44
+
45
+ // Callbacks from `change` method, that wait visibility changes.
46
+ _changeCallbacks: [],
47
+
48
+ // Callbacks from `onVisible` method, that wait when page become to be
49
+ // visible.
50
+ _onVisibleCallbacks: [],
51
+
52
+ // Callbacks from `afterPrerendering` method, that wait when visibility
53
+ // state change from “prerender”.
54
+ _afterPrerenderingCallbacks: [],
55
+
56
+ // Last timer number.
57
+ _lastTimer: 0,
58
+
59
+ // Callbacks and intervals added by `every` method.
60
+ _timers: { },
61
+
62
+ // Variable to check hidden-visible state changes.
63
+ _hiddenBefore: false,
64
+
65
+ // Initialize variables on page loading.
66
+ _init: function () {
67
+ this._hiddenBefore = this.hidden();
68
+
69
+ if ( defined(window.jQuery) && defined(jQuery.every) ) {
70
+ this._setInterval = this._chronoSetInterval;
71
+ } else {
72
+ this._setInterval = this._originalSetInterval;
73
+ }
74
+ },
75
+
76
+ // Detect vendor prefix and return it.
77
+ _prefix: function () {
78
+ if ( null !== this._chechedPrefix ) {
79
+ return this._chechedPrefix;
80
+ }
81
+ if ( defined(this._doc.visibilityState) ) {
82
+ return this._chechedPrefix = '';
83
+ }
84
+ var name;
85
+ for ( var i = 0; i < this._prefixes.length; i++ ) {
86
+ name = this._prefixes[i] + 'VisibilityState';
87
+ if ( defined(this._doc[name]) ) {
88
+ return this._chechedPrefix = this._prefixes[i];
89
+ }
90
+ }
91
+ },
92
+
93
+ // Return property name with vendor prefix.
94
+ _name: function (name) {
95
+ var prefix = this._prefix();
96
+ if ( '' == prefix ) {
97
+ return name;
98
+ } else {
99
+ return prefix +
100
+ name.substr(0, 1).toUpperCase() + name.substr(1);
101
+ }
102
+ },
103
+
104
+ // Return document’s property value with name with vendor prefix.
105
+ _prop: function (name) {
106
+ return this._doc[this._name(name)]
107
+ },
108
+
109
+ // Listener for `visibilitychange` event.
110
+ _onVisibilityChange: function(event) {
111
+ var isHidden = this.hidden(),
112
+ state = this.state();
113
+
114
+ for ( var i = 0; i < this._changeCallbacks.length; i++ ) {
115
+ this._changeCallbacks[i].call(this._doc, event, state);
116
+ }
117
+
118
+ var hiddenBefore = this._hiddenBefore;
119
+ if ( (isHidden && !hiddenBefore) || (!isHidden && hiddenBefore) ) {
120
+ for ( i in this._timers ) {
121
+ this._stopTimer(i);
122
+ this._runTimer(i, !isHidden);
123
+ }
124
+ }
125
+
126
+ if ( !isHidden ) {
127
+ for ( var i = 0; i < this._onVisibleCallbacks.length; i++ ) {
128
+ this._onVisibleCallbacks[i]();
129
+ }
130
+ this._onVisibleCallbacks = [];
131
+ }
132
+
133
+ if ( 'prerender' != this.state() ) {
134
+ var after = this._afterPrerenderingCallbacks.length
135
+ for ( var i = 0; i < after; i++ ) {
136
+ this._afterPrerenderingCallbacks[i]();
137
+ }
138
+ this._afterPrerenderingCallbacks = [];
139
+ }
140
+
141
+ this._hiddenBefore = isHidden;
142
+ },
143
+
144
+ // Set listener for `visibilitychange` event.
145
+ _setListener: function () {
146
+ if ( this._listening ) {
147
+ return;
148
+ }
149
+ var event = this._prefix() + 'visibilitychange';
150
+ this._doc.addEventListener(event, function () {
151
+ Visibility._onVisibilityChange.apply(Visibility, arguments);
152
+ }, false);
153
+ this._listening = true;
154
+ this._hiddenBefore = this.hidden();
155
+ },
156
+
157
+ // Set interval directly by `setInterval` function without any syntax
158
+ // sugar.
159
+ _originalSetInterval: function (callback, interval) {
160
+ return setInterval(callback, interval);
161
+ },
162
+
163
+ // Set interval by jQuery Chrono plugin. Add syntax sugar to `interval`
164
+ // and `hiddenInterval` arguments, such as "1 second" and others.
165
+ //
166
+ // It will be automatically set to `_setInterval` on loading if
167
+ // you include jQuery Chrono plugin before Visibility.js.
168
+ _chronoSetInterval: function (callback, internal) {
169
+ return jQuery.every(internal, callback);
170
+ },
171
+
172
+ // Set interval by `setInterval`. Allow to change function for tests or
173
+ // syntax sugar in `interval` arguments.
174
+ //
175
+ // Function will be automatically set in `_init` method (which will be
176
+ // call on script loading). So you must include jQuery Chrono plugin
177
+ // before Visibility.js.
178
+ _setInterval: null,
179
+
180
+ // Try to run timer from every method by it’s ID. It will be use
181
+ // `interval` or `hiddenInterval` depending on visibility state.
182
+ // If page is hidden and `hiddenInterval` is null,
183
+ // it will not run timer.
184
+ //
185
+ // Argument `now` say, that timers must be execute now too.
186
+ _runTimer: function (id, now) {
187
+ var interval,
188
+ timer = this._timers[id];
189
+ if ( this.hidden() ) {
190
+ if ( null === timer.hiddenInterval ) {
191
+ return;
192
+ }
193
+ interval = timer.hiddenInterval;
194
+ } else {
195
+ interval = timer.interval;
196
+ }
197
+ if ( now ) {
198
+ timer.callback.call(window);
199
+ }
200
+ timer.intervalID = this._setInterval(timer.callback, interval);
201
+ },
202
+
203
+ // Stop timer from `every` method by it’s ID.
204
+ _stopTimer: function (id) {
205
+ var timer = this._timers[id];
206
+ clearInterval(timer.intervalID);
207
+ delete timer.intervalID;
208
+ },
209
+
210
+ // Return true if browser support Page Visibility API.
211
+ //
212
+ // if ( Visibility.isSupported() ) {
213
+ // Statistics.startTrackingVisibility();
214
+ // Visibility.change(function(e, state)) {
215
+ // Statistics.trackVisibility(state);
216
+ // });
217
+ // }
218
+ isSupported: function () {
219
+ return defined(this._prefix());
220
+ },
221
+
222
+ // Return true if page now isn’t visible to user.
223
+ //
224
+ // if ( !Visibility.hidden() ) {
225
+ // VideoPlayer.play();
226
+ // }
227
+ //
228
+ // It is just proxy to `document.hidden`, but use vendor prefix.
229
+ hidden: function () {
230
+ if ( !this.isSupported() ) {
231
+ return false;
232
+ }
233
+ return this._prop('hidden');
234
+ },
235
+
236
+ // Return visibility state: 'visible', 'hidden' or 'prerender'.
237
+ //
238
+ // if ( 'prerender' == Visibility.state() ) {
239
+ // Statistics.pageIsPrerendering();
240
+ // }
241
+ //
242
+ // Don’t use `Visibility.state()` to detect, is page visible, because
243
+ // visibility states can extend in next API versions.
244
+ // Use more simpler and general `Visibility.hidden()` for this cases.
245
+ //
246
+ // It is just proxy to `document.visibilityState`, but use
247
+ // vendor prefix.
248
+ state: function () {
249
+ if ( !this.isSupported() ) {
250
+ return 'visible';
251
+ }
252
+ return this._prop('visibilityState');
253
+ },
254
+
255
+ // Call callback when visibility will be changed. First argument for
256
+ // callback will be original event object, second will be visibility
257
+ // state name.
258
+ //
259
+ // If Page Visibility API doesn’t supported method will be return false
260
+ // and callback never will be called.
261
+ //
262
+ // Visibility.change(function(e, state) {
263
+ // Statistics.visibilityChange(state);
264
+ // });
265
+ //
266
+ // It is just proxy to `visibilitychange` event, but use vendor prefix.
267
+ change: function (callback) {
268
+ if ( !this.isSupported() ) {
269
+ return false;
270
+ }
271
+ this._changeCallbacks.push(callback);
272
+ this._setListener();
273
+ return true;
274
+ },
275
+
276
+ // Call callback only when page become to visible for user or
277
+ // call it now if page is visible now or Page Visibility API
278
+ // doesn’t supported.
279
+ //
280
+ // Return true if callback if called now.
281
+ //
282
+ // Visibility.onVisible(function() {
283
+ // Notification.animateNotice("Hello");
284
+ // });
285
+ onVisible: function (callback) {
286
+ if ( !this.isSupported() || !this.hidden() ) {
287
+ callback();
288
+ return true;
289
+ }
290
+ this._onVisibleCallbacks.push(callback);
291
+ this._setListener();
292
+ },
293
+
294
+ // Call `callback` in any state, expect “prerender”. If current state
295
+ // is “prerender” it will wait until state will be changed.
296
+ // If Page Visibility API doesn’t supported, it will call `callback`
297
+ // immediately.
298
+ //
299
+ // Visibility.afterPrerendering(function () {
300
+ // Statistics.countVisitor();
301
+ // });
302
+ afterPrerendering: function (callback) {
303
+ if ( !this.isSupported() || 'prerender' != this.state() ) {
304
+ callback();
305
+ return true;
306
+ }
307
+ this._afterPrerenderingCallbacks.push(callback);
308
+ this._setListener();
309
+ },
310
+
311
+ // Run callback every `interval` milliseconds if page is visible and
312
+ // every `hiddenInterval` milliseconds if page is hidden.
313
+ //
314
+ // Visibility.every(60 * 1000, 5 * 60 * 1000, function() {
315
+ // checkNewMails();
316
+ // });
317
+ //
318
+ // You can skip `hiddenInterval` and callback will be called only if
319
+ // page is visible.
320
+ //
321
+ // Visibility.every(1000, function() {
322
+ // updateCountdown();
323
+ // });
324
+ //
325
+ // It is analog of `setInterval(callback, interval)` but use visibility
326
+ // state.
327
+ //
328
+ // It return timer ID, that you can use in `Visibility.stop(id)` to stop
329
+ // timer (`clearInterval` analog).
330
+ // Warning: timer ID is different from intervalID from `setInterval`,
331
+ // so don’t use it in `clearInterval`.
332
+ //
333
+ // On change state from hidden to visible timers will be execute.
334
+ //
335
+ // If you include jQuery Chrono plugin before Visibility.js, you could
336
+ // use Chrono’s syntax sugar in interval arguments:
337
+ //
338
+ // Visibility.every('second', function() {
339
+ // updateCountdown();
340
+ // });
341
+ // Visibility.every('1 minute', '5 minutes', function() {
342
+ // checkNewMails();
343
+ // });
344
+ every: function (interval, hiddenInterval, callback) {
345
+ if ( !defined(callback) ) {
346
+ callback = hiddenInterval;
347
+ hiddenInterval = null;
348
+ }
349
+ this._lastTimer += 1;
350
+ var number = this._lastTimer;
351
+ this._timers[number] = ({
352
+ interval: interval,
353
+ hiddenInterval: hiddenInterval,
354
+ callback: callback
355
+ });
356
+ this._runTimer(number, false);
357
+ if ( this.isSupported() ) {
358
+ this._setListener();
359
+ }
360
+ return number;
361
+ },
362
+
363
+ // Stop timer from `every` method by it ID (`every` method return it).
364
+ //
365
+ // slideshow = Visibility.every(5 * 1000, function() {
366
+ // changeSlide();
367
+ // });
368
+ // $('.stopSlideshow').click(function() {
369
+ // Visibility.stop(slideshow);
370
+ // });
371
+ stop: function(id) {
372
+ var timer = this._timers[id]
373
+ if ( !defined(timer) ) {
374
+ return false;
375
+ }
376
+ this._stopTimer(id);
377
+ delete this._timers[id];
378
+ return timer;
379
+ }
380
+
381
+ };
382
+
383
+ Visibility._init();
384
+
385
+ })();
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visibilityjs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ version: "0.3"
10
+ platform: ruby
11
+ authors:
12
+ - "Andrey \xE2\x80\x9CA.I.\xE2\x80\x9D Sitnik"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-31 00:00:00 +04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 62196457
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ - beta
33
+ - 5
34
+ version: 2.0.0.beta.5
35
+ requirement: *id001
36
+ type: :runtime
37
+ name: sprockets
38
+ description: Visibility.js allow you to determine whether your web page is visible to an user, is hidden in background tab or is prerendering. It allows you use the page visibility state in JavaScript logic and improve browser performance or improve user interface experience.
39
+ email:
40
+ - andrey@sitnik.ru
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files:
46
+ - LICENSE
47
+ - README.md
48
+ - ChangeLog
49
+ files:
50
+ - vendor/assets/javascripts/visibility.js
51
+ - lib/visibilityjs.rb
52
+ - LICENSE
53
+ - README.md
54
+ - ChangeLog
55
+ has_rdoc: true
56
+ homepage: https://github.com/evilmartians/visibility.js
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: "Visibility.js \xE2\x80\x93 a wrapper for the Page Visibility API."
89
+ test_files: []
90
+