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_calendar.h"
17
18 #include <cstdint>
19 #include <optional>
20
21 #include "base/geometry/dimension.h"
22 #include "base/log/ace_scoring_log.h"
23 #include "base/memory/ace_type.h"
24 #include "base/utils/utils.h"
25 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
26 #include "core/common/ace_application_info.h"
27 #include "core/common/container.h"
28 #include "core/components/common/properties/color.h"
29 #include "core/components_ng/pattern/calendar/calendar_model_ng.h"
30 #include "frameworks/bridge/declarative_frontend/jsview/js_calendar_controller.h"
31 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
32 #include "frameworks/bridge/declarative_frontend/jsview/models/calendar_model_impl.h"
33
34 namespace OHOS::Ace {
35 std::unique_ptr<CalendarModel> CalendarModel::instance_ = nullptr;
36 std::mutex CalendarModel::mutex_;
37
GetInstance()38 CalendarModel* CalendarModel::GetInstance()
39 {
40 if (!instance_) {
41 std::lock_guard<std::mutex> lock(mutex_);
42 if (!instance_) {
43 #ifdef NG_BUILD
44 instance_.reset(new NG::CalendarModelNG());
45 #else
46 if (Container::IsCurrentUseNewPipeline()) {
47 instance_.reset(new NG::CalendarModelNG());
48 } else {
49 instance_.reset(new Framework::CalendarModelImpl());
50 }
51 #endif
52 }
53 }
54 return instance_.get();
55 }
56 } // namespace OHOS::Ace
57
58 namespace OHOS::Ace::Framework {
59 namespace {
60 constexpr int32_t CALENDAR_INVALID = -1;
61 } // namespace
62
JSBind(BindingTarget globalObj)63 void JSCalendar::JSBind(BindingTarget globalObj)
64 {
65 JSClass<JSCalendar>::Declare("Calendar");
66 MethodOptions opt = MethodOptions::NONE;
67 JSClass<JSCalendar>::StaticMethod("create", &JSCalendar::Create, opt);
68 JSClass<JSCalendar>::StaticMethod("showLunar", &JSCalendar::SetShowLunar, opt);
69 JSClass<JSCalendar>::StaticMethod("showHoliday", &JSCalendar::SetShowHoliday, opt);
70 JSClass<JSCalendar>::StaticMethod("needSlide", &JSCalendar::SetNeedSlide, opt);
71 JSClass<JSCalendar>::StaticMethod("startOfWeek", &JSCalendar::SetStartOfWeek, opt);
72 JSClass<JSCalendar>::StaticMethod("offDays", &JSCalendar::SetOffDays, opt);
73 JSClass<JSCalendar>::StaticMethod("onSelectChange", &JSCalendar::JsOnSelectedChange, opt);
74 JSClass<JSCalendar>::StaticMethod("onRequestData", &JSCalendar::JsOnRequestData, opt);
75 JSClass<JSCalendar>::StaticMethod("direction", &JSCalendar::SetDirection, opt);
76 JSClass<JSCalendar>::StaticMethod("currentDayStyle", &JSCalendar::SetCurrentDayStyle, opt);
77 JSClass<JSCalendar>::StaticMethod("nonCurrentDayStyle", &JSCalendar::SetNonCurrentDayStyle, opt);
78 JSClass<JSCalendar>::StaticMethod("todayStyle", &JSCalendar::SetTodayStyle, opt);
79 JSClass<JSCalendar>::StaticMethod("weekStyle", &JSCalendar::SetWeekStyle, opt);
80 JSClass<JSCalendar>::StaticMethod("workStateStyle", &JSCalendar::SetWorkStateStyle, opt);
81 JSClass<JSCalendar>::InheritAndBind<JSViewAbstract>(globalObj);
82 }
83
Create(const JSCallbackInfo & info)84 void JSCalendar::Create(const JSCallbackInfo& info)
85 {
86 if (!info[0]->IsObject()) {
87 return;
88 }
89 auto obj = JSRef<JSObject>::Cast(info[0]);
90 auto dataJsVal = obj->GetProperty("date");
91 auto currentDataJsVal = obj->GetProperty("currentData");
92 auto preDataJsVal = obj->GetProperty("preData");
93 auto nextDataJsVal = obj->GetProperty("nextData");
94 if (!(dataJsVal->IsObject() && currentDataJsVal->IsObject() && preDataJsVal->IsObject() &&
95 nextDataJsVal->IsObject())) {
96 return;
97 }
98 auto date = JSRef<JSObject>::Cast(dataJsVal);
99 auto currentData = JSRef<JSObject>::Cast(currentDataJsVal);
100 auto preData = JSRef<JSObject>::Cast(preDataJsVal);
101 auto nextData = JSRef<JSObject>::Cast(nextDataJsVal);
102 auto controllerObj = obj->GetProperty("controller");
103 auto yearValue = date->GetProperty("year");
104 auto monthValue = date->GetProperty("month");
105 auto dayValue = date->GetProperty("day");
106 if (!yearValue->IsNumber() || !monthValue->IsNumber() || !dayValue->IsNumber()) {
107 return;
108 }
109 CalendarDay day;
110 day.month.year = yearValue->ToNumber<int32_t>();
111 day.month.month = monthValue->ToNumber<int32_t>();
112 day.day = dayValue->ToNumber<int32_t>();
113 CalendarModelData calendarData;
114 calendarData.date = day;
115 ObtainedMonth currentMonthData = GetCurrentData(currentData);
116 ObtainedMonth preMonthData = GetPreData(preData);
117 ObtainedMonth nextMonthData = GetNextData(nextData);
118 calendarData.currentData = currentMonthData;
119 calendarData.preData = preMonthData;
120 calendarData.nextData = nextMonthData;
121 if (controllerObj->IsObject()) {
122 auto jsCalendarController = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSCalendarController>();
123 if (jsCalendarController) {
124 calendarData.controller = jsCalendarController->GetController();
125 }
126 }
127 CalendarModel::GetInstance()->Create(calendarData);
128 }
129
SetCalendarData(const JSRef<JSObject> & obj,MonthState monthState,const RefPtr<CalendarComponentV2> & component)130 void JSCalendar::SetCalendarData(
131 const JSRef<JSObject>& obj, MonthState monthState, const RefPtr<CalendarComponentV2>& component)
132 {
133 CHECK_NULL_VOID(component);
134
135 #if defined(PREVIEW)
136 if (obj->IsUndefined()) {
137 LOGE("obj is undefined");
138 return;
139 }
140 #endif
141
142 auto yearValue = obj->GetProperty("year");
143 auto monthValue = obj->GetProperty("month");
144 auto arrayValue = obj->GetProperty("data");
145 auto data = JsonUtil::ParseJsonString(arrayValue->ToString());
146 if (!yearValue->IsNumber() || !monthValue->IsNumber() || !data->IsArray()) {
147 return;
148 }
149 ObtainedMonth obtainedMonth;
150 obtainedMonth.year = yearValue->ToNumber<int32_t>();
151 obtainedMonth.month = monthValue->ToNumber<int32_t>();
152 std::vector<CalendarDay> days;
153 auto child = data->GetChild();
154 while (child && child->IsValid()) {
155 CalendarDay day;
156 day.index = child->GetInt("index");
157 day.lunarMonth = child->GetString("lunarMonth");
158 day.lunarDay = child->GetString("lunarDay");
159 day.dayMark = child->GetString("dayMark");
160 day.dayMarkValue = child->GetString("dayMarkValue");
161 day.month.year = child->GetInt("year");
162 day.month.month = child->GetInt("month");
163 day.day = child->GetInt("day");
164 if (day.day == 1 && obtainedMonth.firstDayIndex == CALENDAR_INVALID) {
165 obtainedMonth.firstDayIndex = day.index;
166 }
167 day.isFirstOfLunar = child->GetBool("isFirstOfLunar");
168 day.hasSchedule = child->GetBool("hasSchedule");
169 day.markLunarDay = child->GetBool("markLunarDay");
170 days.emplace_back(std::move(day));
171 child = child->GetNext();
172 }
173 obtainedMonth.days = days;
174 component->SetCalendarData(obtainedMonth);
175 }
176
GetCalendarData(const JSRef<JSObject> & obj,MonthState monthState)177 ObtainedMonth JSCalendar::GetCalendarData(const JSRef<JSObject>& obj, MonthState monthState)
178 {
179 #if defined(PREVIEW)
180 if (obj->IsUndefined()) {
181 LOGE("obj is undefined");
182 return ObtainedMonth();
183 }
184 #endif
185
186 auto yearValue = obj->GetProperty("year");
187 auto monthValue = obj->GetProperty("month");
188 auto arrayValue = obj->GetProperty("data");
189 auto data = JsonUtil::ParseJsonString(arrayValue->ToString());
190 if (!yearValue->IsNumber() || !monthValue->IsNumber() || !data->IsArray()) {
191 return ObtainedMonth();
192 }
193 ObtainedMonth obtainedMonth;
194 obtainedMonth.year = yearValue->ToNumber<int32_t>();
195 obtainedMonth.month = monthValue->ToNumber<int32_t>();
196 std::vector<CalendarDay> days;
197 auto child = data->GetChild();
198 while (child && child->IsValid()) {
199 CalendarDay day;
200 day.index = child->GetInt("index");
201 day.lunarMonth = child->GetString("lunarMonth");
202 day.lunarDay = child->GetString("lunarDay");
203 day.dayMark = child->GetString("dayMark");
204 day.dayMarkValue = child->GetString("dayMarkValue");
205 day.month.year = child->GetInt("year");
206 day.month.month = child->GetInt("month");
207 day.day = child->GetInt("day");
208 if (day.day == 1 && obtainedMonth.firstDayIndex == CALENDAR_INVALID) {
209 obtainedMonth.firstDayIndex = day.index;
210 }
211 day.isFirstOfLunar = child->GetBool("isFirstOfLunar");
212 day.hasSchedule = child->GetBool("hasSchedule");
213 day.markLunarDay = child->GetBool("markLunarDay");
214 days.emplace_back(std::move(day));
215 child = child->GetNext();
216 }
217 obtainedMonth.days = days;
218 return obtainedMonth;
219 }
220
SetCardCalendar(bool cardCalendar)221 void JSCalendar::SetCardCalendar(bool cardCalendar)
222 {
223 auto component = GetComponent();
224 CHECK_NULL_VOID(component);
225
226 component->SetCardCalendar(cardCalendar);
227 }
228
SetDate(const JSRef<JSObject> & obj,const RefPtr<CalendarComponentV2> & component)229 void JSCalendar::SetDate(const JSRef<JSObject>& obj, const RefPtr<CalendarComponentV2>& component)
230 {
231 if (component) {
232 auto yearValue = obj->GetProperty("year");
233 auto monthValue = obj->GetProperty("month");
234 auto dayValue = obj->GetProperty("day");
235 if (!yearValue->IsNumber() || !monthValue->IsNumber() || !dayValue->IsNumber()) {
236 return;
237 }
238 CalendarDay day;
239 day.month.year = yearValue->ToNumber<int32_t>();
240 day.month.month = monthValue->ToNumber<int32_t>();
241 day.day = dayValue->ToNumber<int32_t>();
242 component->SetCalendarDate(day);
243 }
244 }
245
SetHolidays(const std::string & holidays)246 void JSCalendar::SetHolidays(const std::string& holidays)
247 {
248 auto component = GetComponent();
249 CHECK_NULL_VOID(component);
250
251 component->SetHolidays(holidays);
252 }
253
SetOffDays(int32_t offDays)254 void JSCalendar::SetOffDays(int32_t offDays)
255 {
256 uint32_t bit = 0b1;
257 std::string result;
258 const static int32_t dayOfWeek = 7;
259 for (auto i = 0; i < dayOfWeek; ++i) {
260 if (bit & static_cast<uint32_t>(offDays)) {
261 result += std::to_string(i);
262 result += ",";
263 }
264 bit <<= 1;
265 }
266
267 CalendarModel::GetInstance()->SetOffDays(result);
268 }
269
SetShowHoliday(const JSCallbackInfo & info)270 void JSCalendar::SetShowHoliday(const JSCallbackInfo& info)
271 {
272 bool showHoliday = true;
273 if (info.Length() < 1) {
274 LOGE("The info is wrong, it is supposed to have atleast 1 arguments");
275 return;
276 }
277 if (info[0]->IsBoolean()) {
278 showHoliday = info[0]->ToBoolean();
279 }
280 CalendarModel::GetInstance()->SetShowHoliday(showHoliday);
281 }
282
SetShowLunar(const JSCallbackInfo & info)283 void JSCalendar::SetShowLunar(const JSCallbackInfo& info)
284 {
285 bool showLunar = false;
286 if (info.Length() < 1) {
287 LOGE("The info is wrong, it is supposed to have atleast 1 arguments");
288 return;
289 }
290 if (info[0]->IsBoolean()) {
291 showLunar = info[0]->ToBoolean();
292 }
293 CalendarModel::GetInstance()->SetShowLunar(showLunar);
294 }
295
SetStartOfWeek(const JSCallbackInfo & info)296 void JSCalendar::SetStartOfWeek(const JSCallbackInfo& info)
297 {
298 if (info.Length() < 1) {
299 LOGE("The info is wrong, it is supposed to have atleast 1 arguments");
300 return;
301 }
302 if (info[0]->IsNumber()) {
303 auto startOfWeek = info[0]->ToNumber<int32_t>();
304 CalendarModel::GetInstance()->SetStartOfWeek(startOfWeek);
305 }
306 }
307
SetNeedSlide(const JSCallbackInfo & info)308 void JSCalendar::SetNeedSlide(const JSCallbackInfo& info)
309 {
310 bool needSlide = false;
311 if (info.Length() < 1) {
312 LOGE("The info is wrong, it is supposed to have atleast 1 arguments");
313 return;
314 }
315 if (info[0]->IsBoolean()) {
316 needSlide = info[0]->ToBoolean();
317 }
318 CalendarModel::GetInstance()->SetNeedSlide(needSlide);
319 }
320
SetWorkDays(const std::string & workDays)321 void JSCalendar::SetWorkDays(const std::string& workDays)
322 {
323 auto component = GetComponent();
324 CHECK_NULL_VOID(component);
325
326 component->SetWorkDays(workDays);
327 }
328
GetComponent()329 RefPtr<CalendarComponentV2> JSCalendar::GetComponent()
330 {
331 auto stack = ViewStackProcessor::GetInstance();
332 if (!stack) {
333 return nullptr;
334 }
335 auto component = AceType::DynamicCast<CalendarComponentV2>(stack->GetMainComponent());
336 if (AceApplicationInfo::GetInstance().IsRightToLeft()) {
337 component->SetTextDirection(TextDirection::RTL);
338 }
339 return component;
340 }
341
JsOnSelectedChange(const JSCallbackInfo & info)342 void JSCalendar::JsOnSelectedChange(const JSCallbackInfo& info)
343 {
344 if (info.Length() < 1 || !info[0]->IsFunction()) {
345 LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
346 return;
347 }
348
349 if (info[0]->IsFunction()) {
350 auto selectedChangeFuc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
351 auto selectedChange = [execCtx = info.GetExecutionContext(), func = std::move(selectedChangeFuc)](
352 const std::string& info) {
353 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
354 std::vector<std::string> keys = { "year", "month", "day" };
355 ACE_SCORING_EVENT("Calendar.onSelectedChange");
356 func->Execute(keys, info);
357 };
358 CalendarModel::GetInstance()->SetSelectedChangeEvent(std::move(selectedChange));
359 }
360 }
361
JsOnRequestData(const JSCallbackInfo & info)362 void JSCalendar::JsOnRequestData(const JSCallbackInfo& info)
363 {
364 if (info.Length() < 1 || !info[0]->IsFunction()) {
365 LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
366 return;
367 }
368 auto requestDataFuc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
369 auto requestData = [execCtx = info.GetExecutionContext(), func = std::move(requestDataFuc)](
370 const std::string& info) {
371 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
372 ACE_SCORING_EVENT("Calendar.onRequestData");
373 std::vector<std::string> keys = { "year", "month", "currentMonth", "currentYear", "monthState" };
374 func->Execute(keys, info);
375 };
376 CalendarModel::GetInstance()->SetOnRequestDataEvent(std::move(requestData));
377 }
378
SetCurrentData(const JSRef<JSObject> & obj,const RefPtr<CalendarComponentV2> & component)379 void JSCalendar::SetCurrentData(const JSRef<JSObject>& obj, const RefPtr<CalendarComponentV2>& component)
380 {
381 SetCalendarData(obj, MonthState::CUR_MONTH, component);
382 }
383
GetCurrentData(const JSRef<JSObject> & obj)384 ObtainedMonth JSCalendar::GetCurrentData(const JSRef<JSObject>& obj)
385 {
386 return GetCalendarData(obj, MonthState::CUR_MONTH);
387 }
388
SetPreData(const JSRef<JSObject> & obj,const RefPtr<CalendarComponentV2> & component)389 void JSCalendar::SetPreData(const JSRef<JSObject>& obj, const RefPtr<CalendarComponentV2>& component)
390 {
391 SetCalendarData(obj, MonthState::PRE_MONTH, component);
392 }
393
GetPreData(const JSRef<JSObject> & obj)394 ObtainedMonth JSCalendar::GetPreData(const JSRef<JSObject>& obj)
395 {
396 return GetCalendarData(obj, MonthState::PRE_MONTH);
397 }
398
SetNextData(const JSRef<JSObject> & obj,const RefPtr<CalendarComponentV2> & component)399 void JSCalendar::SetNextData(const JSRef<JSObject>& obj, const RefPtr<CalendarComponentV2>& component)
400 {
401 SetCalendarData(obj, MonthState::NEXT_MONTH, component);
402 }
403
GetNextData(const JSRef<JSObject> & obj)404 ObtainedMonth JSCalendar::GetNextData(const JSRef<JSObject>& obj)
405 {
406 return GetCalendarData(obj, MonthState::NEXT_MONTH);
407 }
408
SetDirection(const JSCallbackInfo & info)409 void JSCalendar::SetDirection(const JSCallbackInfo& info)
410 {
411 if (info.Length() < 1) {
412 LOGE("The info is wrong, it is supposed to have atleast 1 arguments");
413 return;
414 }
415 if (info[0]->IsNumber()) {
416 auto dir = info[0]->ToNumber<int32_t>();
417 CalendarModel::GetInstance()->SetDirection(dir);
418 }
419 }
420
SetCurrentDayStyle(const JSCallbackInfo & info)421 void JSCalendar::SetCurrentDayStyle(const JSCallbackInfo& info)
422 {
423 if (info.Length() < 1 || !info[0]->IsObject()) {
424 LOGW("Invalid params");
425 return;
426 }
427 auto obj = JSRef<JSObject>::Cast(info[0]);
428 CurrentDayStyleData currentDayStyleData;
429 Color dayColor;
430 if (ConvertFromJSValue(obj->GetProperty("dayColor"), dayColor)) {
431 currentDayStyleData.dayColor = dayColor;
432 }
433 Color lunarColor;
434 if (ConvertFromJSValue(obj->GetProperty("lunarColor"), lunarColor)) {
435 currentDayStyleData.lunarColor = lunarColor;
436 }
437 Color markLunarColor;
438 if (ConvertFromJSValue(obj->GetProperty("markLunarColor"), markLunarColor)) {
439 currentDayStyleData.markLunarColor = markLunarColor;
440 }
441 CalcDimension dayFontSize;
442 if (ParseJsDimensionFp(obj->GetProperty("dayFontSize"), dayFontSize)) {
443 currentDayStyleData.dayFontSize = dayFontSize;
444 }
445 CalcDimension lunarDayFontSize;
446 if (ParseJsDimensionFp(obj->GetProperty("lunarDayFontSize"), lunarDayFontSize)) {
447 currentDayStyleData.lunarDayFontSize = lunarDayFontSize;
448 }
449 CalcDimension dayHeight;
450 if (ParseJsDimensionFp(obj->GetProperty("dayHeight"), dayHeight)) {
451 currentDayStyleData.dayHeight = dayHeight;
452 }
453 CalcDimension dayWidth;
454 if (ParseJsDimensionFp(obj->GetProperty("dayWidth"), dayWidth)) {
455 currentDayStyleData.dayWidth = dayWidth;
456 }
457 CalcDimension gregorianCalendarHeight;
458 if (ParseJsDimensionFp(obj->GetProperty("gregorianCalendarHeight"), gregorianCalendarHeight)) {
459 currentDayStyleData.gregorianCalendarHeight = gregorianCalendarHeight;
460 }
461 CalcDimension lunarHeight;
462 if (ParseJsDimensionFp(obj->GetProperty("lunarHeight"), lunarHeight)) {
463 currentDayStyleData.lunarHeight = lunarHeight;
464 }
465 CalcDimension dayYAxisOffset;
466 if (ParseJsDimensionFp(obj->GetProperty("dayYAxisOffset"), dayYAxisOffset)) {
467 currentDayStyleData.dayYAxisOffset = dayYAxisOffset;
468 }
469 CalcDimension lunarDayYAxisOffset;
470 if (ParseJsDimensionFp(obj->GetProperty("lunarDayYAxisOffset"), lunarDayYAxisOffset)) {
471 currentDayStyleData.lunarDayYAxisOffset = lunarDayYAxisOffset;
472 }
473 CalcDimension underscoreXAxisOffset;
474 if (ParseJsDimensionFp(obj->GetProperty("underscoreXAxisOffset"), underscoreXAxisOffset)) {
475 currentDayStyleData.underscoreXAxisOffset = underscoreXAxisOffset;
476 }
477 CalcDimension underscoreYAxisOffset;
478 if (ParseJsDimensionFp(obj->GetProperty("underscoreYAxisOffset"), underscoreYAxisOffset)) {
479 currentDayStyleData.underscoreYAxisOffset = underscoreYAxisOffset;
480 }
481 CalcDimension scheduleMarkerXAxisOffset;
482 if (ParseJsDimensionFp(obj->GetProperty("scheduleMarkerXAxisOffset"), scheduleMarkerXAxisOffset)) {
483 currentDayStyleData.scheduleMarkerXAxisOffset = scheduleMarkerXAxisOffset;
484 }
485 CalcDimension scheduleMarkerYAxisOffset;
486 if (ParseJsDimensionFp(obj->GetProperty("scheduleMarkerYAxisOffset"), scheduleMarkerYAxisOffset)) {
487 currentDayStyleData.scheduleMarkerYAxisOffset = scheduleMarkerYAxisOffset;
488 }
489 CalcDimension colSpace;
490 if (ParseJsDimensionFp(obj->GetProperty("colSpace"), colSpace)) {
491 currentDayStyleData.colSpace = colSpace;
492 }
493 CalcDimension dailyFiveRowSpace;
494 if (ParseJsDimensionFp(obj->GetProperty("dailyFiveRowSpace"), dailyFiveRowSpace)) {
495 currentDayStyleData.dailyFiveRowSpace = dailyFiveRowSpace;
496 }
497 CalcDimension dailySixRowSpace;
498 if (ParseJsDimensionFp(obj->GetProperty("dailySixRowSpace"), dailySixRowSpace)) {
499 currentDayStyleData.dailySixRowSpace = dailySixRowSpace;
500 }
501 CalcDimension underscoreWidth;
502 if (ParseJsDimensionFp(obj->GetProperty("underscoreWidth"), underscoreWidth)) {
503 currentDayStyleData.underscoreWidth = underscoreWidth;
504 }
505 CalcDimension underscoreLength;
506 if (ParseJsDimensionFp(obj->GetProperty("underscoreLength"), underscoreLength)) {
507 currentDayStyleData.underscoreLength = underscoreLength;
508 }
509 CalcDimension scheduleMarkerRadius;
510 if (ParseJsDimensionFp(obj->GetProperty("scheduleMarkerRadius"), scheduleMarkerRadius)) {
511 currentDayStyleData.scheduleMarkerRadius = scheduleMarkerRadius;
512 }
513 CalcDimension boundaryRowOffset;
514 if (ParseJsDimensionFp(obj->GetProperty("boundaryRowOffset"), boundaryRowOffset)) {
515 currentDayStyleData.boundaryRowOffset = boundaryRowOffset;
516 }
517 CalcDimension boundaryColOffset;
518 if (ParseJsDimensionFp(obj->GetProperty("boundaryColOffset"), boundaryColOffset)) {
519 currentDayStyleData.boundaryColOffset = boundaryColOffset;
520 }
521
522 CurrentDayStyleData CurrentDayStyleDataImpl;
523 ConvertFromJSValue(obj->GetProperty("dayColor"), CurrentDayStyleDataImpl.dayColor);
524 ConvertFromJSValue(obj->GetProperty("lunarColor"), CurrentDayStyleDataImpl.lunarColor);
525 ConvertFromJSValue(obj->GetProperty("markLunarColor"), CurrentDayStyleDataImpl.markLunarColor);
526 CalcDimension dayFontSize_impl;
527 if (ParseJsDimensionFp(obj->GetProperty("dayFontSize"), dayFontSize_impl)) {
528 CurrentDayStyleDataImpl.dayFontSize = dayFontSize_impl;
529 }
530 CalcDimension lunarDayFontSize_impl;
531 if (ParseJsDimensionFp(obj->GetProperty("lunarDayFontSize"), lunarDayFontSize_impl)) {
532 CurrentDayStyleDataImpl.lunarDayFontSize = lunarDayFontSize_impl;
533 }
534 ConvertFromJSValue(obj->GetProperty("dayHeight"), CurrentDayStyleDataImpl.dayHeight);
535 ConvertFromJSValue(obj->GetProperty("dayWidth"), CurrentDayStyleDataImpl.dayWidth);
536 ConvertFromJSValue(obj->GetProperty("gregorianCalendarHeight"), CurrentDayStyleDataImpl.gregorianCalendarHeight);
537 ConvertFromJSValue(obj->GetProperty("lunarHeight"), CurrentDayStyleDataImpl.lunarHeight);
538 ConvertFromJSValue(obj->GetProperty("dayYAxisOffset"), CurrentDayStyleDataImpl.dayYAxisOffset);
539 ConvertFromJSValue(obj->GetProperty("lunarDayYAxisOffset"), CurrentDayStyleDataImpl.lunarDayYAxisOffset);
540 ConvertFromJSValue(obj->GetProperty("underscoreXAxisOffset"), CurrentDayStyleDataImpl.underscoreXAxisOffset);
541 ConvertFromJSValue(obj->GetProperty("underscoreYAxisOffset"), CurrentDayStyleDataImpl.underscoreYAxisOffset);
542 ConvertFromJSValue(obj->GetProperty("scheduleMarkerXAxisOffset"),
543 CurrentDayStyleDataImpl.scheduleMarkerXAxisOffset);
544 ConvertFromJSValue(obj->GetProperty("scheduleMarkerYAxisOffset"),
545 CurrentDayStyleDataImpl.scheduleMarkerYAxisOffset);
546 ConvertFromJSValue(obj->GetProperty("colSpace"), CurrentDayStyleDataImpl.colSpace);
547 ConvertFromJSValue(obj->GetProperty("dailyFiveRowSpace"), CurrentDayStyleDataImpl.dailyFiveRowSpace);
548 ConvertFromJSValue(obj->GetProperty("dailySixRowSpace"), CurrentDayStyleDataImpl.dailySixRowSpace);
549 ConvertFromJSValue(obj->GetProperty("underscoreWidth"), CurrentDayStyleDataImpl.underscoreWidth);
550 ConvertFromJSValue(obj->GetProperty("underscoreLength"), CurrentDayStyleDataImpl.underscoreLength);
551 ConvertFromJSValue(obj->GetProperty("scheduleMarkerRadius"), CurrentDayStyleDataImpl.scheduleMarkerRadius);
552 ConvertFromJSValue(obj->GetProperty("boundaryRowOffset"), CurrentDayStyleDataImpl.boundaryRowOffset);
553 ConvertFromJSValue(obj->GetProperty("boundaryColOffset"), CurrentDayStyleDataImpl.boundaryColOffset);
554 ConvertFromJSValue(obj->GetProperty("touchCircleStrokeWidth"), CurrentDayStyleDataImpl.touchCircleStrokeWidth);
555
556 CalendarModel::GetInstance()->SetCurrentDayStyle(currentDayStyleData, CurrentDayStyleDataImpl);
557 }
558
SetNonCurrentDayStyle(const JSCallbackInfo & info)559 void JSCalendar::SetNonCurrentDayStyle(const JSCallbackInfo& info)
560 {
561 if (info.Length() < 1 || !info[0]->IsObject()) {
562 LOGW("Invalid params");
563 return;
564 }
565 auto obj = JSRef<JSObject>::Cast(info[0]);
566 NonCurrentDayStyleData nonCurrentDayStyleData;
567 Color nonCurrentMonthDayColor;
568 if (ConvertFromJSValue(obj->GetProperty("nonCurrentMonthDayColor"), nonCurrentMonthDayColor)) {
569 nonCurrentDayStyleData.nonCurrentMonthDayColor = nonCurrentMonthDayColor;
570 }
571 Color nonCurrentMonthLunarColor;
572 if (ConvertFromJSValue(obj->GetProperty("nonCurrentMonthLunarColor"), nonCurrentMonthLunarColor)) {
573 nonCurrentDayStyleData.nonCurrentMonthLunarColor = nonCurrentMonthLunarColor;
574 }
575 Color nonCurrentMonthWorkDayMarkColor;
576 if (ConvertFromJSValue(obj->GetProperty("nonCurrentMonthWorkDayMarkColor"), nonCurrentMonthWorkDayMarkColor)) {
577 nonCurrentDayStyleData.nonCurrentMonthWorkDayMarkColor = nonCurrentMonthWorkDayMarkColor;
578 }
579 Color nonCurrentMonthOffDayMarkColor;
580 if (ConvertFromJSValue(obj->GetProperty("nonCurrentMonthOffDayMarkColor"), nonCurrentMonthOffDayMarkColor)) {
581 nonCurrentDayStyleData.nonCurrentMonthOffDayMarkColor = nonCurrentMonthOffDayMarkColor;
582 }
583 CalendarModel::GetInstance()->SetNonCurrentDayStyle(nonCurrentDayStyleData);
584 }
585
SetTodayStyle(const JSCallbackInfo & info)586 void JSCalendar::SetTodayStyle(const JSCallbackInfo& info)
587 {
588 if (info.Length() < 1 || !info[0]->IsObject()) {
589 LOGW("Invalid params");
590 return;
591 }
592 auto obj = JSRef<JSObject>::Cast(info[0]);
593 TodayStyleData todayStyle;
594 Color focusedDayColor;
595 if (ConvertFromJSValue(obj->GetProperty("focusedDayColor"), focusedDayColor)) {
596 todayStyle.focusedDayColor = focusedDayColor;
597 }
598 Color focusedLunarColor;
599 if (ConvertFromJSValue(obj->GetProperty("focusedLunarColor"), focusedLunarColor)) {
600 todayStyle.focusedLunarColor = focusedLunarColor;
601 }
602 Color focusedAreaBackgroundColor;
603 if (ConvertFromJSValue(obj->GetProperty("focusedAreaBackgroundColor"), focusedAreaBackgroundColor)) {
604 todayStyle.focusedAreaBackgroundColor = focusedAreaBackgroundColor;
605 }
606 CalcDimension focusedAreaRadius;
607 if (ConvertFromJSValue(obj->GetProperty("focusedAreaRadius"), focusedAreaRadius)) {
608 todayStyle.focusedAreaRadius = focusedAreaRadius;
609 }
610 CalendarModel::GetInstance()->SetTodayStyle(todayStyle);
611 }
612
SetWeekStyle(const JSCallbackInfo & info)613 void JSCalendar::SetWeekStyle(const JSCallbackInfo& info)
614 {
615 if (info.Length() < 1 || !info[0]->IsObject()) {
616 LOGW("Invalid params");
617 return;
618 }
619 auto obj = JSRef<JSObject>::Cast(info[0]);
620 WeekStyleData weekStyle;
621 Color weekColor;
622 if (ConvertFromJSValue(obj->GetProperty("weekColor"), weekColor)) {
623 weekStyle.weekColor = weekColor;
624 }
625 Color weekendDayColor;
626 if (ConvertFromJSValue(obj->GetProperty("weekendDayColor"), weekendDayColor)) {
627 weekStyle.weekendDayColor = weekendDayColor;
628 }
629 Color weekendLunarColor;
630 if (ConvertFromJSValue(obj->GetProperty("weekendLunarColor"), weekendLunarColor)) {
631 weekStyle.weekendLunarColor = weekendLunarColor;
632 }
633 CalcDimension weekFontSize;
634 if (ParseJsDimensionFp(obj->GetProperty("weekFontSize"), weekFontSize)) {
635 weekStyle.weekFontSize = weekFontSize;
636 }
637 CalcDimension weekHeight;
638 if (ConvertFromJSValue(obj->GetProperty("weekHeight"), weekHeight)) {
639 weekStyle.weekHeight = weekHeight;
640 }
641 CalcDimension weekWidth;
642 if (ConvertFromJSValue(obj->GetProperty("weekWidth"), weekWidth)) {
643 weekStyle.weekWidth = weekWidth;
644 }
645 CalcDimension weekAndDayRowSpace;
646 if (ConvertFromJSValue(obj->GetProperty("weekAndDayRowSpace"), weekAndDayRowSpace)) {
647 weekStyle.weekAndDayRowSpace = weekAndDayRowSpace;
648 }
649 CalendarModel::GetInstance()->SetWeekStyle(weekStyle);
650 }
651
SetWorkStateStyle(const JSCallbackInfo & info)652 void JSCalendar::SetWorkStateStyle(const JSCallbackInfo& info)
653 {
654 if (info.Length() < 1 || !info[0]->IsObject()) {
655 LOGW("Invalid params");
656 return;
657 }
658 auto obj = JSRef<JSObject>::Cast(info[0]);
659 WorkStateStyleData workStateStyle;
660 Color workDayMarkColor;
661 if (ConvertFromJSValue(obj->GetProperty("workDayMarkColor"), workDayMarkColor)) {
662 workStateStyle.workDayMarkColor = workDayMarkColor;
663 }
664 Color offDayMarkColor;
665 if (ConvertFromJSValue(obj->GetProperty("offDayMarkColor"), offDayMarkColor)) {
666 workStateStyle.offDayMarkColor = offDayMarkColor;
667 }
668 CalcDimension workDayMarkSize;
669 if (ConvertFromJSValue(obj->GetProperty("workDayMarkSize"), workDayMarkSize)) {
670 workStateStyle.workDayMarkSize = workDayMarkSize;
671 }
672 CalcDimension offDayMarkSize;
673 if (ConvertFromJSValue(obj->GetProperty("offDayMarkSize"), offDayMarkSize)) {
674 workStateStyle.offDayMarkSize = offDayMarkSize;
675 }
676 CalcDimension workStateWidth;
677 if (ConvertFromJSValue(obj->GetProperty("workStateWidth"), workStateWidth)) {
678 workStateStyle.workStateWidth = workStateWidth;
679 }
680 CalcDimension workStateHorizontalMovingDistance;
681 if (ConvertFromJSValue(
682 obj->GetProperty("workStateHorizontalMovingDistance"), workStateHorizontalMovingDistance)) {
683 workStateStyle.workStateHorizontalMovingDistance = workStateHorizontalMovingDistance;
684 }
685 CalcDimension workStateVerticalMovingDistance;
686 if (ConvertFromJSValue(obj->GetProperty("workStateVerticalMovingDistance"), workStateVerticalMovingDistance)) {
687 workStateStyle.workStateVerticalMovingDistance = workStateVerticalMovingDistance;
688 }
689 CalendarModel::GetInstance()->SetWorkStateStyle(workStateStyle);
690 }
691 } // namespace OHOS::Ace::Framework
692