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 <[email protected]>
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