1 /*
2  * Copyright (c) 2022-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_common_view.h"
17 
18 #include "bridge/declarative_frontend/jsview/models/common_view_model_impl.h"
19 #include "core/components_ng/pattern/common_view/common_view_model_ng.h"
20 
21 namespace OHOS::Ace {
22 std::unique_ptr<CommonViewModel> CommonViewModel::instance_ = nullptr;
23 std::mutex CommonViewModel::mutex_;
24 
GetInstance()25 CommonViewModel* CommonViewModel::GetInstance()
26 {
27     if (!instance_) {
28         std::lock_guard<std::mutex> lock(mutex_);
29         if (!instance_) {
30 #ifdef NG_BUILD
31             instance_.reset(new NG::CommonViewModelNG());
32 #else
33             if (Container::IsCurrentUseNewPipeline()) {
34                 instance_.reset(new NG::CommonViewModelNG());
35             } else {
36                 instance_.reset(new Framework::CommonViewModelImpl());
37             }
38 #endif
39         }
40     }
41     return instance_.get();
42 }
43 } // namespace OHOS::Ace
44 
45 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)46 void JSCommonView::Create(const JSCallbackInfo& info)
47 {
48     CommonViewModel::GetInstance()->Create(false);
49 }
50 
JSBind(BindingTarget globalObj)51 void JSCommonView::JSBind(BindingTarget globalObj)
52 {
53     JSClass<JSCommonView>::Declare("__Common__");
54     MethodOptions opt = MethodOptions::NONE;
55     JSClass<JSCommonView>::StaticMethod("create", &JSCommonView::Create, opt);
56     JSClass<JSCommonView>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
57     JSClass<JSCommonView>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
58     JSClass<JSCommonView>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
59     JSClass<JSCommonView>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
60     JSClass<JSCommonView>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
61     JSClass<JSCommonView>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
62     JSClass<JSCommonView>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
63     JSClass<JSCommonView>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
64     JSClass<JSCommonView>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
65     JSClass<JSCommonView>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
66 
67     JSClass<JSCommonView>::InheritAndBind<JSContainerBase>(globalObj);
68 }
69 } // namespace OHOS::Ace::Framework
70