• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/components/hyperlink/hyperlink_theme.h"
26 #include "core/components_ng/base/view_stack_model.h"
27 
28 
29 namespace OHOS::Ace {
30 
31 std::unique_ptr<HyperlinkModel> HyperlinkModel::instance_ = nullptr;
32 std::mutex HyperlinkModel::mutex_;
33 
GetInstance()34 HyperlinkModel* HyperlinkModel::GetInstance()
35 {
36     if (!instance_) {
37         std::lock_guard<std::mutex> lock(mutex_);
38         if (!instance_) {
39 #ifdef NG_BUILD
40             instance_.reset(new NG::HyperlinkModelNG());
41 #else
42             if (Container::IsCurrentUseNewPipeline()) {
43                 instance_.reset(new NG::HyperlinkModelNG());
44             } else {
45                 instance_.reset(new Framework::HyperlinkModelImpl());
46             }
47 #endif
48         }
49     }
50     return instance_.get();
51 }
52 
53 } // namespace OHOS::Ace
54 
55 namespace OHOS::Ace::Framework {
56 
JSBind(BindingTarget globalObj)57 void JSHyperlink::JSBind(BindingTarget globalObj)
58 {
59     JSClass<JSHyperlink>::Declare("Hyperlink");
60 
61     MethodOptions opt = MethodOptions::NONE;
62     JSClass<JSHyperlink>::StaticMethod("create", &JSHyperlink::Create, opt);
63     JSClass<JSHyperlink>::StaticMethod("color", &JSHyperlink::SetColor, opt);
64     JSClass<JSHyperlink>::StaticMethod("pop", &JSHyperlink::Pop);
65 
66     JSClass<JSHyperlink>::StaticMethod("draggable", &JSHyperlink::JsSetDraggable);
67     JSClass<JSHyperlink>::Inherit<JSInteractableView>();
68 
69     JSClass<JSHyperlink>::InheritAndBind<JSViewAbstract>(globalObj);
70 }
71 
Create(const JSCallbackInfo & args)72 void JSHyperlink::Create(const JSCallbackInfo& args)
73 {
74     std::string address;
75     ParseJsString(args[0], address);
76 
77     std::string summary;
78     if (args.Length() == 2) {
79         ParseJsString(args[1], summary);
80     }
81 
82     HyperlinkModel::GetInstance()->Create(address, summary);
83 }
84 
SetColor(const JSCallbackInfo & info)85 void JSHyperlink::SetColor(const JSCallbackInfo& info)
86 {
87     Color color;
88     if (!ParseJsColor(info[0], color)) {
89         auto pipelineContext = PipelineBase::GetCurrentContext();
90         CHECK_NULL_VOID(pipelineContext);
91         auto theme = pipelineContext->GetTheme<HyperlinkTheme>();
92         CHECK_NULL_VOID(theme);
93         color = theme->GetTextColor();
94     }
95     HyperlinkModel::GetInstance()->SetColor(color);
96 }
97 
Pop()98 void JSHyperlink::Pop()
99 {
100     if (Container::IsCurrentUseNewPipeline()) {
101         ViewStackModel::GetInstance()->PopContainer();
102         return;
103     }
104 
105     HyperlinkModel::GetInstance()->Pop();
106 }
107 
JsSetDraggable(bool draggable)108 void JSHyperlink::JsSetDraggable(bool draggable)
109 {
110     HyperlinkModel::GetInstance()->SetDraggable(draggable);
111 }
112 
113 } // namespace OHOS::Ace::Framework