Add check to only init and run JS code once - even if included multiple times.

This commit is contained in:
Bryce Curtis 2011-03-30 13:29:24 -05:00
parent b850d225f4
commit 908485751b
16 changed files with 139 additions and 59 deletions

View File

@ -6,7 +6,10 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
function Acceleration(x, y, z) { if (!PhoneGap.hasResource("accelerometer")) {
PhoneGap.addResource("accelerometer");
Acceleration = function(x, y, z) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
@ -17,7 +20,7 @@ function Acceleration(x, y, z) {
* This class provides access to device accelerometer data. * This class provides access to device accelerometer data.
* @constructor * @constructor
*/ */
function Accelerometer() { Accelerometer = function() {
/** /**
* The last known acceleration. type=Acceleration() * The last known acceleration. type=Acceleration()
@ -119,3 +122,4 @@ PhoneGap.addConstructor(function() {
navigator.accelerometer = new Accelerometer(); navigator.accelerometer = new Accelerometer();
} }
}); });
};

View File

@ -6,10 +6,13 @@
* Copyright (c) 2010-2011, IBM Corporation * Copyright (c) 2010-2011, IBM Corporation
*/ */
if (!PhoneGap.hasResource("app")) {
PhoneGap.addResource("app");
/** /**
* Constructor * Constructor
*/ */
function App() {} App = function() {};
/** /**
* Clear the resource cache. * Clear the resource cache.
@ -87,3 +90,4 @@ App.prototype.exitApp = function() {
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
navigator.app = window.app = new App(); navigator.app = window.app = new App();
}); });
};

View File

@ -6,6 +6,9 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("camera")) {
PhoneGap.addResource("camera");
/** /**
* This class provides access to the device camera. * This class provides access to the device camera.
* *
@ -91,3 +94,4 @@ PhoneGap.addConstructor(function() {
navigator.camera = new Camera(); navigator.camera = new Camera();
} }
}); });
};

View File

@ -6,11 +6,14 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("compass")) {
PhoneGap.addResource("compass");
/** /**
* This class provides access to device Compass data. * This class provides access to device Compass data.
* @constructor * @constructor
*/ */
function Compass() { Compass = function() {
/** /**
* The last known Compass position. * The last known Compass position.
*/ */
@ -20,7 +23,7 @@ function Compass() {
* List of compass watch timers * List of compass watch timers
*/ */
this.timers = {}; this.timers = {};
} };
Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"]; Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
@ -113,3 +116,4 @@ PhoneGap.addConstructor(function() {
navigator.compass = new Compass(); navigator.compass = new Compass();
} }
}); });
};

View File

@ -6,6 +6,9 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("contact")) {
PhoneGap.addResource("contact");
/** /**
* Contains information about a single contact. * Contains information about a single contact.
* @param {DOMString} id unique identifier * @param {DOMString} id unique identifier
@ -295,3 +298,4 @@ PhoneGap.addConstructor(function() {
navigator.service.contacts = new Contacts(); navigator.service.contacts = new Contacts();
} }
}); });
};

View File

@ -8,6 +8,9 @@
// TODO: Needs to be commented // TODO: Needs to be commented
if (!PhoneGap.hasResource("crypto")) {
PhoneGap.addResource("crypto");
var Crypto = function() { var Crypto = function() {
}; };
@ -34,4 +37,4 @@ PhoneGap.addConstructor(function() {
navigator.Crypto = new Crypto(); navigator.Crypto = new Crypto();
} }
}); });
};

View File

@ -6,12 +6,15 @@
* Copyright (c) 2010-2011, IBM Corporation * Copyright (c) 2010-2011, IBM Corporation
*/ */
if (!PhoneGap.hasResource("device")) {
PhoneGap.addResource("device");
/** /**
* This represents the mobile device, and provides properties for inspecting the model, version, UUID of the * This represents the mobile device, and provides properties for inspecting the model, version, UUID of the
* phone, etc. * phone, etc.
* @constructor * @constructor
*/ */
function Device() { Device = function() {
this.available = PhoneGap.available; this.available = PhoneGap.available;
this.platform = null; this.platform = null;
this.version = null; this.version = null;
@ -35,7 +38,7 @@ function Device() {
console.log("Error initializing PhoneGap: " + e); console.log("Error initializing PhoneGap: " + e);
alert("Error initializing PhoneGap: "+e); alert("Error initializing PhoneGap: "+e);
}); });
} };
/** /**
* Get device info * Get device info
@ -97,3 +100,4 @@ Device.prototype.exitApp = function() {
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device(); navigator.device = window.device = new Device();
}); });
};

View File

@ -6,6 +6,9 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("file")) {
PhoneGap.addResource("file");
/** /**
* This class provides generic read and write access to the mobile device file system. * This class provides generic read and write access to the mobile device file system.
* They are not used to read files from a server. * They are not used to read files from a server.
@ -16,11 +19,11 @@
* This is the fields returned when navigator.fileMgr.getFileProperties() * This is the fields returned when navigator.fileMgr.getFileProperties()
* is called. * is called.
*/ */
function FileProperties(filePath) { FileProperties = function(filePath) {
this.filePath = filePath; this.filePath = filePath;
this.size = 0; this.size = 0;
this.lastModifiedDate = null; this.lastModifiedDate = null;
} };
/** /**
* Represents a single file. * Represents a single file.
@ -31,17 +34,17 @@ function FileProperties(filePath) {
* lastModifiedDate {Date} last modified date * lastModifiedDate {Date} last modified date
* size {Number} size of the file in bytes * size {Number} size of the file in bytes
*/ */
function File(name, fullPath, type, lastModifiedDate, size) { File = function(name, fullPath, type, lastModifiedDate, size) {
this.name = name || null; this.name = name || null;
this.fullPath = fullPath || null; this.fullPath = fullPath || null;
this.type = type || null; this.type = type || null;
this.lastModifiedDate = lastModifiedDate || null; this.lastModifiedDate = lastModifiedDate || null;
this.size = size || 0; this.size = size || 0;
} };
function FileError() { FileError = function() {
this.code = null; this.code = null;
} };
// File error codes // File error codes
// Found in DOMException // Found in DOMException
@ -64,8 +67,8 @@ FileError.PATH_EXISTS_ERR = 12;
// File manager // File manager
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
function FileMgr() { FileMgr = function() {
} };
FileMgr.prototype.getFileProperties = function(filePath) { FileMgr.prototype.getFileProperties = function(filePath) {
return PhoneGap.exec(null, null, "File", "getFileProperties", [filePath]); return PhoneGap.exec(null, null, "File", "getFileProperties", [filePath]);
@ -129,7 +132,7 @@ PhoneGap.addConstructor(function() {
* The root directory is the root of the file system. * The root directory is the root of the file system.
* To read from the SD card, the file name is "sdcard/my_file.txt" * To read from the SD card, the file name is "sdcard/my_file.txt"
*/ */
function FileReader() { FileReader = function() {
this.fileName = ""; this.fileName = "";
this.readyState = 0; this.readyState = 0;
@ -147,7 +150,7 @@ function FileReader() {
this.onerror = null; // When the read has failed (see errors). this.onerror = null; // When the read has failed (see errors).
this.onloadend = null; // When the request has completed (either in success or failure). this.onloadend = null; // When the request has completed (either in success or failure).
this.onabort = null; // When the read has been aborted. For instance, by invoking the abort() method. this.onabort = null; // When the read has been aborted. For instance, by invoking the abort() method.
} };
// States // States
FileReader.EMPTY = 0; FileReader.EMPTY = 0;
@ -379,7 +382,7 @@ FileReader.prototype.readAsArrayBuffer = function(file) {
* @param file {File} File object containing file properties * @param file {File} File object containing file properties
* @param append if true write to the end of the file, otherwise overwrite the file * @param append if true write to the end of the file, otherwise overwrite the file
*/ */
function FileWriter(file) { FileWriter = function(file) {
this.fileName = ""; this.fileName = "";
this.length = 0; this.length = 0;
if (file) { if (file) {
@ -403,7 +406,7 @@ function FileWriter(file) {
this.onwriteend = null; // When the request has completed (either in success or failure). this.onwriteend = null; // When the request has completed (either in success or failure).
this.onabort = null; // When the write has been aborted. For instance, by invoking the abort() method. this.onabort = null; // When the write has been aborted. For instance, by invoking the abort() method.
this.onerror = null; // When the write has failed (see errors). this.onerror = null; // When the write has failed (see errors).
} };
// States // States
FileWriter.INIT = 0; FileWriter.INIT = 0;
@ -633,7 +636,7 @@ FileWriter.prototype.truncate = function(size) {
); );
}; };
function LocalFileSystem() { LocalFileSystem = function() {
}; };
// File error codes // File error codes
@ -760,7 +763,7 @@ LocalFileSystem.prototype._castDate = function(pluginResult) {
* *
* {Date} modificationTime (readonly) * {Date} modificationTime (readonly)
*/ */
function Metadata() { Metadata = function() {
this.modificationTime=null; this.modificationTime=null;
}; };
@ -770,7 +773,7 @@ function Metadata() {
* @param {boolean} create file or directory if it doesn't exist * @param {boolean} create file or directory if it doesn't exist
* @param {boolean} exclusive if true the command will fail if the file or directory exists * @param {boolean} exclusive if true the command will fail if the file or directory exists
*/ */
function Flags(create, exclusive) { Flags = function(create, exclusive) {
this.create = create || false; this.create = create || false;
this.exclusive = exclusive || false; this.exclusive = exclusive || false;
}; };
@ -781,7 +784,7 @@ function Flags(create, exclusive) {
* {DOMString} name the unique name of the file system (readonly) * {DOMString} name the unique name of the file system (readonly)
* {DirectoryEntry} root directory of the file system (readonly) * {DirectoryEntry} root directory of the file system (readonly)
*/ */
function FileSystem() { FileSystem = function() {
this.name = null; this.name = null;
this.root = null; this.root = null;
}; };
@ -795,7 +798,7 @@ function FileSystem() {
* {DOMString} fullPath the absolute full path to the directory (readonly) * {DOMString} fullPath the absolute full path to the directory (readonly)
* {FileSystem} filesystem on which the directory resides (readonly) * {FileSystem} filesystem on which the directory resides (readonly)
*/ */
function DirectoryEntry() { DirectoryEntry = function() {
this.isFile = false; this.isFile = false;
this.isDirectory = true; this.isDirectory = true;
this.name = null; this.name = null;
@ -911,7 +914,7 @@ DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCall
/** /**
* An interface that lists the files and directories in a directory. * An interface that lists the files and directories in a directory.
*/ */
function DirectoryReader(fullPath){ DirectoryReader = function(fullPath){
this.fullPath = fullPath || null; this.fullPath = fullPath || null;
}; };
@ -934,7 +937,7 @@ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback)
* {DOMString} fullPath the absolute full path to the file (readonly) * {DOMString} fullPath the absolute full path to the file (readonly)
* {FileSystem} filesystem on which the directory resides (readonly) * {FileSystem} filesystem on which the directory resides (readonly)
*/ */
function FileEntry() { FileEntry = function() {
this.isFile = true; this.isFile = true;
this.isDirectory = false; this.isDirectory = false;
this.name = null; this.name = null;
@ -1048,3 +1051,4 @@ PhoneGap.addConstructor(function() {
if(typeof window.requestFileSystem == "undefined") window.requestFileSystem = pgLocalFileSystem.requestFileSystem; if(typeof window.requestFileSystem == "undefined") window.requestFileSystem = pgLocalFileSystem.requestFileSystem;
if(typeof window.resolveLocalFileSystemURI == "undefined") window.resolveLocalFileSystemURI = pgLocalFileSystem.resolveLocalFileSystemURI; if(typeof window.resolveLocalFileSystemURI == "undefined") window.resolveLocalFileSystemURI = pgLocalFileSystem.resolveLocalFileSystemURI;
}); });
};

View File

@ -6,26 +6,29 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("filetransfer")) {
PhoneGap.addResource("filetransfer");
/** /**
* FileTransfer uploads a file to a remote server. * FileTransfer uploads a file to a remote server.
*/ */
function FileTransfer() {} FileTransfer = function() {};
/** /**
* FileUploadResult * FileUploadResult
*/ */
function FileUploadResult() { FileUploadResult = function() {
this.bytesSent = 0; this.bytesSent = 0;
this.responseCode = null; this.responseCode = null;
this.response = null; this.response = null;
} };
/** /**
* FileTransferError * FileTransferError
*/ */
function FileTransferError() { FileTransferError = function() {
this.code = null; this.code = null;
} };
FileTransferError.FILE_NOT_FOUND_ERR = 1; FileTransferError.FILE_NOT_FOUND_ERR = 1;
FileTransferError.INVALID_URL_ERR = 2; FileTransferError.INVALID_URL_ERR = 2;
@ -69,9 +72,10 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
* @param mimeType {String} Mimetype of the uploaded file. Defaults to image/jpeg. * @param mimeType {String} Mimetype of the uploaded file. Defaults to image/jpeg.
* @param params {Object} Object with key: value params to send to the server. * @param params {Object} Object with key: value params to send to the server.
*/ */
function FileUploadOptions(fileKey, fileName, mimeType, params) { FileUploadOptions = function(fileKey, fileName, mimeType, params) {
this.fileKey = fileKey || null; this.fileKey = fileKey || null;
this.fileName = fileName || null; this.fileName = fileName || null;
this.mimeType = mimeType || null; this.mimeType = mimeType || null;
this.params = params || null; this.params = params || null;
} };
};

View File

@ -6,18 +6,21 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("geolocation")) {
PhoneGap.addResource("geolocation");
/** /**
* This class provides access to device GPS data. * This class provides access to device GPS data.
* @constructor * @constructor
*/ */
function Geolocation() { Geolocation = function() {
// The last known GPS position. // The last known GPS position.
this.lastPosition = null; this.lastPosition = null;
// Geolocation listeners // Geolocation listeners
this.listeners = {}; this.listeners = {};
} };
/** /**
* Position error object * Position error object
@ -25,10 +28,10 @@ function Geolocation() {
* @param code * @param code
* @param message * @param message
*/ */
function PositionError(code, message) { PositionError = function(code, message) {
this.code = code; this.code = code;
this.message = message; this.message = message;
} };
PositionError.PERMISSION_DENIED = 1; PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2; PositionError.POSITION_UNAVAILABLE = 2;
@ -191,4 +194,4 @@ PhoneGap.addConstructor(function() {
Geolocation.usingPhoneGap = true; Geolocation.usingPhoneGap = true;
} }
}); });
};

View File

@ -6,6 +6,9 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("media")) {
PhoneGap.addResource("media");
/** /**
* List of media objects. * List of media objects.
* PRIVATE * PRIVATE
@ -128,10 +131,10 @@ Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"];
* This class contains information about any Media errors. * This class contains information about any Media errors.
* @constructor * @constructor
*/ */
function MediaError() { MediaError = function() {
this.code = null; this.code = null;
this.message = ""; this.message = "";
} };
MediaError.MEDIA_ERR_ABORTED = 1; MediaError.MEDIA_ERR_ABORTED = 1;
MediaError.MEDIA_ERR_NETWORK = 2; MediaError.MEDIA_ERR_NETWORK = 2;
@ -198,4 +201,4 @@ Media.prototype.stopRecord = function() {
Media.prototype.release = function() { Media.prototype.release = function() {
PhoneGap.exec(null, null, "Media", "release", [this.id]); PhoneGap.exec(null, null, "Media", "release", [this.id]);
}; };
};

View File

@ -6,14 +6,17 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("network")) {
PhoneGap.addResource("network");
/** /**
* This class contains information about any NetworkStatus. * This class contains information about any NetworkStatus.
* @constructor * @constructor
*/ */
function NetworkStatus() { NetworkStatus = function() {
//this.code = null; //this.code = null;
//this.message = ""; //this.message = "";
} };
NetworkStatus.NOT_REACHABLE = 0; NetworkStatus.NOT_REACHABLE = 0;
NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
@ -23,14 +26,14 @@ NetworkStatus.REACHABLE_VIA_WIFI_NETWORK = 2;
* This class provides access to device Network data (reachability). * This class provides access to device Network data (reachability).
* @constructor * @constructor
*/ */
function Network() { Network = function() {
/** /**
* The last known Network status. * The last known Network status.
* { hostName: string, ipAddress: string, * { hostName: string, ipAddress: string,
remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) } remoteHostStatus: int(0/1/2), internetConnectionStatus: int(0/1/2), localWiFiConnectionStatus: int (0/2) }
*/ */
this.lastReachability = null; this.lastReachability = null;
} };
/** /**
* Called by the geolocation framework when the reachability status has changed. * Called by the geolocation framework when the reachability status has changed.
@ -61,4 +64,4 @@ PhoneGap.addConstructor(function() {
navigator.network = new Network(); navigator.network = new Network();
} }
}); });
};

View File

@ -6,11 +6,14 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("notification")) {
PhoneGap.addResource("notification");
/** /**
* This class provides access to notifications on the device. * This class provides access to notifications on the device.
*/ */
function Notification() { Notification = function() {
} };
/** /**
* Open a native alert dialog, with a customizable title and button text. * Open a native alert dialog, with a customizable title and button text.
@ -115,4 +118,4 @@ PhoneGap.addConstructor(function() {
navigator.notification = new Notification(); navigator.notification = new Notification();
} }
}); });
};

View File

@ -6,6 +6,7 @@
* Copyright (c) 2010-2011, IBM Corporation * Copyright (c) 2010-2011, IBM Corporation
*/ */
if (typeof PhoneGap === "undefined") {
/** /**
* The order of events during page load and PhoneGap startup is as follows: * The order of events during page load and PhoneGap startup is as follows:
@ -48,6 +49,30 @@ var PhoneGap = {
} }
}; };
/**
* List of resource files loaded by PhoneGap.
* This is used to ensure JS and other files are loaded only once.
*/
PhoneGap.resources = {base: true};
/**
* Determine if resource has been loaded by PhoneGap
*
* @param name
* @return
*/
PhoneGap.hasResource = function(name) {
return PhoneGap.resources[name];
};
/**
* Add a resource to list of loaded resources by PhoneGap
*
* @param name
*/
PhoneGap.addResource = function(name) {
PhoneGap.resources[name] = true;
};
/** /**
* Custom pub-sub channel that can have functions subscribed to it * Custom pub-sub channel that can have functions subscribed to it
@ -906,3 +931,4 @@ var PluginManager = {
navigator.app.addService(serviceType, className); navigator.app.addService(serviceType, className);
} }
}; };
};

View File

@ -6,6 +6,9 @@
* Copyright (c) 2010, IBM Corporation * Copyright (c) 2010, IBM Corporation
*/ */
if (!PhoneGap.hasResource("position")) {
PhoneGap.addResource("position");
/** /**
* This class contains position information. * This class contains position information.
* @param {Object} lat * @param {Object} lat
@ -17,12 +20,12 @@
* @param {Object} vel * @param {Object} vel
* @constructor * @constructor
*/ */
function Position(coords, timestamp) { Position = function(coords, timestamp) {
this.coords = coords; this.coords = coords;
this.timestamp = (timestamp !== 'undefined') ? timestamp : new Date().getTime(); this.timestamp = (timestamp !== 'undefined') ? timestamp : new Date().getTime();
} };
function Coordinates(lat, lng, alt, acc, head, vel, altacc) { Coordinates = function(lat, lng, alt, acc, head, vel, altacc) {
/** /**
* The latitude of the position. * The latitude of the position.
*/ */
@ -51,13 +54,13 @@ function Coordinates(lat, lng, alt, acc, head, vel, altacc) {
* The altitude accuracy of the position. * The altitude accuracy of the position.
*/ */
this.altitudeAccuracy = (altacc !== 'undefined') ? altacc : null; this.altitudeAccuracy = (altacc !== 'undefined') ? altacc : null;
} };
/** /**
* This class specifies the options for requesting position data. * This class specifies the options for requesting position data.
* @constructor * @constructor
*/ */
function PositionOptions() { PositionOptions = function() {
/** /**
* Specifies the desired position accuracy. * Specifies the desired position accuracy.
*/ */
@ -67,18 +70,19 @@ function PositionOptions() {
* is called. * is called.
*/ */
this.timeout = 10000; this.timeout = 10000;
} };
/** /**
* This class contains information about any GSP errors. * This class contains information about any GSP errors.
* @constructor * @constructor
*/ */
function PositionError() { PositionError = function() {
this.code = null; this.code = null;
this.message = ""; this.message = "";
} };
PositionError.UNKNOWN_ERROR = 0; PositionError.UNKNOWN_ERROR = 0;
PositionError.PERMISSION_DENIED = 1; PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2; PositionError.POSITION_UNAVAILABLE = 2;
PositionError.TIMEOUT = 3; PositionError.TIMEOUT = 3;
};

View File

@ -12,6 +12,9 @@
* most manufacturers ship with Android 1.5 and do not do OTA Updates, this is required * most manufacturers ship with Android 1.5 and do not do OTA Updates, this is required
*/ */
if (!PhoneGap.hasResource("storage")) {
PhoneGap.addResource("storage");
/** /**
* Storage object that is called by native code when performing queries. * Storage object that is called by native code when performing queries.
* PRIVATE METHOD * PRIVATE METHOD
@ -392,4 +395,4 @@ PhoneGap.addConstructor(function() {
PhoneGap.waitForInitialization("cupcakeStorage"); PhoneGap.waitForInitialization("cupcakeStorage");
} }
}); });
};