• 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 "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     RefPtr<ResourceObject> addressResObj;
81     std::string summary;
82     RefPtr<ResourceObject> contentResObj;
83     auto addressRet = ParseJsString(args[0], address, addressResObj);
84     bool contentRet = false;
85 
86     if (args.Length() == 2) {
87         contentRet = ParseJsString(args[1], summary, contentResObj);
88     }
89     HyperlinkModel::GetInstance()->Create(address, summary);
90     if (addressRet && SystemProperties::ConfigChangePerform() && addressResObj) {
91         RegisterResource<std::string>("Address", addressResObj, address);
92     } else {
93         UnRegisterResource("Address");
94     }
95     if (contentRet && SystemProperties::ConfigChangePerform() && contentResObj) {
96         RegisterResource<std::string>("Content", contentResObj, summary);
97     } else {
98         UnRegisterResource("Content");
99     }
100 }
101 
SetColor(const JSCallbackInfo & info)102 void JSHyperlink::SetColor(const JSCallbackInfo& info)
103 {
104     Color color;
105     RefPtr<ResourceObject> resObj;
106     if (!ParseJsColor(info[0], color, resObj)) {
107         auto pipelineContext = PipelineBase::GetCurrentContext();
108         CHECK_NULL_VOID(pipelineContext);
109         auto theme = pipelineContext->GetTheme<HyperlinkTheme>();
110         CHECK_NULL_VOID(theme);
111         color = theme->GetTextColor();
112         UnRegisterResource("Color");
113     } else if (SystemProperties::ConfigChangePerform() && resObj) {
114         RegisterResource<Color>("Color", resObj, color);
115     }
116     HyperlinkModel::GetInstance()->SetColor(color);
117 }
118 
Pop()119 void JSHyperlink::Pop()
120 {
121     if (ViewStackModel::GetInstance()->IsPrebuilding()) {
122         return ViewStackModel::GetInstance()->PushPrebuildCompCmd("[JSHyperlink][pop]", &JSHyperlink::Pop);
123     }
124     if (Container::IsCurrentUseNewPipeline()) {
125         ViewStackModel::GetInstance()->PopContainer();
126         return;
127     }
128 
129     HyperlinkModel::GetInstance()->Pop();
130 }
131 
JsSetDraggable(bool draggable)132 void JSHyperlink::JsSetDraggable(bool draggable)
133 {
134     HyperlinkModel::GetInstance()->SetDraggable(draggable);
135 }
136 
JsResponseRegion(const JSCallbackInfo & info)137 void JSHyperlink::JsResponseRegion(const JSCallbackInfo& info)
138 {
139     JSViewAbstract::JsResponseRegion(info);
140     HyperlinkModel::GetInstance()->SetResponseRegion(true);
141 }
142 
143 } // namespace OHOS::Ace::Framework