Compare View

switch
from
...
to
 
Commits (3)

Diff

Showing 1 changed file Inline Diff

scripts/SettingsController.js View file @ 8a9a3e9
angular.module('flashy.SettingsController', ['ui.router']). 1 1 angular.module('flashy.SettingsController', ['ui.router']).
2 2
controller('SettingsController', function($rootScope, $resource, $scope, $state, $http, UserService) { 3 3 controller('SettingsController', function($rootScope, $resource, $scope, $state, $http, UserService) {
$scope.error = false; 4 4 $scope.error = false;
$scope.success = false; 5 5 $scope.success = false;
$scope.mismatch = false; 6 6 $scope.mismatch = false;
$scope.unacceptable = false; 7 7 $scope.unacceptable = false;
8 8
$scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { 9 9 $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) {
console.log('in change password'); 10 10 console.log('in change password');
11 11
$http.patch('/api/me/', { 12 12 $http.patch('/api/me/', {
'old_password': oldPassword, 13 13 'old_password': oldPassword,
'new_password': newPassword 14 14 'new_password': newPassword
}). 15 15 }).
success(function(data) { 16 16 success(function(data) {
17 17
console.log('password successfully changes'); 18 18 console.log('password successfully changes');
19 19
}). 20 20 }).
error(function(data) { 21 21 error(function(data) {
console.log('not changed'); 22 22 console.log('not changed');
}); 23 23 });
24 24
25 25
}; 26 26 };
27 27
$rootScope.SectionResource = $resource('/api/sections/:sectionId/'); 28 28 $rootScope.SectionResource = $resource('/api/sections/:sectionId/');
$rootScope.currentSection = {}; 29 29 $rootScope.currentSection = {};
$rootScope.UserService = UserService; 30 30 $rootScope.UserService = UserService;
31 31
$scope.dropClass = function(section) { 32 32 $scope.dropClass = function(section) {
$http.post('/api/sections/' + section.id + '/drop/'). 33 33 $http.post('/api/sections/' + section.id + '/drop/').
success(function(data) { 34 34 success(function(data) {
console.log(section.short_name + ' dropped'); 35 35 console.log(section.short_name + ' dropped');
36 36
Materialize.toast('Dropped', 3000, 'rounded'); 37 37 Materialize.toast('Dropped', 3000, 'rounded');
}). 38 38 }).
error(function(err) { 39 39 error(function(err) {
console.log('no drop for you'); 40 40 console.log('no drop for you');
}); 41 41 });
}; 42 42 };
43 43
44 44
45 45
console.log('checking to see if chrome'); 46 46 console.log('checking to see if chrome');
47 47
48 if (!chrome) {
49 pushSwitch.disabled = true;
if (!chrome) { 48 50 return;
pushSwitch.disabled = true; 49 51 }
52
return; 50 53 console.log('chrome');
} 51 54
52 55 console.log('executing things outside of module');
console.log('chrome'); 53 56 var PUSH_SERVER_URL = '/api/subscribe/';
54 57 var UNPUSH_SERVER_URL = '/api/unsubscribe/';
console.log('executing things outside of module'); 55 58
var PUSH_SERVER_URL = '/api/subscribe/'; 56 59 function onPushSubscription(pushSubscription) {
var UNPUSH_SERVER_URL = '/api/unsubscribe/'; 57 60 console.log('pushSubscription = ', pushSubscription.endpoint);
58 61 // Here we would normally send the endpoint
function onPushSubscription(pushSubscription) { 59 62 // and subscription ID to our server.
console.log('pushSubscription = ', pushSubscription.endpoint); 60 63 // In this demo we just use send these values to
// Here we would normally send the endpoint 61 64 // our server via XHR which sends a push message.
// and subscription ID to our server. 62 65
// In this demo we just use send these values to 63 66 var endpoint = pushSubscription.endpoint;
// our server via XHR which sends a push message. 64 67 var subscriptionId = pushSubscription.subscriptionId;
65 68
var endpoint = pushSubscription.endpoint; 66 69 console.log('registration_id: ', subscriptionId);
var subscriptionId = pushSubscription.subscriptionId; 67 70 $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId});
68 71 }
console.log('registration_id: ', subscriptionId); 69 72
$http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); 70 73 function removeSubscription(pushSubscription) {
} 71 74 console.log('removing subscription');
72 75 console.log('pushSubscription endpoint = ', pushSubscription.endpoint);
function removeSubscription(pushSubscription) { 73 76
console.log('removing subscription'); 74 77 var subscriptionId = pushSubscription.subscriptionId;
console.log('pushSubscription endpoint = ', pushSubscription.endpoint); 75 78
76 79 console.log('registration_id: ', subscriptionId);
var subscriptionId = pushSubscription.subscriptionId; 77 80 $http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId});
78 81 }
console.log('registration_id: ', subscriptionId); 79 82
$http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId}); 80 83 function subscribeDevice() {
} 81 84 // We need the service worker registration to access the push manager
82 85 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
function subscribeDevice() { 83 86 serviceWorkerRegistration.pushManager.subscribe()
// We need the service worker registration to access the push manager 84 87 .then(onPushSubscription)
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 85 88 .catch(function(e) {
serviceWorkerRegistration.pushManager.subscribe() 86 89 console.log('Error in subscribing');
.then(onPushSubscription) 87 90 // Check for a permission prompt issue
.catch(function(e) { 88 91 if ('permissions' in navigator) {
console.log('Error in subscribing'); 89 92 navigator.permissions.query({name: 'push', userVisibleOnly: true})
// Check for a permission prompt issue 90 93 .then(function(permissionStatus) {
if ('permissions' in navigator) { 91 94 console.log('subscribe() Error: Push permission status = ',
navigator.permissions.query({name: 'push', userVisibleOnly: true}) 92 95 permissionStatus);
.then(function(permissionStatus) { 93 96 if (permissionStatus.status === 'denied') {
console.log('subscribe() Error: Push permission status = ', 94 97 pushSwitch.checked = false;
permissionStatus); 95 98 pushSwitch.disabled = true;
if (permissionStatus.status === 'denied') { 96 99 // The user blocked the permission prompt
pushSwitch.checked = false; 97 100 console.log('Ooops Notifications are Blocked',
pushSwitch.disabled = true; 98 101 'Unfortunately you just permanently blocked notifications. ' +
// The user blocked the permission prompt 99 102 'Please unblock / allow them to switch on push ' +
console.log('Ooops Notifications are Blocked', 100 103 'notifications.');
'Unfortunately you just permanently blocked notifications. ' + 101 104 } else {
'Please unblock / allow them to switch on push ' + 102 105 pushSwitch.checked = false;
'notifications.'); 103 106 console.log('Ooops Push Couldn\'t Register',
} else { 104 107 '<p>When we tried to ' +
pushSwitch.checked = false; 105 108 'get the subscription ID for GCM, something went wrong,' +
console.log('Ooops Push Couldn\'t Register', 106 109 ' not sure why.</p>' +
'<p>When we tried to ' + 107 110 '<p>Have you defined "gcm_sender_id" and ' +
'get the subscription ID for GCM, something went wrong,' + 108 111 '"gcm_user_visible_only" in the manifest?</p>' +
' not sure why.</p>' + 109 112 '<p>Error message: ' +
'<p>Have you defined "gcm_sender_id" and ' + 110 113 e.message +
'"gcm_user_visible_only" in the manifest?</p>' + 111 114 '</p>');
'<p>Error message: ' + 112 115 }
e.message + 113 116 }).catch(function(err) {
'</p>'); 114 117 pushSwitch.checked = false;
} 115 118 console.log('Ooops Push Couldn\'t Register',
}).catch(function(err) { 116 119 '<p>When we tried to ' +
pushSwitch.checked = false; 117 120 'get the subscription ID for GCM, something went wrong, not ' +
console.log('Ooops Push Couldn\'t Register', 118 121 'sure why.</p>' +
'<p>When we tried to ' + 119 122 '<p>Have you defined "gcm_sender_id" and ' +
'get the subscription ID for GCM, something went wrong, not ' + 120 123 '"gcm_user_visible_only" in the manifest?</p>' +
'sure why.</p>' + 121 124 '<p>Error message: ' +
'<p>Have you defined "gcm_sender_id" and ' + 122 125 err.message +
'"gcm_user_visible_only" in the manifest?</p>' + 123 126 '</p>');
'<p>Error message: ' + 124 127 });
err.message + 125 128 } else {
'</p>'); 126 129 // Use notification permission to do something
}); 127 130 if (Notification.permission === 'denied') {
} else { 128 131 pushSwitch.disabled = true;
// Use notification permission to do something 129 132 pushSwitch.checked = false;
if (Notification.permission === 'denied') { 130 133 console.log('Ooops Notifications are Blocked',
pushSwitch.disabled = true; 131 134 'Unfortunately you just permanently blocked notifications. ' +
pushSwitch.checked = false; 132 135 'Please unblock / allow them to switch on push notifications.');
console.log('Ooops Notifications are Blocked', 133 136 } else {
'Unfortunately you just permanently blocked notifications. ' + 134 137 pushSwitch.checked = false;
'Please unblock / allow them to switch on push notifications.'); 135 138 console.log('Ooops Push Couldn\'t Register',
} else { 136 139 '<p>When we tried to ' +
pushSwitch.checked = false; 137 140 'get the subscription ID for GCM, something went wrong, not ' +
console.log('Ooops Push Couldn\'t Register', 138 141 'sure why.</p>' +
'<p>When we tried to ' + 139 142 '<p>Have you defined "gcm_sender_id" and ' +
'get the subscription ID for GCM, something went wrong, not ' + 140 143 '"gcm_user_visible_only" in the manifest?</p>' +
'sure why.</p>' + 141 144 '<p>Error message: ' +
'<p>Have you defined "gcm_sender_id" and ' + 142 145 e.message +
'"gcm_user_visible_only" in the manifest?</p>' + 143 146 '</p>');
'<p>Error message: ' + 144 147 }
e.message + 145 148 }
'</p>'); 146 149 });
} 147 150 });
} 148 151 }
}); 149 152
}); 150 153 function unsubscribeDevice() {
} 151 154 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
152 155 serviceWorkerRegistration.pushManager.getSubscription().then(
function unsubscribeDevice() { 153 156 function(pushSubscription) {
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 154 157 // Check we have everything we need to unsubscribe
serviceWorkerRegistration.pushManager.getSubscription().then( 155 158 if (!pushSubscription) {
function(pushSubscription) { 156 159 pushSwitch.checked = false;
// Check we have everything we need to unsubscribe 157 160 return;
if (!pushSubscription) { 158 161 }
pushSwitch.checked = false; 159 162
return; 160 163 // TODO: Remove the device details from the server
} 161 164 // i.e. the pushSubscription.subscriptionId and
162 165 // pushSubscription.endpoint
// TODO: Remove the device details from the server 163 166 var subscriptionId = pushSubscription.subscriptionId;
// i.e. the pushSubscription.subscriptionId and 164 167
// pushSubscription.endpoint 165 168 pushSubscription.unsubscribe().then(function(successful) {
var subscriptionId = pushSubscription.subscriptionId; 166 169 console.log('Unsubscribed from push: ', successful);
167 170
pushSubscription.unsubscribe().then(function(successful) { 168 171 if (!successful) {
console.log('Unsubscribed from push: ', successful); 169 172 // The unsubscribe was unsuccessful, but we can
170 173 // remove the subscriptionId from our server
if (!successful) { 171 174 // and notifications will stop
// The unsubscribe was unsuccessful, but we can 172 175 // This just may be in a bad state when the user returns
// remove the subscriptionId from our server 173 176 pushSwitch.checked = true;
// and notifications will stop 174 177 removeSubscription(pushSubscription);
// This just may be in a bad state when the user returns 175 178 console.error('We were unable to unregister from push, but we removed'+
pushSwitch.checked = true; 176 179 'registration id from the server');
removeSubscription(pushSubscription); 177 180 }
console.error('We were unable to unregister from push, but we removed'+ 178 181
'registration id from the server'); 179 182 }).catch(function(e) {
} 180 183 console.log('Unsubscribtion error: ', e);
181 184 });
}).catch(function(e) { 182 185 }.bind(this)).catch(function(e) {
console.log('Unsubscribtion error: ', e); 183 186 console.error('Error thrown while revoking push notifications. ' +
}); 184 187 'Most likely because push was never registered', e);
}.bind(this)).catch(function(e) { 185 188 });
console.error('Error thrown while revoking push notifications. ' + 186 189 });
'Most likely because push was never registered', e); 187 190 }
}); 188 191
}); 189 192 function permissionStatusChange(permissionStatus) {
} 190 193 console.log('permissionStatusChange = ', permissionStatus);
191 194 // If the notification permission is denied, it's a permanent block
function permissionStatusChange(permissionStatus) { 192 195 switch (permissionStatus.status) {
console.log('permissionStatusChange = ', permissionStatus); 193 196 case 'denied':
// If the notification permission is denied, it's a permanent block 194 197 pushSwitch.disabled = true;
switch (permissionStatus.status) { 195 198 console.log('Ooops Push has been Blocked',
case 'denied': 196 199 'Unfortunately the user permanently blocked push. Please unblock / ' +
pushSwitch.disabled = true; 197 200 'allow them to switch on push notifications.');
console.log('Ooops Push has been Blocked', 198 201 break;
'Unfortunately the user permanently blocked push. Please unblock / ' + 199 202 case 'granted':
'allow them to switch on push notifications.'); 200 203 // Set the state of the push switch
break; 201 204 console.log('case granted');
case 'granted': 202 205 break;
// Set the state of the push switch 203 206 case 'prompt':
console.log('case granted'); 204 207 pushSwitch.checked = false;
break; 205 208 console.log('case prompt');
case 'prompt': 206 209 break;
pushSwitch.checked = false; 207 210 }
console.log('case prompt'); 208 211 }
break; 209 212
} 210 213 function setUpPushPermission() {
} 211 214 navigator.permissions.query({name: 'push', userVisibleOnly: true})
212 215 .then(function(permissionStatus) {
function setUpPushPermission() { 213 216 // Set the initial state
navigator.permissions.query({name: 'push', userVisibleOnly: true}) 214 217 permissionStatusChange(permissionStatus);
.then(function(permissionStatus) { 215 218
// Set the initial state 216 219 // Handle Permission State Changes
permissionStatusChange(permissionStatus); 217 220 permissionStatus.onchange = function() {
218 221 permissionStatusChange(this);
// Handle Permission State Changes 219 222 };
permissionStatus.onchange = function() { 220 223
permissionStatusChange(this); 221 224 // Check if push is supported and what the current state is
}; 222 225 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
223 226 // Let's see if we have a subscription already
// Check if push is supported and what the current state is 224 227 serviceWorkerRegistration.pushManager.getSubscription()
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 225 228 .then(function(subscription) {
// Let's see if we have a subscription already 226 229 if (!subscription) {
serviceWorkerRegistration.pushManager.getSubscription() 227 230 // NOOP
.then(function(subscription) { 228 231 return;
if (!subscription) { 229 232 }
// NOOP 230 233
return; 231 234 console.log('update current state.');
} 232 235 // Update the current state with the
233 236 // subscriptionid and endpoint
console.log('update current state.'); 234 237 onPushSubscription(subscription);
// Update the current state with the 235 238 })
// subscriptionid and endpoint 236 239 .catch(function(e) {
onPushSubscription(subscription); 237 240 console.log('An error occured while calling getSubscription()', e);
}) 238 241 });
.catch(function(e) { 239 242 });
console.log('An error occured while calling getSubscription()', e); 240 243 }).catch(function(err) {
}); 241 244 console.log('Ooops Unable to check the permission',
}); 242 245 'Unfortunately the permission for push notifications couldn\'t be ' +
}).catch(function(err) { 243 246 'checked. Are you on Chrome 43+?');
console.log('Ooops Unable to check the permission', 244 247 });
'Unfortunately the permission for push notifications couldn\'t be ' + 245 248 }
'checked. Are you on Chrome 43+?'); 246 249
}); 247 250 function setUpNotificationPermission() {
} 248 251 console.log('setting notification setting');
249 252
function setUpNotificationPermission() { 250 253 if (Notification.permission === 'default') {
console.log('setting notification setting'); 251 254 console.log('notification permissions === default');
252 255 return;
if (Notification.permission === 'default') { 253 256 }
console.log('notification permissions === default'); 254 257
return; 255 258 // Check if push is supported and what the current state is
} 256 259 navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
257 260 // Let's see if we have a subscription already
// Check if push is supported and what the current state is 258 261 serviceWorkerRegistration.pushManager.getSubscription()
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { 259 262 .then(function(subscription) {
// Let's see if we have a subscription already 260 263 if (!subscription) {
serviceWorkerRegistration.pushManager.getSubscription() 261 264 // NOOP
.then(function(subscription) { 262 265 console.log('not subscription');
if (!subscription) { 263 266 return;
// NOOP 264 267 }
console.log('not subscription'); 265 268
return; 266 269 // Update the current state with the
} 267 270 // subscriptionid and endpoint
268 271 console.log('onpushsubscription should be entered');
// Update the current state with the 269 272 onPushSubscription(subscription);
// subscriptionid and endpoint 270 273 })
console.log('onpushsubscription should be entered'); 271 274 .catch(function(e) {
onPushSubscription(subscription); 272 275 console.log('An error occured while calling getSubscription()', e);
}) 273 276 });
.catch(function(e) { 274 277 });
console.log('An error occured while calling getSubscription()', e); 275 278 }
}); 276 279
}); 277 280 // Once the service worker is registered set the initial state
} 278 281 function initialiseState() {
279 282 // Check if notifications are supported
// Once the service worker is registered set the initial state 280 283 if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
function initialiseState() { 281 284 console.warn('Notifications aren\'t supported.');
// Check if notifications are supported 282 285 return;
if (!('showNotification' in ServiceWorkerRegistration.prototype)) { 283 286 }
console.warn('Notifications aren\'t supported.'); 284 287 // Check the current Notification permission.
return; 285 288 // If its denied, it's a permanent block until the
} 286 289 // user changes the permission
// Check the current Notification permission. 287 290 else if (Notification.permission === 'denied') {
// If its denied, it's a permanent block until the 288 291 console.log('Ooops Notifications are Blocked',
// user changes the permission 289 292 'Unfortunately notifications are permanently blocked. Please unblock / ' +
else if (Notification.permission === 'denied') { 290 293 'allow them to switch on push notifications.');
console.log('Ooops Notifications are Blocked', 291 294 return;
'Unfortunately notifications are permanently blocked. Please unblock / ' + 292 295 }
'allow them to switch on push notifications.'); 293 296 // Check if push messaging is supported
return; 294 297 else if (!('PushManager' in window)) {
} 295 298 console.warn('Push messaging isn\'t supported.');
// Check if push messaging is supported 296 299 return;
else if (!('PushManager' in window)) { 297 300 }
console.warn('Push messaging isn\'t supported.'); 298 301
return; 299 302 pushSwitch.disabled = false;
} 300 303 // Is the Permissions API supported
301 304 if ('permissions' in navigator) {
pushSwitch.disabled = false; 302 305 console.log('setting push permissions');
// Is the Permissions API supported 303 306 setUpPushPermission();
if ('permissions' in navigator) { 304 307 return;
console.log('setting push permissions'); 305 308 } else {
setUpPushPermission(); 306 309 console.log('setting notification permissions');
return; 307 310 setUpNotificationPermission();
} else { 308 311 }
console.log('setting notification permissions'); 309 312 }
setUpNotificationPermission(); 310 313
} 311 314 var enablePushSwitch = $('.js-checkbox');
315
} 312 316 var pushSwitch = document.getElementById("notifbox");
313 317 pushSwitch.disabled = true;
var enablePushSwitch = $('.js-checkbox'); 314 318
319 var ua = navigator.userAgent.toLowerCase();
320 var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
321
322 if(!isAndroid) {
323 // Do something!
324 // Redirect to Android-site?
325 pushSwitch.disabled = true;
326 console.log("not android");