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 LOGE("Child is nullptr, add node failed, slot:%{public}d.", slot);
45 return;
46 }
47 if (tabContentChild_) {
48 tabContentChild_->InsertChild(slot, child->GetRootComponent());
49 }
50 }
51
PrepareChangeListener(const RefPtr<DOMTabs> & parentNode)52 void DOMTabContent::PrepareChangeListener(const RefPtr<DOMTabs>& parentNode)
53 {
54 auto weak = AceType::WeakClaim(this);
55 WeakPtr<DOMTabs> weakParent = parentNode;
56 auto changeCallback = [weak, weakParent](uint32_t currentIndex) {
57 auto domNode = weak.Upgrade();
58 if (!domNode) {
59 LOGE("get dom node failed!");
60 return;
61 }
62
63 auto parentRef = weakParent.Upgrade();
64 if (!parentRef) {
65 LOGE("get parent node failed!");
66 return;
67 }
68 for (const auto& node : parentRef->GetChildList()) {
69 if (node->GetTag() == DOM_NODE_TAG_TAB_BAR) {
70 const auto& tabBarNode = AceType::DynamicCast<DOMTabBar>(node);
71 if (tabBarNode) {
72 tabBarNode->UpdateIndex(currentIndex);
73 break;
74 }
75 }
76 }
77 };
78 auto changeMarker = BackEndEventManager<void(uint32_t)>::GetInstance().GetAvailableMarker();
79 BackEndEventManager<void(uint32_t)>::GetInstance().BindBackendEvent(changeMarker, changeCallback);
80 tabContentChild_->SetDomChangeEventId(changeMarker);
81 }
82
OnMounted(const RefPtr<DOMNode> & parentNode)83 void DOMTabContent::OnMounted(const RefPtr<DOMNode>& parentNode)
84 {
85 if (!parentNode) {
86 return;
87 }
88 if (parentNode->GetTag() == DOM_NODE_TAG_TABS) {
89 const auto& parentNodeTmp = AceType::DynamicCast<DOMTabs>(parentNode);
90 if (!parentNodeTmp) {
91 return;
92 }
93 index_ = parentNodeTmp->GetTabIndex();
94 controllerId_ = parentNodeTmp->GetTabControllerId();
95 const auto& controller = parentNodeTmp->GetTabController();
96 controller->SetIndexWithoutChangeContent(static_cast<int32_t>(index_));
97 tabContentChild_->SetController(controller);
98
99 vertical_ = parentNodeTmp->IsVertical();
100 tabContentChild_->SetVertical(vertical_);
101
102 changeEventId_ = EventMarker(
103 parentNodeTmp->GetTabEventId(), parentNodeTmp->GetTabEventType(), parentNodeTmp->GetTabPageId());
104 if (parentNodeTmp->GetTabEventType() == DOM_CHANGE) {
105 tabContentChild_->SetChangeEventId(changeEventId_);
106 }
107 PrepareChangeListener(parentNodeTmp);
108 }
109 }
110
OnChildNodeRemoved(const RefPtr<DOMNode> & child)111 void DOMTabContent::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
112 {
113 if (!child) {
114 LOGE("Child is nullptr, remove node failed.");
115 return;
116 }
117 if (tabContentChild_) {
118 tabContentChild_->RemoveChild(child->GetRootComponent());
119 }
120 }
121
PrepareSpecializedComponent()122 void DOMTabContent::PrepareSpecializedComponent()
123 {
124 if (tabContentChild_) {
125 tabContentChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
126 }
127 }
128
129 } // namespace OHOS::Ace::Framework
130