1 /*
2  * Copyright (c) 2021-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 #include "frameworks/bridge/common/dom/dom_tab_content.h"
17 
18 #include "frameworks/bridge/common/dom/dom_tab_bar.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 
21 namespace OHOS::Ace::Framework {
22 
DOMTabContent(NodeId nodeId,const std::string & nodeName)23 DOMTabContent::DOMTabContent(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
24 {
25     std::list<RefPtr<Component>> tabContents;
26     RefPtr<TabController> controller;
27     tabContentChild_ = AceType::MakeRefPtr<TabContentComponent>(tabContents, controller);
28 }
29 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)30 bool DOMTabContent::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
31 {
32     if (attr.first == DOM_TAB_CONTENT_SCROLLABLE) {
33         scrollable_ = StringToBool(attr.second);
34         tabContentChild_->SetScrollable(scrollable_);
35         return true;
36     }
37     return false;
38 }
39 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)40 void DOMTabContent::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
41 {
42     if (!child) {
43         return;
44     }
45     if (tabContentChild_) {
46         tabContentChild_->InsertChild(slot, child->GetRootComponent());
47     }
48 }
49 
PrepareChangeListener(const RefPtr<DOMTabs> & parentNode)50 void DOMTabContent::PrepareChangeListener(const RefPtr<DOMTabs>& parentNode)
51 {
52     auto weak = AceType::WeakClaim(this);
53     WeakPtr<DOMTabs> weakParent = parentNode;
54     auto changeCallback = [weak, weakParent](uint32_t currentIndex) {
55         auto domNode = weak.Upgrade();
56         if (!domNode) {
57             return;
58         }
59 
60         auto parentRef = weakParent.Upgrade();
61         if (!parentRef) {
62             return;
63         }
64         for (const auto& node : parentRef->GetChildList()) {
65             if (node->GetTag() == DOM_NODE_TAG_TAB_BAR) {
66                 const auto& tabBarNode = AceType::DynamicCast<DOMTabBar>(node);
67                 if (tabBarNode) {
68                     tabBarNode->UpdateIndex(currentIndex);
69                     break;
70                 }
71             }
72         }
73     };
74     auto changeMarker = BackEndEventManager<void(uint32_t)>::GetInstance().GetAvailableMarker();
75     BackEndEventManager<void(uint32_t)>::GetInstance().BindBackendEvent(changeMarker, changeCallback);
76     tabContentChild_->SetDomChangeEventId(changeMarker);
77 }
78 
OnMounted(const RefPtr<DOMNode> & parentNode)79 void DOMTabContent::OnMounted(const RefPtr<DOMNode>& parentNode)
80 {
81     if (!parentNode) {
82         return;
83     }
84     if (parentNode->GetTag() == DOM_NODE_TAG_TABS) {
85         const auto& parentNodeTmp = AceType::DynamicCast<DOMTabs>(parentNode);
86         if (!parentNodeTmp) {
87             return;
88         }
89         index_ = parentNodeTmp->GetTabIndex();
90         controllerId_ = parentNodeTmp->GetTabControllerId();
91         const auto& controller = parentNodeTmp->GetTabController();
92         controller->SetIndexWithoutChangeContent(static_cast<int32_t>(index_));
93         tabContentChild_->SetController(controller);
94 
95         vertical_ = parentNodeTmp->IsVertical();
96         tabContentChild_->SetVertical(vertical_);
97 
98         changeEventId_ = EventMarker(
99             parentNodeTmp->GetTabEventId(), parentNodeTmp->GetTabEventType(), parentNodeTmp->GetTabPageId());
100         if (parentNodeTmp->GetTabEventType() == DOM_CHANGE) {
101             tabContentChild_->SetChangeEventId(changeEventId_);
102         }
103         PrepareChangeListener(parentNodeTmp);
104     }
105 }
106 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)107 void DOMTabContent::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
108 {
109     if (!child) {
110         return;
111     }
112     if (tabContentChild_) {
113         tabContentChild_->RemoveChild(child->GetRootComponent());
114     }
115 }
116 
PrepareSpecializedComponent()117 void DOMTabContent::PrepareSpecializedComponent()
118 {
119     if (tabContentChild_) {
120         tabContentChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
121     }
122 }
123 
124 } // namespace OHOS::Ace::Framework
125