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_tabs.h"
17
18 #include "frameworks/bridge/common/dom/dom_tab_bar.h"
19 #include "frameworks/bridge/common/dom/dom_tab_content.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace::Framework {
23
24 // global variable to track the tabs' controllerId
25 static uint32_t g_domNodeControllerId = 0;
26
GetGlobalTabControllerId()27 uint32_t DOMTabs::GetGlobalTabControllerId()
28 {
29 return ++g_domNodeControllerId;
30 }
31
DOMTabs(NodeId nodeId,const std::string & nodeName)32 DOMTabs::DOMTabs(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
33 {
34 flexChild_ = CreateChild();
35 tabControllerId_ = GetGlobalTabControllerId();
36 tabController_ = TabController::GetController(tabControllerId_);
37 }
38
CreateChild() const39 RefPtr<FlexComponent> DOMTabs::CreateChild() const
40 {
41 RefPtr<FlexComponent> child;
42 if (vertical_) {
43 child = AceType::MakeRefPtr<RowComponent>(
44 FlexAlign::FLEX_START, FlexAlign::CENTER, std::list<RefPtr<Component>>());
45 } else {
46 child = AceType::MakeRefPtr<ColumnComponent>(
47 FlexAlign::FLEX_START, FlexAlign::CENTER, std::list<RefPtr<Component>>());
48 }
49 if (IsRightToLeft()) {
50 child->SetTextDirection(TextDirection::RTL);
51 } else {
52 child->SetTextDirection(TextDirection::LTR);
53 }
54 return child;
55 }
56
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)57 bool DOMTabs::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
58 {
59 if (attr.first == TAB_INDEX) {
60 tabIndex_ = std::max(0, StringToInt(attr.second));
61 LOGD("DOMTabs Index: %{public}u ControllerId: %{public}u", tabIndex_, tabControllerId_);
62 if (tabController_) {
63 tabController_->SetIndexByController(tabIndex_);
64 }
65 for (const auto& domChild : GetChildList()) {
66 if (AceType::InstanceOf<DOMTabBar>(domChild)) {
67 AceType::DynamicCast<DOMTabBar>(domChild)->UpdateIndex(tabIndex_);
68 break;
69 }
70 }
71 return true;
72 } else if (attr.first == TAB_IS_VERTICAL) {
73 if (vertical_ != StringToBool(attr.second)) {
74 vertical_ = StringToBool(attr.second);
75 auto newChild = CreateChild();
76 for (const auto& child : flexChild_->GetChildren()) {
77 newChild->AppendChild(child);
78 }
79 flexChild_ = newChild;
80 for (const auto& domChild : GetChildList()) {
81 if (AceType::InstanceOf<DOMTabBar>(domChild)) {
82 AceType::DynamicCast<DOMTabBar>(domChild)->SetVertical(vertical_);
83 }
84 if (AceType::InstanceOf<DOMTabContent>(domChild)) {
85 AceType::DynamicCast<DOMTabContent>(domChild)->SetVertical(vertical_);
86 }
87 }
88 }
89 return true;
90 }
91 return false;
92 }
93
AddSpecializedEvent(int32_t pageId,const std::string & event)94 bool DOMTabs::AddSpecializedEvent(int32_t pageId, const std::string& event)
95 {
96 if (event == DOM_CHANGE) {
97 tabPageId_ = static_cast<uint32_t>(pageId);
98 tabEventId_ = GetNodeIdForEvent();
99 tabEventType_ = event;
100 LOGD("DOMTabs AddEvent pageId:%{private}u EventId:%{private}s EventType:%{private}s",
101 tabPageId_, tabEventId_.c_str(), tabEventType_.c_str());
102 return true;
103 }
104 return false;
105 }
106
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)107 void DOMTabs::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
108 {
109 if (!child) {
110 LOGE("Child is nullptr, add node failed, slot:%{public}d.", slot);
111 return;
112 }
113 flexChild_->InsertChild(slot, child->GetRootComponent());
114 }
115
OnChildNodeRemoved(const RefPtr<DOMNode> & child)116 void DOMTabs::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
117 {
118 if (!child) {
119 LOGE("Child is nullptr, remove node failed.");
120 return;
121 }
122 flexChild_->RemoveChild(child->GetRootComponent());
123 }
124
125 } // namespace OHOS::Ace::Framework
126