1 /*
2 * Copyright (c) 2023 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 "bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
19 #include "core/components/hyperlink/hyperlink_component.h"
20 #include "core/components/text/text_component.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
23 #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h"
24 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
25 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
26 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
28 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
29 
30 namespace OHOS::Ace::Framework {
Create(const std::string & address,const std::string & summary)31 void HyperlinkModelImpl::Create(const std::string& address, const std::string& summary)
32 {
33     std::list<RefPtr<Component>> children;
34     RefPtr<OHOS::Ace::HyperlinkComponent> component = AceType::MakeRefPtr<HyperlinkComponent>(children);
35     component->SetAddress(address);
36     if (!summary.empty()) {
37         component->SetSummary(summary);
38     }
39 
40     ViewStackProcessor::GetInstance()->Push(component);
41     JSInteractableView::SetFocusable(false);
42     JSInteractableView::SetFocusNode(true);
43 }
44 
Pop()45 void HyperlinkModelImpl::Pop()
46 {
47     auto hyperlink =
48         AceType::DynamicCast<OHOS::Ace::HyperlinkComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
49     JSContainerBase::Pop();
50     if (hyperlink) {
51         std::string summary = hyperlink->GetSummary();
52         std::list<RefPtr<Component>> children = hyperlink->GetChildren();
53         std::list<RefPtr<Component>> flexChild;
54         if (!summary.empty()) {
55             RefPtr<OHOS::Ace::TextComponent> text = AceType::MakeRefPtr<TextComponent>(summary);
56             flexChild.emplace_back(text);
57         }
58         for (const auto& child : children) {
59             flexChild.emplace_back(child);
60         }
61         RefPtr<OHOS::Ace::ColumnComponent> columnComponent =
62             AceType::MakeRefPtr<OHOS::Ace::ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, flexChild);
63         columnComponent->SetMainAxisSize(MainAxisSize::MIN);
64         columnComponent->SetCrossAxisSize(CrossAxisSize::MIN);
65         hyperlink->ClearChildren();
66         hyperlink->AppendChild(columnComponent);
67     }
68 }
69 
SetColor(const Color & value)70 void HyperlinkModelImpl::SetColor(const Color& value)
71 {
72     auto component = GetComponent();
73     if (!component) {
74         LOGE("component is not valid");
75         return;
76     }
77     component->SetColor(value);
78 }
79 
GetComponent()80 RefPtr<HyperlinkComponent> HyperlinkModelImpl::GetComponent()
81 {
82     auto stack = ViewStackProcessor::GetInstance();
83     if (!stack) {
84         return nullptr;
85     }
86     return AceType::DynamicCast<HyperlinkComponent>(stack->GetMainComponent());
87 }
88 } // namespace OHOS::Ace::Framework