1 /*
2  * Copyright (c) 2021-2023 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_blank.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "core/components/common/properties/color.h"
20 #include "core/components_ng/pattern/blank/blank_model_ng.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/models/blank_model_impl.h"
22 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
23 
24 namespace OHOS::Ace {
25 std::unique_ptr<BlankModel> BlankModel::instance_ = nullptr;
26 std::mutex BlankModel::mutex_;
27 
GetInstance()28 BlankModel* BlankModel::GetInstance()
29 {
30     if (!instance_) {
31         std::lock_guard<std::mutex> lock(mutex_);
32         if (!instance_) {
33 #ifdef NG_BUILD
34             instance_.reset(new NG::BlankModelNG());
35 #else
36             if (Container::IsCurrentUseNewPipeline()) {
37                 instance_.reset(new NG::BlankModelNG());
38             } else {
39                 instance_.reset(new Framework::BlankModelImpl());
40             }
41 #endif
42         }
43     }
44     return instance_.get();
45 }
46 } // namespace OHOS::Ace
47 
48 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)49 void JSBlank::Create(const JSCallbackInfo& info)
50 {
51     CalcDimension blankMin(0.0, DimensionUnit::VP);
52     BlankModel::GetInstance()->Create();
53     if (info[0]->IsUndefined()) {
54         BlankModel::GetInstance()->SetBlankMin(blankMin);
55         return;
56     }
57     if (ParseJsDimensionVp(info[0], blankMin)) {
58         if (blankMin.IsNegative() || blankMin.Unit() == DimensionUnit::PERCENT) {
59             blankMin.SetValue(0.0);
60         }
61         BlankModel::GetInstance()->SetBlankMin(blankMin);
62     }
63 }
64 
Height(const JSCallbackInfo & info)65 void JSBlank::Height(const JSCallbackInfo& info)
66 {
67     JSViewAbstract::JsHeight(info);
68     CalcDimension value;
69     if (!ParseJsDimensionVp(info[0], value)) {
70         return;
71     }
72 
73     BlankModel::GetInstance()->SetHeight(value);
74 }
75 
Color(const JSCallbackInfo & info)76 void JSBlank::Color(const JSCallbackInfo& info)
77 {
78     class Color value;
79     if (!ParseJsColor(info[0], value)) {
80         BlankModel::GetInstance()->SetColor(Color::TRANSPARENT);
81         return;
82     }
83     BlankModel::GetInstance()->SetColor(value);
84 }
85 
JSBind(BindingTarget globalObj)86 void JSBlank::JSBind(BindingTarget globalObj)
87 {
88     JSClass<JSBlank>::Declare("Blank");
89     MethodOptions opt = MethodOptions::NONE;
90     JSClass<JSBlank>::StaticMethod("create", &JSBlank::Create, opt);
91     JSClass<JSBlank>::StaticMethod("height", &JSBlank::Height, opt);
92     JSClass<JSBlank>::StaticMethod("color", &JSBlank::Color, opt);
93     JSClass<JSBlank>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
94     JSClass<JSBlank>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
95     JSClass<JSBlank>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
96     JSClass<JSBlank>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
97     JSClass<JSBlank>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
98     JSClass<JSBlank>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
99     JSClass<JSBlank>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
100     JSClass<JSBlank>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
101     JSClass<JSBlank>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
102     JSClass<JSBlank>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
103 
104     JSClass<JSBlank>::InheritAndBind<JSViewAbstract>(globalObj);
105 }
106 } // namespace OHOS::Ace::Framework
107