Repository: cordova-plugin-inappbrowser
Updated Branches:
  refs/heads/master 73ed40fe0 -> b024104a5


CB-10451 InAppBrowser: loadstart event is not triggered on Windows
CB-10452 InAppBrowser: 'exit' event is not triggered on Windows
CB-10454 InAppBrowser: 'loaderror' event does not have code and message on 
Windows
CB-10450 InAppBrowser: Unable to get property 'canGoBack' of undefined on 
Windows

github close #145


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/commit/b024104a
Tree: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/tree/b024104a
Diff: 
http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/diff/b024104a

Branch: refs/heads/master
Commit: b024104a54e50cc404d156274012b7665c42f876
Parents: 73ed40f
Author: sgrebnov <v-seg...@microsoft.com>
Authored: Thu Jan 28 19:08:27 2016 +0300
Committer: sgrebnov <v-seg...@microsoft.com>
Committed: Mon Feb 1 14:24:55 2016 +0300

----------------------------------------------------------------------
 src/windows/InAppBrowserProxy.js | 371 ++++++++++++++++++----------------
 1 file changed, 197 insertions(+), 174 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser/blob/b024104a/src/windows/InAppBrowserProxy.js
----------------------------------------------------------------------
diff --git a/src/windows/InAppBrowserProxy.js b/src/windows/InAppBrowserProxy.js
index 962c05e..f6d1da6 100644
--- a/src/windows/InAppBrowserProxy.js
+++ b/src/windows/InAppBrowserProxy.js
@@ -25,7 +25,6 @@
 
 
 var cordova = require('cordova'),
-    channel = require('cordova/channel'),
     urlutil = require('cordova/urlutil');
 
 var browserWrap,
@@ -35,11 +34,12 @@ var browserWrap,
     backButton,
     forwardButton,
     closeButton,
-    bodyOverflowStyle;
+    bodyOverflowStyle,
+    navigationEventsCallback;
 
 // 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,17 +48,21 @@ 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) {
+            if (navigationButtonsDiv && popup) {
                 if (popup.canGoBack) {
                     backButton.removeAttribute("disabled");
                 } else {
@@ -92,209 +96,228 @@ function attachNavigationEvents(element, callback) {
 
 var IAB = {
     close: function (win, lose) {
-        if (browserWrap) {
-            if (win) win({ type: "exit" });
-
-            browserWrap.parentNode.removeChild(browserWrap);
-            // Reset body overflow style to initial value
-            document.body.style.msOverflowStyle = bodyOverflowStyle;
-            browserWrap = null;
-            popup = null;
-        }
+        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;
+            }
+        });
     },
     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;
-
-        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");
-
-                document.head.appendChild(browserWrapStyle);
-
-                browserWrap = document.createElement("div");
-                browserWrap.className = "inAppBrowserWrap";
-
-                if (features.indexOf("fullscreen=yes") > -1) {
-                    browserWrap.classList.add("inAppBrowserWrapFullscreen");
-                }
+        // 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;
+
+            navigationEventsCallback = win;
+
+            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");
 
-                // Save body overflow style to be able to reset it back later
-                bodyOverflowStyle = document.body.style.msOverflowStyle;
+                    document.head.appendChild(browserWrapStyle);
 
-                browserWrap.onclick = function () {
-                    setTimeout(function () {
-                        IAB.close(win);
-                    }, 0);
-                };
+                    browserWrap = document.createElement("div");
+                    browserWrap.className = "inAppBrowserWrap";
 
-                document.body.appendChild(browserWrap);
-                // Hide scrollbars for the whole body while inappbrowser's 
window is open
-                document.body.style.msOverflowStyle = "none";
-            }
+                    if (features.indexOf("fullscreen=yes") > -1) {
+                        
browserWrap.classList.add("inAppBrowserWrapFullscreen");
+                    }
 
-            if (features.indexOf("hidden=yes") !== -1) {
-                browserWrap.style.display = "none";
-            }
+                    // Save body overflow style to be able to reset it back 
later
+                    bodyOverflowStyle = document.body.style.msOverflowStyle;
 
-            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%";
+                    browserWrap.onclick = function () {
+                        setTimeout(function () {
+                            IAB.close(navigationEventsCallback);
+                        }, 0);
+                    };
 
-            browserWrap.appendChild(popup);
+                    document.body.appendChild(browserWrap);
+                    // Hide scrollbars for the whole body while inappbrowser's 
window is open
+                    document.body.style.msOverflowStyle = "none";
+                }
 
-            if (features.indexOf("location=yes") !== -1 || 
features.indexOf("location") === -1) {
-                popup.style.height = "calc(100% - 70px)";
+                if (features.indexOf("hidden=yes") !== -1) {
+                    browserWrap.style.display = "none";
+                }
 
-                navigationButtonsDiv = document.createElement("div");
-                navigationButtonsDiv.className = "inappbrowser-app-bar";
-                navigationButtonsDiv.onclick = function (e) {
-                    e.cancelBubble = true;
-                };
+                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%";
 
-                navigationButtonsDivInner = document.createElement("div");
-                navigationButtonsDivInner.className = 
"inappbrowser-app-bar-inner"
-                navigationButtonsDivInner.onclick = function (e) {
-                    e.cancelBubble = true;
-                };
+                browserWrap.appendChild(popup);
 
-                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 (features.indexOf("location=yes") !== -1 || 
features.indexOf("location") === -1) {
+                    popup.style.height = "calc(100% - 70px)";
 
-                forwardButton = document.createElement("div");
-                forwardButton.innerText = "forward";
-                forwardButton.className = "app-bar-action action-forward";
-                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("div");
-                closeButton.innerText = "close";
-                closeButton.className = "app-bar-action action-close";
-                closeButton.addEventListener("click", function (e) {
-                    setTimeout(function () {
-                        IAB.close(win);
-                    }, 0);
-                });
+                    navigationButtonsDivInner = document.createElement("div");
+                    navigationButtonsDivInner.className = 
"inappbrowser-app-bar-inner"
+                    navigationButtonsDivInner.onclick = function (e) {
+                        e.cancelBubble = true;
+                    };
 
-                if (!isWebViewAvailable) {
-                    // iframe navigation is not yet supported
-                    backButton.setAttribute("disabled", "true");
-                    forwardButton.setAttribute("disabled", "true");
+                    backButton = document.createElement("div");
+                    backButton.innerText = "back";
+                    backButton.className = "app-bar-action action-back";
+                    backButton.addEventListener("click", function (e) {
+                        if (popup.canGoBack)
+                            popup.goBack();
+                    });
+
+                    forwardButton = document.createElement("div");
+                    forwardButton.innerText = "forward";
+                    forwardButton.className = "app-bar-action action-forward";
+                    forwardButton.addEventListener("click", function (e) {
+                        if (popup.canGoForward)
+                            popup.goForward();
+                    });
+
+                    closeButton = document.createElement("div");
+                    closeButton.innerText = "close";
+                    closeButton.className = "app-bar-action action-close";
+                    closeButton.addEventListener("click", function (e) {
+                        setTimeout(function () {
+                            IAB.close(navigationEventsCallback);
+                        }, 0);
+                    });
+
+                    if (!isWebViewAvailable) {
+                        // iframe navigation is not yet supported
+                        backButton.setAttribute("disabled", "true");
+                        forwardButton.setAttribute("disabled", "true");
+                    }
+
+                    navigationButtonsDivInner.appendChild(backButton);
+                    navigationButtonsDivInner.appendChild(forwardButton);
+                    navigationButtonsDivInner.appendChild(closeButton);
+                    
navigationButtonsDiv.appendChild(navigationButtonsDivInner);
+
+                    browserWrap.appendChild(navigationButtonsDiv);
+                } else {
+                    popup.style.height = "100%";
                 }
 
-                navigationButtonsDivInner.appendChild(backButton);
-                navigationButtonsDivInner.appendChild(forwardButton);
-                navigationButtonsDivInner.appendChild(closeButton);
-                navigationButtonsDiv.appendChild(navigationButtonsDivInner);
+                // start listening for navigation events
+                attachNavigationEvents(popup, navigationEventsCallback);
 
-                browserWrap.appendChild(navigationButtonsDiv);
-            } else {
-                popup.style.height = "100%";
-            }
-
-            // start listening for navigation events
-            attachNavigationEvents(popup, win);
-
-            if (isWebViewAvailable) {
-                strUrl = strUrl.replace("ms-appx://", "ms-appx-web://");
+                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];
-
-        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();
-        }
+        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();
+            }
+        });
     },
 
     injectScriptFile: function (win, fail, args) {
-        var filePath = args[0],
-            hasCallback = args[1];
-
-        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();
+        setImmediate(function () {
+            var filePath = args[0],
+                hasCallback = args[1];
+
+            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();
+                    });
                 });
-            });
-        }
+            }
+        });
     },
 
     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];
-
-        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
-            });
-        }
+        setImmediate(function () {
+            var filePath = args[0],
+                hasCallback = args[1];
+
+            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
+                });
+            }
+        });
     }
 };
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to