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