From 908485751beb5433036795021979deb1af11fe02 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Wed, 30 Mar 2011 13:29:24 -0500 Subject: [PATCH] Add check to only init and run JS code once - even if included multiple times. --- framework/assets/js/accelerometer.js | 8 ++++-- framework/assets/js/app.js | 6 +++- framework/assets/js/camera.js | 4 +++ framework/assets/js/compass.js | 8 ++++-- framework/assets/js/contact.js | 4 +++ framework/assets/js/crypto.js | 5 +++- framework/assets/js/device.js | 8 ++++-- framework/assets/js/file.js | 42 +++++++++++++++------------- framework/assets/js/filetransfer.js | 18 +++++++----- framework/assets/js/geolocation.js | 13 +++++---- framework/assets/js/media.js | 9 ++++-- framework/assets/js/network.js | 13 +++++---- framework/assets/js/notification.js | 9 ++++-- framework/assets/js/phonegap.js.base | 26 +++++++++++++++++ framework/assets/js/position.js | 20 +++++++------ framework/assets/js/storage.js | 5 +++- 16 files changed, 139 insertions(+), 59 deletions(-) diff --git a/framework/assets/js/accelerometer.js b/framework/assets/js/accelerometer.js index 2368b596..704048c2 100755 --- a/framework/assets/js/accelerometer.js +++ b/framework/assets/js/accelerometer.js @@ -6,7 +6,10 @@ * 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.y = y; this.z = z; @@ -17,7 +20,7 @@ function Acceleration(x, y, z) { * This class provides access to device accelerometer data. * @constructor */ -function Accelerometer() { +Accelerometer = function() { /** * The last known acceleration. type=Acceleration() @@ -119,3 +122,4 @@ PhoneGap.addConstructor(function() { navigator.accelerometer = new Accelerometer(); } }); +}; diff --git a/framework/assets/js/app.js b/framework/assets/js/app.js index 5a45cdda..0ee96a7f 100755 --- a/framework/assets/js/app.js +++ b/framework/assets/js/app.js @@ -6,10 +6,13 @@ * Copyright (c) 2010-2011, IBM Corporation */ +if (!PhoneGap.hasResource("app")) { +PhoneGap.addResource("app"); + /** * Constructor */ -function App() {} +App = function() {}; /** * Clear the resource cache. @@ -87,3 +90,4 @@ App.prototype.exitApp = function() { PhoneGap.addConstructor(function() { navigator.app = window.app = new App(); }); +}; diff --git a/framework/assets/js/camera.js b/framework/assets/js/camera.js index 1e4a75a6..c1ccc6e2 100755 --- a/framework/assets/js/camera.js +++ b/framework/assets/js/camera.js @@ -6,6 +6,9 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("camera")) { +PhoneGap.addResource("camera"); + /** * This class provides access to the device camera. * @@ -91,3 +94,4 @@ PhoneGap.addConstructor(function() { navigator.camera = new Camera(); } }); +}; diff --git a/framework/assets/js/compass.js b/framework/assets/js/compass.js index ffb16463..9c544128 100755 --- a/framework/assets/js/compass.js +++ b/framework/assets/js/compass.js @@ -6,11 +6,14 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("compass")) { +PhoneGap.addResource("compass"); + /** * This class provides access to device Compass data. * @constructor */ -function Compass() { +Compass = function() { /** * The last known Compass position. */ @@ -20,7 +23,7 @@ function Compass() { * List of compass watch timers */ this.timers = {}; -} +}; Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"]; @@ -113,3 +116,4 @@ PhoneGap.addConstructor(function() { navigator.compass = new Compass(); } }); +}; diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 67559c60..b222eab9 100755 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -6,6 +6,9 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("contact")) { +PhoneGap.addResource("contact"); + /** * Contains information about a single contact. * @param {DOMString} id unique identifier @@ -295,3 +298,4 @@ PhoneGap.addConstructor(function() { navigator.service.contacts = new Contacts(); } }); +}; diff --git a/framework/assets/js/crypto.js b/framework/assets/js/crypto.js index 65ea7a5b..434ff84e 100755 --- a/framework/assets/js/crypto.js +++ b/framework/assets/js/crypto.js @@ -8,6 +8,9 @@ // TODO: Needs to be commented +if (!PhoneGap.hasResource("crypto")) { +PhoneGap.addResource("crypto"); + var Crypto = function() { }; @@ -34,4 +37,4 @@ PhoneGap.addConstructor(function() { navigator.Crypto = new Crypto(); } }); - +}; diff --git a/framework/assets/js/device.js b/framework/assets/js/device.js index 59d57880..0e41e1c1 100755 --- a/framework/assets/js/device.js +++ b/framework/assets/js/device.js @@ -6,12 +6,15 @@ * 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 * phone, etc. * @constructor */ -function Device() { +Device = function() { this.available = PhoneGap.available; this.platform = null; this.version = null; @@ -35,7 +38,7 @@ function Device() { console.log("Error initializing PhoneGap: " + e); alert("Error initializing PhoneGap: "+e); }); -} +}; /** * Get device info @@ -97,3 +100,4 @@ Device.prototype.exitApp = function() { PhoneGap.addConstructor(function() { navigator.device = window.device = new Device(); }); +}; diff --git a/framework/assets/js/file.js b/framework/assets/js/file.js index 75c8e64f..3dbcf464 100755 --- a/framework/assets/js/file.js +++ b/framework/assets/js/file.js @@ -6,6 +6,9 @@ * 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. * They are not used to read files from a server. @@ -16,11 +19,11 @@ * This is the fields returned when navigator.fileMgr.getFileProperties() * is called. */ -function FileProperties(filePath) { +FileProperties = function(filePath) { this.filePath = filePath; this.size = 0; this.lastModifiedDate = null; -} +}; /** * Represents a single file. @@ -31,17 +34,17 @@ function FileProperties(filePath) { * lastModifiedDate {Date} last modified date * 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.fullPath = fullPath || null; this.type = type || null; this.lastModifiedDate = lastModifiedDate || null; this.size = size || 0; -} +}; -function FileError() { +FileError = function() { this.code = null; -} +}; // File error codes // Found in DOMException @@ -64,8 +67,8 @@ FileError.PATH_EXISTS_ERR = 12; // File manager //----------------------------------------------------------------------------- -function FileMgr() { -} +FileMgr = function() { +}; FileMgr.prototype.getFileProperties = function(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. * To read from the SD card, the file name is "sdcard/my_file.txt" */ -function FileReader() { +FileReader = function() { this.fileName = ""; this.readyState = 0; @@ -147,7 +150,7 @@ function FileReader() { this.onerror = null; // When the read has failed (see errors). 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. -} +}; // States FileReader.EMPTY = 0; @@ -379,7 +382,7 @@ FileReader.prototype.readAsArrayBuffer = function(file) { * @param file {File} File object containing file properties * @param append if true write to the end of the file, otherwise overwrite the file */ -function FileWriter(file) { +FileWriter = function(file) { this.fileName = ""; this.length = 0; if (file) { @@ -403,7 +406,7 @@ function FileWriter(file) { 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.onerror = null; // When the write has failed (see errors). -} +}; // States FileWriter.INIT = 0; @@ -633,7 +636,7 @@ FileWriter.prototype.truncate = function(size) { ); }; -function LocalFileSystem() { +LocalFileSystem = function() { }; // File error codes @@ -760,7 +763,7 @@ LocalFileSystem.prototype._castDate = function(pluginResult) { * * {Date} modificationTime (readonly) */ -function Metadata() { +Metadata = function() { this.modificationTime=null; }; @@ -770,7 +773,7 @@ function Metadata() { * @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 */ -function Flags(create, exclusive) { +Flags = function(create, exclusive) { this.create = create || false; this.exclusive = exclusive || false; }; @@ -781,7 +784,7 @@ function Flags(create, exclusive) { * {DOMString} name the unique name of the file system (readonly) * {DirectoryEntry} root directory of the file system (readonly) */ -function FileSystem() { +FileSystem = function() { this.name = null; this.root = null; }; @@ -795,7 +798,7 @@ function FileSystem() { * {DOMString} fullPath the absolute full path to the directory (readonly) * {FileSystem} filesystem on which the directory resides (readonly) */ -function DirectoryEntry() { +DirectoryEntry = function() { this.isFile = false; this.isDirectory = true; this.name = null; @@ -911,7 +914,7 @@ DirectoryEntry.prototype.removeRecursively = function(successCallback, errorCall /** * An interface that lists the files and directories in a directory. */ -function DirectoryReader(fullPath){ +DirectoryReader = function(fullPath){ this.fullPath = fullPath || null; }; @@ -934,7 +937,7 @@ DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) * {DOMString} fullPath the absolute full path to the file (readonly) * {FileSystem} filesystem on which the directory resides (readonly) */ -function FileEntry() { +FileEntry = function() { this.isFile = true; this.isDirectory = false; this.name = null; @@ -1048,3 +1051,4 @@ PhoneGap.addConstructor(function() { if(typeof window.requestFileSystem == "undefined") window.requestFileSystem = pgLocalFileSystem.requestFileSystem; if(typeof window.resolveLocalFileSystemURI == "undefined") window.resolveLocalFileSystemURI = pgLocalFileSystem.resolveLocalFileSystemURI; }); +}; diff --git a/framework/assets/js/filetransfer.js b/framework/assets/js/filetransfer.js index 58a8314d..40bd784d 100644 --- a/framework/assets/js/filetransfer.js +++ b/framework/assets/js/filetransfer.js @@ -6,26 +6,29 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("filetransfer")) { +PhoneGap.addResource("filetransfer"); + /** * FileTransfer uploads a file to a remote server. */ -function FileTransfer() {} +FileTransfer = function() {}; /** * FileUploadResult */ -function FileUploadResult() { +FileUploadResult = function() { this.bytesSent = 0; this.responseCode = null; this.response = null; -} +}; /** * FileTransferError */ -function FileTransferError() { +FileTransferError = function() { this.code = null; -} +}; FileTransferError.FILE_NOT_FOUND_ERR = 1; 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 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.fileName = fileName || null; this.mimeType = mimeType || null; this.params = params || null; -} +}; +}; diff --git a/framework/assets/js/geolocation.js b/framework/assets/js/geolocation.js index 8da7a740..82b463d7 100755 --- a/framework/assets/js/geolocation.js +++ b/framework/assets/js/geolocation.js @@ -6,18 +6,21 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("geolocation")) { +PhoneGap.addResource("geolocation"); + /** * This class provides access to device GPS data. * @constructor */ -function Geolocation() { +Geolocation = function() { // The last known GPS position. this.lastPosition = null; // Geolocation listeners this.listeners = {}; -} +}; /** * Position error object @@ -25,10 +28,10 @@ function Geolocation() { * @param code * @param message */ -function PositionError(code, message) { +PositionError = function(code, message) { this.code = code; this.message = message; -} +}; PositionError.PERMISSION_DENIED = 1; PositionError.POSITION_UNAVAILABLE = 2; @@ -191,4 +194,4 @@ PhoneGap.addConstructor(function() { Geolocation.usingPhoneGap = true; } }); - +}; diff --git a/framework/assets/js/media.js b/framework/assets/js/media.js index 09cee8f1..0b9155cb 100755 --- a/framework/assets/js/media.js +++ b/framework/assets/js/media.js @@ -6,6 +6,9 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("media")) { +PhoneGap.addResource("media"); + /** * List of media objects. * PRIVATE @@ -128,10 +131,10 @@ Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"]; * This class contains information about any Media errors. * @constructor */ -function MediaError() { +MediaError = function() { this.code = null; this.message = ""; -} +}; MediaError.MEDIA_ERR_ABORTED = 1; MediaError.MEDIA_ERR_NETWORK = 2; @@ -198,4 +201,4 @@ Media.prototype.stopRecord = function() { Media.prototype.release = function() { PhoneGap.exec(null, null, "Media", "release", [this.id]); }; - +}; diff --git a/framework/assets/js/network.js b/framework/assets/js/network.js index 01907f8c..ccf5b120 100755 --- a/framework/assets/js/network.js +++ b/framework/assets/js/network.js @@ -6,14 +6,17 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("network")) { +PhoneGap.addResource("network"); + /** * This class contains information about any NetworkStatus. * @constructor */ -function NetworkStatus() { +NetworkStatus = function() { //this.code = null; //this.message = ""; -} +}; NetworkStatus.NOT_REACHABLE = 0; 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). * @constructor */ -function Network() { +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) } */ this.lastReachability = null; -} +}; /** * Called by the geolocation framework when the reachability status has changed. @@ -61,4 +64,4 @@ PhoneGap.addConstructor(function() { navigator.network = new Network(); } }); - +}; diff --git a/framework/assets/js/notification.js b/framework/assets/js/notification.js index a3304d7d..7623d3f2 100755 --- a/framework/assets/js/notification.js +++ b/framework/assets/js/notification.js @@ -6,11 +6,14 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("notification")) { +PhoneGap.addResource("notification"); + /** * 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. @@ -115,4 +118,4 @@ PhoneGap.addConstructor(function() { navigator.notification = new Notification(); } }); - +}; diff --git a/framework/assets/js/phonegap.js.base b/framework/assets/js/phonegap.js.base index c9181b5e..98966d62 100755 --- a/framework/assets/js/phonegap.js.base +++ b/framework/assets/js/phonegap.js.base @@ -6,6 +6,7 @@ * Copyright (c) 2010-2011, IBM Corporation */ +if (typeof PhoneGap === "undefined") { /** * 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 @@ -906,3 +931,4 @@ var PluginManager = { navigator.app.addService(serviceType, className); } }; +}; diff --git a/framework/assets/js/position.js b/framework/assets/js/position.js index 94468a37..3a7454be 100755 --- a/framework/assets/js/position.js +++ b/framework/assets/js/position.js @@ -6,6 +6,9 @@ * Copyright (c) 2010, IBM Corporation */ +if (!PhoneGap.hasResource("position")) { +PhoneGap.addResource("position"); + /** * This class contains position information. * @param {Object} lat @@ -17,12 +20,12 @@ * @param {Object} vel * @constructor */ -function Position(coords, timestamp) { +Position = function(coords, timestamp) { this.coords = coords; 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. */ @@ -51,13 +54,13 @@ function Coordinates(lat, lng, alt, acc, head, vel, altacc) { * The altitude accuracy of the position. */ this.altitudeAccuracy = (altacc !== 'undefined') ? altacc : null; -} +}; /** * This class specifies the options for requesting position data. * @constructor */ -function PositionOptions() { +PositionOptions = function() { /** * Specifies the desired position accuracy. */ @@ -67,18 +70,19 @@ function PositionOptions() { * is called. */ this.timeout = 10000; -} +}; /** * This class contains information about any GSP errors. * @constructor */ -function PositionError() { +PositionError = function() { this.code = null; this.message = ""; -} +}; PositionError.UNKNOWN_ERROR = 0; PositionError.PERMISSION_DENIED = 1; PositionError.POSITION_UNAVAILABLE = 2; PositionError.TIMEOUT = 3; +}; diff --git a/framework/assets/js/storage.js b/framework/assets/js/storage.js index 9fb50f49..b2255663 100755 --- a/framework/assets/js/storage.js +++ b/framework/assets/js/storage.js @@ -12,6 +12,9 @@ * 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. * PRIVATE METHOD @@ -392,4 +395,4 @@ PhoneGap.addConstructor(function() { PhoneGap.waitForInitialization("cupcakeStorage"); } }); - +};