/* Javascript for the Skytap XBlock. */

function SkytapXBlock(runtime, element) {
    "use strict";

    // Set up gettext in case it isn't available in the client runtime:
    if (typeof gettext == 'undefined') {
        window.gettext = function gettext_stub(string) { return string; };
        window.ngettext = function ngettext_stub(strA, strB, n) { return n == 1 ? strA : strB; };
    }

    var launchForm = $('.skytap-launch-form', element),
        launchButton = launchForm.find('.skytap-launch'),
        launchSpinner = launchForm.find('.skytap-spinner'),
        launchXHR;

    // Prepare UI
    launchSpinner.hide();

    // Set up click handler for button that allows learners to launch exercise environment
    launchButton.on('click', function(e) {
        e.preventDefault();

        var handlerUrl = runtime.handlerUrl(element, 'launch'),
            keyboardLayout = launchForm.find('select').val();

        if (launchXHR) {
            launchXHR.abort();
        }

        launchSpinner.show();
        launchButton.prop('disabled', true);

        launchXHR = $.post(handlerUrl, JSON.stringify(keyboardLayout))
            .success(function(response) {
                var url = response.sharing_portal_url;
                var sharingPortal = window.open(url, '_blank');
                if (sharingPortal === null) {
                    alert(
                        gettext("The browser's popup blocker prevented the exercise environment from being launched.")
                    );
                } else {
                    sharingPortal.focus();
                }
            })
            .error(function(jqXHR, textStatus, errorThrown) {
                var error;
                if (jqXHR.hasOwnProperty('responseJSON') && jqXHR.responseJSON.hasOwnProperty('error')) {
                    error = jqXHR.responseJSON.error;
                } else {
                    error = gettext('An unknown error occurred while launching.');
                }
                $('#skytap-error-message').text('Error: ' + error);
            })
            .complete(function() {
                launchSpinner.hide();
                launchButton.prop('disabled', false);
            });

    });

}

@@ -1228,16 +1228,761 @@
 al_url;%0A
+%0A                /*%0A                 The standard behaviour is to open the exercise environment in a new tab using a popup,%0A                 to allow the user to keep the course tab open too. However on iOS devices, popups are%0A                 blocked by default and the user is not informed that the popup failed to open.%0A                 Therefore for iOS devices a redirect is used instead.%0A                 */%0A                var iOS = navigator.userAgent.match(/(iPod%7CiPhone%7CiPad)/);%0A                if (iOS) %7B%0A                    // simply redirect for mobile Safari%0A                    window.location = url;%0A                %7D else %7B%0A                    // desktop browsers offer an easy way to allow the popup so being blocked is ok%0A    
         
@@ -2049,24 +2049,28 @@
             
+ 
+   
 if (sharingP
@@ -2077,17 +2077,16 @@
 ortal ==
-=
  null) %7B
@@ -2110,15 +2110,235 @@
     
+   
-alert(%0A
+ // use '==' instead of '===' to test for both null (from desktop browsers blocking the popup)%0A                        // and also undefined (from mobile Safari refusing to show the popup)%0A                        alert(%0A    
     
@@ -2472,19 +2472,27 @@
         
+   
+ 
 );%0A
+    
         
@@ -2528,16 +2528,20 @@
         
+    
 sharingP
@@ -2551,24 +2551,46 @@
 al.focus();%0A
+                    %7D%0A
             

Change the launch behaviour to redirect iOS users to the sharing portal rather than open a popup as popups are silently blocked by default in mobile safari