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