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_polyline.h"
17
18 #include "core/common/container.h"
19 #include "core/components_ng/pattern/shape/polygon_model.h"
20 #include "frameworks/base/memory/referenced.h"
21
22 namespace OHOS::Ace::Framework {
23
Create(const JSCallbackInfo & info)24 void JSPolyline::Create(const JSCallbackInfo& info)
25 {
26 PolygonModel::GetInstance()->Create(false);
27 JSShapeAbstract::SetSize(info);
28 }
29
JSBind(BindingTarget globalObj)30 void JSPolyline::JSBind(BindingTarget globalObj)
31 {
32 JSClass<JSPolyline>::Declare("Polyline");
33 MethodOptions opt = MethodOptions::NONE;
34 JSClass<JSPolyline>::StaticMethod("create", &JSPolyline::Create, opt);
35
36 JSClass<JSPolyline>::StaticMethod("width", &JSShapeAbstract::JsWidth);
37 JSClass<JSPolyline>::StaticMethod("height", &JSShapeAbstract::JsHeight);
38 JSClass<JSPolyline>::StaticMethod("points", &JSPolyline::JSPoints);
39
40 JSClass<JSPolyline>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
41 JSClass<JSPolyline>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
42 JSClass<JSPolyline>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
43 JSClass<JSPolyline>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
44
45 JSClass<JSPolyline>::InheritAndBind<JSShapeAbstract>(globalObj);
46 }
47
JSPoints(const JSCallbackInfo & info)48 void JSPolyline::JSPoints(const JSCallbackInfo& info)
49 {
50 if (info.Length() < 1 || !info[0]->IsArray()) {
51 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
52 return;
53 }
54 ShapePoint point;
55 ShapePoints points;
56
57 JSRef<JSArray> pointsArray = JSRef<JSArray>::Cast(info[0]);
58 if (pointsArray->Length() < 2) {
59 LOGE("Polyline have at least 2 points");
60 return;
61 }
62 for (size_t i = 0; i < pointsArray->Length(); i++) {
63 JSRef<JSVal> val = pointsArray->GetValueAt(i);
64 if (!val->IsArray()) {
65 LOGE("point is not array.");
66 continue;
67 }
68 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(val);
69 if (pointArray->GetValueAt(0)->IsNumber()) {
70 point.first = Dimension(pointArray->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
71 } else if (pointArray->GetValueAt(0)->IsString()) {
72 point.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true);
73 } else {
74 LOGE("Polyline point should be Number or String");
75 return;
76 }
77 if (pointArray->GetValueAt(1)->IsNumber()) {
78 point.second = Dimension(pointArray->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
79 } else if (pointArray->GetValueAt(1)->IsString()) {
80 point.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true);
81 } else {
82 LOGE("Polyline point should be Number or String");
83 return;
84 }
85 points.push_back(point);
86 }
87 PolygonModel::GetInstance()->SetPoints(points);
88 }
89
90 } // namespace OHOS::Ace::Framework
91