CB-10467 Hardware back button, while InAppBrowser is opened, closes the app too in addition to closing InAppBrowser

This commit is contained in:
daserge 2016-08-16 20:10:15 +03:00
parent 3f63aaee58
commit 2776b14db6
3 changed files with 62 additions and 8 deletions

View File

@ -131,6 +131,7 @@ instance, or the system browser.
- __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally. - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
- __fullscreen__: set to `yes` to create the browser control without a border around it. Please note that if __location=no__ is also specified, there will be no control presented to user to close IAB window. - __fullscreen__: set to `yes` to create the browser control without a border around it. Please note that if __location=no__ is also specified, there will be no control presented to user to close IAB window.
- __hardwareback__: works the same way as on Android platform.
### Supported Platforms ### Supported Platforms

View File

@ -33,7 +33,8 @@ var browserWrap,
forwardButton, forwardButton,
closeButton, closeButton,
bodyOverflowStyle, bodyOverflowStyle,
navigationEventsCallback; navigationEventsCallback,
hardwareBackCallback;
// x-ms-webview is available starting from Windows 8.1 (platformId is 'windows') // x-ms-webview is available starting from Windows 8.1 (platformId is 'windows')
// http://msdn.microsoft.com/en-us/library/windows/apps/dn301831.aspx // http://msdn.microsoft.com/en-us/library/windows/apps/dn301831.aspx
@ -105,6 +106,8 @@ var IAB = {
document.body.style.msOverflowStyle = bodyOverflowStyle; document.body.style.msOverflowStyle = bodyOverflowStyle;
browserWrap = null; browserWrap = null;
popup = null; popup = null;
document.removeEventListener("backbutton", hardwareBackCallback, false);
} }
}); });
}, },
@ -176,6 +179,41 @@ var IAB = {
browserWrap.appendChild(popup); browserWrap.appendChild(popup);
var closeHandler = function (e) {
setTimeout(function () {
IAB.close(navigationEventsCallback);
}, 0);
};
if (features.indexOf("hardwareback=yes") > -1 || features.indexOf("hardwareback") === -1) {
hardwareBackCallback = function () {
if (browserWrap.style.display === 'none') {
// NOTE: backbutton handlers have to throw an exception in order to prevent
// returning 'true' inside cordova-js, which would mean that the event is handled by user.
// Throwing an exception means that the default/system navigation behavior will take place,
// which is to exit the app if the navigation stack is empty.
throw 'Exit the app';
}
if (popup.canGoBack) {
popup.goBack();
} else {
closeHandler();
}
};
} else if (features.indexOf("hardwareback=no") > -1) {
hardwareBackCallback = function () {
if (browserWrap.style.display === 'none') {
// See comment above
throw 'Exit the app';
}
closeHandler();
};
}
document.addEventListener("backbutton", hardwareBackCallback, false);
if (features.indexOf("location=yes") !== -1 || features.indexOf("location") === -1) { if (features.indexOf("location=yes") !== -1 || features.indexOf("location") === -1) {
popup.style.height = "calc(100% - 70px)"; popup.style.height = "calc(100% - 70px)";
@ -210,11 +248,7 @@ var IAB = {
closeButton = document.createElement("div"); closeButton = document.createElement("div");
closeButton.innerText = "close"; closeButton.innerText = "close";
closeButton.className = "app-bar-action action-close"; closeButton.className = "app-bar-action action-close";
closeButton.addEventListener("click", function (e) { closeButton.addEventListener("click", closeHandler);
setTimeout(function () {
IAB.close(navigationEventsCallback);
}, 0);
});
if (!isWebViewAvailable) { if (!isWebViewAvailable) {
// iframe navigation is not yet supported // iframe navigation is not yet supported

View File

@ -438,16 +438,24 @@ exports.defineManualTests = function (contentEl, createActionButton) {
'<p/> <div id="openAnchor2"></div>' + '<p/> <div id="openAnchor2"></div>' +
'Expected result: open successfully in InAppBrowser to the local page, scrolled to the beginning of the tall div with border.'; 'Expected result: open successfully in InAppBrowser to the local page, scrolled to the beginning of the tall div with border.';
var hardwareback_tests = '<h1>HardwareBack</h1>' +
'<p/> <div id="openHardwareBackDefault"></div>' +
'Expected result: By default hardwareback is yes so pressing back button should navigate backwards in history then close InAppBrowser' +
'<p/> <div id="openHardwareBackYes"></div>' +
'Expected result: hardwareback=yes pressing back button should navigate backwards in history then close InAppBrowser' +
'<p/> <div id="openHardwareBackNo"></div>' +
'Expected result: hardwareback=no pressing back button should close InAppBrowser regardless history';
// CB-7490 We need to wrap this code due to Windows security restrictions // CB-7490 We need to wrap this code due to Windows security restrictions
// see http://msdn.microsoft.com/en-us/library/windows/apps/hh465380.aspx#differences for details // see http://msdn.microsoft.com/en-us/library/windows/apps/hh465380.aspx#differences for details
if (window.MSApp && window.MSApp.execUnsafeLocalFunction) { if (window.MSApp && window.MSApp.execUnsafeLocalFunction) {
MSApp.execUnsafeLocalFunction(function() { MSApp.execUnsafeLocalFunction(function() {
contentEl.innerHTML = info_div + local_tests + white_listed_tests + non_white_listed_tests + page_with_redirects_tests + pdf_url_tests + invalid_url_tests + contentEl.innerHTML = info_div + local_tests + white_listed_tests + non_white_listed_tests + page_with_redirects_tests + pdf_url_tests + invalid_url_tests +
css_js_injection_tests + open_hidden_tests + clearing_cache_tests + video_tag_tests + local_with_anchor_tag_tests; css_js_injection_tests + open_hidden_tests + clearing_cache_tests + video_tag_tests + local_with_anchor_tag_tests + hardwareback_tests;
}); });
} else { } else {
contentEl.innerHTML = info_div + local_tests + white_listed_tests + non_white_listed_tests + page_with_redirects_tests + pdf_url_tests + invalid_url_tests + contentEl.innerHTML = info_div + local_tests + white_listed_tests + non_white_listed_tests + page_with_redirects_tests + pdf_url_tests + invalid_url_tests +
css_js_injection_tests + open_hidden_tests + clearing_cache_tests + video_tag_tests + local_with_anchor_tag_tests; css_js_injection_tests + open_hidden_tests + clearing_cache_tests + video_tag_tests + local_with_anchor_tag_tests + hardwareback_tests;
} }
document.getElementById("user-agent").textContent = navigator.userAgent; document.getElementById("user-agent").textContent = navigator.userAgent;
@ -632,4 +640,15 @@ exports.defineManualTests = function (contentEl, createActionButton) {
createActionButton('Anchor2', function () { createActionButton('Anchor2', function () {
doOpen(localhtml + '#anchor2', '_blank'); doOpen(localhtml + '#anchor2', '_blank');
}, 'openAnchor2'); }, 'openAnchor2');
// Hardwareback
createActionButton('no hardwareback (defaults to yes)', function () {
doOpen('http://cordova.apache.org', '_blank');
}, 'openHardwareBackDefault');
createActionButton('hardwareback=yes', function () {
doOpen('http://cordova.apache.org', '_blank', 'hardwareback=yes');
}, 'openHardwareBackYes');
createActionButton('hardwareback=no', function () {
doOpen('http://cordova.apache.org', '_blank', 'hardwareback=no');
}, 'openHardwareBackNo');
}; };