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 if (!instance_) {
36 std::lock_guard<std::mutex> lock(mutex_);
37 if (!instance_) {
38 #ifdef NG_BUILD
39 instance_.reset(new NG::HyperlinkModelNG());
40 #else
41 if (Container::IsCurrentUseNewPipeline()) {
42 instance_.reset(new NG::HyperlinkModelNG());
43 } else {
44 instance_.reset(new Framework::HyperlinkModelImpl());
45 }
46 #endif
47 }
48 }
49 return instance_.get();
50 }
51
52 } // namespace OHOS::Ace
53
54 namespace OHOS::Ace::Framework {
55
JSBind(BindingTarget globalObj)56 void JSHyperlink::JSBind(BindingTarget globalObj)
57 {
58 JSClass<JSHyperlink>::Declare("Hyperlink");
59
60 MethodOptions opt = MethodOptions::NONE;
61 JSClass<JSHyperlink>::StaticMethod("create", &JSHyperlink::Create, opt);
62 JSClass<JSHyperlink>::StaticMethod("color", &JSHyperlink::SetColor, opt);
63 JSClass<JSHyperlink>::StaticMethod("pop", &JSHyperlink::Pop);
64 JSClass<JSHyperlink>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
65 JSClass<JSHyperlink>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
66 JSClass<JSHyperlink>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
67 JSClass<JSHyperlink>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
68 JSClass<JSHyperlink>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
69 JSClass<JSHyperlink>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
70 JSClass<JSHyperlink>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
71 JSClass<JSHyperlink>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
72
73 JSClass<JSHyperlink>::StaticMethod("draggable", &JSHyperlink::JsSetDraggable);
74 JSClass<JSHyperlink>::StaticMethod("responseRegion", &JSHyperlink::JsResponseRegion);
75 JSClass<JSHyperlink>::Inherit<JSInteractableView>();
76
77 JSClass<JSHyperlink>::InheritAndBind<JSViewAbstract>(globalObj);
78 }
79
Create(const JSCallbackInfo & args)80 void JSHyperlink::Create(const JSCallbackInfo& args)
81 {
82 std::string address;
83 ParseJsString(args[0], address);
84
85 std::string summary;
86 if (args.Length() == 2) {
87 ParseJsString(args[1], summary);
88 }
89
90 HyperlinkModel::GetInstance()->Create(address, summary);
91 }
92
SetColor(const JSCallbackInfo & info)93 void JSHyperlink::SetColor(const JSCallbackInfo& info)
94 {
95 Color color;
96 if (!ParseJsColor(info[0], color)) {
97 auto pipelineContext = PipelineBase::GetCurrentContext();
98 CHECK_NULL_VOID(pipelineContext);
99 auto theme = pipelineContext->GetTheme<HyperlinkTheme>();
100 CHECK_NULL_VOID(theme);
101 color = theme->GetTextColor();
102 }
103 HyperlinkModel::GetInstance()->SetColor(color);
104 }
105
Pop()106 void JSHyperlink::Pop()
107 {
108 if (Container::IsCurrentUseNewPipeline()) {
109 ViewStackModel::GetInstance()->PopContainer();
110 return;
111 }
112
113 HyperlinkModel::GetInstance()->Pop();
114 }
115
JsSetDraggable(bool draggable)116 void JSHyperlink::JsSetDraggable(bool draggable)
117 {
118 HyperlinkModel::GetInstance()->SetDraggable(draggable);
119 }
120
JsResponseRegion(const JSCallbackInfo & info)121 void JSHyperlink::JsResponseRegion(const JSCallbackInfo& info)
122 {
123 JSViewAbstract::JsResponseRegion(info);
124 HyperlinkModel::GetInstance()->SetResponseRegion(true);
125 }
126
127 } // namespace OHOS::Ace::Framework