1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_ITEM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_ITEM_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "base/utils/macros.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/components_ng/base/inspector_filter.h"
25 #include "core/components_ng/base/ui_node.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 #include "core/pipeline/base/element_register.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 // Syntax nodes are used for ForEach, LazyForEach, and IfElse nodes to wrap up the generated sub components and are
32 // uniquely identified by keys.
33 class ACE_EXPORT SyntaxItem : public UINode {
34     DECLARE_ACE_TYPE(SyntaxItem, UINode);
35 
36 public:
SyntaxItem(const std::string & key)37     explicit SyntaxItem(const std::string& key)
38         : UINode(V2::JS_SYNTAX_ITEM_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId()), key_(key)
39     {}
~SyntaxItem()40     ~SyntaxItem() override
41     {
42         SetRemoveSilently(true);
43     }
44 
IsAtomicNode()45     bool IsAtomicNode() const override
46     {
47         return false;
48     }
49 
GetKey()50     const std::string& GetKey() const
51     {
52         return key_;
53     }
54 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)55     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
56     {
57         UINode::ToJsonValue(json, filter);
58         json->PutExtAttr("key", key_.c_str(), filter);
59     }
60 
FromJson(const std::unique_ptr<JsonValue> & json)61     void FromJson(const std::unique_ptr<JsonValue>& json) override
62     {
63         UINode::FromJson(json);
64     }
65 
CreateSyntaxItemNode(const std::string & key)66     static RefPtr<SyntaxItem> CreateSyntaxItemNode(const std::string& key)
67     {
68         auto node = AceType::MakeRefPtr<SyntaxItem>(key);
69         ElementRegister::GetInstance()->AddUINode(node);
70         return node;
71     }
72 
73 private:
74     std::string key_;
75 
76     ACE_DISALLOW_COPY_AND_MOVE(SyntaxItem);
77 };
78 
79 } // namespace OHOS::Ace::NG
80 
81 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_ITEM_H
82