Skip to content

Commit

Permalink
Cleanup network.js.
Browse files Browse the repository at this point in the history
Make Network object available through navigator.network only (hide constructore).

Keep navigator.network object from being defined more than once.
  • Loading branch information
Justin Tyberg authored and mwbrooks committed Apr 18, 2011
1 parent 8156b2b commit ab8922e
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions javascript/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,61 @@
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2010, Nitobi Software Inc.
* Copyright (c) 2010, IBM Corporation
* Copyright (c) 2010-2011, IBM Corporation
*/

/**
* Network status.
*/
NetworkStatus = {
NOT_REACHABLE: 0,
REACHABLE_VIA_CARRIER_DATA_NETWORK: 1,
REACHABLE_VIA_WIFI_NETWORK: 2
NOT_REACHABLE: 0,
REACHABLE_VIA_CARRIER_DATA_NETWORK: 1,
REACHABLE_VIA_WIFI_NETWORK: 2
};

/**
* This class provides access to device Network data (reachability).
* @constructor
* navigator.network
*/
function Network() {
(function() {
/**
* The last known Network status.
* { hostName: string, ipAddress: string,
remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) }
* Check to see that navigator.network has not been initialized.
*/
this.lastReachability = null;
};

/**
* Determine if a URI is reachable over the network.
* @param {Object} uri
* @param {Function} callback
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(uri, callback, options) {
var isIpAddress = false;
if (options && options.isIpAddress) {
isIpAddress = options.isIpAddress;
if (typeof navigator.network !== "undefined") {
return;
}
PhoneGap.exec(callback, null, 'Network Status', 'isReachable', [uri, isIpAddress]);
};

/**
* This class provides access to device Network data (reachability).
* @constructor
*/
function Network() {
/**
* The last known Network status.
* { hostName: string, ipAddress: string,
remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) }
*/
this.lastReachability = null;
};

/**
* Determine if a URI is reachable over the network.
*
* @param {Object} uri
* @param {Function} callback
* @param {Object} options (isIpAddress:boolean)
*/
Network.prototype.isReachable = function(uri, callback, options) {
var isIpAddress = false;
if (options && options.isIpAddress) {
isIpAddress = options.isIpAddress;
}
PhoneGap.exec(callback, null, 'Network Status', 'isReachable', [uri, isIpAddress]);
};

PhoneGap.addConstructor(function() {
if (typeof navigator.network == "undefined") navigator.network = new Network();
});
/**
* Define navigator.network object.
*/
PhoneGap.addConstructor(function() {
navigator.network = new Network();
});
}());

0 comments on commit ab8922e

Please sign in to comment.