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         if (commands->IsString()) {
53             SetCommands(commands->ToString());
54         }
55     }
56 }
57 
SetCommands(const std::string& commands)58 void JSPath::SetCommands(const std::string& commands)
59 {
60     PathModel::GetInstance()->SetCommands(commands);
61 }
62 
ObjectCommands(const JSCallbackInfo& info)63 void JSPath::ObjectCommands(const JSCallbackInfo& info)
64 {
65     info.ReturnSelf();
66     if (info.Length() > 0 && info[0]->IsString()) {
67         auto path = AceType::DynamicCast<Path>(basicShape_);
68         if (path) {
69             path->SetValue(info[0]->ToString());
70         }
71     }
72 }
73 
ConstructorCallback(const JSCallbackInfo& info)74 void JSPath::ConstructorCallback(const JSCallbackInfo& info)
75 {
76     auto jsPath = AceType::MakeRefPtr<JSPath>();
77     auto path = AceType::MakeRefPtr<Path>();
78     if (info.Length() > 0 && info[0]->IsObject()) {
79         JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
80         JSRef<JSVal> commands = params->GetProperty("commands");
81         if (commands->IsString()) {
82             path->SetValue(commands->ToString());
83         }
84     }
85     jsPath->SetBasicShape(path);
86     jsPath->IncRefCount();
87     info.SetReturnValue(AceType::RawPtr(jsPath));
88 }
89 
DestructorCallback(JSPath* jsPath)90 void JSPath::DestructorCallback(JSPath* jsPath)
91 {
92     if (jsPath != nullptr) {
93         jsPath->DecRefCount();
94     }
95 }
96 
JSBind(BindingTarget globalObj)97 void JSPath::JSBind(BindingTarget globalObj)
98 {
99     JSClass<JSPath>::Declare("Path");
100     JSClass<JSPath>::StaticMethod("create", &JSPath::Create);
101     JSClass<JSPath>::StaticMethod("commands", &JSPath::SetCommands);
102 
103     JSClass<JSPath>::CustomMethod("commands", &JSPath::ObjectCommands);
104     JSClass<JSPath>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
105     JSClass<JSPath>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
106     JSClass<JSPath>::CustomMethod("position", &JSShapeAbstract::ObjectPosition);
107 
108     JSClass<JSPath>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
109     JSClass<JSPath>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
110     JSClass<JSPath>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
111     JSClass<JSPath>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
112     JSClass<JSPath>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
113     JSClass<JSPath>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
114 
115     JSClass<JSPath>::InheritAndBind<JSShapeAbstract>(
116         globalObj, JSPath::ConstructorCallback, JSPath::DestructorCallback);
117 }
118 
119 } // namespace OHOS::Ace::Framework
120