From 09e0226fef66e2e4b716a47286fde450d638aaea Mon Sep 17 00:00:00 2001 From: Andrew Dodson Date: Tue, 2 Feb 2016 20:24:54 +0000 Subject: [PATCH 1/3] Extract phonegap augmentation out --- src/hello.js | 108 +++++------------------------------------- src/hello.phonegap.js | 88 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 97 deletions(-) diff --git a/src/hello.js b/src/hello.js index 944e83ed..d86ecd8f 100644 --- a/src/hello.js +++ b/src/hello.js @@ -1260,102 +1260,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 @@ -1369,7 +1273,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..a2345919 100644 --- a/src/hello.phonegap.js +++ b/src/hello.phonegap.js @@ -11,4 +11,92 @@ 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) { + + // utilPopup + 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(); + } + } + }; + + // 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; + }; + })(); From 30e1d62bf73a8a2100af17bdfb739b4953d93bd1 Mon Sep 17 00:00:00 2001 From: Andrew Dodson Date: Tue, 2 Feb 2016 23:11:20 +0000 Subject: [PATCH 2/3] Refactor the phonegap logic --- Gruntfile.js | 1 + src/hello.phonegap.js | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) 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.phonegap.js b/src/hello.phonegap.js index a2345919..760b5dba 100644 --- a/src/hello.phonegap.js +++ b/src/hello.phonegap.js @@ -18,7 +18,7 @@ // Replace popup hello.utils.popup = function(url, redirectUri, options) { - // utilPopup + // 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 @@ -61,7 +61,7 @@ // 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+';"'}); + popup.executeScript({code: 'window.location.href = "' + location + ';"'}); }, search: a.search, @@ -71,6 +71,9 @@ close: function() { if (popup.close) { popup.close(); + try { + popup.closed = true; + } catch (_e) {} } } }; @@ -82,20 +85,11 @@ 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; }; From be6fa556e3f9628e9b08cde4c7caeb8f04053af1 Mon Sep 17 00:00:00 2001 From: Andrew Dodson Date: Fri, 12 Feb 2016 22:20:58 +0000 Subject: [PATCH 3/3] lint --- src/hello.phonegap.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hello.phonegap.js b/src/hello.phonegap.js index 760b5dba..da9de149 100644 --- a/src/hello.phonegap.js +++ b/src/hello.phonegap.js @@ -73,7 +73,8 @@ popup.close(); try { popup.closed = true; - } catch (_e) {} + } + catch (_e) {} } } };