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 "bridge/declarative_frontend/jsview/js_calendar_controller.h"
17
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "bridge/declarative_frontend/jsview/models/calendar_controller_model_impl.h"
20 #include "core/components_ng/pattern/calendar/calendar_controller_model_ng.h"
21
22 namespace OHOS::Ace {
23 std::unique_ptr<CalendarControllerModel> CalendarControllerModel::instance_ = nullptr;
24 std::once_flag CalendarControllerModel::onceFlag_;
25
GetInstance()26 CalendarControllerModel* CalendarControllerModel::GetInstance()
27 {
28 std::call_once(onceFlag_, []() {
29 #ifdef NG_BUILD
30 instance_.reset(new NG::CalendarControllerModelNG());
31 #else
32 if (Container::IsCurrentUseNewPipeline()) {
33 instance_.reset(new NG::CalendarControllerModelNG());
34 } else {
35 instance_.reset(new Framework::CalendarControllerModelImpl());
36 }
37 #endif
38 });
39
40 return instance_.get();
41 }
42 } // namespace OHOS::Ace
43
44 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)45 void JSCalendarController::JSBind(BindingTarget globalObj)
46 {
47 JSClass<JSCalendarController>::Declare("CalendarController");
48 JSClass<JSCalendarController>::CustomMethod("backToToday", &JSCalendarController::BackToToday);
49 JSClass<JSCalendarController>::CustomMethod("goTo", &JSCalendarController::GoTo);
50 JSClass<JSCalendarController>::Bind(globalObj,
51 JSCalendarController::Constructor, JSCalendarController::Destructor);
52 }
53
Constructor(const JSCallbackInfo & args)54 void JSCalendarController::Constructor(const JSCallbackInfo& args)
55 {
56 auto jsCalendarController = Referenced::MakeRefPtr<JSCalendarController>();
57 auto controller = CalendarControllerModel::GetInstance()->GetController();
58 jsCalendarController->SetController(controller);
59 jsCalendarController->IncRefCount();
60 args.SetReturnValue(Referenced::RawPtr(jsCalendarController));
61 }
62
Destructor(JSCalendarController * controller)63 void JSCalendarController::Destructor(JSCalendarController* controller)
64 {
65 if (controller != nullptr) {
66 controller->DecRefCount();
67 }
68 }
69
BackToToday(const JSCallbackInfo & args)70 void JSCalendarController::BackToToday(const JSCallbackInfo& args)
71 {
72 ContainerScope scope(instanceId_);
73 CalendarControllerModel::GetInstance()->BackToToday(controller_);
74 }
75
GoTo(const JSCallbackInfo & info)76 void JSCalendarController::GoTo(const JSCallbackInfo& info)
77 {
78 ContainerScope scope(instanceId_);
79 if (info.Length() != 1 || !info[0]->IsObject()) {
80 return;
81 }
82 auto obj = JSRef<JSObject>::Cast(info[0]);
83 int32_t year = 0;
84 int32_t month = 0;
85 int32_t day = 0;
86 ConvertFromJSValue(obj->GetProperty("year"), year);
87 ConvertFromJSValue(obj->GetProperty("month"), month);
88 ConvertFromJSValue(obj->GetProperty("day"), day);
89 CalendarControllerModel::GetInstance()->GoTo(year, month, day, controller_);
90 }
91 } // namespace OHOS::Ace::Framework