diff --git a/Gruntfile.js b/Gruntfile.js index 908d72a5..0790a29d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -43,6 +43,7 @@ module.exports = function(grunt) { 'src/hello.polyfill.js', 'src/hello.js', 'src/hello.chromeapp.js', + 'src/hello.phonegap.js', 'src/modules/dropbox.js', 'src/modules/facebook.js', 'src/modules/flickr.js', diff --git a/src/hello.js b/src/hello.js index 7235fa8a..d8fd41e0 100644 --- a/src/hello.js +++ b/src/hello.js @@ -1259,102 +1259,6 @@ hello.utils.extend(hello.utils, { optionsArray.push(name + (value !== null ? '=' + value : '')); }); - // Create a function for reopening the popup, and assigning events to the new popup object - // This is a fix whereby triggering the - var open = function(url) { - - // Trigger callback - var popup = window.open( - url, - '_blank', - optionsArray.join(',') - ); - - // PhoneGap support - // Add an event listener to listen to the change in the popup windows URL - // This must appear before popup.focus(); - try { - if (popup && popup.addEventListener) { - - // Get the origin of the redirect URI - - var a = hello.utils.url(redirectUri); - var redirectUriOrigin = a.origin || (a.protocol + '//' + a.hostname); - - // Listen to changes in the InAppBrowser window - - popup.addEventListener('loadstart', function(e) { - - var url = e.url; - - // Is this the path, as given by the redirectUri? - // Check the new URL agains the redirectUriOrigin. - // According to #63 a user could click 'cancel' in some dialog boxes .... - // The popup redirects to another page with the same origin, yet we still wish it to close. - - if (url.indexOf(redirectUriOrigin) !== 0) { - return; - } - - // Split appart the URL - var a = hello.utils.url(url); - - // We dont have window operations on the popup so lets create some - // The location can be augmented in to a location object like so... - - var _popup = { - location: { - // Change the location of the popup - assign: function(location) { - - // Unfourtunatly an app is may not change the location of a InAppBrowser window. - // So to shim this, just open a new one. - - popup.addEventListener('exit', function() { - - // For some reason its failing to close the window if a new window opens too soon. - - setTimeout(function() { - open(location); - }, 1000); - }); - }, - - search: a.search, - hash: a.hash, - href: a.href - }, - close: function() { - if (popup.close) { - popup.close(); - } - } - }; - - // Then this URL contains information which HelloJS must process - // URL string - // Window - any action such as window relocation goes here - // Opener - the parent window which opened this, aka this script - - hello.utils.responseHandler(_popup, window); - - // Always close the popup regardless of whether the hello.utils.responseHandler detects a state parameter or not in the querystring. - // Such situations might arise such as those in #63 - - _popup.close(); - - }); - } - } - catch (e) {} - - if (popup && popup.focus) { - popup.focus(); - } - - return popup; - }; - // Call the open() function with the initial path // // OAuth redirect, fixes URI fragments from being lost in Safari @@ -1368,7 +1272,17 @@ hello.utils.extend(hello.utils, { url = redirectUri + '#oauth_redirect=' + encodeURIComponent(encodeURIComponent(url)); } - return open(url); + var popup = window.open( + url, + '_blank', + optionsArray.join(',') + ); + + if (popup && popup.focus) { + popup.focus(); + } + + return popup; }, // OAuth and API response handler diff --git a/src/hello.phonegap.js b/src/hello.phonegap.js index 903b9a08..da9de149 100644 --- a/src/hello.phonegap.js +++ b/src/hello.phonegap.js @@ -11,4 +11,87 @@ hello.utils.iframe = function(url, redirectUri) { hello.utils.popup(url, redirectUri, {hidden: 'yes'}); }; + + // Augment the popup + var utilPopup = hello.utils.popup; + + // Replace popup + hello.utils.popup = function(url, redirectUri, options) { + + // Run the standard + var popup = utilPopup.call(this, url, redirectUri, options); + + // Create a function for reopening the popup, and assigning events to the new popup object + // PhoneGap support + // Add an event listener to listen to the change in the popup windows URL + // This must appear before popup.focus(); + try { + if (popup && popup.addEventListener) { + + // Get the origin of the redirect URI + + var a = hello.utils.url(redirectUri); + var redirectUriOrigin = a.origin || (a.protocol + '//' + a.hostname); + + // Listen to changes in the InAppBrowser window + + popup.addEventListener('loadstart', function(e) { + + var url = e.url; + + // Is this the path, as given by the redirectUri? + // Check the new URL agains the redirectUriOrigin. + // According to #63 a user could click 'cancel' in some dialog boxes .... + // The popup redirects to another page with the same origin, yet we still wish it to close. + + if (url.indexOf(redirectUriOrigin) !== 0) { + return; + } + + // Split appart the URL + var a = hello.utils.url(url); + + // We dont have window operations on the popup so lets create some + // The location can be augmented in to a location object like so... + + var _popup = { + location: { + // Change the location of the popup + assign: function(location) { + + // Unfourtunatly an app is may not change the location of a InAppBrowser window. + // So to shim this, just open a new one. + popup.executeScript({code: 'window.location.href = "' + location + ';"'}); + }, + + search: a.search, + hash: a.hash, + href: a.href + }, + close: function() { + if (popup.close) { + popup.close(); + try { + popup.closed = true; + } + catch (_e) {} + } + } + }; + + // Then this URL contains information which HelloJS must process + // URL string + // Window - any action such as window relocation goes here + // Opener - the parent window which opened this, aka this script + + hello.utils.responseHandler(_popup, window); + + }); + } + } + catch (e) {} + + return popup; + }; + })();