1 /*
2 * Copyright (c) 2021-2022 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_path.h"
17
18 #include "bridge/declarative_frontend/jsview/models/path_model_impl.h"
19 #include "core/common/container.h"
20 #include "core/components/shape/shape_component.h"
21 #include "core/components_ng/pattern/shape/path_model.h"
22 #include "core/components_ng/pattern/shape/path_model_ng.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24
25 namespace OHOS::Ace {
26
27 std::unique_ptr<PathModel> PathModel::instance_ = nullptr;
28
GetInstance()29 PathModel* PathModel::GetInstance()
30 {
31 if (!instance_) {
32 #ifdef NG_BUILD
33 instance_.reset(new NG::PathModelNG());
34 #else
35 if (Container::IsCurrentUseNewPipeline()) {
36 instance_.reset(new NG::PathModelNG());
37 } else {
38 instance_.reset(new Framework::PathModelImpl());
39 }
40 #endif
41 }
42 return instance_.get();
43 }
44
45 } // namespace OHOS::Ace
46
47 namespace OHOS::Ace::Framework {
48
Create(const JSCallbackInfo & info)49 void JSPath::Create(const JSCallbackInfo& info)
50 {
51 PathModel::GetInstance()->Create();
52 JSShapeAbstract::SetSize(info);
53 if (info.Length() > 0 && info[0]->IsObject()) {
54 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
55 JSRef<JSVal> commands = obj->GetProperty("commands");
56 if (commands->IsString()) {
57 SetCommands(commands->ToString());
58 }
59 }
60 }
61
SetCommands(const std::string & commands)62 void JSPath::SetCommands(const std::string& commands)
63 {
64 PathModel::GetInstance()->SetCommands(commands);
65 }
66
ObjectCommands(const JSCallbackInfo & info)67 void JSPath::ObjectCommands(const JSCallbackInfo& info)
68 {
69 info.ReturnSelf();
70 if (info.Length() > 0 && info[0]->IsString()) {
71 auto path = AceType::DynamicCast<Path>(basicShape_);
72 if (path) {
73 path->SetValue(info[0]->ToString());
74 }
75 }
76 }
77
ConstructorCallback(const JSCallbackInfo & info)78 void JSPath::ConstructorCallback(const JSCallbackInfo& info)
79 {
80 auto jsPath = AceType::MakeRefPtr<JSPath>();
81 auto path = AceType::MakeRefPtr<Path>();
82 if (info.Length() > 0 && info[0]->IsObject()) {
83 JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
84 JSRef<JSVal> commands = params->GetProperty("commands");
85 if (commands->IsString()) {
86 path->SetValue(commands->ToString());
87 }
88 }
89 jsPath->SetBasicShape(path);
90 jsPath->IncRefCount();
91 info.SetReturnValue(AceType::RawPtr(jsPath));
92 }
93
DestructorCallback(JSPath * jsPath)94 void JSPath::DestructorCallback(JSPath* jsPath)
95 {
96 if (jsPath != nullptr) {
97 jsPath->DecRefCount();
98 }
99 }
100
JSBind(BindingTarget globalObj)101 void JSPath::JSBind(BindingTarget globalObj)
102 {
103 JSClass<JSPath>::Declare("Path");
104 JSClass<JSPath>::StaticMethod("create", &JSPath::Create);
105 JSClass<JSPath>::StaticMethod("commands", &JSPath::SetCommands);
106
107 JSClass<JSPath>::CustomMethod("commands", &JSPath::ObjectCommands);
108 JSClass<JSPath>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
109 JSClass<JSPath>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
110
111 JSClass<JSPath>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
112 JSClass<JSPath>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
113 JSClass<JSPath>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
114 JSClass<JSPath>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
115 JSClass<JSPath>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
116 JSClass<JSPath>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
117
118 JSClass<JSPath>::Inherit<JSShapeAbstract>();
119 JSClass<JSPath>::Bind(globalObj, JSPath::ConstructorCallback, JSPath::DestructorCallback);
120 }
121
122 } // namespace OHOS::Ace::Framework
123