Path: blob/trunk/third_party/closure/goog/events/onlinehandler.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview This event handler will dispatch events when16* {@code navigator.onLine} changes. HTML5 defines two events, online and17* offline that is fired on the window. As of today 3 browsers support these18* events: Firefox 3 (Gecko 1.9), Opera 9.5, and IE8. If we have any of these19* we listen to the 'online' and 'offline' events on the current window20* object. Otherwise we poll the navigator.onLine property to detect changes.21*22* Note that this class only reflects what the browser tells us and this usually23* only reflects changes to the File -> Work Offline menu item.24*25* @author [email protected] (Erik Arvidsson)26* @see ../demos/onlinehandler.html27*/2829// TODO(arv): We should probably implement some kind of polling service and/or30// a poll for changes event handler that can be used to fire events when a state31// changes.3233goog.provide('goog.events.OnlineHandler');34goog.provide('goog.events.OnlineHandler.EventType');3536goog.require('goog.Timer');37goog.require('goog.events.BrowserFeature');38goog.require('goog.events.EventHandler');39goog.require('goog.events.EventTarget');40goog.require('goog.events.EventType');41goog.require('goog.net.NetworkStatusMonitor');42434445/**46* Basic object for detecting whether the online state changes.47* @constructor48* @extends {goog.events.EventTarget}49* @implements {goog.net.NetworkStatusMonitor}50*/51goog.events.OnlineHandler = function() {52goog.events.OnlineHandler.base(this, 'constructor');5354/**55* @private {goog.events.EventHandler<!goog.events.OnlineHandler>}56*/57this.eventHandler_ = new goog.events.EventHandler(this);5859// Some browsers do not support navigator.onLine and therefore we don't60// bother setting up events or timers.61if (!goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY) {62return;63}6465if (goog.events.BrowserFeature.HAS_HTML5_NETWORK_EVENT_SUPPORT) {66var target = goog.events.BrowserFeature.HTML5_NETWORK_EVENTS_FIRE_ON_BODY ?67document.body :68window;69this.eventHandler_.listen(70target, [goog.events.EventType.ONLINE, goog.events.EventType.OFFLINE],71this.handleChange_);72} else {73this.online_ = this.isOnline();74this.timer_ = new goog.Timer(goog.events.OnlineHandler.POLL_INTERVAL_);75this.eventHandler_.listen(this.timer_, goog.Timer.TICK, this.handleTick_);76this.timer_.start();77}78};79goog.inherits(goog.events.OnlineHandler, goog.events.EventTarget);808182/**83* Enum for the events dispatched by the OnlineHandler.84* @enum {string}85* @deprecated Use goog.net.NetworkStatusMonitor.EventType instead.86*/87goog.events.OnlineHandler.EventType = goog.net.NetworkStatusMonitor.EventType;888990/**91* The time to wait before checking the {@code navigator.onLine} again.92* @type {number}93* @private94*/95goog.events.OnlineHandler.POLL_INTERVAL_ = 250;969798/**99* Stores the last value of the online state so we can detect if this has100* changed.101* @type {boolean}102* @private103*/104goog.events.OnlineHandler.prototype.online_;105106107/**108* The timer object used to poll the online state.109* @type {goog.Timer}110* @private111*/112goog.events.OnlineHandler.prototype.timer_;113114115/** @override */116goog.events.OnlineHandler.prototype.isOnline = function() {117return goog.events.BrowserFeature.HAS_NAVIGATOR_ONLINE_PROPERTY ?118navigator.onLine :119true;120};121122123/**124* Called every time the timer ticks to see if the state has changed and when125* the online state changes the method handleChange_ is called.126* @private127*/128goog.events.OnlineHandler.prototype.handleTick_ = function() {129var online = this.isOnline();130if (online != this.online_) {131this.online_ = online;132this.handleChange_();133}134};135136137/**138* Called when the online state changes. This dispatches the139* {@code ONLINE} and {@code OFFLINE} events respectively.140* @private141*/142goog.events.OnlineHandler.prototype.handleChange_ = function() {143var type = this.isOnline() ? goog.net.NetworkStatusMonitor.EventType.ONLINE :144goog.net.NetworkStatusMonitor.EventType.OFFLINE;145this.dispatchEvent(type);146};147148149/** @override */150goog.events.OnlineHandler.prototype.disposeInternal = function() {151goog.events.OnlineHandler.base(this, 'disposeInternal');152this.eventHandler_.dispose();153this.eventHandler_ = null;154if (this.timer_) {155this.timer_.dispose();156this.timer_ = null;157}158};159160161