/**
 *  Register our Service Worker.
 *
 *  To start using our service worker, we first need to register it. Make sure
 *  that the service worker file is at the root url of your project, e.g.
 *  `/service-worker.js`, so that it can control all the pages in your app.
 *
 *  But before we do all of this, we first check whether or not the browser
 *  supports service workers or not. Make sure that the app is still usable
 *  even on browsers that do not support service workers, and just enhance the
 *  experience for those who do. This is called "Progressive Enhancement".
 **/

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
        console.info('Service worker successfully registered.');
    }).catch(function(error) {
        console.error('Failed to register service worker', error);
    });
}





/**
 *  Handle push notifications subscriptions.
 *
 *  When the subscription button is clicked, we first retrieve the user's
 *  subscription status (whether user is subscribed already or not yet) and
 *  then perform the appropriate action based on that.
 *
 *  We also update the label of the button after the user subscribes to or
 *  unsubscribes from push notifications.
 **/

$('.subscription-button').on('click', function() {
    if (!supportsPushNotifications()) {
        return false;
    }
    var subscriptionButton = $(this);

    navigator.serviceWorker.getRegistration().then(function(registration) {
        if (registration !== undefined) {
            subscriptionButton.prop('disabled', true);

            registration.pushManager.getSubscription().then(function(subscription) {
                if (subscription === null) {
                    return subscribeToPushNotifications(registration);
                }
                return unsubscribeFromPushNotifications(subscription);
            }).then(function() {
                subscriptionButton.prop('disabled', false);
            }).catch(function(error) {
                console.error('Failed to get push notification subscription.', error);
            });
        }
    }).catch(function(error) {
        console.error('Failed to get service worker registration.', error);
    });
});


function subscribeToPushNotifications(registration) {
    return registration.pushManager.subscribe({ userVisibleOnly: true }).then(function(subscription) {
        changeSubscribeToUnsubscribe();
        return storeSubscriptionInServerStorage(subscription);
    }).catch(function(error) {
        console.error('Failed to subscribe to push notifications.', error);
    });
}


function unsubscribeFromPushNotifications(subscription) {
    return subscription.unsubscribe().then(function() {
        changeUnsubscribeToSubscribe();
        return deleteSubscriptionFromServerStorage(subscription);
    }).catch(function(error) {
        console.error('Failed to unsubscribe from push notifications.', error);
    });
}

@@ -2951,28 +2951,1843 @@
 ations.', error);%0A    %7D);%0A%7D%0A
+%0A%0A%0A%0A%0A/**%0A *  Handle message transmissions through background sync.%0A *%0A *  If the browser supports background sync, we disable the current%0A *  transmission form behavior (send transmission to server through AJAX),%0A *  modify it to save the message to IndexedDB, and then register for a%0A *  background sync.%0A *%0A *  If background sync is not supported or when registering for background sync%0A *  fails, fallback to the AJAX way of sending the transmission to the server.%0A **/%0A%0Aif (supportsBackgroundSync()) %7B%0A    $('.transmit-form').off();%0A%0A    $('.transmit-form').on('submit', function(e) %7B%0A        e.preventDefault();%0A%0A        var name = localStorage.getItem('name');%0A        var message = $('.transmit-form input').val().trim();%0A%0A        if (name.length %3E 0 && message.length %3E 0) %7B%0A            disableTransmissionForm();%0A%0A            var transmission = %7B jedi: name, message: message %7D;%0A            saveTransmissionToIndexedDB(transmission)%0A                .then(registerForBackgroundSync)%0A                .then(enableTransmissionForm)%0A                .then(resetTransmissionForm)%0A                .catch(function(error) %7B%0A                    sendTransmissionToServer(transmission)%0A                        .then(enableTransmissionForm)%0A                        .then(resetTransmissionForm)%0A                        .catch(handleTransmissionFailure);%0A                %7D);%0A        %7D%0A    %7D);%0A%7D%0A%0A%0Afunction registerForBackgroundSync() %7B%0A    return navigator.serviceWorker.getRegistration().then(function(registration) %7B%0A        if (registration !== undefined) %7B%0A            return registration.sync.register('transmit-message');%0A        %7D%0A        throw new Error('No service worker registered yet.');%0A    %7D);%0A%7D%0A%0A%0Afunction supportsBackgroundSync() %7B%0A    return ('serviceWorker' in navigator) && ('SyncManager' in window);%0A%7D%0A

save new transmissions to indexeddb instead of sending to server