You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2013/06/28 02:26:25 UTC
git commit: Made WP7 LocalStorage polyfil part of the native code via
IBrowserDecorator
Updated Branches:
refs/heads/master a87588d68 -> 98e64cd15
Made WP7 LocalStorage polyfil part of the native code via IBrowserDecorator
Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/98e64cd1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/98e64cd1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/98e64cd1
Branch: refs/heads/master
Commit: 98e64cd158b2d21ea85fa9fc81f43cb686c845b3
Parents: a87588d
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Thu Jun 27 17:26:01 2013 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Thu Jun 27 17:26:01 2013 -0700
----------------------------------------------------------------------
wp7/template/cordovalib/CordovaView.xaml.cs | 13 +-
wp7/template/cordovalib/DOMStorageHelper.cs | 150 +++++++++++++++++++++--
2 files changed, 143 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/98e64cd1/wp7/template/cordovalib/CordovaView.xaml.cs
----------------------------------------------------------------------
diff --git a/wp7/template/cordovalib/CordovaView.xaml.cs b/wp7/template/cordovalib/CordovaView.xaml.cs
index ba9ee49..61f673b 100644
--- a/wp7/template/cordovalib/CordovaView.xaml.cs
+++ b/wp7/template/cordovalib/CordovaView.xaml.cs
@@ -188,6 +188,10 @@ namespace WPCordovaClassLib
orientHelper.Browser = CordovaBrowser;
browserDecorators.Add("Orientation", orientHelper);
+ DOMStorageHelper storageHelper = new DOMStorageHelper();
+ storageHelper.Browser = CordovaBrowser;
+ browserDecorators.Add("DOMStorage", storageHelper);
+
}
@@ -241,7 +245,7 @@ namespace WPCordovaClassLib
- //this.domStorageHelper = new DOMStorageHelper(this.CordovaBrowser);
+
try
{
@@ -440,13 +444,6 @@ namespace WPCordovaClassLib
void GapBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
string commandStr = e.Value;
-
- if (commandStr.IndexOf("DOMStorage") == 0)
- {
- // this.domStorageHelper.HandleStorageCommand(commandStr);
- return;
- }
-
string commandName = commandStr.Split('/').FirstOrDefault();
if (browserDecorators.ContainsKey(commandName))
http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/98e64cd1/wp7/template/cordovalib/DOMStorageHelper.cs
----------------------------------------------------------------------
diff --git a/wp7/template/cordovalib/DOMStorageHelper.cs b/wp7/template/cordovalib/DOMStorageHelper.cs
index f57bae4..59389c6 100644
--- a/wp7/template/cordovalib/DOMStorageHelper.cs
+++ b/wp7/template/cordovalib/DOMStorageHelper.cs
@@ -27,6 +27,7 @@ using System.Collections.Generic;
using Microsoft.Phone.Controls;
using System.Linq;
using WPCordovaClassLib.Cordova.JSON;
+using WPCordovaClassLib.CordovaLib;
/*
* Translates DOMStorage API between JS and Isolated Storage
@@ -35,13 +36,11 @@ using WPCordovaClassLib.Cordova.JSON;
namespace WPCordovaClassLib
{
- public class DOMStorageHelper
+ public class DOMStorageHelper : IBrowserDecorator
{
- protected WebBrowser webBrowser1;
-
- public DOMStorageHelper(WebBrowser gapBrowser)
+ public WebBrowser Browser { get; set; }
+ public DOMStorageHelper()
{
- this.webBrowser1 = gapBrowser;
// always clear session at creation
UserSettings["sessionStorage"] = new Dictionary<string, string>();
@@ -77,10 +76,137 @@ namespace WPCordovaClassLib
return UserSettings[type] as Dictionary<string, string>;
}
-
- public void HandleStorageCommand(string commandStr)
+ public void InjectScript()
{
+ string script = @"(function(win, doc) {
+var docDomain = null;
+try {
+ docDomain = doc.domain;
+} catch (err) {}
+if (!docDomain || docDomain.length === 0) {
+ var DOMStorage = function(type) {
+ if (type == 'sessionStorage') {
+ this._type = type;
+ }
+ Object.defineProperty(this, 'length', {
+ configurable: true,
+ get: function() {
+ return this.getLength();
+ }
+ });
+ };
+ DOMStorage.prototype = {
+ _type: 'localStorage',
+ _result: null,
+ keys: null,
+ onResult: function(key, valueStr) {
+ if (!this.keys) {
+ this.keys = [];
+ }
+ this._result = valueStr;
+ },
+ onKeysChanged: function(jsonKeys) {
+ this.keys = JSON.parse(jsonKeys);
+ var key;
+ for (var n = 0, len = this.keys.length; n < len; n++) {
+ key = this.keys[n];
+ if (!this.hasOwnProperty(key)) {
+ Object.defineProperty(this, key, {
+ configurable: true,
+ get: function() {
+ return this.getItem(key);
+ },
+ set: function(val) {
+ return this.setItem(key, val);
+ }
+ });
+ }
+ }
+ },
+ initialize: function() {
+ window.external.Notify('DOMStorage/' + this._type + '/load/keys');
+ },
+ getLength: function() {
+ if (!this.keys) {
+ this.initialize();
+ }
+ return this.keys.length;
+ },
+ key: function(n) {
+ if (!this.keys) {
+ this.initialize();
+ }
+ if (n >= this.keys.length) {
+ return null;
+ } else {
+ return this.keys[n];
+ }
+ },
+ getItem: function(key) {
+ if (!this.keys) {
+ this.initialize();
+ }
+ var retVal = null;
+ if (this.keys.indexOf(key) > -1) {
+ window.external.Notify('DOMStorage/' + this._type + '/get/' + key);
+ retVal = window.unescape(decodeURIComponent(this._result));
+ this._result = null;
+ }
+ return retVal;
+ },
+ setItem: function(key, value) {
+ if (!this.keys) {
+ this.initialize();
+ }
+ window.external.Notify('DOMStorage/' + this._type + '/set/' + key + '/' + encodeURIComponent(window.escape(value)));
+ },
+ removeItem: function(key) {
+ if (!this.keys) {
+ this.initialize();
+ }
+ var index = this.keys.indexOf(key);
+ if (index > -1) {
+ this.keys.splice(index, 1);
+ window.external.Notify('DOMStorage/' + this._type + '/remove/' + key);
+ delete this[key];
+ }
+ },
+ clear: function() {
+ if (!this.keys) {
+ this.initialize();
+ }
+ for (var n = 0, len = this.keys.length; n < len; n++) {
+ delete this[this.keys[n]];
+ }
+ this.keys = [];
+ window.external.Notify('DOMStorage/' + this._type + '/clear/');
+ }
+ };
+ if (typeof window.localStorage === 'undefined') {
+ Object.defineProperty(window, 'localStorage', {
+ writable: false,
+ configurable: false,
+ value: new DOMStorage('localStorage')
+ });
+ window.localStorage.initialize();
+ }
+ if (typeof window.sessionStorage === 'undefined') {
+ Object.defineProperty(window, 'sessionStorage', {
+ writable: false,
+ configurable: false,
+ value: new DOMStorage('sessionStorage')
+ });
+ window.sessionStorage.initialize();
+ }
+}
+})(window, document);";
+
+ Browser.InvokeScript("execScript", new string[] { script });
+ }
+
+ public bool HandleCommand(string commandStr)
+ {
string[] split = commandStr.Split('/');
if (split.Length > 3)
{
@@ -99,11 +225,11 @@ namespace WPCordovaClassLib
if (currentStorage.Keys.Contains(param))
{
string value = currentStorage[param];
- webBrowser1.InvokeScript("execScript", "window." + type + ".onResult('" + param + "','" + value + "');");
+ Browser.InvokeScript("execScript", "window." + type + ".onResult('" + param + "','" + value + "');");
}
else
{
- webBrowser1.InvokeScript("execScript", "window." + type + ".onResult('" + param + "');");
+ Browser.InvokeScript("execScript", "window." + type + ".onResult('" + param + "');");
}
}
@@ -113,7 +239,7 @@ namespace WPCordovaClassLib
string[] keys = currentStorage.Keys.ToArray();
string jsonString = JsonHelper.Serialize(keys);
string callbackJS = "window." + type + ".onKeysChanged('" + jsonString + "');";
- webBrowser1.InvokeScript("execScript", callbackJS);
+ Browser.InvokeScript("execScript", callbackJS);
}
break;
case "set":
@@ -124,7 +250,7 @@ namespace WPCordovaClassLib
string[] keys = currentStorage.Keys.ToArray();
string jsonString = JsonHelper.Serialize(keys);
string callbackJS = "window." + type + ".onKeysChanged('" + jsonString + "');";
- webBrowser1.InvokeScript("execScript", callbackJS);
+ Browser.InvokeScript("execScript", callbackJS);
}
break;
case "remove":
@@ -139,7 +265,7 @@ namespace WPCordovaClassLib
}
}
-
+ return true;
}
}
}