Updated Storage project by separating the implementation into a CSS-accessible class from the developer-facing class. The initial JavaScript implementation follows the W3C web storage specification; the Flash side uses SharedObject.
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/75ee02d2 Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/75ee02d2 Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/75ee02d2 Branch: refs/heads/spark Commit: 75ee02d2817d5a5c6bf8e976bfd8a305bb5b09e5 Parents: 392faf5 Author: Peter Ent <[email protected]> Authored: Thu Feb 25 17:24:22 2016 -0500 Committer: Peter Ent <[email protected]> Committed: Thu Feb 25 17:24:22 2016 -0500 ---------------------------------------------------------------------- .../Storage/src/main/flex/StorageClasses.as | 5 +- .../flex/org/apache/flex/storage/IWebStorage.as | 100 +++++++++ .../org/apache/flex/storage/LocalStorage.as | 120 +++------- .../storage/providers/LocalStorageProvider.as | 220 +++++++++++++++++++ .../src/main/resources/basic-manifest.xml | 2 +- .../src/main/resources/compile-asjs-config.xml | 4 + .../src/main/resources/compile-config.xml | 5 + .../Storage/src/main/resources/defaults.css | 26 +++ 8 files changed, 388 insertions(+), 94 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/flex/StorageClasses.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/flex/StorageClasses.as b/frameworks/projects/Storage/src/main/flex/StorageClasses.as index 1f6b80a..db34477 100644 --- a/frameworks/projects/Storage/src/main/flex/StorageClasses.as +++ b/frameworks/projects/Storage/src/main/flex/StorageClasses.as @@ -16,7 +16,7 @@ // limitations under the License. // //////////////////////////////////////////////////////////////////////////////// -package +package { /** @@ -26,8 +26,9 @@ package * from the classes specified in manifest.xml. */ internal class StorageClasses -{ +{ import org.apache.flex.storage.LocalStorage; LocalStorage; + import org.apache.flex.storage.providers.LocalStorageProvider; LocalStorageProvider; } } http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IWebStorage.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IWebStorage.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IWebStorage.as new file mode 100644 index 0000000..3bc044d --- /dev/null +++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IWebStorage.as @@ -0,0 +1,100 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.storage +{ +/** + * The IWebStorage interface provides a template for classes to use that wish + * to allow storage of small amounts of data into a web browser or perhaps on a + * mobile device. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ +public interface IWebStorage +{ + /** + * Returns true if the platform provides local storage. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function storageAvailable() : Boolean; + + /** + * Stores a value with a key. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function setItem(key:String, value:String) : Boolean; + + /** + * Returns the value of a key, if present. If the key is not + * present in the storage area, null or undefined may be returned. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function getItem(key:String) : String; + + /** + * Removes the key=value pair from storage. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function removeItem(key:String) : Boolean; + + /** + * Returns true if there is a value associated with the key. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function hasItem(key:String) : Boolean; + + /** + * Clears the storage area of all key=value pairs. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + function clear() : void; +} +} http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/LocalStorage.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/LocalStorage.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/LocalStorage.as index 9edd82c..1a6018e 100644 --- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/LocalStorage.as +++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/LocalStorage.as @@ -18,20 +18,25 @@ //////////////////////////////////////////////////////////////////////////////// package org.apache.flex.storage { -COMPILE::AS3 { - import flash.net.SharedObject; -} + import org.apache.flex.storage.providers.LocalStorageProvider; + import org.apache.flex.core.ValuesManager; /** * The LocalStorage class allows apps to store small amounts of data * locally, in the browser's permitted storage area. This data will persist * between browser invocations. The data is stored in key=value pairs. * + * This class uses the ValuesManager to determine a storage provider - an implementation + * class the actually does the storing and retrieving. To change the provider implementation, + * set a ClassReference for the LocalStorage CSS style. The default is the + * org.apache.flex.storage.providers.LocalStorageProvider class. + * + * @see org.apache.flex.storage.IWebStorage + * @see org.apache.flex.storage.provides.LocalStorageProvider * @langversion 3.0 * @playerversion Flash 10.2 * @playerversion AIR 2.6 * @productversion FlexJS 0.0 - * @flexjsignoreimport window */ public class LocalStorage { @@ -47,17 +52,13 @@ public class LocalStorage */ public function LocalStorage() { - COMPILE::AS3 { - try { - sharedObject = SharedObject.getLocal("flexjs","/",false); - } catch(e) { - sharedObject = null; - } - } + storageProvider = ValuesManager.valuesImpl.newInstance(this, "iStorageProvider") as IWebStorage; } - COMPILE::AS3 - private var sharedObject:SharedObject; + /** + * The implementation of the storage system. + */ + private var storageProvider:IWebStorage; /** * Returns true if the platform provides local storage. @@ -70,26 +71,11 @@ public class LocalStorage */ public function storageAvailable():Boolean { - var result:Boolean = false; - - COMPILE::AS3 { - result = (sharedObject != null); - } - - COMPILE::JS { - try { - result = 'localStorage' in window && window['localStorage'] !== null; - } catch(e) { - result = false; - } - } - - return result; + return storageProvider.storageAvailable(); } /** - * Stores a value with a key. The value may be converted to a String, depending - * on the platform. + * Stores a value with a key. * * @langversion 3.0 * @playerversion Flash 10.2 @@ -97,20 +83,12 @@ public class LocalStorage * @productversion FlexJS 0.0 * @flexjsignoreimport window */ - public function setItem(key:String, value:String) : Boolean + public function setItem(key:String, value:Object) : Boolean { - if (!storageAvailable()) return false; - - COMPILE::AS3 { - sharedObject.data[key] = value; - sharedObject.flush(); - } - - COMPILE::JS { - window.localStorage.setItem(key, value); - } - - return true; + // turn the value into a string in some fashion, if possible, return + // the knowlege of what type value really is for getItem(). + var valueAsString:String = value.toString(); + return storageProvider.setItem(key, valueAsString); } /** @@ -124,21 +102,12 @@ public class LocalStorage * @productversion FlexJS 0.0 * @flexjsignoreimport window */ - public function getItem(key:String) : String + public function getItem(key:String) : Object { - if (!storageAvailable()) return null; - - var result:String = null; - - COMPILE::AS3 { - result = sharedObject.data[key] as String; - } - - COMPILE::JS { - result = window.localStorage.getItem(key); - } - - return result; + var value:Object = storageProvider.getItem(key); + // perhaps figure out what value is exactly and return that + // object. + return value; } /** @@ -154,18 +123,7 @@ public class LocalStorage */ public function removeItem(key:String) : Boolean { - if (!storageAvailable()) return null; - - COMPILE::AS3 { - delete sharedObject.data[key]; - sharedObject.flush(); - } - - COMPILE::JS { - window.localStorage.removeItem(key); - } - - return true; + return storageProvider.removeItem(key); } /** @@ -179,19 +137,7 @@ public class LocalStorage */ public function hasItem(key:String) : Boolean { - if (!storageAvailable()) return false; - - var result:Boolean = false; - - COMPILE::AS3 { - result = sharedObject.data.hasOwnProperty(key); - } - - COMPILE::JS { - result = (window.localStorage[key] !== null); - } - - return result; + return storageProvider.hasItem(key); } /** @@ -205,15 +151,7 @@ public class LocalStorage */ public function clear() : void { - if (!storageAvailable()) return; - - COMPILE::AS3 { - sharedObject.clear(); - } - - COMPILE::JS { - window.localStorage.clear(); - } + storageProvider.clear(); } } } http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/LocalStorageProvider.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/LocalStorageProvider.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/LocalStorageProvider.as new file mode 100644 index 0000000..5370923 --- /dev/null +++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/LocalStorageProvider.as @@ -0,0 +1,220 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.storage.providers +{ + import org.apache.flex.storage.IWebStorage; + +COMPILE::AS3 { + import flash.net.SharedObject; +} + +/** + * The LocalStorageProvider class allows apps to store small amounts of data + * locally, in the browser's permitted storage area. This data will persist + * between browser invocations. The data is stored in key=value pairs and the + * value must be a string. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ +public class LocalStorageProvider implements IWebStorage +{ + + /** + * Constructor. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function LocalStorageProvider() + { + COMPILE::AS3 { + try { + sharedObject = SharedObject.getLocal("flexjs","/",false); + } catch(e) { + sharedObject = null; + } + } + } + + COMPILE::AS3 + private var sharedObject:SharedObject; + + /** + * Returns true if the platform provides local storage. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function storageAvailable():Boolean + { + var result:Boolean = false; + + COMPILE::AS3 { + result = (sharedObject != null); + } + + COMPILE::JS { + try { + result = 'localStorage' in window && window['localStorage'] !== null; + } catch(e) { + result = false; + } + } + + return result; + } + + /** + * Stores a value with a key. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function setItem(key:String, value:String) : Boolean + { + if (!storageAvailable()) return false; + + COMPILE::AS3 { + sharedObject.data[key] = value; + sharedObject.flush(); + } + + COMPILE::JS { + window.localStorage.setItem(key, value); + } + + return true; + } + + /** + * Returns the value associated with the key, or undefined if there is + * no value stored. Note that a String version of the value is stored. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function getItem(key:String) : String + { + if (!storageAvailable()) return null; + + var result:String = null; + + COMPILE::AS3 { + result = sharedObject.data[key] as String; + } + + COMPILE::JS { + result = window.localStorage.getItem(key); + } + + return result; + } + + /** + * Removed the value and, possibly, the key from local storage. On some + * platforms, retriving the value after removing it will be an error, on + * others it may return undefined or null. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function removeItem(key:String) : Boolean + { + if (!storageAvailable()) return null; + + COMPILE::AS3 { + delete sharedObject.data[key]; + sharedObject.flush(); + } + + COMPILE::JS { + window.localStorage.removeItem(key); + } + + return true; + } + + /** + * Returns true if there is a value stored for the key. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function hasItem(key:String) : Boolean + { + if (!storageAvailable()) return false; + + var result:Boolean = false; + + COMPILE::AS3 { + result = sharedObject.data.hasOwnProperty(key); + } + + COMPILE::JS { + result = (window.localStorage[key] !== null); + } + + return result; + } + + /** + * Clears all values from local storage. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + * @flexjsignoreimport window + */ + public function clear() : void + { + if (!storageAvailable()) return; + + COMPILE::AS3 { + sharedObject.clear(); + } + + COMPILE::JS { + window.localStorage.clear(); + } + } +} +} http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/resources/basic-manifest.xml ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/resources/basic-manifest.xml b/frameworks/projects/Storage/src/main/resources/basic-manifest.xml index cd0f5b4..b502203 100644 --- a/frameworks/projects/Storage/src/main/resources/basic-manifest.xml +++ b/frameworks/projects/Storage/src/main/resources/basic-manifest.xml @@ -20,5 +20,5 @@ <componentPackage> - + <component id="LocalStorage" class="org.apache.flex.storage.LocalStorage"/> </componentPackage> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/resources/compile-asjs-config.xml ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/resources/compile-asjs-config.xml b/frameworks/projects/Storage/src/main/resources/compile-asjs-config.xml index 0d410aa..6b3c6d5 100644 --- a/frameworks/projects/Storage/src/main/resources/compile-asjs-config.xml +++ b/frameworks/projects/Storage/src/main/resources/compile-asjs-config.xml @@ -42,6 +42,10 @@ <locale/> <library-path> + <!-- asjscompc won't 'link' these classes in, but will list their requires + if these swcs are on the external-library-path then their requires + will not be listed --> + <path-element>../../../../../externs/Core.swc</path-element> </library-path> <namespaces> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/resources/compile-config.xml ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/resources/compile-config.xml b/frameworks/projects/Storage/src/main/resources/compile-config.xml index bdd4048..8ca6f9c 100644 --- a/frameworks/projects/Storage/src/main/resources/compile-config.xml +++ b/frameworks/projects/Storage/src/main/resources/compile-config.xml @@ -23,6 +23,7 @@ <external-library-path> <path-element>${env.AIR_HOME}/frameworks/libs/air/airglobal.swc</path-element> + <path-element>../../../../../libs/Core.swc</path-element> </external-library-path> <mxml> @@ -59,6 +60,10 @@ </compiler> <include-file> + <name>defaults.css</name> + <path>defaults.css</path> + </include-file> + <include-file> <name>js/out/*</name> <path>../../../target/generated-sources/flexjs/*</path> </include-file> http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/75ee02d2/frameworks/projects/Storage/src/main/resources/defaults.css ---------------------------------------------------------------------- diff --git a/frameworks/projects/Storage/src/main/resources/defaults.css b/frameworks/projects/Storage/src/main/resources/defaults.css new file mode 100644 index 0000000..9d82e6f --- /dev/null +++ b/frameworks/projects/Storage/src/main/resources/defaults.css @@ -0,0 +1,26 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +@namespace "library://ns.apache.org/flexjs/basic"; +@namespace svg "library://ns.apache.org/flexjs/svg"; + +LocalStorage +{ + IStorageProvider: ClassReference("org.apache.flex.storage.providers.LocalStorageProvider"); +} \ No newline at end of file
