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 "frameworks/bridge/common/dom/dom_navigation_menu.h"
17 
18 #include "frameworks/bridge/common/dom/dom_navigation_bar.h"
19 
20 namespace OHOS::Ace::Framework {
21 
DOMNavigationMenu(NodeId nodeId,const std::string & nodeName)22 DOMNavigationMenu::DOMNavigationMenu(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
23 {
24     menuChild_ = AceType::MakeRefPtr<MenuComponent>(std::to_string(nodeId), nodeName);
25     menuChild_->SetTextDirection((IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR));
26 }
27 
InitializeStyle()28 void DOMNavigationMenu::InitializeStyle()
29 {
30     menuChild_->InitTheme(GetThemeManager());
31 }
32 
ResetInitializedStyle()33 void DOMNavigationMenu::ResetInitializedStyle()
34 {
35     InitializeStyle();
36 }
37 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)38 void DOMNavigationMenu::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
39 {
40     if (!child) {
41         return;
42     }
43     auto option = AceType::DynamicCast<OptionComponent>(child->GetSpecializedComponent());
44     if (!option) {
45         return;
46     }
47     if (navigationBar_) {
48         navigationBar_->GetData()->allMenuItems.push_back(option);
49         UpdateNavigationBar();
50     }
51 }
52 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)53 void DOMNavigationMenu::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
54 {
55     if (!child) {
56         return;
57     }
58     auto option = AceType::DynamicCast<OptionComponent>(child->GetSpecializedComponent());
59     if (!option) {
60         return;
61     }
62     if (navigationBar_) {
63         navigationBar_->GetData()->allMenuItems.remove(option);
64         UpdateNavigationBar();
65     }
66 }
67 
OnMounted(const RefPtr<DOMNode> & parentNode)68 void DOMNavigationMenu::OnMounted(const RefPtr<DOMNode>& parentNode)
69 {
70     auto parentDom = AceType::DynamicCast<DomNavigationBar>(parentNode);
71     if (!parentDom) {
72         return;
73     }
74 
75     RefPtr<Component> component = parentDom->GetSpecializedComponent();
76     navigationBar_ = AceType::DynamicCast<NavigationBarComponent>(component);
77 }
78 
AddSpecializedEvent(int32_t pageId,const std::string & event)79 bool DOMNavigationMenu::AddSpecializedEvent(int32_t pageId, const std::string& event)
80 {
81     if (event == DOM_SELECTED) {
82         menuChild_->SetOnSuccess(EventMarker(GetNodeIdForEvent(), event, pageId));
83         return true;
84     }
85     return false;
86 }
87 
UpdateNavigationBar()88 void DOMNavigationMenu::UpdateNavigationBar()
89 {
90     navigationBar_->MarkNeedUpdate();
91     auto pipelineContext = pipelineContext_.Upgrade();
92     if (!pipelineContext) {
93         return;
94     }
95     pipelineContext->ScheduleUpdate(GetParentNode()->GetRootComponent());
96 }
97 
98 } // namespace OHOS::Ace::Framework