1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_hyperlink.h"
17
18 #include "core/components/hyperlink/hyperlink_theme.h"
19 #include "core/components_ng/base/view_stack_model.h"
20 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
21 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h"
25 #include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h"
26 #include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h"
27
28 namespace OHOS::Ace {
29
30 std::unique_ptr<HyperlinkModel> HyperlinkModel::instance_ = nullptr;
31 std::mutex HyperlinkModel::mutex_;
32
GetInstance()33 HyperlinkModel* HyperlinkModel::GetInstance()
34 {
35 #ifdef NG_BUILD
36 static NG::HyperlinkModelNG instance;
37 return &instance;
38 #else
39 if (Container::IsCurrentUseNewPipeline()) {
40 static NG::HyperlinkModelNG instance;
41 return &instance;
42 } else {
43 static Framework::HyperlinkModelImpl instance;
44 return &instance;
45 }
46 #endif
47 }
48
49 } // namespace OHOS::Ace
50
51 namespace OHOS::Ace::Framework {
52
JSBind(BindingTarget globalObj)53 void JSHyperlink::JSBind(BindingTarget globalObj)
54 {
55 JSClass<JSHyperlink>::Declare("Hyperlink");
56
57 MethodOptions opt = MethodOptions::NONE;
58 JSClass<JSHyperlink>::StaticMethod("create", &JSHyperlink::Create, opt);
59 JSClass<JSHyperlink>::StaticMethod("color", &JSHyperlink::SetColor, opt);
60 JSClass<JSHyperlink>::StaticMethod("pop", &JSHyperlink::Pop);
61 JSClass<JSHyperlink>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
62 JSClass<JSHyperlink>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
63 JSClass<JSHyperlink>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
64 JSClass<JSHyperlink>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
65 JSClass<JSHyperlink>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
66 JSClass<JSHyperlink>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
67 JSClass<JSHyperlink>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
68 JSClass<JSHyperlink>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
69
70 JSClass<JSHyperlink>::StaticMethod("draggable", &JSHyperlink::JsSetDraggable);
71 JSClass<JSHyperlink>::StaticMethod("responseRegion", &JSHyperlink::JsResponseRegion);
72 JSClass<JSHyperlink>::Inherit<JSInteractableView>();
73
74 JSClass<JSHyperlink>::InheritAndBind<JSViewAbstract>(globalObj);
75 }
76
Create(const JSCallbackInfo & args)77 void JSHyperlink::Create(const JSCallbackInfo& args)
78 {
79 std::string address;
80 ParseJsString(args[0], address);
81
82 std::string summary;
83 if (args.Length() == 2) {
84 ParseJsString(args[1], summary);
85 }
86
87 HyperlinkModel::GetInstance()->Create(address, summary);
88 }
89
SetColor(const JSCallbackInfo & info)90 void JSHyperlink::SetColor(const JSCallbackInfo& info)
91 {
92 Color color;
93 if (!ParseJsColor(info[0], color)) {
94 auto pipelineContext = PipelineBase::GetCurrentContext();
95 CHECK_NULL_VOID(pipelineContext);
96 auto theme = pipelineContext->GetTheme<HyperlinkTheme>();
97 CHECK_NULL_VOID(theme);
98 color = theme->GetTextColor();
99 }
100 HyperlinkModel::GetInstance()->SetColor(color);
101 }
102
Pop()103 void JSHyperlink::Pop()
104 {
105 if (Container::IsCurrentUseNewPipeline()) {
106 ViewStackModel::GetInstance()->PopContainer();
107 return;
108 }
109
110 HyperlinkModel::GetInstance()->Pop();
111 }
112
JsSetDraggable(bool draggable)113 void JSHyperlink::JsSetDraggable(bool draggable)
114 {
115 HyperlinkModel::GetInstance()->SetDraggable(draggable);
116 }
117
JsResponseRegion(const JSCallbackInfo & info)118 void JSHyperlink::JsResponseRegion(const JSCallbackInfo& info)
119 {
120 JSViewAbstract::JsResponseRegion(info);
121 HyperlinkModel::GetInstance()->SetResponseRegion(true);
122 }
123
124 } // namespace OHOS::Ace::Framework