1 /*
2 * Copyright (c) 2021 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_polygon.h"
17
18 #include "bridge/declarative_frontend/jsview/models/polygon_model_impl.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/pattern/shape/polygon_model.h"
21 #include "core/components_ng/pattern/shape/polygon_model_ng.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<PolygonModel> PolygonModel::instance_ = nullptr;
26
GetInstance()27 PolygonModel* PolygonModel::GetInstance()
28 {
29 if (!instance_) {
30 #ifdef NG_BUILD
31 instance_.reset(new NG::PolygonModelNG());
32 #else
33 if (Container::IsCurrentUseNewPipeline()) {
34 instance_.reset(new NG::PolygonModelNG());
35 } else {
36 instance_.reset(new Framework::PolygonModelImpl());
37 }
38 #endif
39 }
40 return instance_.get();
41 }
42
43 } // namespace OHOS::Ace
44
45 namespace OHOS::Ace::Framework {
46
Create(const JSCallbackInfo & info)47 void JSPolygon::Create(const JSCallbackInfo& info)
48 {
49 PolygonModel::GetInstance()->Create(true);
50 JSShapeAbstract::SetSize(info);
51 }
52
JSBind(BindingTarget globalObj)53 void JSPolygon::JSBind(BindingTarget globalObj)
54 {
55 JSClass<JSPolygon>::Declare("Polygon");
56 MethodOptions opt = MethodOptions::NONE;
57 JSClass<JSPolygon>::StaticMethod("create", &JSPolygon::Create, opt);
58
59 JSClass<JSPolygon>::StaticMethod("width", &JSShapeAbstract::JsWidth);
60 JSClass<JSPolygon>::StaticMethod("height", &JSShapeAbstract::JsHeight);
61 JSClass<JSPolygon>::StaticMethod("points", &JSPolygon::JsPoints);
62
63 JSClass<JSPolygon>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
64 JSClass<JSPolygon>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
65 JSClass<JSPolygon>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
66 JSClass<JSPolygon>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
67
68 JSClass<JSPolygon>::Inherit<JSShapeAbstract>();
69 JSClass<JSPolygon>::Bind(globalObj);
70 }
71
JsPoints(const JSCallbackInfo & info)72 void JSPolygon::JsPoints(const JSCallbackInfo& info)
73 {
74 if (info.Length() < 1) {
75 LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
76 return;
77 }
78 ShapePoint shapePoint;
79 ShapePoints shapePoints;
80 JSRef<JSArray> pointsArray = JSRef<JSArray>::Cast(info[0]);
81 if (pointsArray->Length() < 2) {
82 LOGE("Less than two parameters");
83 return;
84 } else {
85 for (size_t i = 0; i < pointsArray->Length(); i++) {
86 JSRef<JSArray> pointArray = pointsArray->GetValueAt(i);
87 if (pointArray->GetValueAt(0)->IsNumber()) {
88 shapePoint.first = Dimension(pointArray->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
89 } else if (pointArray->GetValueAt(0)->IsString()) {
90 shapePoint.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true);
91 } else {
92 LOGE("Polygon point should be Number or String");
93 return;
94 }
95 if (pointArray->GetValueAt(1)->IsNumber()) {
96 shapePoint.second = Dimension(pointArray->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
97 } else if (pointArray->GetValueAt(1)->IsString()) {
98 shapePoint.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true);
99 } else {
100 LOGE("Polygon point should be Number or String");
101 return;
102 }
103 shapePoints.push_back(shapePoint);
104 }
105 PolygonModel::GetInstance()->SetPoints(shapePoints);
106 }
107 }
108
109 } // namespace OHOS::Ace::Framework
110