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