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 {
GetInstance()26 PathModel* PathModel::GetInstance()
27 {
28 #ifdef NG_BUILD
29 static NG::PathModelNG instance;
30 return &instance;
31 #else
32 if (Container::IsCurrentUseNewPipeline()) {
33 static NG::PathModelNG instance;
34 return &instance;
35 } else {
36 static Framework::PathModelImpl instance;
37 return &instance;
38 }
39 #endif
40 }
41 } // namespace OHOS::Ace
42
43 namespace OHOS::Ace::Framework {
44
Create(const JSCallbackInfo & info)45 void JSPath::Create(const JSCallbackInfo& info)
46 {
47 PathModel::GetInstance()->Create();
48 JSShapeAbstract::SetSize(info);
49 if (info.Length() > 0 && info[0]->IsObject()) {
50 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
51 JSRef<JSVal> commands = obj->GetProperty("commands");
52 std::string strRet;
53 RefPtr<ResourceObject> commandsResObj;
54 if (!ParseJsString(commands, strRet, commandsResObj)) {
55 return;
56 }
57 UnRegisterResource("PathCommands");
58 if (SystemProperties::ConfigChangePerform() && commandsResObj) {
59 RegisterResource<std::string>("PathCommands", commandsResObj, strRet);
60 }
61 PathModel::GetInstance()->SetCommands(strRet);
62 }
63 }
64
SetCommands(const JSCallbackInfo & info)65 void JSPath::SetCommands(const JSCallbackInfo& info)
66 {
67 if (info.Length() < 1) {
68 return;
69 }
70
71 UnRegisterResource("PathCommands");
72 if (info[0]->IsUndefined()) {
73 PathModel::GetInstance()->SetCommands("undefined");
74 } else if (info[0]->IsObject()) {
75 JSRef<JSObject> commandsObj = JSRef<JSObject>::Cast(info[0]);
76 std::string strRet;
77 RefPtr<ResourceObject> commandsResObj;
78 if (!ParseJsString(commandsObj, strRet, commandsResObj)) {
79 return;
80 }
81 if (SystemProperties::ConfigChangePerform() && commandsResObj) {
82 RegisterResource<std::string>("PathCommands", commandsResObj, strRet);
83 }
84 PathModel::GetInstance()->SetCommands(strRet);
85 } else if (info[0]->IsString()) {
86 PathModel::GetInstance()->SetCommands(info[0]->ToString());
87 }
88 }
89
ObjectCommands(const JSCallbackInfo & info)90 void JSPath::ObjectCommands(const JSCallbackInfo& info)
91 {
92 info.ReturnSelf();
93 if (info.Length() > 0 && info[0]->IsString()) {
94 auto path = AceType::DynamicCast<Path>(basicShape_);
95 if (path) {
96 path->SetValue(info[0]->ToString());
97 }
98 }
99 }
100
ConstructorCallback(const JSCallbackInfo & info)101 void JSPath::ConstructorCallback(const JSCallbackInfo& info)
102 {
103 auto jsPath = AceType::MakeRefPtr<JSPath>();
104 auto path = AceType::MakeRefPtr<Path>();
105 if (info.Length() > 0 && info[0]->IsObject()) {
106 JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
107 JSRef<JSVal> commands = params->GetProperty("commands");
108 if (commands->IsString()) {
109 path->SetValue(commands->ToString());
110 }
111 }
112 jsPath->SetBasicShape(path);
113 jsPath->IncRefCount();
114 info.SetReturnValue(AceType::RawPtr(jsPath));
115 }
116
DestructorCallback(JSPath * jsPath)117 void JSPath::DestructorCallback(JSPath* jsPath)
118 {
119 if (jsPath != nullptr) {
120 jsPath->DecRefCount();
121 }
122 }
123
JSBind(BindingTarget globalObj)124 void JSPath::JSBind(BindingTarget globalObj)
125 {
126 JSClass<JSPath>::Declare("Path");
127 JSClass<JSPath>::StaticMethod("create", &JSPath::Create);
128 JSClass<JSPath>::StaticMethod("commands", &JSPath::SetCommands);
129
130 JSClass<JSPath>::CustomMethod("commands", &JSPath::ObjectCommands);
131 JSClass<JSPath>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
132 JSClass<JSPath>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
133 JSClass<JSPath>::CustomMethod("position", &JSShapeAbstract::ObjectPosition);
134
135 JSClass<JSPath>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
136 JSClass<JSPath>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
137 JSClass<JSPath>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
138 JSClass<JSPath>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
139 JSClass<JSPath>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
140 JSClass<JSPath>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
141
142 JSClass<JSPath>::InheritAndBind<JSShapeAbstract>(
143 globalObj, JSPath::ConstructorCallback, JSPath::DestructorCallback);
144 }
145
146 } // namespace OHOS::Ace::Framework
147