• 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 
26 namespace OHOS::Ace {
27 
28 std::unique_ptr<HyperlinkModel> HyperlinkModel::instance_ = nullptr;
29 
GetInstance()30 HyperlinkModel* HyperlinkModel::GetInstance()
31 {
32     if (!instance_) {
33 #ifdef NG_BUILD
34         instance_.reset(new NG::HyperlinkModelNG());
35 #else
36         if (Container::IsCurrentUseNewPipeline()) {
37             instance_.reset(new NG::HyperlinkModelNG());
38         } else {
39             instance_.reset(new Framework::HyperlinkModelImpl());
40         }
41 #endif
42     }
43     return instance_.get();
44 }
45 
46 } // namespace OHOS::Ace
47 
48 namespace OHOS::Ace::Framework {
49 
JSBind(BindingTarget globalObj)50 void JSHyperlink::JSBind(BindingTarget globalObj)
51 {
52     JSClass<JSHyperlink>::Declare("Hyperlink");
53 
54     MethodOptions opt = MethodOptions::NONE;
55     JSClass<JSHyperlink>::StaticMethod("create", &JSHyperlink::Create, opt);
56     JSClass<JSHyperlink>::StaticMethod("color", &JSHyperlink::SetColor, opt);
57     JSClass<JSHyperlink>::StaticMethod("pop", &JSHyperlink::Pop);
58 
59     JSClass<JSHyperlink>::Inherit<JSViewAbstract>();
60     JSClass<JSHyperlink>::Inherit<JSInteractableView>();
61     JSClass<JSHyperlink>::Bind(globalObj);
62 }
63 
Create(const JSCallbackInfo & args)64 void JSHyperlink::Create(const JSCallbackInfo& args)
65 {
66     std::string address;
67     ParseJsString(args[0], address);
68 
69     std::string summary;
70     if (args.Length() == 2) {
71         ParseJsString(args[1], summary);
72     }
73 
74     HyperlinkModel::GetInstance()->Create(address, summary);
75 }
76 
SetColor(const JSCallbackInfo & info)77 void JSHyperlink::SetColor(const JSCallbackInfo& info)
78 {
79     if (info.Length() < 1) {
80         LOGE("The argv is wrong, it is supposed to have at least 1 argument");
81         return;
82     }
83     Color color;
84     if (!ParseJsColor(info[0], color)) {
85         return;
86     }
87     HyperlinkModel::GetInstance()->SetColor(color);
88 }
89 
Pop()90 void JSHyperlink::Pop()
91 {
92     if (Container::IsCurrentUseNewPipeline()) {
93         JSViewAbstract::Pop();
94         return;
95     }
96 
97     HyperlinkModel::GetInstance()->Pop();
98 }
99 
100 } // namespace OHOS::Ace::Framework
101