flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aha...@apache.org
Subject [1/5] git commit: [flex-asjs] [refs/heads/develop] - add binding implementation for MXMLItemRenderer
Date Fri, 09 Dec 2016 15:12:15 GMT
Repository: flex-asjs
Updated Branches:
  refs/heads/develop 6cb27ccb0 -> 57dc53036


add binding implementation for MXMLItemRenderer


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/e2285d29
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/e2285d29
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/e2285d29

Branch: refs/heads/develop
Commit: e2285d29ff0e3049e4027078613fee0b604302c8
Parents: 7b17642
Author: Alex Harui <aharui@apache.org>
Authored: Fri Dec 9 07:03:13 2016 -0800
Committer: Alex Harui <aharui@apache.org>
Committed: Fri Dec 9 07:06:58 2016 -0800

----------------------------------------------------------------------
 .../flex/binding/ItemRendererDataBinding.as     | 341 +++++++++++++++++++
 .../flex/binding/ItemRendererSimpleBinding.as   | 151 ++++++++
 .../src/main/resources/basic-manifest.xml       |   2 +
 3 files changed, 494 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/e2285d29/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererDataBinding.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererDataBinding.as
b/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererDataBinding.as
new file mode 100644
index 0000000..48afdba
--- /dev/null
+++ b/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererDataBinding.as
@@ -0,0 +1,341 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.binding
+{
+    import org.apache.flex.binding.ConstantBinding;
+    import org.apache.flex.binding.GenericBinding;
+    import org.apache.flex.binding.PropertyWatcher;
+    import org.apache.flex.binding.SimpleBinding;
+    import org.apache.flex.binding.WatcherBase;
+    import org.apache.flex.core.IBead;
+    import org.apache.flex.core.IStrand;
+    import org.apache.flex.events.Event;
+    import org.apache.flex.events.IEventDispatcher;
+    
+    /**
+     *  The ItemRendererDataBinding class implements databinding for
+     *  ItemRenderer instances.  Different classes can have
+     *  different databinding implementation that optimize for
+     *  the different lifecycles.  For example, an item renderer
+     *  databinding implementation can wait to execute databindings
+     *  until the data property is set.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.0
+     */
+	public class ItemRendererDataBinding implements IBead
+	{
+        /**
+         *  Constructor.
+         *  
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public function ItemRendererDataBinding()
+		{
+			super();
+		}
+        
+        private var _strand:IStrand;
+        
+        /**
+         *  @copy org.apache.flex.core.IBead#strand
+         *  
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+        public function set strand(value:IStrand):void
+        {
+            _strand = value;
+            var fieldWatcher:Object;
+            var isStatic:Boolean;
+            var sb:SimpleBinding;
+            if (!("_bindings" in _strand))
+                return;
+            var bindingData:Array = _strand["_bindings"];
+            var destObject:Object;
+            var n:int = bindingData[0];
+            var bindings:Array = [];
+            var i:int;
+            var index:int = 1;
+            for (i = 0; i < n; i++)
+            {
+                var binding:Object = {};
+                binding.source = bindingData[index++];
+				binding.destFunc = bindingData[index++];
+                binding.destination = bindingData[index++];
+                bindings.push(binding);
+            }
+            var watchers:Object = decodeWatcher(bindingData.slice(index));
+            for (i = 0; i < n; i++)
+            {
+                    binding = bindings[i];
+                var destination:IStrand;
+                if (binding.source is String)
+                {
+                    fieldWatcher = watchers.watcherMap[binding.source];
+                    if (!fieldWatcher) makeConstantBinding(binding, _strand);
+                    else if (fieldWatcher.eventNames is String)
+                    {
+                        isStatic = fieldWatcher.type == "static";
+                        sb = new SimpleBinding(isStatic);
+                        sb.destinationPropertyName = binding.destination[1];
+                        sb.eventName = fieldWatcher.eventNames as String;
+                        sb.sourcePropertyName = binding.source;
+                        if (isStatic)
+                            sb.setDocument(fieldWatcher.parentObj);
+                        else sb.setDocument(_strand);
+                        destObject = _strand[binding.destination[0]];
+                        destination = destObject as IStrand;
+                        if (destination)
+                            destination.addBead(sb);
+                        else
+                        {
+                            if (destObject)
+                            {
+                                sb.destination = destObject;
+                                _strand.addBead(sb);
+                            }
+                            else
+                            {
+                                deferredBindings[binding.destination[0]] = sb;
+                                IEventDispatcher(_strand).addEventListener("valueChange",
deferredBindingsHandler);
+                            }
+                        }
+                    }
+                }
+                else
+                if (binding.source is Array
+                        && binding.source[0] == "data"
+                        && binding.source.length == 2 && binding.destination.length
== 2)
+                {
+                    var irsb:ItemRendererSimpleBinding = new ItemRendererSimpleBinding();
+                    irsb.destinationID = binding.destination[0];
+                    irsb.destinationPropertyName = binding.destination[1];
+                    irsb.sourcePropertyName = binding.source[1];
+                    irsb.setDocument(_strand);
+                    _strand.addBead(irsb);
+                }
+                else
+                {
+                    makeGenericBinding(binding, i, watchers);
+                }
+            }
+        }
+
+        private function makeConstantBinding(binding:Object,strand:IStrand):void{
+            var cb:ConstantBinding = new ConstantBinding();
+            cb.destinationPropertyName = binding.destination[1];
+            if (binding.source is String) {
+                cb.sourcePropertyName = binding.source;
+            } else {
+                cb.sourceID = binding.source[0];
+                cb.sourcePropertyName = binding.source[1];
+            }
+            cb.setDocument(strand);
+            var destObject:Object = strand[binding.destination[0]];
+            var destination:IStrand = destObject as IStrand;
+            if (destination)
+                destination.addBead(cb);
+            else
+            {
+                if (destObject)
+                {
+                    cb.destination = destObject;
+                    strand.addBead(cb);
+                }
+                else
+                {
+                    deferredBindings[binding.destination[0]] = cb;
+                    IEventDispatcher(strand).addEventListener("valueChange", deferredBindingsHandler);
+                }
+            }
+
+        }
+
+        private function makeGenericBinding(binding:Object, index:int, watchers:Object):void
+        {
+            var gb:GenericBinding = new GenericBinding();
+            gb.setDocument(_strand);
+            gb.destinationData = binding.destination;
+			gb.destinationFunction = binding.destFunc;
+            gb.source = binding.source;
+            setupWatchers(gb, index, watchers.watchers, null);
+        }
+        
+        /**
+         * @flexjsignorecoercion Function
+         */
+        private function setupWatchers(gb:GenericBinding, index:int, watchers:Array, parentWatcher:WatcherBase):void
+        {
+            var foundWatcher:Boolean = false;
+            
+            var n:int = watchers.length;
+            for (var i:int = 0; i < n; i++)
+            {
+                var watcher:Object = watchers[i];
+                var isValidWatcher:Boolean = false;
+                if (typeof(watcher.bindings) == "number")
+                    isValidWatcher = (watcher.bindings == index);
+                else
+                    isValidWatcher = (watcher.bindings.indexOf(index) != -1);
+                if (isValidWatcher)
+                {
+                    var type:String = watcher.type;
+					var parentObj:Object = _strand;
+                    switch (type)
+                    {
+						case "static":
+                            parentObj = watcher.parentObj;
+                            gb.staticRoot = parentObj;
+                            gb.isStatic = true;
+                        case "property":
+                        {
+                            var pw:PropertyWatcher = new PropertyWatcher(_strand, 
+                                        watcher.propertyName, 
+                                        watcher.eventNames,
+                                        (typeof(gb.source) === "function" && watcher.children
== null) ? gb.source as Function : watcher.getterFunction);
+                            watcher.watcher = pw;
+                            if (parentWatcher)
+                                pw.parentChanged(parentWatcher.value);
+                            else
+                                pw.parentChanged(parentObj);
+                            if (parentWatcher)
+                                parentWatcher.addChild(pw);
+                            if (watcher.children == null)
+                                pw.addBinding(gb);
+                            foundWatcher = true;
+                            break;
+                        }
+
+                    }
+                    if (watcher.children)
+                    {
+                        setupWatchers(gb, index, watcher.children.watchers, watcher.watcher);
+                    }
+                }
+            }
+            if (!foundWatcher )            {
+                // might be a binding to a function that doesn't have change events
+                // so just force an update via parentWatcher (if it is set, null if not)
+                if (parentWatcher) gb.valueChanged(parentWatcher.value);
+                else gb.valueChanged(null);
+
+            }
+        }
+        
+        private function decodeWatcher(bindingData:Array):Object
+        {
+            var watcherMap:Object = {};
+            var watchers:Array = [];
+            var n:int = bindingData.length;
+            var index:int = 0;
+            var watcherData:Object;
+            while (index < n - 1)
+            {
+                var watcherIndex:int = bindingData[index++];
+                var type:int = bindingData[index++];
+                switch (type)
+                {
+                    case 0:
+                    {
+                        watcherData = { type: "function" };
+                        watcherData.functionName = bindingData[index++];
+						watcherData.paramFunction = bindingData[index++];
+                        watcherData.eventNames = bindingData[index++];
+                        watcherData.bindings = bindingData[index++];
+                        break;
+                    }
+                    case 1:
+					{
+						watcherData = { type: "static" };
+						watcherData.propertyName = bindingData[index++];
+						watcherData.eventNames = bindingData[index++];
+						watcherData.bindings = bindingData[index++];
+						watcherData.getterFunction = bindingData[index++];
+						watcherData.parentObj = bindingData[index++];
+						watcherMap[watcherData.propertyName] = watcherData;
+						break;
+					}
+                    case 2:
+                    {
+                        watcherData = { type: "property" };
+                        watcherData.propertyName = bindingData[index++];
+                        watcherData.eventNames = bindingData[index++];
+                        watcherData.bindings = bindingData[index++];
+                        watcherData.getterFunction = bindingData[index++];
+                        watcherMap[watcherData.propertyName] = watcherData;
+                        break;
+                    }
+                    case 3:
+                    {
+                        watcherData = { type: "xml" };
+                        watcherData.propertyName = bindingData[index++];
+                        watcherData.bindings = bindingData[index++];
+                        watcherMap[watcherData.propertyName] = watcherData;
+                        break;
+                    }
+                }
+                watcherData.children = bindingData[index++];
+                if (watcherData.children != null)
+                {
+                    watcherData.children = decodeWatcher(watcherData.children);
+                }
+                watcherData.index = watcherIndex;
+                watchers.push(watcherData);
+            }            
+            return { watchers: watchers, watcherMap: watcherMap };
+        }
+        
+        private var deferredBindings:Object = {};
+        private function deferredBindingsHandler(event:Event):void
+        {
+            for (var p:String in deferredBindings)
+            {
+                if (_strand[p] != null)
+                {
+                    var destination:IStrand = _strand[p] as IStrand;
+					if (destination)
+	                    destination.addBead(deferredBindings[p]);
+					else
+					{
+						var destObject:Object = _strand[p];
+						if (destObject)
+						{
+							deferredBindings[p].destination = destObject;
+							_strand.addBead(deferredBindings[p]);
+						}
+						else
+						{
+							trace("unexpected condition in deferredBindingsHandler");
+						}
+					}
+                    delete deferredBindings[p];
+                }
+            }
+        }
+        
+    }
+}

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/e2285d29/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererSimpleBinding.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererSimpleBinding.as
b/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererSimpleBinding.as
new file mode 100644
index 0000000..5354a8c
--- /dev/null
+++ b/frameworks/projects/Binding/src/main/flex/org/apache/flex/binding/ItemRendererSimpleBinding.as
@@ -0,0 +1,151 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.binding
+{
+import org.apache.flex.core.IBead;
+import org.apache.flex.core.IStrand;
+import org.apache.flex.core.IDocument;
+import org.apache.flex.events.IEventDispatcher;
+import org.apache.flex.events.Event;
+import org.apache.flex.events.ValueChangeEvent;
+
+/**
+ *  The SimpleBinding class is lightweight data-binding class that
+ *  is optimized for simple assignments of one object's property to
+ *  another object's property.
+ *
+ *  @langversion 3.0
+ *  @playerversion Flash 10.2
+ *  @playerversion AIR 2.6
+ *  @productversion FlexJS 0.0
+ */
+public class ItemRendererSimpleBinding implements IBead, IDocument
+{
+	/**
+	 *  Constructor.
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public function ItemRendererSimpleBinding()
+	{
+	}
+
+	/**
+
+	 *  The source object that dispatches an event
+	 *  when the property changes
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	protected var source:Object;
+
+	/**
+	 *  The host mxml document for the source and
+	 *  destination objects.  The source object
+	 *  is either this document for simple bindings
+	 *  like {foo} where foo is a property on
+	 *  the mxml documnet, or found as document[sourceID]
+	 *  for simple bindings like {someid.someproperty}
+	 *  It may be the document class for local static
+	 *  bindables (e.g. from a script block)
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	protected var document:Object;
+
+
+	/**
+	 *  The destination object.  It is always the same
+	 *  as the strand.  SimpleBindings are attached to
+	 *  the strand of the destination object.
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public var destinationID:String;
+
+	/**
+	 *  If not null, the name of a property on the
+	 *  mxml document that is being watched for changes.
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public var sourcePropertyName:String;
+
+	/**
+	 *  The name of the property on the strand that
+	 *  is set when the source property changes.
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public var destinationPropertyName:String;
+
+
+
+	/**
+	 *  @copy org.apache.flex.core.IBead#strand
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public function set strand(value:IStrand):void
+	{
+		document.addEventListener("dataChange",
+							dataChangeHandler);
+	}
+
+	/**
+	 *  @copy org.apache.flex.core.IDocument#setDocument()
+	 *
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @playerversion AIR 2.6
+	 *  @productversion FlexJS 0.0
+	 */
+	public function setDocument(document:Object, id:String = null):void
+	{
+		this.document = document;
+	}
+
+	private function dataChangeHandler(event:Event):void
+	{
+		document[destinationID][destinationPropertyName] = document.data[sourcePropertyName];
+	}
+
+}
+}

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/e2285d29/frameworks/projects/Binding/src/main/resources/basic-manifest.xml
----------------------------------------------------------------------
diff --git a/frameworks/projects/Binding/src/main/resources/basic-manifest.xml b/frameworks/projects/Binding/src/main/resources/basic-manifest.xml
index 4e7193b..44d5164 100644
--- a/frameworks/projects/Binding/src/main/resources/basic-manifest.xml
+++ b/frameworks/projects/Binding/src/main/resources/basic-manifest.xml
@@ -22,10 +22,12 @@
 <componentPackage>
 
     <component id="ConstantBinding" class="org.apache.flex.binding.ConstantBinding"/>
+    <component id="ItemRendererSimpleBinding" class="org.apache.flex.binding.ItemRendererSimpleBinding"/>
     <component id="SimpleBinding" class="org.apache.flex.binding.SimpleBinding"/>
     <component id="ViewDataBinding" class="org.apache.flex.binding.ViewDataBinding"/>
     <component id="MXMLBeadViewDataBinding" class="org.apache.flex.binding.MXMLBeadViewDataBinding"/>
     <component id="ApplicationDataBinding" class="org.apache.flex.binding.ApplicationDataBinding"/>
     <component id="ContainerDataBinding" class="org.apache.flex.binding.ContainerDataBinding"/>
+    <component id="ItemRendererDataBinding" class="org.apache.flex.binding.ItemRendererDataBinding"/>
 
 </componentPackage>


Mime
View raw message