summaryrefslogtreecommitdiff
path: root/StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows
diff options
context:
space:
mode:
Diffstat (limited to 'StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows')
-rw-r--r--[-rwxr-xr-x]StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows/InAppBrowserProxy.js420
1 files changed, 236 insertions, 184 deletions
diff --git a/StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows/InAppBrowserProxy.js b/StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows/InAppBrowserProxy.js
index fc037bdb..23f6e547 100755..100644
--- a/StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows/InAppBrowserProxy.js
+++ b/StoneIsland/plugins/cordova-plugin-inappbrowser/src/windows/InAppBrowserProxy.js
@@ -19,13 +19,10 @@
*
*/
-/*jslint sloppy:true */
-/*global Windows:true, require, document, setTimeout, window, module */
-
-
+/* jslint sloppy:true */
+/* global Windows:true, setImmediate */
var cordova = require('cordova'),
- channel = require('cordova/channel'),
urlutil = require('cordova/urlutil');
var browserWrap,
@@ -35,11 +32,13 @@ var browserWrap,
backButton,
forwardButton,
closeButton,
- bodyOverflowStyle;
+ bodyOverflowStyle,
+ navigationEventsCallback,
+ hardwareBackCallback;
// x-ms-webview is available starting from Windows 8.1 (platformId is 'windows')
// http://msdn.microsoft.com/en-us/library/windows/apps/dn301831.aspx
-var isWebViewAvailable = cordova.platformId == 'windows';
+var isWebViewAvailable = cordova.platformId === 'windows';
function attachNavigationEvents(element, callback) {
if (isWebViewAvailable) {
@@ -48,19 +47,32 @@ function attachNavigationEvents(element, callback) {
});
element.addEventListener("MSWebViewNavigationCompleted", function (e) {
- callback({ type: e.isSuccess ? "loadstop" : "loaderror", url: e.uri}, {keepCallback: true});
+ if (e.isSuccess) {
+ callback({ type: "loadstop", url: e.uri }, { keepCallback: true });
+ } else {
+ callback({ type: "loaderror", url: e.uri, code: e.webErrorStatus, message: "Navigation failed with error code " + e.webErrorStatus}, { keepCallback: true });
+ }
});
element.addEventListener("MSWebViewUnviewableContentIdentified", function (e) {
// WebView found the content to be not HTML.
// http://msdn.microsoft.com/en-us/library/windows/apps/dn609716.aspx
- callback({ type: "loaderror", url: e.uri}, {keepCallback: true});
+ callback({ type: "loaderror", url: e.uri, code: e.webErrorStatus, message: "Navigation failed with error code " + e.webErrorStatus}, { keepCallback: true });
});
element.addEventListener("MSWebViewContentLoading", function (e) {
- if (navigationButtonsDiv) {
- backButton.disabled = !popup.canGoBack;
- forwardButton.disabled = !popup.canGoForward;
+ if (navigationButtonsDiv && popup) {
+ if (popup.canGoBack) {
+ backButton.removeAttribute("disabled");
+ } else {
+ backButton.setAttribute("disabled", "true");
+ }
+
+ if (popup.canGoForward) {
+ forwardButton.removeAttribute("disabled");
+ } else {
+ forwardButton.setAttribute("disabled", "true");
+ }
}
});
} else {
@@ -83,228 +95,266 @@ function attachNavigationEvents(element, callback) {
var IAB = {
close: function (win, lose) {
- if (browserWrap) {
- if (win) win({ type: "exit" });
+ setImmediate(function () {
+ if (browserWrap) {
+ if (navigationEventsCallback) {
+ navigationEventsCallback({ type: "exit" });
+ }
- browserWrap.parentNode.removeChild(browserWrap);
- // Reset body overflow style to initial value
- document.body.style.msOverflowStyle = bodyOverflowStyle;
- browserWrap = null;
- popup = null;
- }
+ browserWrap.parentNode.removeChild(browserWrap);
+ // Reset body overflow style to initial value
+ document.body.style.msOverflowStyle = bodyOverflowStyle;
+ browserWrap = null;
+ popup = null;
+
+ document.removeEventListener("backbutton", hardwareBackCallback, false);
+ }
+ });
},
show: function (win, lose) {
- if (browserWrap) {
- browserWrap.style.display = "block";
- }
+ setImmediate(function () {
+ if (browserWrap) {
+ browserWrap.style.display = "block";
+ }
+ });
},
open: function (win, lose, args) {
- var strUrl = args[0],
- target = args[1],
- features = args[2],
- url;
+ // make function async so that we can add navigation events handlers before view is loaded and navigation occured
+ setImmediate(function () {
+ var strUrl = args[0],
+ target = args[1],
+ features = args[2],
+ url;
- if (target === "_system") {
- url = new Windows.Foundation.Uri(strUrl);
- Windows.System.Launcher.launchUriAsync(url);
- } else if (target === "_self" || !target) {
- window.location = strUrl;
- } else {
- // "_blank" or anything else
- if (!browserWrap) {
- var browserWrapStyle = document.createElement('link');
- browserWrapStyle.rel = "stylesheet";
- browserWrapStyle.type = "text/css";
- browserWrapStyle.href = urlutil.makeAbsolute("/www/css/inappbrowser.css");
+ navigationEventsCallback = win;
- document.head.appendChild(browserWrapStyle);
+ if (target === "_system") {
+ url = new Windows.Foundation.Uri(strUrl);
+ Windows.System.Launcher.launchUriAsync(url);
+ } else if (target === "_self" || !target) {
+ window.location = strUrl;
+ } else {
+ // "_blank" or anything else
+ if (!browserWrap) {
+ var browserWrapStyle = document.createElement('link');
+ browserWrapStyle.rel = "stylesheet";
+ browserWrapStyle.type = "text/css";
+ browserWrapStyle.href = urlutil.makeAbsolute("/www/css/inappbrowser.css");
- browserWrap = document.createElement("div");
- browserWrap.className = "inAppBrowserWrap";
+ document.head.appendChild(browserWrapStyle);
- if (features.indexOf("fullscreen=yes") > -1) {
- browserWrap.classList.add("inAppBrowserWrapFullscreen");
- }
+ browserWrap = document.createElement("div");
+ browserWrap.className = "inAppBrowserWrap";
- // Save body overflow style to be able to reset it back later
- bodyOverflowStyle = document.body.style.msOverflowStyle;
+ if (features.indexOf("fullscreen=yes") > -1) {
+ browserWrap.classList.add("inAppBrowserWrapFullscreen");
+ }
- browserWrap.onclick = function () {
- setTimeout(function () {
- IAB.close(win);
- }, 0);
- };
+ // Save body overflow style to be able to reset it back later
+ bodyOverflowStyle = document.body.style.msOverflowStyle;
- document.body.appendChild(browserWrap);
- // Hide scrollbars for the whole body while inappbrowser's window is open
- document.body.style.msOverflowStyle = "none";
- }
+ browserWrap.onclick = function () {
+ setTimeout(function () {
+ IAB.close(navigationEventsCallback);
+ }, 0);
+ };
- if (features.indexOf("hidden=yes") !== -1) {
- browserWrap.style.display = "none";
- }
+ document.body.appendChild(browserWrap);
+ // Hide scrollbars for the whole body while inappbrowser's window is open
+ document.body.style.msOverflowStyle = "none";
+ }
- popup = document.createElement(isWebViewAvailable ? "x-ms-webview" : "iframe");
- if (popup instanceof HTMLIFrameElement) {
- // For iframe we need to override bacground color of parent element here
- // otherwise pages without background color set will have transparent background
- popup.style.backgroundColor = "white";
- }
- popup.style.borderWidth = "0px";
- popup.style.width = "100%";
+ if (features.indexOf("hidden=yes") !== -1) {
+ browserWrap.style.display = "none";
+ }
- browserWrap.appendChild(popup);
+ popup = document.createElement(isWebViewAvailable ? "x-ms-webview" : "iframe");
+ if (popup instanceof HTMLIFrameElement) {
+ // For iframe we need to override bacground color of parent element here
+ // otherwise pages without background color set will have transparent background
+ popup.style.backgroundColor = "white";
+ }
+ popup.style.borderWidth = "0px";
+ popup.style.width = "100%";
+ popup.style.marginBottom = "-3px";
- if (features.indexOf("location=yes") !== -1 || features.indexOf("location") === -1) {
- popup.style.height = "calc(100% - 60px)";
+ browserWrap.appendChild(popup);
- navigationButtonsDiv = document.createElement("div");
- navigationButtonsDiv.style.height = "60px";
- navigationButtonsDiv.style.backgroundColor = "#404040";
- navigationButtonsDiv.style.zIndex = "999";
- navigationButtonsDiv.onclick = function (e) {
- e.cancelBubble = true;
+ var closeHandler = function (e) {
+ setTimeout(function () {
+ IAB.close(navigationEventsCallback);
+ }, 0);
};
- navigationButtonsDivInner = document.createElement("div");
- navigationButtonsDivInner.style.paddingTop = "10px";
- navigationButtonsDivInner.style.height = "50px";
- navigationButtonsDivInner.style.width = "160px";
- navigationButtonsDivInner.style.margin = "0 auto";
- navigationButtonsDivInner.style.backgroundColor = "#404040";
- navigationButtonsDivInner.style.zIndex = "999";
- navigationButtonsDivInner.onclick = function (e) {
- e.cancelBubble = true;
- };
+ 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';
+ }
- backButton = document.createElement("button");
- backButton.style.width = "40px";
- backButton.style.height = "40px";
- backButton.style.borderRadius = "40px";
+ closeHandler();
+ };
+ }
- backButton.innerText = "<-";
- backButton.addEventListener("click", function (e) {
- if (popup.canGoBack)
- popup.goBack();
- });
+ document.addEventListener("backbutton", hardwareBackCallback, false);
- forwardButton = document.createElement("button");
- forwardButton.style.marginLeft = "20px";
- forwardButton.style.width = "40px";
- forwardButton.style.height = "40px";
- forwardButton.style.borderRadius = "40px";
+ if (features.indexOf("location=yes") !== -1 || features.indexOf("location") === -1) {
+ popup.style.height = "calc(100% - 70px)";
- forwardButton.innerText = "->";
- forwardButton.addEventListener("click", function (e) {
- if (popup.canGoForward)
- popup.goForward();
- });
+ navigationButtonsDiv = document.createElement("div");
+ navigationButtonsDiv.className = "inappbrowser-app-bar";
+ navigationButtonsDiv.onclick = function (e) {
+ e.cancelBubble = true;
+ };
- closeButton = document.createElement("button");
- closeButton.style.marginLeft = "20px";
- closeButton.style.width = "40px";
- closeButton.style.height = "40px";
- closeButton.style.borderRadius = "40px";
+ navigationButtonsDivInner = document.createElement("div");
+ navigationButtonsDivInner.className = "inappbrowser-app-bar-inner";
+ navigationButtonsDivInner.onclick = function (e) {
+ e.cancelBubble = true;
+ };
- closeButton.innerText = "x";
- closeButton.addEventListener("click", function (e) {
- setTimeout(function () {
- IAB.close(win);
- }, 0);
- });
+ backButton = document.createElement("div");
+ backButton.innerText = "back";
+ backButton.className = "app-bar-action action-back";
+ backButton.addEventListener("click", function (e) {
+ if (popup.canGoBack)
+ popup.goBack();
+ });
- if (!isWebViewAvailable) {
- // iframe navigation is not yet supported
- backButton.disabled = true;
- forwardButton.disabled = true;
- }
+ forwardButton = document.createElement("div");
+ forwardButton.innerText = "forward";
+ forwardButton.className = "app-bar-action action-forward";
+ forwardButton.addEventListener("click", function (e) {
+ if (popup.canGoForward)
+ popup.goForward();
+ });
- navigationButtonsDivInner.appendChild(backButton);
- navigationButtonsDivInner.appendChild(forwardButton);
- navigationButtonsDivInner.appendChild(closeButton);
- navigationButtonsDiv.appendChild(navigationButtonsDivInner);
+ closeButton = document.createElement("div");
+ closeButton.innerText = "close";
+ closeButton.className = "app-bar-action action-close";
+ closeButton.addEventListener("click", closeHandler);
- browserWrap.appendChild(navigationButtonsDiv);
- } else {
- popup.style.height = "100%";
- }
+ if (!isWebViewAvailable) {
+ // iframe navigation is not yet supported
+ backButton.setAttribute("disabled", "true");
+ forwardButton.setAttribute("disabled", "true");
+ }
- // start listening for navigation events
- attachNavigationEvents(popup, win);
+ navigationButtonsDivInner.appendChild(backButton);
+ navigationButtonsDivInner.appendChild(forwardButton);
+ navigationButtonsDivInner.appendChild(closeButton);
+ navigationButtonsDiv.appendChild(navigationButtonsDivInner);
- if (isWebViewAvailable) {
- strUrl = strUrl.replace("ms-appx://", "ms-appx-web://");
+ browserWrap.appendChild(navigationButtonsDiv);
+ } else {
+ popup.style.height = "100%";
+ }
+
+ // start listening for navigation events
+ attachNavigationEvents(popup, navigationEventsCallback);
+
+ if (isWebViewAvailable) {
+ strUrl = strUrl.replace("ms-appx://", "ms-appx-web://");
+ }
+ popup.src = strUrl;
}
- popup.src = strUrl;
- }
+ });
},
injectScriptCode: function (win, fail, args) {
- var code = args[0],
- hasCallback = args[1];
+ setImmediate(function () {
+ var code = args[0],
+ hasCallback = args[1];
- if (isWebViewAvailable && browserWrap && popup) {
- var op = popup.invokeScriptAsync("eval", code);
- op.oncomplete = function (e) {
- // return null if event target is unavailable by some reason
- var result = (e && e.target) ? [e.target.result] : [null];
- hasCallback && win(result);
- };
- op.onerror = function () { };
- op.start();
- }
+ if (isWebViewAvailable && browserWrap && popup) {
+ var op = popup.invokeScriptAsync("eval", code);
+ op.oncomplete = function (e) {
+ if (hasCallback) {
+ // return null if event target is unavailable by some reason
+ var result = (e && e.target) ? [e.target.result] : [null];
+ win(result);
+ }
+ };
+ op.onerror = function () { };
+ op.start();
+ }
+ });
},
injectScriptFile: function (win, fail, args) {
- var filePath = args[0],
- hasCallback = args[1];
+ setImmediate(function () {
+ var filePath = args[0],
+ hasCallback = args[1];
- if (!!filePath) {
- filePath = urlutil.makeAbsolute(filePath);
- }
+ if (!!filePath) {
+ filePath = urlutil.makeAbsolute(filePath);
+ }
- if (isWebViewAvailable && browserWrap && popup) {
- var uri = new Windows.Foundation.Uri(filePath);
- Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
- Windows.Storage.FileIO.readTextAsync(file).done(function (code) {
- var op = popup.invokeScriptAsync("eval", code);
- op.oncomplete = function(e) {
- var result = [e.target.result];
- hasCallback && win(result);
- };
- op.onerror = function () { };
- op.start();
+ if (isWebViewAvailable && browserWrap && popup) {
+ var uri = new Windows.Foundation.Uri(filePath);
+ Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).done(function (file) {
+ Windows.Storage.FileIO.readTextAsync(file).done(function (code) {
+ var op = popup.invokeScriptAsync("eval", code);
+ op.oncomplete = function(e) {
+ if (hasCallback) {
+ var result = [e.target.result];
+ win(result);
+ }
+ };
+ op.onerror = function () { };
+ op.start();
+ });
});
- });
- }
+ }
+ });
},
injectStyleCode: function (win, fail, args) {
- var code = args[0],
- hasCallback = args[1];
+ setImmediate(function () {
+ var code = args[0],
+ hasCallback = args[1];
- if (isWebViewAvailable && browserWrap && popup) {
- injectCSS(popup, code, hasCallback && win);
- }
+ if (isWebViewAvailable && browserWrap && popup) {
+ injectCSS(popup, code, hasCallback && win);
+ }
+ });
},
injectStyleFile: function (win, fail, args) {
- var filePath = args[0],
- hasCallback = args[1];
+ setImmediate(function () {
+ var filePath = args[0],
+ hasCallback = args[1];
- filePath = filePath && urlutil.makeAbsolute(filePath);
+ filePath = filePath && urlutil.makeAbsolute(filePath);
- if (isWebViewAvailable && browserWrap && popup) {
- var uri = new Windows.Foundation.Uri(filePath);
- Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) {
- return Windows.Storage.FileIO.readTextAsync(file);
- }).done(function (code) {
- injectCSS(popup, code, hasCallback && win);
- }, function () {
- // no-op, just catch an error
- });
- }
+ if (isWebViewAvailable && browserWrap && popup) {
+ var uri = new Windows.Foundation.Uri(filePath);
+ Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (file) {
+ return Windows.Storage.FileIO.readTextAsync(file);
+ }).done(function (code) {
+ injectCSS(popup, code, hasCallback && win);
+ }, function () {
+ // no-op, just catch an error
+ });
+ }
+ });
}
};
@@ -316,7 +366,9 @@ function injectCSS (webView, cssCode, callback) {
var op = webView.invokeScriptAsync("eval", evalWrapper);
op.oncomplete = function() {
- callback && callback([]);
+ if (callback) {
+ callback([]);
+ }
};
op.onerror = function () { };
op.start();