You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ha...@apache.org on 2021/12/19 10:10:31 UTC

[royale-asjs] branch develop updated: Added ILabeledData

This is an automated email from the ASF dual-hosted git repository.

harbs pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new 9013e91  Added ILabeledData
9013e91 is described below

commit 9013e9151552ef14a568fba2f60cae25b0f85ab6
Author: Harbs <ha...@in-tools.com>
AuthorDate: Sun Dec 19 12:10:22 2021 +0200

    Added ILabeledData
---
 .../apache/royale/html/util/getLabelFromData.as    | 31 +++++++++++++++++++---
 .../projects/Core/src/main/royale/CoreClasses.as   |  1 +
 .../royale/org/apache/royale/core/ILabeledData.as  | 25 +++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
index f8613a5..18aa873 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
@@ -21,24 +21,49 @@ package org.apache.royale.html.util
 
     import org.apache.royale.core.IHasLabelField;
     import org.apache.royale.core.IHasDataField;
+    import org.apache.royale.core.ILabeledData;
 
     /**
+     *  Utility function to get a label string from a value object
+     *  Strings are just returned as-is
+     *  The most effective way to use the function for data is to use `ILabeledData`.
+     *  If your data is an instantiated class, always implement `ILabeledData`
+     *  and returns the correct value with the `label` getter.
+     *  This ensures that it will work even after full minimization.
+     *  If you are using plain objects (i.e. using `JSON.parse()` or similar) it will use the following logic flow:
+     *  First it tries a `labelField`
+     *  Then the `dataField`
+     *  If both of those fail, it tries a `label` property
+     *  If all else fails, it just converts the object to a string
+     * 
 	 *  @langversion 3.0
 	 *  @playerversion Flash 10.2
 	 *  @playerversion AIR 2.6
 	 *  @productversion Royale 0.9.3
      *  @royaleignorecoercion org.apache.royale.core.IHasLabelField
      *  @royaleignorecoercion org.apache.royale.core.IHasDataField
-     * Utility function to get a label string from a value object
+     *  @royaleignorecoercion org.apache.royale.core.ILabeledData
      */
     public function getLabelFromData(obj:Object,data:Object):String
     {
         // slightly more code, but we bail early if it's a string which is often
         if (data is String) return "" + data;
         if(!data) return "";
+        if(data is ILabeledData) return (data as ILabeledData).label;
+        if (obj is IHasLabelField &&
+            (obj as IHasLabelField).labelField &&
+            data[(obj as IHasLabelField).labelField] != null)
+        {
+            return "" + data[(obj as IHasLabelField).labelField];
+        } 
+            
+        if (obj is IHasDataField &&
+            (obj as IHasDataField).dataField &&
+            data[(obj as IHasDataField).dataField] != null)
+        {
+            return "" + data[(obj as IHasDataField).dataField];
+        }
 
-        if (obj is IHasLabelField && (obj as IHasLabelField).labelField && data[(obj as IHasLabelField).labelField] != null) return "" + data[(obj as IHasLabelField).labelField];
-        if (obj is IHasDataField && (obj as IHasDataField).dataField && data[(obj as IHasDataField).dataField] != null) return "" + data[(obj as IHasDataField).dataField];
         var label:String = data["label"];
         if(label != null){
             return label;
diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as b/frameworks/projects/Core/src/main/royale/CoreClasses.as
index 488d788..84ffda3 100644
--- a/frameworks/projects/Core/src/main/royale/CoreClasses.as
+++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as
@@ -34,6 +34,7 @@ internal class CoreClasses
 	import org.apache.royale.core.LayoutBase; LayoutBase;
 	import org.apache.royale.core.ContainerBaseStrandChildren; ContainerBaseStrandChildren;
 	import org.apache.royale.core.ApplicationBase; ApplicationBase;
+	import org.apache.royale.core.ILabeledData; ILabeledData;
 	import org.apache.royale.core.IList; IList;
 	import org.apache.royale.core.IIcon; IIcon;
 	import org.apache.royale.core.ITextButton; ITextButton;
diff --git a/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as b/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as
new file mode 100644
index 0000000..d449f42
--- /dev/null
+++ b/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as
@@ -0,0 +1,25 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.royale.core
+{
+	public interface ILabeledData
+	{
+		function get label():String;
+	}
+}
\ No newline at end of file

Re: [royale-asjs] branch develop updated: Added ILabeledData

Posted by Harbs <ha...@gmail.com>.
A bit of a heads up:

You might have noticed a number of commits from me today. I noticed that the function which gets labels in item renderers will not work well for aggressively minified applications.

My commits were to fix that.

Here’s the current status:

1. It’s doubtful that Jewel, MDL, MX or Spark will work correctly for aggressively minified applications even after my changes when using item renderers in many cases. That’s because those projects each have code which assume that bracket access to properties will work. That is not the case with all combinations of compiler options.
2. If you want to use instantiated classes as data for item renderers, your classes should implement ILabeledData. That will ensure that getLabelFromData will always work.
3. If you use plain objects, you should be able to use either labelField or dataField in the appropriate classes to specify the object field, but make sure your dynamic fields are preserved.
4. Plain objects with a “label” property will work too.
5. Otherwise make sure that your object returns the right thing in “valueOf”.

HTH…
Harbs

> On Dec 19, 2021, at 12:10 PM, harbs@apache.org wrote:
> 
> This is an automated email from the ASF dual-hosted git repository.
> 
> harbs pushed a commit to branch develop
> in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
> 
> 
> The following commit(s) were added to refs/heads/develop by this push:
>     new 9013e91  Added ILabeledData
> 9013e91 is described below
> 
> commit 9013e9151552ef14a568fba2f60cae25b0f85ab6
> Author: Harbs <ha...@in-tools.com>
> AuthorDate: Sun Dec 19 12:10:22 2021 +0200
> 
>    Added ILabeledData
> ---
> .../apache/royale/html/util/getLabelFromData.as    | 31 +++++++++++++++++++---
> .../projects/Core/src/main/royale/CoreClasses.as   |  1 +
> .../royale/org/apache/royale/core/ILabeledData.as  | 25 +++++++++++++++++
> 3 files changed, 54 insertions(+), 3 deletions(-)
> 
> diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
> index f8613a5..18aa873 100644
> --- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
> +++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/html/util/getLabelFromData.as
> @@ -21,24 +21,49 @@ package org.apache.royale.html.util
> 
>     import org.apache.royale.core.IHasLabelField;
>     import org.apache.royale.core.IHasDataField;
> +    import org.apache.royale.core.ILabeledData;
> 
>     /**
> +     *  Utility function to get a label string from a value object
> +     *  Strings are just returned as-is
> +     *  The most effective way to use the function for data is to use `ILabeledData`.
> +     *  If your data is an instantiated class, always implement `ILabeledData`
> +     *  and returns the correct value with the `label` getter.
> +     *  This ensures that it will work even after full minimization.
> +     *  If you are using plain objects (i.e. using `JSON.parse()` or similar) it will use the following logic flow:
> +     *  First it tries a `labelField`
> +     *  Then the `dataField`
> +     *  If both of those fail, it tries a `label` property
> +     *  If all else fails, it just converts the object to a string
> +     * 
> 	 *  @langversion 3.0
> 	 *  @playerversion Flash 10.2
> 	 *  @playerversion AIR 2.6
> 	 *  @productversion Royale 0.9.3
>      *  @royaleignorecoercion org.apache.royale.core.IHasLabelField
>      *  @royaleignorecoercion org.apache.royale.core.IHasDataField
> -     * Utility function to get a label string from a value object
> +     *  @royaleignorecoercion org.apache.royale.core.ILabeledData
>      */
>     public function getLabelFromData(obj:Object,data:Object):String
>     {
>         // slightly more code, but we bail early if it's a string which is often
>         if (data is String) return "" + data;
>         if(!data) return "";
> +        if(data is ILabeledData) return (data as ILabeledData).label;
> +        if (obj is IHasLabelField &&
> +            (obj as IHasLabelField).labelField &&
> +            data[(obj as IHasLabelField).labelField] != null)
> +        {
> +            return "" + data[(obj as IHasLabelField).labelField];
> +        } 
> +            
> +        if (obj is IHasDataField &&
> +            (obj as IHasDataField).dataField &&
> +            data[(obj as IHasDataField).dataField] != null)
> +        {
> +            return "" + data[(obj as IHasDataField).dataField];
> +        }
> 
> -        if (obj is IHasLabelField && (obj as IHasLabelField).labelField && data[(obj as IHasLabelField).labelField] != null) return "" + data[(obj as IHasLabelField).labelField];
> -        if (obj is IHasDataField && (obj as IHasDataField).dataField && data[(obj as IHasDataField).dataField] != null) return "" + data[(obj as IHasDataField).dataField];
>         var label:String = data["label"];
>         if(label != null){
>             return label;
> diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as b/frameworks/projects/Core/src/main/royale/CoreClasses.as
> index 488d788..84ffda3 100644
> --- a/frameworks/projects/Core/src/main/royale/CoreClasses.as
> +++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as
> @@ -34,6 +34,7 @@ internal class CoreClasses
> 	import org.apache.royale.core.LayoutBase; LayoutBase;
> 	import org.apache.royale.core.ContainerBaseStrandChildren; ContainerBaseStrandChildren;
> 	import org.apache.royale.core.ApplicationBase; ApplicationBase;
> +	import org.apache.royale.core.ILabeledData; ILabeledData;
> 	import org.apache.royale.core.IList; IList;
> 	import org.apache.royale.core.IIcon; IIcon;
> 	import org.apache.royale.core.ITextButton; ITextButton;
> diff --git a/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as b/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as
> new file mode 100644
> index 0000000..d449f42
> --- /dev/null
> +++ b/frameworks/projects/Core/src/main/royale/org/apache/royale/core/ILabeledData.as
> @@ -0,0 +1,25 @@
> +////////////////////////////////////////////////////////////////////////////////
> +//
> +//  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.royale.core
> +{
> +	public interface ILabeledData
> +	{
> +		function get label():String;
> +	}
> +}
> \ No newline at end of file