cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [16/30] git commit: Add apps hosted by cordova serve. (Context menu not injected into app)
Date Thu, 23 May 2013 20:03:39 GMT
Add apps hosted by cordova serve. (Context menu not injected into app)


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/636794a8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/636794a8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/636794a8

Branch: refs/heads/master
Commit: 636794a83fe4e89e592ca61bd927295d0feb23c4
Parents: dd4d655
Author: Shravan Narayan <shravanrn@google.com>
Authored: Wed May 8 17:52:15 2013 -0400
Committer: Shravan Narayan <shravanrn@google.com>
Committed: Wed May 8 18:38:55 2013 -0400

----------------------------------------------------------------------
 www/cdvah_js/AddCtrl.js         |   28 ++++--
 www/cdvah_js/AppsService.js     |  151 +++++++++++++++++++++++-----------
 www/cdvah_js/ListCtrl.js        |    2 +-
 www/cdvah_js/ResourcesLoader.js |   97 ++++++++++++++--------
 www/cdvah_views/add.html        |   12 ++-
 5 files changed, 195 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/636794a8/www/cdvah_js/AddCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah_js/AddCtrl.js b/www/cdvah_js/AddCtrl.js
index 09a1c50..7f6a34c 100644
--- a/www/cdvah_js/AddCtrl.js
+++ b/www/cdvah_js/AddCtrl.js
@@ -3,21 +3,29 @@
     /* global myApp */
     myApp.controller("AddCtrl", ["$scope", "AppsService", function ($scope, AppsService)
{
 
-        $scope.addApp = function(appName, appSource, appSourcePattern) {
+        $scope.addApp = function(appName, appSource, appSourcePattern, appSourceServe) {
+            var serviceCall;
             if(appSource === "pattern") {
                 if(!appSourcePattern) {
-                    alert("Url not specified");
+                    alert("Url of package not specified");
                     return;
                 }
-
-                AppsService.addAppFromPattern(appName, appSourcePattern)
-                .then(function() {
-                    alert("Successfully installed");
-                }, function(error) {
-                    console.error(error);
-                    alert("Unable to add application because: \n" + JSON.stringify(error));
-                });
+                serviceCall = AppsService.addAppFromPattern(appName, appSourcePattern);
+            } else if(appSource === "serve") {
+                if(!appSourceServe) {
+                    alert("Url of config file not specified");
+                    return;
+                }
+                serviceCall = AppsService.addAppFromServe(appName, appSourceServe);
             }
+
+            serviceCall.then(function() {
+                alert("Successfully installed");
+            }, function(error) {
+                console.error(error);
+                alert("Unable to add application because: \n" + error);
+            });
+
         };
     }]);
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/636794a8/www/cdvah_js/AppsService.js
----------------------------------------------------------------------
diff --git a/www/cdvah_js/AppsService.js b/www/cdvah_js/AppsService.js
index 9afe5a3..824cb8a 100644
--- a/www/cdvah_js/AppsService.js
+++ b/www/cdvah_js/AppsService.js
@@ -47,42 +47,35 @@
             });
         }
 
-        function registerApp(appName, appSource, appSourcePattern) {
+        function addNewAppFromServe(appName, appSourceServe){
+            return registerApp(appName, "serve", appSourceServe);
+        }
+
+        function registerApp(appName, appSource, appSourceData) {
             return ResourcesLoader.readJSONFileContents(APPS_JSON)
             .then(function(result){
                 result.installedApps = result.installedApps || [];
                 result.installedApps.push({
                     "Name" :  appName,
                     "Source" : appSource,
-                    "Data" : appSourcePattern,
+                    "Data" : appSourceData,
                     "Installed" : (new Date()).toLocaleString()
                 });
                 return ResourcesLoader.writeJSONFileContents(APPS_JSON, result);
             });
         }
 
-        function cleanPath(path){
-            if(path.indexOf("file://") === 0){
-                path = path.substring("file://".length);
-            }
-            // remove trailing slash
-            return (path.substring(path.length - 1) === "/") ? path.substring(0, path.length
- 1) : path;
-        }
-
         function isPathAbsolute(path){
             return (path.match(/^[a-z0-9+.-]+:/) != null);
         }
 
-        function getAppStartPageFromConfig(configFile, appBaseDirectory) {
-            configFile = cleanPath(configFile);
-            appBaseDirectory = "file://" + cleanPath(appBaseDirectory);
-
+        function getAppStartPageFromConfig(configFile, appBaseLocation) {
             return ResourcesLoader.readFileContents(configFile)
             .then(function(contents){
                 if(!contents) {
                     throw new Error("Config file is empty. Unable to find a start page for
your app.");
                 } else {
-                    var startLocation = appBaseDirectory + "/index.html";
+                    var startLocation = appBaseLocation + "/index.html";
                     var parser = new DOMParser();
                     var xmlDoc = parser.parseFromString(contents, "text/xml");
                     var els = xmlDoc.getElementsByTagName("content");
@@ -97,7 +90,7 @@
                                     startLocation = srcValue;
                                 } else {
                                     srcValue = srcValue.charAt(0) === "/" ? srcValue.substring(1)
: srcValue;
-                                    startLocation = appBaseDirectory + "/" + srcValue;
+                                    startLocation = appBaseLocation + "/" + srcValue;
                                 }
                                 break;
                             }
@@ -139,31 +132,93 @@
             });
         }
 
-        return {
-            //return promise with the array of apps
-            getAppsList : function(getFullEntries) {
-                return ResourcesLoader.ensureDirectoryExists(APPS_JSON)
-                .then(function() {
-                    return ResourcesLoader.readJSONFileContents(APPS_JSON);
-                })
-                .then(function(result){
-                    result.installedApps = result.installedApps || [];
-                    var newAppsList = [];
+        function getAppsList(getFullEntries){
+            return ResourcesLoader.ensureDirectoryExists(APPS_JSON)
+            .then(function() {
+                return ResourcesLoader.readJSONFileContents(APPS_JSON);
+            })
+            .then(function(result){
+                result.installedApps = result.installedApps || [];
+                var newAppsList = [];
 
-                    for(var i = 0; i < result.installedApps.length; i++){
-                        if(getFullEntries) {
-                            newAppsList.push(result.installedApps[i]);
-                        } else {
-                            newAppsList.push(result.installedApps[i].Name);
-                        }
+                for(var i = 0; i < result.installedApps.length; i++){
+                    if(getFullEntries) {
+                        newAppsList.push(result.installedApps[i]);
+                    } else {
+                        newAppsList.push(result.installedApps[i].Name);
                     }
+                }
 
-                    return newAppsList;
-                });
+                return newAppsList;
+            });
+        }
+
+        function isUniqueApp(appName){
+            return getAppsList(false /* App names only */)
+            .then(function(appsList){
+                if(appsList.indexOf(appName) !== -1) {
+                    throw new Error("An app with this name already exists");
+                }
+            });
+        }
+
+        function getWWWDirAndStartPageFromConfigForApp(appName){
+            var platformWWWLocation;
+            return getAppsList(true /* Get full app entry */)
+            .then(function(appEntries){
+                var entry;
+                for(var i = 0; i < appEntries.length; i++){
+                    if(appEntries[i].Name === appName){
+                        entry = appEntries[i];
+                        break;
+                    }
+                }
+                if(!entry){
+                    throw new Error("Could not find the app " + appName + " in the installed
apps");
+                }
+                if(entry.Source === "pattern"){
+                    return ResourcesLoader.getFullFilePath(INSTALL_DIRECTORY + appName +
"/" + platformId)
+                    .then(function(platformLocation){
+                        return {
+                            platformWWWLocation : platformLocation + "/www/",
+                            configLocation : platformLocation + "/config.xml"
+                        };
+                    });
+                } else if(entry.Source === "serve"){
+                    var configFile = entry.Data;
+                    var location = configFile.indexOf("/config.xml");
+                    if(location === -1){
+                        throw new Error("The location of config.xml provided is invalid.
Expected the location to end with 'config.xml'");
+                    }
+                    //grab path including upto last slash
+                    var appLocation =  configFile.substring(0, location + 1);
+                    return {
+                        platformWWWLocation : appLocation,
+                        configLocation : configFile
+                    };
+                } else {
+                    throw new Error("Unknown app source: " + entry.Source);
+                }
+            })
+            .then(function(appPaths){
+                platformWWWLocation = appPaths.platformWWWLocation;
+                return getAppStartPageFromConfig(appPaths.configLocation, appPaths.platformWWWLocation);
+            })
+            .then(function(startLocation){
+                return {
+                    startLocation : startLocation,
+                    platformWWWLocation : platformWWWLocation
+                };
+            });
+        }
+
+        return {
+            //return promise with the array of apps
+            getAppsList : function(getFullEntries) {
+                return getAppsList(getFullEntries);
             },
 
             launchApp : function(appName) {
-                var platformWWWLocation;
                 var startLocation;
                 return ResourcesLoader.readJSONFileContents(METADATA_JSON)
                 .then(function(settings){
@@ -172,17 +227,13 @@
                     return ResourcesLoader.writeJSONFileContents(METADATA_JSON, settings);
                 })
                 .then(function(){
-                    return ResourcesLoader.getFullFilePath(INSTALL_DIRECTORY + appName +
"/" + platformId);
-                })
-                .then(function(platformLocation){
-                    platformWWWLocation = platformLocation + "/www/";
-                    return getAppStartPageFromConfig(platformLocation + "/config.xml", platformWWWLocation);
+                    return getWWWDirAndStartPageFromConfigForApp(appName);
                 })
-                .then(function(_startLocation){
-                    startLocation = _startLocation;
+                .then(function(startLocationAndWWWLocation){
+                    startLocation = startLocationAndWWWLocation.startLocation;
                     var promises = [];
                     for (var i = preLaunchHooks.length - 1; i >= 0; i--) {
-                        promises.push(preLaunchHooks[i](appName, platformWWWLocation));
+                        promises.push(preLaunchHooks[i](appName, startLocationAndWWWLocation.platformWWWLocation));
                     }
                     return Q.all(promises);
                 })
@@ -192,15 +243,19 @@
             },
 
             addAppFromPattern : function(appName, appSourcePattern) {
-                return this.getAppsList(false /* App names only */)
-                .then(function(appsList){
-                    if(appsList.indexOf(appName) !== -1) {
-                        throw new Error("An app with this name already exists");
-                    }
+                return isUniqueApp(appName)
+                .then(function(){
                     return addNewAppFromPattern(appName, appSourcePattern);
                 });
             },
 
+            addAppFromServe : function(appName, appSourceServe) {
+                return isUniqueApp(appName)
+                .then(function(){
+                    return addNewAppFromServe(appName, appSourceServe);
+                });
+            },
+
             uninstallApp : function(appName) {
                 return removeApp(appName);
             },

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/636794a8/www/cdvah_js/ListCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah_js/ListCtrl.js b/www/cdvah_js/ListCtrl.js
index 5dd0794..326f394 100644
--- a/www/cdvah_js/ListCtrl.js
+++ b/www/cdvah_js/ListCtrl.js
@@ -87,7 +87,7 @@
             return AppsService.launchApp(app)
             .then(null, function(error){
                 console.error("Error during loading of app " + app + ": " + error);
-                alert("Something went wrong during the loading of the app. Please try again.");
+                alert("Something went wrong during the loading of the app. Please try again."
+ error);
             });
         };
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/636794a8/www/cdvah_js/ResourcesLoader.js
----------------------------------------------------------------------
diff --git a/www/cdvah_js/ResourcesLoader.js b/www/cdvah_js/ResourcesLoader.js
index d9ca013..e1df6db 100644
--- a/www/cdvah_js/ResourcesLoader.js
+++ b/www/cdvah_js/ResourcesLoader.js
@@ -72,6 +72,14 @@
             return path;
         }
 
+        function getScheme(uri){
+            var ret = uri.match(/^[a-z0-9+.-]+(?=:)/);
+            if(!ret){
+                return;
+            }
+            return ret[0];
+        }
+
         //promise returns the directory entry
         function getDirectoryEntry(directoryName) {
             var deferred = Q.defer();
@@ -196,6 +204,23 @@
             });
         }
 
+        function xhrGet(url){
+            var deferred = Q.defer();
+            var xhr = new XMLHttpRequest();
+            xhr.onreadystatechange = function() {
+                if (xhr.readyState === 4) {
+                    if(xhr.status === 200) {
+                        deferred.resolve(xhr);
+                    } else {
+                        deferred.reject("XHR return status: " + xhr.statusText);
+                    }
+                }
+            };
+            xhr.open("GET", url, true);
+            xhr.send();
+            return deferred.promise;
+        }
+
         return {
             doesFileExist : function(fileName){
                 return initialiseFileSystem()
@@ -283,24 +308,37 @@
 
             //returns a promise with the contents of the file
             readFileContents : function(fileName) {
-                return initialiseFileSystem()
-                .then(function(){
-                    return getFile(fileName);
-                })
-                .then(function(file){
-                    var deferred = Q.defer();
-
-                    var reader = new $window.FileReader();
-                    reader.onload = function(evt) {
-                        var text = evt.target.result;
-                        deferred.resolve(text);
-                    };
-                    reader.onerror = function(evt) {
-                        deferred.reject(new Error(evt));
-                    };
-                    reader.readAsText(file);
-
-                    return deferred.promise;
+                return Q.fcall(function(){
+                    var scheme = getScheme(fileName);
+                    // assume file scheme by default
+                    if(!scheme || scheme === "file") {
+                        return initialiseFileSystem()
+                        .then(function(){
+                            return getFile(fileName);
+                        })
+                        .then(function(file){
+                            var deferred = Q.defer();
+
+                            var reader = new $window.FileReader();
+                            reader.onload = function(evt) {
+                                var text = evt.target.result;
+                                deferred.resolve(text);
+                            };
+                            reader.onerror = function(evt) {
+                                deferred.reject(new Error(evt));
+                            };
+                            reader.readAsText(file);
+
+                            return deferred.promise;
+                        });
+                    } else if(scheme === "http" || scheme === "https") {
+                        return xhrGet(fileName)
+                        .then(function(xhr){
+                            return xhr.responseText;
+                        });
+                    } else {
+                        throw new Error("Cannot read file " + fileName);
+                    }
                 });
             },
 
@@ -321,7 +359,13 @@
             writeFileContents : function(fileName, contents) {
                 return initialiseFileSystem()
                 .then(function(){
-                    return writeToFile(fileName, contents, false /* append */);
+                    var scheme = getScheme(fileName);
+                    // assume file scheme by default
+                    if(!scheme || scheme === "file") {
+                        return writeToFile(fileName, contents, false /* append */);
+                    } else {
+                        throw new Error("Cannot write to " + fileName);
+                    }
                 });
             },
 
@@ -383,20 +427,7 @@
             },
 
             xhrGet : function(url) {
-                var deferred = Q.defer();
-                var xhr = new XMLHttpRequest();
-                xhr.onreadystatechange = function() {
-                    if (xhr.readyState === 4) {
-                        if(xhr.status === 200) {
-                            deferred.resolve(xhr);
-                        } else {
-                            deferred.reject("XHR return status: " + xhr.statusText);
-                        }
-                    }
-                };
-                xhr.open("GET", url, true);
-                xhr.send();
-                return deferred.promise;
+                return xhrGet(url);
             }
         };
     }]);

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/636794a8/www/cdvah_views/add.html
----------------------------------------------------------------------
diff --git a/www/cdvah_views/add.html b/www/cdvah_views/add.html
index 902c46d..af318b8 100644
--- a/www/cdvah_views/add.html
+++ b/www/cdvah_views/add.html
@@ -6,12 +6,18 @@
     <span ng-show="addForm.appName.$error.maxlength">Too long</span>
     <br />
 
-    <input type="radio" ng-model="appSource" name="appSource" value="pattern" ng-init="appPatternChecked=true;
appSource='pattern'" ng-checked="appPatternChecked" />
+    <input type="radio" ng-model="appSource" name="appSource" value="pattern" ng-init="appSource='pattern'"
ng-checked="appSource=='pattern'" />
         <label for="inputAppPattern">Enter the URL to a file</label><br />
-        <input id="inputAppPattern" type="text" name="appSourcePattern" ng-model="appSourcePattern"
ng-disabled="!appPatternChecked" ng-required="appPatternChecked" />
+        <input id="inputAppPattern" type="text" name="appSourcePattern" ng-model="appSourcePattern"
ng-disabled="appSource!='pattern'" ng-required="appSource=='pattern'" />
         <span ng-show="addForm.appSourcePattern.$error.required">Required</span>
         <br />
-    <button ng-click="addApp(appName, appSource, appSourcePattern)" ng-disabled="!(addForm.$valid)">Add</button>
+    <input type="radio" ng-model="appSource" name="appSource" value="serve" ng-checked="appSource=='serve'"
/>
+        <label for="inputAppServe">Enter the URL to the server hosting the app</label><br
/>
+        <input id="inputAppServe" type="text" name="appSourceServe" ng-model="appSourceServe"
ng-disabled="appSource!='serve'" ng-pattern="/.*\/config.xml(\?.*|#.*)?$/" ng-required="appSource=='serve'"
/>
+        <span ng-show="addForm.appSourceServe.$error.required">Required</span>
+        <span ng-show="addForm.appSourceServe.$error.pattern">Url must point to a config.xml
file</span>
+        <br />
+    <button ng-click="addApp(appName, appSource, appSourcePattern, appSourceServe)" ng-disabled="!(addForm.$valid)">Add</button>
     <br />
     <a href="#/">Back To Main Menu</a>
 </form>
\ No newline at end of file


Mime
View raw message