Skip to content

Commit

Permalink
Merge pull request #27 from shankari/version_6_javascript_fixes
Browse files Browse the repository at this point in the history
Version 6 javascript fixes
  • Loading branch information
shankari committed Mar 14, 2016
2 parents 520688b + c3ec7f0 commit 692e8c9
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 78 deletions.
6 changes: 3 additions & 3 deletions config.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="edu.berkeley.eecs.emission"
version="0.0.5"
android-versionCode="5"
ios-CFBundleVersion="5"
version="0.0.6"
android-versionCode="6"
ios-CFBundleVersion="6"
xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>emission</name>
<description>
Expand Down
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,17 @@
{
"locator": "https://github.com/e-mission/cordova-server-sync.git",
"id": "edu.berkeley.eecs.emission.cordova.serversync"
},
{
"locator": "https://github.com/shankari/cordova-broadcaster.git",
"id": "cordova-plugin-broadcaster"
}
],
"cordovaPlatforms": [
"ios",
"android",
{
"platform": "ios",
"version": "",
"version": "4.1.0",
"locator": "ios"
},
{
"platform": "android",
"version": "",
"version": "5.1.1",
"locator": "android"
}
]
Expand Down
17 changes: 3 additions & 14 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<meta http-equiv="Content-Security-Policy"
content="default-src 'self' data: gap: https://ssl.gstatic.com http://nominatim.openstreetmap.org 'unsafe-inline' 'unsafe-eval';
content="default-src 'self' data: gap: https://ssl.gstatic.com http://nominatim.openstreetmap.org 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' http://*.tile.openstreetmap.org">
<title></title>
Expand Down Expand Up @@ -53,22 +53,11 @@
<script src="js/intro.js"></script>
<script src="js/main.js"></script>
<script src="js/recent.js"></script>
<script src="js/common.js"></script>
<script src="js/diary.js"></script>

<script type="text/javascript">
console.log("About to bootstrap angular");
// BEGIN DEVICE VERSION
document.addEventListener('deviceready', function onDeviceReady() {
console.log("bootstrapping angular");
// alert('Time to connect to javascript');
angular.bootstrap(document.getElementsByTagName('body')[0], ['emission']);
// alert('Device is ready, time to bootstrap');
}, false);
</script>

</head>

<body>
<body ng-app="emission">
<!--
The nav bar that will be updated as we navigate between views.
-->
Expand Down
22 changes: 18 additions & 4 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module('emission', ['ionic', 'emission.controllers','emission.services',
'emission.intro', 'emission.main'])

.run(function($ionicPlatform) {
console.log("Starting run");
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
Expand All @@ -24,9 +25,11 @@ angular.module('emission', ['ionic', 'emission.controllers','emission.services',
StatusBar.styleDefault();
}
});
console.log("Ending run");
})

.config(function($stateProvider, $urlRouterProvider) {
console.log("Starting config");
var waitFn = function($q) {
var deferred = $q.defer();
ionic.Platform.ready(function() {
Expand All @@ -41,16 +44,27 @@ angular.module('emission', ['ionic', 'emission.controllers','emission.services',
// The 'intro' and 'diary' states are found in their respective modules
// Each state's controller can be found in controllers.js
$stateProvider
// set up a state for the splash screen. This has no parents and no children
// because it is basically just used to load the user's preferred screen.
// This cannot directly use plugins - has to check for them first.
.state('splash', {
url: '/splash',
templateUrl: 'templates/splash/splash.html',
controller: 'SplashCtrl'
})

// setup an abstract state for the root
.state('root', {
// setup an abstract state for the root. Only children of this can be loaded
// as preferred screens, and all children of this can assume that the device
// is ready.
.state('root', {
url: '/root',
abstract: true,
template: '<ion-nav-view/>',
controller: 'RootCtrl'
});

// alert("about to fall back to otherwise");
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/root/intro');

$urlRouterProvider.otherwise('/splash');
console.log("Ending config");
});
132 changes: 132 additions & 0 deletions www/js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
angular.module('emission.main.common',['ui-leaflet', 'nvd3ChartDirectives',
'ionic-datepicker'])

.controller("CommonCtrl", function($scope, $http, $ionicPopup,
leafletData, CommHelper) {
console.log("controller CommonCtrl called");

var db = window.cordova.plugins.BEMUserCache;
$scope.mapCtrl = {};
$scope.mapCtrl.selKey = "common-trips";

angular.extend($scope.mapCtrl, {
defaults : {
tileLayer: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
tileLayerOptions: {
opacity: 0.9,
detectRetina: true,
reuseTiles: true,
}
},
events: {
map: {
enable: ['zoomstart', 'drag', 'click', 'mousemove', 'contextmenu'],
logic: 'emit'
}
}
});

$scope.$on('leafletDirectiveMap.click', function(event){
alert('click '+JSON.stringify(event)+' detected');
});

$scope.$on('leafletDirectiveMap.contextmenu', function(event){
alert('conextmenu '+JSON.stringify(event)+' detected');
});

var onEachFeature = function(feature, layer) {
console.log("onEachFeature called with "+JSON.stringify(feature));
console.log("type is "+feature.geometry.type);
var popupContent = "<p>I started out as a GeoJSON " +
feature.geometry.type + ", but now I'm a Leaflet vector!</p>";
layer.on('click', function(e) { console.log("layer clicked"); alert(feature.geometry.type) } );
// layer.bindPopup(popupContent);
/*
switch(feature.geometry.type) {
case "Point": layer.bindPopup(popupContent); break;
case "LineString": layer.bindPopup(popupContent); break;
}
*/
}

$scope.refreshMap = function() {
db.getDocument($scope.mapCtrl.selKey, function(entryList) {
cmGraph = JSON.parse(entryList);
var places = cmGraph.common_places.map(function(place) {
return {
"type": "Feature",
"id": place._id,
"geometry": place.location,
"properties": {
"successors": place.successors
}
};
});
// places.map($scope.getDisplayName);
var trips = cmGraph.common_trips.map(function(trip) {
return {
"type": "Feature",
"id": trip._id,
"geometry": {
"type": "LineString",
"coordinates": [trip.start_loc.coordinates, trip.end_loc.coordinates]
},
"properties": {
"probabilities": trip.probabilites
}
};
});
$scope.$apply(function() {
$scope.mapCtrl.geojson = {}
$scope.mapCtrl.geojson.data = {
"type": "FeatureCollection",
"features": places.concat(trips)
};
$scope.mapCtrl.geojson.onEachFeature = onEachFeature;
});
});
};

$scope.refreshMap();

// TODO: Refactor into common code - maybe a service?
// Or maybe pre-populate from the server

/*
$scope.getDisplayName = function(place_feature) {
var responseListener = function(data) {
var address = data["address"];
var name = "";
if (address["road"]) {
name = address["road"];
} else if (address["neighbourhood"]) {
name = address["neighbourhood"];
}
if (address["city"]) {
name = name + ", " + address["city"];
} else if (address["town"]) {
name = name + ", " + address["town"];
} else if (address["county"]) {
name = name + ", " + address["county"];
}
console.log("got response, setting display name to "+name);
place_feature.properties.displayName = name;
};
var url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" + place_feature.geometry.coordinates[1]
+ "&lon=" + place_feature.geometry.coordinates[0];
console.log("About to make call "+url);
$http.get(url).then(function(response) {
console.log("while reading data from nominatim, status = "+response.status
+" data = "+JSON.stringify(response.data));
responseListener(response.data);
}, function(response) {
console.log("while reading data from nominatim, status = "+response.status);
notFoundFn(day, response);
});
};
*/

});
66 changes: 39 additions & 27 deletions www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@

angular.module('emission.controllers', [])

.controller('RootCtrl', function($scope, $state, $ionicPopup, $ionicLoading) {
console.log('RootCtrl invoked');
alert("Welcome to e-mission! This will be removed post-alpha");
$ionicLoading.show({
template: 'Checking startup state...'
});
var prefs = window.plugins.appPreferences;
prefs.fetch('setup_complete').then(function(value) {
console.log('setup_complete result '+value);
$scope.$apply(function() {
if (value == true) {
$state.go('root.main.diary');
$ionicLoading.hide();
} else {
$state.go('root.intro');
$ionicLoading.hide();
}
});
}, function(error) {
$scope.$apply(function() {
$ionicPopup.alert({template: "setup_complete, error -> "+error});
$state.go('root.intro');
$ionicLoading.hide();
});
});
console.log('RootCtrl invoke finished');
})
.controller('RootCtrl', function($scope) {})

.controller('DashCtrl', function($scope) {})

.controller('SplashCtrl', function($scope, $ionicPlatform, $state, $interval, $rootScope) {
console.log('SplashCtrl invoked');
// Currently loads main or intro based on whether onboarding is complete.
// But easily extensible to storing the last screen that the user was on,
// or the users' preferred screen
var changeState = function(destState) {
console.log("loading "+destState);
$state.go(destState);
$interval.cancel(currPromise);
};

var loadPreferredScreen = function() {
console.log("Checking to see whether we are ready to load the screen");
if (window.plugins && window.plugins.appPreferences) {
var prefs = plugins.appPreferences;
prefs.fetch('setup_complete').then(function(value) {
console.log('setup_complete result '+value);
if (value == true) {
changeState('root.main.diary');
} else {
changeState('root.intro');
}
}, function(error) {
console.log("error "+error+" loading root.intro");
changeState('root.intro');
});
} else {
console.log("appPreferences plugin not installed, waiting...");
}
}
var currPromise = $interval(loadPreferredScreen, 1000);
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
console.log("Finished changing state from "+JSON.stringify(fromState)
+ " to "+JSON.stringify(toState));
});
console.log('SplashCtrl invoke finished');
})


.controller('ChatsCtrl', function($scope, Chats) {
// With the new view caching in Ionic, Controllers are only called
Expand Down
14 changes: 13 additions & 1 deletion www/js/diary.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,21 @@ angular.module('emission.main.diary',['ui-leaflet', 'nvd3ChartDirectives',
return alertPopup;
}

/*
* Disabling the reload of the page on background sync because it doesn't
* work correctly. on iOS, plugins are not loaded if backgroundFetch or
* remote push are invoked, since they don't initialize the app. On
* android, it looks like the thread ends before the maps are fully loaded,
* so we have half displayed, frozen maps. We should really check the
* status, reload here if active and reload everything on resume.
* For now, we just add a refresh button to avoid maintaining state.
window.broadcaster.addEventListener( "edu.berkeley.eecs.emission.sync.NEW_DATA", function( e ) {
window.Logger.log(window.Logger.LEVEL_INFO,
"new data received! reload data for the current day"+$scope.data.currDay);
// TODO: Should we reload for the most recent day instead?
$window.location.reload();
// readAndUpdateForDay($scope.data.currDay);
});
*/

/*
Let us assume that we have recieved a list of trips for that date from somewhere
Expand Down Expand Up @@ -362,6 +370,10 @@ angular.module('emission.main.diary',['ui-leaflet', 'nvd3ChartDirectives',
// in the cache.
readAndUpdateForDay($scope.data.currDay);

$scope.refresh = function() {
readAndUpdateForDay($scope.data.currDay);
}

/*
$scope.to_directive = function(trip) {
retVal = {};
Expand Down
12 changes: 11 additions & 1 deletion www/js/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

angular.module('emission.main', ['emission.main.diary', 'emission.main.recent'])
angular.module('emission.main', ['emission.main.diary', 'emission.main.common', 'emission.main.recent'])

.config(function($stateProvider, $ionicConfigProvider) {
$stateProvider
Expand All @@ -22,6 +22,16 @@ angular.module('emission.main', ['emission.main.diary', 'emission.main.recent'])
}
})

.state('root.main.common', {
url: '/common',
views: {
'main-common': {
templateUrl: 'templates/main-common.html',
controller: 'CommonCtrl'
}
}
})

.state('root.main.recent', {
url: '/recent',
abstract: true,
Expand Down
Loading

0 comments on commit 692e8c9

Please sign in to comment.