cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [12/13] git commit: Add "edit" functionality. Allows changing the app name and url.
Date Mon, 06 Jan 2014 20:25:52 GMT
Add "edit" functionality. Allows changing the app name and url.


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/a2b62686
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/a2b62686
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/a2b62686

Branch: refs/heads/master
Commit: a2b626865818aaa224f3ce7648e52e8fa491256e
Parents: a33443b
Author: Braden Shepherdson <braden.shepherdson@gmail.com>
Authored: Tue Dec 3 14:16:41 2013 -0800
Committer: Braden Shepherdson <braden.shepherdson@gmail.com>
Committed: Mon Jan 6 15:25:00 2014 -0500

----------------------------------------------------------------------
 www/cdvah/js/AddCtrl.js        | 85 +++++++++++++++++++++++++++++--------
 www/cdvah/js/AppsService.js    | 10 +++++
 www/cdvah/js/CrxInstaller.js   |  7 +--
 www/cdvah/js/ListCtrl.js       |  5 +++
 www/cdvah/js/ServeInstaller.js | 11 ++---
 www/cdvah/js/app.js            | 14 ++++++
 www/cdvah/views/add.html       |  8 +++-
 www/cdvah/views/list.html      |  1 +
 8 files changed, 109 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/AddCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/AddCtrl.js b/www/cdvah/js/AddCtrl.js
index f06bde2..1373196 100644
--- a/www/cdvah/js/AddCtrl.js
+++ b/www/cdvah/js/AddCtrl.js
@@ -2,31 +2,82 @@
     'use strict';
 
     /* global myApp */
-    myApp.controller('AddCtrl', ['$q', 'notifier', '$location', '$rootScope', '$scope', '$window',
'AppsService', function($q, notifier, $location, $rootScope, $scope, $window, AppsService)
{
+    myApp.controller('AddCtrl', ['$q', 'notifier', '$location', '$rootScope', '$scope', '$window',
'$routeParams', 'AppsService', 'UrlCleanup', function($q, notifier, $location, $rootScope,
$scope, $window, $routeParams, AppsService, UrlCleanup) {
+        $scope.editing = $routeParams.appId;
+        var editingApp;
 
-        $rootScope.appTitle = 'Add App';
+        $rootScope.appTitle = $scope.editing ? 'Edit App' : 'Add App';
 
-        $scope.appData = {
-            appUrl : '',
-            installerType: 'serve'
-        };
+        if ($scope.editing) {
+            AppsService.getAppList().then(function(appList) {
+                appList.forEach(function(app) {
+                    if (app.appId == $scope.editing) {
+                        editingApp = app;
+                        $scope.editingApp = app;
+                        $scope.appData = {
+                            appId: app.appId,
+                            appUrl: app.url,
+                            installerType: app.type
+                        };
+                    }
+                });
+                if (!$scope.appData) {
+                    var err = 'Could not find app to edit';
+                    console.error(err);
+                    notifier.error(err);
+                }
+            });
+        } else {
+            $scope.appData = {
+                appUrl : '',
+                installerType: 'serve'
+            };
+        }
 
         $scope.selectTemplate = function() {
             $scope.appData.appUrl = $scope.appData.serveTemplateValue;
         };
 
         $scope.addApp = function() {
-            var serviceCall = AppsService.addApp($scope.appData.installerType, $scope.appData.appUrl);
-
-            serviceCall.then(function(handler) {
-                console.log('App Added');
-                notifier.success('App Added');
-                $location.path('/');
-                return AppsService.updateApp(handler);
-            }, function(error) {
-                console.error(error);
-                notifier.error('Unable to add application because: ' + error.message);
-            });
+            if ($scope.editing) {
+                // Update the app, write them out, and return to the list.
+                // We deliberately disallow changing the type, since that wouldn't work at
all.
+                var oldUrl = editingApp.url;
+                editingApp.appId = $scope.appData.appId;
+                editingApp.url = UrlCleanup($scope.appData.appUrl);
+                var urlChanged = oldUrl != editingApp.url;
+                var p = AppsService.editApp($scope.editing, editingApp).then(function() {
+                    console.log('App edited');
+                    notifier.success('App edited');
+                    $location.path('/');
+                });
+
+                if (urlChanged) {
+                    p.then(function() {
+                        // If the URL changed, trigger an update.
+                        return AppsService.updateApp(editingApp);
+                    }).then(function() {
+                        console.log('Updated app due to URL change');
+                        notifier.success('Updated app due to URL change');
+                    }, function(err) {
+                        var msg = 'Failed to update app: ' + err.message;
+                        console.error(msg);
+                        notifier.error(msg);
+                    });
+                }
+            } else {
+                var serviceCall = AppsService.addApp($scope.appData.installerType, $scope.appData.appUrl);
+
+                serviceCall.then(function(handler) {
+                    console.log('App Added');
+                    notifier.success('App Added');
+                    $location.path('/');
+                    return AppsService.updateApp(handler);
+                }, function(error) {
+                    console.error(error);
+                    notifier.error('Unable to add application because: ' + error.message);
+                });
+            }
         };
 
         // True if the optional barcodescanner plugin is installed.

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/AppsService.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/AppsService.js b/www/cdvah/js/AppsService.js
index c316f18..2fc5de8 100644
--- a/www/cdvah/js/AppsService.js
+++ b/www/cdvah/js/AppsService.js
@@ -94,6 +94,15 @@
                 });
             },
 
+            editApp : function(oldId, installer) {
+                _installers.forEach(function(inst, i) {
+                    if (inst.appId == oldId) {
+                        _installers.splice(i, 1, installer);
+                    }
+                });
+                return writeAppsJson();
+            },
+
             uninstallApp : function(installer) {
                 return installer.deleteFiles()
                 .then(function() {
@@ -112,6 +121,7 @@
                 .then(writeAppsJson);
             },
 
+
             registerInstallerFactory : function(installerFactory) {
                 _installerFactories[installerFactory.type] = installerFactory;
             }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/CrxInstaller.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/CrxInstaller.js b/www/cdvah/js/CrxInstaller.js
index c9f08aa..6637b1e 100644
--- a/www/cdvah/js/CrxInstaller.js
+++ b/www/cdvah/js/CrxInstaller.js
@@ -1,7 +1,7 @@
 (function(){
     'use strict';
     /* global myApp */
-    myApp.run(['$q', 'Installer', 'AppsService', 'ResourcesLoader', function($q, Installer,
AppsService, ResourcesLoader){
+    myApp.run(['$q', 'Installer', 'AppsService', 'ResourcesLoader', 'UrlCleanup', function($q,
Installer, AppsService, ResourcesLoader, UrlCleanup){
 
         var platformId = cordova.require('cordova/platform').id;
 
@@ -38,10 +38,7 @@
         AppsService.registerInstallerFactory({
             type: 'crx',
             createFromUrl: function(url) {
-                // Clean up the URL.
-                if (!/^https?:/.test(url)) {
-                    url = 'http://' + url;
-                }
+                url = UrlCleanup(url);
 
                 // TODO: Fix the missing appId, somehow.
                 return $q.when(new CrxInstaller(url, 'New Chrome App'));

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/ListCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/ListCtrl.js b/www/cdvah/js/ListCtrl.js
index 99712bd..d79eaef 100644
--- a/www/cdvah/js/ListCtrl.js
+++ b/www/cdvah/js/ListCtrl.js
@@ -87,6 +87,11 @@
             }
         };
 
+        $scope.editApp = function(app, event) {
+            event.stopPropagation();
+            $location.path('/edit/' + app.appId);
+        };
+
         $scope.showDetails = function(index) {
             $location.path('/details/' + index);
         };

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/ServeInstaller.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/ServeInstaller.js b/www/cdvah/js/ServeInstaller.js
index 6a8c50b..0cd97f2 100644
--- a/www/cdvah/js/ServeInstaller.js
+++ b/www/cdvah/js/ServeInstaller.js
@@ -4,9 +4,7 @@
     var ASSET_MANIFEST_PATH = 'installmanifest.json';
 
     /* global myApp */
-    myApp.run(['$q', 'Installer', 'AppsService', 'ResourcesLoader', function($q, Installer,
AppsService, ResourcesLoader) {
-        var platformId = cordova.platformId;
-
+    myApp.run(['$q', 'Installer', 'AppsService', 'ResourcesLoader', 'UrlCleanup', function($q,
Installer, AppsService, ResourcesLoader, UrlCleanup) {
         function ServeInstaller(url, appId) {
             Installer.call(this, url, appId);
             // Asset manifest is a cache of what files have been downloaded along with their
etags.
@@ -101,7 +99,7 @@
             .then(function(meta) {
                 self._cachedProjectJson = meta.projectJson;
                 self._cachedConfigXml = meta.configXml;
-                self.appId = meta.appId;
+                self.appId = self.appId || meta.appId;
                 var wwwPath = self._cachedProjectJson['wwwPath'];
                 var files = self._cachedProjectJson['wwwFileList'];
                 files = files.filter(function(f) {
@@ -143,10 +141,7 @@
 
         function createFromUrl(url) {
             // Strip platform and trailing slash if they exist.
-            url = url.replace(/\/$/, '').replace(new RegExp(platformId + '$'), '').replace(/\/$/,
'');
-            if (!/^http:/.test(url)) {
-                url = 'http://' + url;
-            }
+            url = UrlCleanup(url);
             // Fetch config.xml.
             return fetchMetaServeData(url)
             .then(function(meta) {

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/js/app.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/app.js b/www/cdvah/js/app.js
index cb7079f..59e7877 100644
--- a/www/cdvah/js/app.js
+++ b/www/cdvah/js/app.js
@@ -11,6 +11,10 @@ myApp.config(['$routeProvider', function($routeProvider){
         templateUrl: 'views/add.html',
         controller: 'AddCtrl'
     });
+    $routeProvider.when('/edit/:appId', {
+        templateUrl: 'views/add.html',
+        controller: 'AddCtrl'
+    });
     $routeProvider.when('/details/:index', {
         templateUrl: 'views/details.html',
         controller: 'DetailsCtrl'
@@ -24,6 +28,16 @@ document.addEventListener('deviceready', function() {
         myApp.value('INSTALL_DIRECTORY', path + '/apps');
         myApp.value('APPS_JSON', path + '/apps.json');
 
+        myApp.factory('UrlCleanup', function() {
+            return function(url) {
+                url = url.replace(/\/$/, '').replace(new RegExp(cordova.platformId + '$'),
'').replace(/\/$/, '');
+                if (!/^[a-z]+:/.test(url)) {
+                    url = 'http://' + url;
+                }
+                return url;
+            };
+        });
+
         angular.bootstrap(document, ['CordovaAppHarness']);
     });
 }, false);

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/views/add.html
----------------------------------------------------------------------
diff --git a/www/cdvah/views/add.html b/www/cdvah/views/add.html
index 1818ccd..a26f4d0 100644
--- a/www/cdvah/views/add.html
+++ b/www/cdvah/views/add.html
@@ -1,5 +1,8 @@
 <form name="addForm" ng-controller="AddCtrl">
-    <label>How to retrieve the app:<br>
+    <label ng-show="editing">App name<br />
+        <div><input type="text" class="topcoat-text-input" ng-model="appData.appId"
autocorrect="off" autocapitalize="on" /></div>
+    </label>
+    <label ng-show="!editing">How to retrieve the app:<br>
         <select ng-model="appData.installerType">
             <option value="serve">cordova serve</option>
             <option value="cdvh">.cdvh file</option>
@@ -25,7 +28,8 @@
           <button class="topcoat-button" ng-click="fetchQR(appData, 'appUrl')" ng-show="qrEnabled">Scan
QR Code</button><br>
     </div>
     <div class="buttons">
-        <button ng-click="addApp()" class="topcoat-button--cta">Add</button>
+        <button ng-click="addApp()" class="topcoat-button--cta">{{ editing ? 'Edit'
: 'Add' }}</button>
         <a href="#/"><button class="topcoat-button" ng-click="back()">Back</button></a>
     </div>
 </form>
+

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/a2b62686/www/cdvah/views/list.html
----------------------------------------------------------------------
diff --git a/www/cdvah/views/list.html b/www/cdvah/views/list.html
index b6cbda4..d1857dd 100644
--- a/www/cdvah/views/list.html
+++ b/www/cdvah/views/list.html
@@ -11,6 +11,7 @@
             <button ng-click="launchApp(app, $event)">Launch</button>
             <button ng-click="updateApp(app, $event)">Update</button>
             <button ng-click="removeApp(app, $event)">Remove</button>
+            <button ng-click="editApp(app, $event)">Edit</button>
         </li>
     </ul>
 </div>


Mime
View raw message