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         if (tabController_) {
62             tabController_->SetIndexByController(tabIndex_);
63         }
64         for (const auto& domChild : GetChildList()) {
65             if (AceType::InstanceOf<DOMTabBar>(domChild)) {
66                 AceType::DynamicCast<DOMTabBar>(domChild)->UpdateIndex(tabIndex_);
67                 break;
68             }
69         }
70         return true;
71     } else if (attr.first == TAB_IS_VERTICAL) {
72         if (vertical_ != StringToBool(attr.second)) {
73             vertical_ = StringToBool(attr.second);
74             auto newChild = CreateChild();
75             for (const auto& child : flexChild_->GetChildren()) {
76                 newChild->AppendChild(child);
77             }
78             flexChild_ = newChild;
79             for (const auto& domChild : GetChildList()) {
80                 if (AceType::InstanceOf<DOMTabBar>(domChild)) {
81                     AceType::DynamicCast<DOMTabBar>(domChild)->SetVertical(vertical_);
82                 }
83                 if (AceType::InstanceOf<DOMTabContent>(domChild)) {
84                     AceType::DynamicCast<DOMTabContent>(domChild)->SetVertical(vertical_);
85                 }
86             }
87         }
88         return true;
89     }
90     return false;
91 }
92 
AddSpecializedEvent(int32_t pageId,const std::string & event)93 bool DOMTabs::AddSpecializedEvent(int32_t pageId, const std::string& event)
94 {
95     if (event == DOM_CHANGE) {
96         tabPageId_ = static_cast<uint32_t>(pageId);
97         tabEventId_ = GetNodeIdForEvent();
98         tabEventType_ = event;
99         return true;
100     }
101     return false;
102 }
103 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)104 void DOMTabs::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
105 {
106     if (!child) {
107         return;
108     }
109     flexChild_->InsertChild(slot, child->GetRootComponent());
110 }
111 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)112 void DOMTabs::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
113 {
114     if (!child) {
115         return;
116     }
117     flexChild_->RemoveChild(child->GetRootComponent());
118 }
119 
120 } // namespace OHOS::Ace::Framework
121