diff --git a/scripts/CardGridController.js b/scripts/CardGridController.js index 1041647..2f18a58 100644 --- a/scripts/CardGridController.js +++ b/scripts/CardGridController.js @@ -63,12 +63,14 @@ angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSoc $scope.deck_ws.onMessage(function(message) { data = JSON.parse(message.data); console.log('message', data); - card = new Flashcard(data.flashcard.id); + card = new Flashcard(data.flashcard); if (data.event_type == 'card_pulled') { $scope.deck[card.id] = card; + if ($scope.deckPullCallback) $scope.deckPullCallback(card); } if (data.event_type == 'card_unpulled') { $scope.deck[card.id] = undefined; + if ($scope.deckUnpullCallback) $scope.deckUnpullCallback(card); } if (data.event_type == 'card_hidden') { $scope.hideCardFromGrid(card); diff --git a/scripts/DeckController.js b/scripts/DeckController.js index a6177cd..4be611b 100644 --- a/scripts/DeckController.js +++ b/scripts/DeckController.js @@ -5,5 +5,8 @@ angular.module('flashy.DeckController', ['ui.router', 'ngWebSocket']). $scope.refreshLayout(); }); $scope.cards = $scope.deck; + $scope.deckPullCallback = $scope.addCardToGrid; + $scope.deckUnpullCallback = $scope.hideCardFromGrid; + } ); diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js index f5317e8..d3bb5b9 100644 --- a/scripts/SettingsController.js +++ b/scripts/SettingsController.js @@ -9,176 +9,212 @@ angular.module('flashy.SettingsController', ['ui.router']). return; } console.log('chrome'); - }); -console.log('executing things outside of module'); -var PUSH_SERVER_URL = '/app/subscribe/'; + console.log('executing things outside of module'); + var PUSH_SERVER_URL = '/app/subscribe/'; -function onPushSubscription(pushSubscription) { - console.log('pushSubscription = ', pushSubscription.endpoint); - // Here we would normally send the endpoint - // and subscription ID to our server. - // In this demo we just use send these values to - // our server via XHR which sends a push message. + function onPushSubscription(pushSubscription) { + console.log('pushSubscription = ', pushSubscription.endpoint); + // Here we would normally send the endpoint + // and subscription ID to our server. + // In this demo we just use send these values to + // our server via XHR which sends a push message. - console.log('pushSubscription: ', pushSubscription); + console.log('pushSubscription: ', pushSubscription); - // Code to handle the XHR - var formData = new FormData(); + // Code to handle the XHR + var formData = new FormData(); - var endpoint = pushSubscription.endpoint; + var endpoint = pushSubscription.endpoint; - if ('subscriptionId' in pushSubscription) { - // Make the endpoint always contain the subscriptionId - // so the server is always consistent - if (!endpoint.includes(pushSubscription.subscriptionId)) { - endpoint += '/' + pushSubscription.subscriptionId; - } - } + if ('subscriptionId' in pushSubscription) { + // Make the endpoint always contain the subscriptionId + // so the server is always consistent + if (!endpoint.includes(pushSubscription.subscriptionId)) { + endpoint += '/' + pushSubscription.subscriptionId; + } + } - formData.append('registration_id', endpoint); - console.log('registration_id: ', endpoint); + formData.append('registration_id', endpoint); + console.log('registration_id: ', endpoint); - console.log('tryna push'); + console.log('tryna push'); - var req = new XMLHttpRequest(); - req.addEventListener('load', function(response) { - console.log('response: ', response); - }); - req.addEventListener('error', function(error) { - console.log('error: ', error); - }); - req.open('POST', PUSH_SERVER_URL); - req.send(formData); - console.log('pushed?'); + var req = new XMLHttpRequest(); + req.addEventListener('load', function(response) { + console.log('response: ', response); + }); + req.addEventListener('error', function(error) { + console.log('error: ', error); + }); + req.open('POST', PUSH_SERVER_URL); + req.send(formData); + console.log('pushed?'); -} + } -function subscribeDevice() { - // We need the service worker registration to access the push manager - navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { - serviceWorkerRegistration.pushManager.subscribe() - .then(onPushSubscription) - .catch(function(e) { - console.log('Error in subscribing'); - // Check for a permission prompt issue - if ('permissions' in navigator) { - navigator.permissions.query({name: 'push', userVisibleOnly: true}) - .then(function(permissionStatus) { - console.log('subscribe() Error: Push permission status = ', - permissionStatus); - if (permissionStatus.status === 'denied') { - // The user blocked the permission prompt + function subscribeDevice() { + // We need the service worker registration to access the push manager + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + serviceWorkerRegistration.pushManager.subscribe() + .then(onPushSubscription) + .catch(function(e) { + console.log('Error in subscribing'); + // Check for a permission prompt issue + if ('permissions' in navigator) { + navigator.permissions.query({name: 'push', userVisibleOnly: true}) + .then(function(permissionStatus) { + console.log('subscribe() Error: Push permission status = ', + permissionStatus); + if (permissionStatus.status === 'denied') { + // The user blocked the permission prompt + console.log('Ooops Notifications are Blocked', + 'Unfortunately you just permanently blocked notifications. ' + + 'Please unblock / allow them to switch on push ' + + 'notifications.'); + } else { + console.log('Ooops Push Couldn\'t Register', + '

When we tried to ' + + 'get the subscription ID for GCM, something went wrong,' + + ' not sure why.

' + + '

Have you defined "gcm_sender_id" and ' + + '"gcm_user_visible_only" in the manifest?

' + + '

Error message: ' + + e.message + + '

'); + } + }).catch(function(err) { + console.log('Ooops Push Couldn\'t Register', + '

When we tried to ' + + 'get the subscription ID for GCM, something went wrong, not ' + + 'sure why.

' + + '

Have you defined "gcm_sender_id" and ' + + '"gcm_user_visible_only" in the manifest?

' + + '

Error message: ' + + err.message + + '

'); + }); + } else { + // Use notification permission to do something + if (Notification.permission === 'denied') { console.log('Ooops Notifications are Blocked', 'Unfortunately you just permanently blocked notifications. ' + - 'Please unblock / allow them to switch on push ' + - 'notifications.'); + 'Please unblock / allow them to switch on push notifications.'); } else { console.log('Ooops Push Couldn\'t Register', '

When we tried to ' + - 'get the subscription ID for GCM, something went wrong,' + - ' not sure why.

' + + 'get the subscription ID for GCM, something went wrong, not ' + + 'sure why.

' + '

Have you defined "gcm_sender_id" and ' + '"gcm_user_visible_only" in the manifest?

' + '

Error message: ' + e.message + '

'); } - }).catch(function(err) { - console.log('Ooops Push Couldn\'t Register', - '

When we tried to ' + - 'get the subscription ID for GCM, something went wrong, not ' + - 'sure why.

' + - '

Have you defined "gcm_sender_id" and ' + - '"gcm_user_visible_only" in the manifest?

' + - '

Error message: ' + - err.message + - '

'); - }); - } else { - // Use notification permission to do something - if (Notification.permission === 'denied') { - console.log('Ooops Notifications are Blocked', - 'Unfortunately you just permanently blocked notifications. ' + - 'Please unblock / allow them to switch on push notifications.'); - } else { - console.log('Ooops Push Couldn\'t Register', - '

When we tried to ' + - 'get the subscription ID for GCM, something went wrong, not ' + - 'sure why.

' + - '

Have you defined "gcm_sender_id" and ' + - '"gcm_user_visible_only" in the manifest?

' + - '

Error message: ' + - e.message + - '

'); - } - } + } + }); }); - }); -} + } -function unsubscribeDevice() { - navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { - serviceWorkerRegistration.pushManager.getSubscription().then( - function(pushSubscription) { - // Check we have everything we need to unsubscribe - if (!pushSubscription) { - return; - } + function unsubscribeDevice() { + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + serviceWorkerRegistration.pushManager.getSubscription().then( + function(pushSubscription) { + // Check we have everything we need to unsubscribe + if (!pushSubscription) { + return; + } - // TODO: Remove the device details from the server - // i.e. the pushSubscription.subscriptionId and - // pushSubscription.endpoint + // TODO: Remove the device details from the server + // i.e. the pushSubscription.subscriptionId and + // pushSubscription.endpoint - pushSubscription.unsubscribe().then(function(successful) { - console.log('Unsubscribed from push: ', successful); - if (!successful) { - // The unsubscribe was unsuccessful, but we can - // remove the subscriptionId from our server - // and notifications will stop - // This just may be in a bad state when the user returns - console.error('We were unable to unregister from push'); - } + pushSubscription.unsubscribe().then(function(successful) { + console.log('Unsubscribed from push: ', successful); + if (!successful) { + // The unsubscribe was unsuccessful, but we can + // remove the subscriptionId from our server + // and notifications will stop + // This just may be in a bad state when the user returns + console.error('We were unable to unregister from push'); + } - }).catch(function(e) { - console.log('Unsubscribtion error: ', e); - }); - }.bind(this)).catch(function(e) { - console.error('Error thrown while revoking push notifications. ' + - 'Most likely because push was never registered', e); + }).catch(function(e) { + console.log('Unsubscribtion error: ', e); + }); + }.bind(this)).catch(function(e) { + console.error('Error thrown while revoking push notifications. ' + + 'Most likely because push was never registered', e); + }); }); - }); -} + } + + function permissionStatusChange(permissionStatus) { + console.log('permissionStatusChange = ', permissionStatus); + // If the notification permission is denied, it's a permanent block + switch (permissionStatus.status) { + case 'denied': + console.log('Ooops Push has been Blocked', + 'Unfortunately the user permanently blocked push. Please unblock / ' + + 'allow them to switch on push notifications.'); + break; + case 'granted': + // Set the state of the push switch + console.log('case granted'); + break; + case 'prompt': + console.log('case prompt'); + break; + } + } -function permissionStatusChange(permissionStatus) { - console.log('permissionStatusChange = ', permissionStatus); - // If the notification permission is denied, it's a permanent block - switch (permissionStatus.status) { - case 'denied': - console.log('Ooops Push has been Blocked', - 'Unfortunately the user permanently blocked push. Please unblock / ' + - 'allow them to switch on push notifications.'); - break; - case 'granted': - // Set the state of the push switch - console.log('case granted'); - break; - case 'prompt': - console.log('case prompt'); - break; - } -} + function setUpPushPermission() { + navigator.permissions.query({name: 'push', userVisibleOnly: true}) + .then(function(permissionStatus) { + // Set the initial state + permissionStatusChange(permissionStatus); -function setUpPushPermission() { - navigator.permissions.query({name: 'push', userVisibleOnly: true}) - .then(function(permissionStatus) { - // Set the initial state - permissionStatusChange(permissionStatus); + // Handle Permission State Changes + permissionStatus.onchange = function() { + permissionStatusChange(this); + }; + + // Check if push is supported and what the current state is + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + // Let's see if we have a subscription already + serviceWorkerRegistration.pushManager.getSubscription() + .then(function(subscription) { + if (!subscription) { + // NOOP + return; + } + + // Update the current state with the + // subscriptionid and endpoint + onPushSubscription(subscription); + }) + .catch(function(e) { + console.log('An error occured while calling getSubscription()', e); + }); + }); + }).catch(function(err) { + console.log('Ooops Unable to check the permission', + 'Unfortunately the permission for push notifications couldn\'t be ' + + 'checked. Are you on Chrome 43+?'); + }); + } - // Handle Permission State Changes - permissionStatus.onchange = function() { - permissionStatusChange(this); - }; + function setUpNotificationPermission() { + // If the notification permission is denied, it's a permanent block + if (Notification.permission === 'denied') { + console.log('Ooops Notifications are Blocked', + 'Unfortunately notifications are permanently blocked. Please unblock / ' + + 'allow them to switch on push notifications.'); + return; + } else if (Notification.permission === 'default') { + console.log('notification permissions === default'); + return; + } // Check if push is supported and what the current state is navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { @@ -187,93 +223,57 @@ function setUpPushPermission() { .then(function(subscription) { if (!subscription) { // NOOP + console.log('not subscription'); return; } // Update the current state with the // subscriptionid and endpoint + console.log('onpushsubscription should be entered'); onPushSubscription(subscription); }) .catch(function(e) { console.log('An error occured while calling getSubscription()', e); }); }); - }).catch(function(err) { - console.log('Ooops Unable to check the permission', - 'Unfortunately the permission for push notifications couldn\'t be ' + - 'checked. Are you on Chrome 43+?'); - }); -} - -function setUpNotificationPermission() { - // If the notification permission is denied, it's a permanent block - if (Notification.permission === 'denied') { - console.log('Ooops Notifications are Blocked', - 'Unfortunately notifications are permanently blocked. Please unblock / ' + - 'allow them to switch on push notifications.'); - return; - } else if (Notification.permission === 'default') { - console.log('notification permissions === default'); - return; - } + } - // Check if push is supported and what the current state is - navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { - // Let's see if we have a subscription already - serviceWorkerRegistration.pushManager.getSubscription() - .then(function(subscription) { - if (!subscription) { - // NOOP - console.log('not subscription'); - return; - } +// Once the service worker is registered set the initial state + function initialiseState() { + // Is the Permissions API supported + if ('permissions' in navigator) { + console.log('setting push permissions'); + setUpPushPermission(); + return; + } else { + console.log('setting notification permissions'); + setUpNotificationPermission(); + } + } - // Update the current state with the - // subscriptionid and endpoint - console.log('onpushsubscription should be entered'); - onPushSubscription(subscription); - }) - .catch(function(e) { - console.log('An error occured while calling getSubscription()', e); - }); - }); -} + var enablePushSwitch = $('.js-checkbox'); + enablePushSwitch.change(function(e) { + console.log('checkbox changed'); + if (e.target.checked) { + console.log('subscribing device'); + subscribeDevice(); + } else { + console.log('unsubscribing device'); + unsubscribeDevice(); + } + }); -// Once the service worker is registered set the initial state -function initialiseState() { - // Is the Permissions API supported - if ('permissions' in navigator) { - console.log('setting push permissions'); - setUpPushPermission(); - return; - } else { - console.log('setting notification permissions'); - setUpNotificationPermission(); - } -} -window.addEventListener('load', function() { - // When the toggle switch changes, enabled / disable push - // messaging - var enablePushSwitch = document.querySelector('.js-checkbox'); - enablePushSwitch.addEventListener('change', function(e) { - if (e.target.checked) { - subscribeDevice(); + // Check that service workers are supported + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('scripts/service-worker.js') + .then(initialiseState); } else { - unsubscribeDevice(); + // Service Workers aren't supported so you should hide the push UI + // If it's currently visible. + console.log('Ooops Service Workers aren\'t Supported', + 'Service Workers aren\'t supported in this browser. ' + + 'For this demo be sure to use ' + + 'Chrome Canary' + + ' or version 42.'); } }); - - // Check that service workers are supported - if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('scripts/service-worker.js') - .then(initialiseState); - } else { - // Service Workers aren't supported so you should hide the push UI - // If it's currently visible. - console.log('Ooops Service Workers aren\'t Supported', - 'Service Workers aren\'t supported in this browser. ' + - 'For this demo be sure to use ' + - 'Chrome Canary' + - ' or version 42.'); - } -});