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 "frameworks/bridge/declarative_frontend/engine/bindings.h"
19 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/models/hyperlink_model_impl.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
23 #include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model.h"
24 #include "frameworks/core/components_ng/pattern/hyperlink/hyperlink_model_ng.h"
25
26 namespace OHOS::Ace {
27
28 std::unique_ptr<HyperlinkModel> HyperlinkModel::instance_ = nullptr;
29 std::mutex HyperlinkModel::mutex_;
30
GetInstance()31 HyperlinkModel* HyperlinkModel::GetInstance()
32 {
33 if (!instance_) {
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (!instance_) {
36 #ifdef NG_BUILD
37 instance_.reset(new NG::HyperlinkModelNG());
38 #else
39 if (Container::IsCurrentUseNewPipeline()) {
40 instance_.reset(new NG::HyperlinkModelNG());
41 } else {
42 instance_.reset(new Framework::HyperlinkModelImpl());
43 }
44 #endif
45 }
46 }
47 return instance_.get();
48 }
49
50 } // namespace OHOS::Ace
51
52 namespace OHOS::Ace::Framework {
53
JSBind(BindingTarget globalObj)54 void JSHyperlink::JSBind(BindingTarget globalObj)
55 {
56 JSClass<JSHyperlink>::Declare("Hyperlink");
57
58 MethodOptions opt = MethodOptions::NONE;
59 JSClass<JSHyperlink>::StaticMethod("create", &JSHyperlink::Create, opt);
60 JSClass<JSHyperlink>::StaticMethod("color", &JSHyperlink::SetColor, opt);
61 JSClass<JSHyperlink>::StaticMethod("pop", &JSHyperlink::Pop);
62
63 JSClass<JSHyperlink>::StaticMethod("draggable", &JSHyperlink::JsSetDraggable);
64 JSClass<JSHyperlink>::Inherit<JSInteractableView>();
65
66 JSClass<JSHyperlink>::InheritAndBind<JSViewAbstract>(globalObj);
67 }
68
Create(const JSCallbackInfo & args)69 void JSHyperlink::Create(const JSCallbackInfo& args)
70 {
71 std::string address;
72 ParseJsString(args[0], address);
73
74 std::string summary;
75 if (args.Length() == 2) {
76 ParseJsString(args[1], summary);
77 }
78
79 HyperlinkModel::GetInstance()->Create(address, summary);
80 }
81
SetColor(const JSCallbackInfo & info)82 void JSHyperlink::SetColor(const JSCallbackInfo& info)
83 {
84 if (info.Length() < 1) {
85 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
86 return;
87 }
88 Color color;
89 if (!ParseJsColor(info[0], color)) {
90 return;
91 }
92 HyperlinkModel::GetInstance()->SetColor(color);
93 }
94
Pop()95 void JSHyperlink::Pop()
96 {
97 if (Container::IsCurrentUseNewPipeline()) {
98 JSViewAbstract::Pop();
99 return;
100 }
101
102 HyperlinkModel::GetInstance()->Pop();
103 }
104
JsSetDraggable(bool draggable)105 void JSHyperlink::JsSetDraggable(bool draggable)
106 {
107 HyperlinkModel::GetInstance()->SetDraggable(draggable);
108 }
109
110 } // namespace OHOS::Ace::Framework