Permalink
Browse files

ChildBrowser plugin for WindowsPhone Mango

  • Loading branch information...
1 parent b63ba97 commit f056925ad2195e7e66394eb07ed88a750574d19d @purplecabbage purplecabbage committed Oct 20, 2011
Showing with 288 additions and 0 deletions.
  1. +90 −0 WindowsPhone/ChildBrowser/ChildBrowser.js
  2. +198 −0 WindowsPhone/ChildBrowser/ChildBrowserCommand.cs
@@ -0,0 +1,90 @@
+/* MIT licensed */
+// (c) 2010 Jesse MacFadyen, Nitobi
+
+/*global PhoneGap */
+
+function ChildBrowser() {
+ // Does nothing
+}
+
+// Callback when the location of the page changes
+// called from native
+ChildBrowser._onLocationChange = function(newLoc)
+{
+ window.plugins.childBrowser.onLocationChange(newLoc);
+};
+
+// Callback when the user chooses the 'Done' button
+// called from native
+ChildBrowser._onClose = function()
+{
+ window.plugins.childBrowser.onClose();
+};
+
+// Callback when the user chooses the 'open in Safari' button
+// called from native
+ChildBrowser._onOpenExternal = function()
+{
+ window.plugins.childBrowser.onOpenExternal();
+};
+
+// Pages loaded into the ChildBrowser can execute callback scripts, so be careful to
+// check location, and make sure it is a location you trust.
+// Warning ... don't exec arbitrary code, it's risky and could fuck up your app.
+// called from native
+ChildBrowser._onJSCallback = function(js,loc)
+{
+ // Not Implemented
+ //window.plugins.childBrowser.onJSCallback(js,loc);
+};
+
+/* The interface that you will use to access functionality */
+
+// Show a webpage, will result in a callback to onLocationChange
+ChildBrowser.prototype.showWebPage = function(loc,geolocationEnabled)
+{
+ var success = function(msg)
+ {
+ console.log("ChildBrowser.showWebPage success :: " + msg);
+ };
+
+ var error = function(e)
+ {
+ console.log("ChildBrowser.showWebPage error :: " + e);
+ };
+
+ var options =
+ {
+ url:loc,
+ geolocationEnabled:(geolocationEnabled == true)
+
+ };
+
+ PhoneGap.exec(success,error,"ChildBrowserCommand","showWebPage", options);
+ //setTimeout(this.close,5000);
+};
+
+// close the browser, will NOT result in close callback
+ChildBrowser.prototype.close = function()
+{
+ PhoneGap.exec(null,null,"ChildBrowserCommand","close");
+};
+
+// Not Implemented
+ChildBrowser.prototype.jsExec = function(jsString)
+{
+ // Not Implemented!!
+ //PhoneGap.exec("ChildBrowserCommand.jsExec",jsString);
+};
+
+// Note: this plugin does NOT install itself, call this method some time after deviceready to install it
+// it will be returned, and also available globally from window.plugins.childBrowser
+ChildBrowser.install = function()
+{
+ if(!window.plugins) {
+ window.plugins = {};
+ }
+
+ window.plugins.childBrowser = new ChildBrowser();
+ return window.plugins.childBrowser;
+};
@@ -0,0 +1,198 @@
+using System;
+using System.Net;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Ink;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Animation;
+using System.Windows.Shapes;
+using Microsoft.Phone.Controls;
+using System.Diagnostics;
+using System.Runtime.Serialization;
+using WP7GapClassLib.PhoneGap.UI;
+using Microsoft.Phone.Shell;
+
+namespace WP7GapClassLib.PhoneGap.Commands
+{
+ [DataContract]
+ public class BrowserOptions
+ {
+ [DataMember]
+ public string url;
+
+ [DataMember]
+ public bool isGeolocationEnabled;
+ }
+
+ public class ChildBrowserCommand : BaseCommand
+ {
+
+ private static WebBrowser browser;
+ private static ApplicationBarIconButton backButton;
+ private static ApplicationBarIconButton fwdButton;
+
+ // Display an inderminate progress indicator
+ public void showWebPage(string options)
+ {
+ BrowserOptions opts = JSON.JsonHelper.Deserialize<BrowserOptions>(options);
+
+ Uri loc = new Uri(opts.url);
+
+ Deployment.Current.Dispatcher.BeginInvoke(() =>
+ {
+ if (browser != null)
+ {
+ browser.IsGeolocationEnabled = opts.isGeolocationEnabled;
+ browser.Navigate(loc);
+ }
+ else
+ {
+ PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
+ if (frame != null)
+ {
+ PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
+ if (page != null)
+ {
+ Grid grid = page.FindName("LayoutRoot") as Grid;
+ if (grid != null)
+ {
+ browser = new WebBrowser();
+ browser.Navigate(loc);
+
+ browser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(browser_LoadCompleted);
+
+ browser.Navigating += new EventHandler<NavigatingEventArgs>(browser_Navigating);
+ browser.NavigationFailed += new System.Windows.Navigation.NavigationFailedEventHandler(browser_NavigationFailed);
+ browser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(browser_Navigated);
+ browser.IsScriptEnabled = true;
+ browser.IsGeolocationEnabled = opts.isGeolocationEnabled;
+ grid.Children.Add(browser);
+ }
+
+ ApplicationBar bar = new ApplicationBar();
+ bar.BackgroundColor = Colors.Black;
+ bar.IsMenuEnabled = false;
+
+ backButton = new ApplicationBarIconButton();
+ backButton.Text = "Back";
+ backButton.IconUri = new Uri("/Images/appbar.back.rest.png", UriKind.Relative);
+ backButton.Click += new EventHandler(backButton_Click);
+ backButton.IsEnabled = false;
+ bar.Buttons.Add(backButton);
+
+
+ fwdButton = new ApplicationBarIconButton();
+ fwdButton.Text = "Forward";
+ fwdButton.IconUri = new Uri("/Images/appbar.next.rest.png", UriKind.Relative);
+ fwdButton.Click += new EventHandler(fwdButton_Click);
+ fwdButton.IsEnabled = false;
+ bar.Buttons.Add(fwdButton);
+
+ ApplicationBarIconButton closeBtn = new ApplicationBarIconButton();
+ closeBtn.Text = "Close";
+ closeBtn.IconUri = new Uri("/Images/appbar.close.rest.png", UriKind.Relative);
+ closeBtn.Click += new EventHandler(closeBtn_Click);
+ bar.Buttons.Add(closeBtn);
+
+ page.ApplicationBar = bar;
+ }
+
+ }
+ }
+ });
+ }
+
+ void browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
+ {
+
+ }
+
+ void fwdButton_Click(object sender, EventArgs e)
+ {
+ if (browser != null)
+ {
+ try
+ {
+ browser.InvokeScript("execScript", "history.forward();");
+ }
+ catch(Exception)
+ {
+
+ }
+ }
+ }
+
+ void backButton_Click(object sender, EventArgs e)
+ {
+ if (browser != null)
+ {
+ try
+ {
+ browser.InvokeScript("execScript", "history.back();");
+ }
+ catch (Exception)
+ {
+
+ }
+ }
+ }
+
+ void closeBtn_Click(object sender, EventArgs e)
+ {
+ this.close();
+ }
+
+
+ public void close(string options="")
+ {
+ if (browser != null)
+ {
+ Deployment.Current.Dispatcher.BeginInvoke(() =>
+ {
+ PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
+ if (frame != null)
+ {
+ PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
+ if (page != null)
+ {
+ Grid grid = page.FindName("LayoutRoot") as Grid;
+ if (grid != null)
+ {
+ grid.Children.Remove(browser);
+ }
+ page.ApplicationBar = null;
+ }
+ }
+ browser = null;
+ });
+ }
+ }
+
+ void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
+ {
+ string message = "{type:\"locationChanged\",location:\"" + e.Uri.AbsoluteUri + "\"}";
+ PluginResult result = new PluginResult(PluginResult.Status.OK, message);
+ result.KeepCallback = true;
+ this.DispatchCommandResult(result);
+ }
+
+ void browser_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
+ {
+ string message = "{type:\"navigationError\",location:\"" + e.Uri.AbsoluteUri + "\"}";
+ PluginResult result = new PluginResult(PluginResult.Status.ERROR, message);
+ result.KeepCallback = true;
+ this.DispatchCommandResult(result);
+ }
+
+ void browser_Navigating(object sender, NavigatingEventArgs e)
+ {
+ string message = "{type:\"locationAboutToChange\",location:\"" + e.Uri.AbsoluteUri + "\"}";
+ PluginResult result = new PluginResult(PluginResult.Status.OK, message);
+ result.KeepCallback = true;
+ this.DispatchCommandResult(result);
+ }
+
+ }
+}

0 comments on commit f056925

Please sign in to comment.