1 /*
2  * Copyright (c) 2020 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 OHOS_ACELITE_DESCRIPTOR_UTILS_H
17 #define OHOS_ACELITE_DESCRIPTOR_UTILS_H
18 
19 #include "component.h"
20 #include "non_copyable.h"
21 #include "wrapper/js.h"
22 
23 namespace OHOS {
24 namespace ACELite {
25 class DescriptorUtils final : public MemoryHeap {
26 public:
27     ACE_DISALLOW_COPY_AND_MOVE(DescriptorUtils);
~DescriptorUtils()28     ~DescriptorUtils() {}
29 
30     /**
31      * @brief Create a descriptor object of if directive, which like { if: true, getter, render }.
32      * @param getter The getter function.
33      * @param render The render function.
34      * @return The descriptor object of if directive.
35      */
36     static JSValue CreateIfDescriptor(JSValue getter, JSValue render);
37 
38     /**
39      * @brief Create a descriptor object of for directive, which like { for: true, getter, render }.
40      * @param Getter the getter function.
41      * @param Render the render function.
42      * @return The descriptor object of for directive.
43      */
44     static JSValue CreateForDescriptor(JSValue getter, JSValue render);
45 
46     /**
47      * @brief Create a descriptor watcher.
48      * @param element The parent element of descriptor object.
49      * @param descriptor The descriptor object.
50      * @return The descriptor watcher instance.
51      */
52     static JSValue CreateDescriptorWatcher(JSValue element, JSValue descriptor);
53 
54     /**
55      * @brief Check whether the descriptor object is a if directive descriptor or not?
56      * @param descriptor The descriptor object to be checked.
57      * @return The check result.
58      */
59     static bool IsIfDescriptor(JSValue descriptor);
60 
61     /**
62      * @brief Check whether the descriptor object is a for directive descriptor or not?
63      * @param descriptor The descriptor object to be checked.
64      * @return The check result.
65      */
66     static bool IsForDescriptor(JSValue descriptor);
67 
68     /**
69      * @brief Check whether the if directive descriptor object can be shown or not?
70      * @param descriptor The if directive descriptor object to be checked.
71      * @return The check result.
72      */
73     static bool IsIfDescriptorShown(JSValue descriptor);
74 
75     /**
76      * @brief Rendering the if directive descriptor object.
77      * The rendered result will be set to the rendered property of descriptor object,
78      * like { if: true, rendered: {}, getter, render }.
79      * @param descriptor The if directive descriptor object to be rendered.
80      * @return  The rendered result which will be an element.
81      */
82     static JSValue RenderIfDescriptor(JSValue descriptor);
83 
84     /**
85      * @brief Rendering the for directive descriptor object.
86      * The rendered result will be set to the rendered property of descriptor object,
87      * like { for: true, rendered: [...], getter, render }.
88      * @param descriptor The for directive descriptor object to be rendered.
89      * @return  The rendered result which will be an element array.
90      */
91     static JSValue RenderForDescriptor(JSValue descriptor);
92 
93     /**
94      * @brief Get the rendered result of directive descriptor.
95      * @param descriptor The directive descriptor.
96      * @return The rendered result.
97      */
98     static JSValue GetDescriptorRendered(JSValue descriptor);
99 
100     /**
101      * @brief Delete the rendered result of if directive descriptor object.
102      * @param descriptor The if directive descriptor.
103      */
104     static void DelIfDescriptorRendered(JSValue descriptor);
105 
106     /**
107      * @brief Delete the rendered result of for directive descriptor object.
108      * @param descriptor The for directive descriptor.
109      */
110     static void DelForDescriptorRendered(JSValue descriptor);
111 
112     /**
113      * @brief Release a collection of descriptor object or element.
114      * @param descriptorOrElements The collection of descriptor object or element to be released.
115      */
116     static void ReleaseDescriptorOrElements(JSValue descriptorOrElements);
117 
118     /**
119      * @brief Release a descriptor object or element.
120      * @param descriptorOrElement The descriptor object of element to be released.
121      */
122     static void ReleaseDescriptorOrElement(JSValue descriptorOrElement);
123 
124     /**
125      * @brief Release a for directive descriptor object.
126      * @param descriptor The for directive descriptor object to be released.
127      */
128     static void ReleaseForDescriptor(JSValue descriptor);
129 
130     /**
131      * @brief Release a if descriptor object.
132      * @param descriptor The if directive descriptor object to be released.
133      */
134     static void ReleaseIfDescriptor(JSValue descriptor);
135 
136     /**
137      * @brief Release an element.
138      * @param element The element to be released.
139      */
140     static void ReleaseElement(JSValue element);
141 
142     /**
143      * @brief rendered property name
144      */
145     static const char * const DESCRIPTOR_ATTR_RENDERED;
146 
147     /**
148      * @brief getter property name
149      */
150     static const char * const DESCRIPTOR_ATTR_GETTER;
151 
152     /**
153      * @brief render property name
154      */
155     static const char * const DESCRIPTOR_ATTR_RENDER;
156 
157     /**
158      * @brief element property name
159      */
160     static const char * const WATCHER_OPTION_ELEMENT;
161 
162     /**
163      * @brief options property name
164      */
165     static const char * const WATCHER_OPTION_DESCRIPTOR;
166 
167 private:
168     /**
169      * @brief Create a directive descriptor object.
170      * @param type The type of directive descriptor object, which is if or for?
171      * @param getter The getter function.
172      * @param render The render function.
173      * @return The directive descriptor object.
174      */
175     static JSValue CreateDescriptor(const char * const type, JSValue getter, JSValue render);
176 
177     /**
178      * @brief Check the type of a directive descriptor object.
179      * @param type The type of directive descriptor object.
180      * @param descriptor The directive descriptor object to be checked.
181      * @return The check result.
182      */
183     static bool IsDescriptor(const char * const type, JSValue descriptor);
184 
185     /**
186      * @brief if property name
187      */
188     static const char * const DESCRIPTOR_ATTR_IF;
189 
190     /**
191      * @brief for property name
192      */
193     static const char * const DESCRIPTOR_ATTR_FOR;
194 };
195 } // namespace ACELite
196 } // namespace OHOS
197 
198 #endif // OHOS_ACELITE_DESCRIPTOR_UTILS_H
199