1 /*
2 * Copyright (c) 2021 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 #include "core/components/declaration/xcomponent/xcomponent_declaration.h"
17
18 #include "base/utils/string_utils.h"
19 #include "core/components/declaration/common/declaration_constants.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace {
23 using namespace Framework;
24
InitSpecialized()25 void XComponentDeclaration::InitSpecialized()
26 {
27 AddSpecializedAttribute(DeclarationConstants::DEFAULT_XCOMPONENT_ATTR);
28 AddSpecializedEvent(DeclarationConstants::DEFAULT_XCOMPONENT_EVENT);
29 }
30
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)31 bool XComponentDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
32 {
33 static const LinearMapNode<void (*)(XComponentDeclaration&, const std::string&)> xComponentAttrOperators[] = {
34 {
35 DOM_XCOMPONENT_LIBRARYNAME,
36 [](XComponentDeclaration& declaration, const std::string& libraryName) {
37 declaration.SetXComponentLibraryName(libraryName);
38 }
39 },
40 {
41 DOM_XCOMPONENT_NAME,
42 [](XComponentDeclaration& declaration, const std::string& name) {
43 declaration.SetXComponentName(name);
44 }
45 },
46 {
47 DOM_XCOMPONENT_TYPE,
48 [](XComponentDeclaration& declaration, const std::string& type) {
49 declaration.SetXComponentType(type);
50 }
51 },
52 };
53 auto operatorIter = BinarySearchFindIndex(xComponentAttrOperators,
54 ArraySize(xComponentAttrOperators),
55 attr.first.c_str());
56 if (operatorIter != -1) {
57 xComponentAttrOperators[operatorIter].value(*this, attr.second);
58 return true;
59 }
60 return false;
61 }
62
63
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)64 bool XComponentDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
65 {
66 static const LinearMapNode<void (*)(XComponentDeclaration&, const EventMarker&)> eventOperators[] = {
67 {
68 DOM_XCOMPONENT_DESTROY,
69 [](XComponentDeclaration& declaration, const EventMarker& event) {
70 auto& xComponentEvent = declaration.MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
71 if (xComponentEvent.IsValid()) {
72 xComponentEvent.xComponentDestroyEventId = event;
73 }
74 }
75 },
76 {
77 DOM_XCOMPONENT_INIT,
78 [](XComponentDeclaration& declaration, const EventMarker& event) {
79 auto& xComponentEvent = declaration.MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
80 if (xComponentEvent.IsValid()) {
81 xComponentEvent.xComponentInitEventId = event;
82 }
83 }
84 },
85 };
86 auto operatorIter = BinarySearchFindIndex(eventOperators, ArraySize(eventOperators), event.c_str());
87 if (operatorIter != -1) {
88 eventOperators[operatorIter].value(*this, EventMarker(eventId, event, pageId));
89 return true;
90 }
91 return false;
92 }
93 } // namespace OHOS::Ace
94