1 /*
2  * Copyright (c) 2021-2022 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 "core/components_v2/inspector/calendar_composed_element.h"
17 
18 #include "base/log/dump_log.h"
19 #include "core/components/calendar/calendar_element.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_v2/inspector/utils.h"
22 
23 namespace OHOS::Ace::V2 {
24 
25 using CalendarJsonFunc = std::function<std::unique_ptr<JsonValue>(const CalendarComposedElement&)>;
26 
27 const std::unordered_map<std::string, std::function<std::string(const CalendarComposedElement&)>> CREATE_JSON_MAP {
__anonf1513b160102(const CalendarComposedElement& inspector) 28     { "showLunar", [](const CalendarComposedElement& inspector) { return inspector.GetShowLunar(); } },
__anonf1513b160202(const CalendarComposedElement& inspector) 29     { "showHoliday", [](const CalendarComposedElement& inspector) { return inspector.GetShowHoliday(); } },
__anonf1513b160302(const CalendarComposedElement& inspector) 30     { "needSlide", [](const CalendarComposedElement& inspector) { return inspector.GetNeedSlide(); } },
__anonf1513b160402(const CalendarComposedElement& inspector) 31     { "startOfWeek", [](const CalendarComposedElement& inspector) { return inspector.GetStartOfWeek(); } },
__anonf1513b160502(const CalendarComposedElement& inspector) 32     { "direction", [](const CalendarComposedElement& inspector) { return inspector.GetAxis(); } },
__anonf1513b160602(const CalendarComposedElement& inspector) 33     { "offDay", [](const CalendarComposedElement& inspector) { return inspector.GetOffDay(); } },
34 };
35 
36 const std::unordered_map<std::string, CalendarJsonFunc> CREATE_CALENDAR_THEME_JASON {
__anonf1513b160702() 37     { "currentDayStyle", [](const CalendarComposedElement& inspector) { return inspector.GetCurrentDayStyle(); } },
38     { "nonCurrentDayStyle",
__anonf1513b160802() 39         [](const CalendarComposedElement& inspector) { return inspector.GetNonCurrentDayStyle(); } },
__anonf1513b160902() 40     { "todayStyle", [](const CalendarComposedElement& inspector) { return inspector.GetTodayStyle(); } },
__anonf1513b160a02() 41     { "weekStyle", [](const CalendarComposedElement& inspector) { return inspector.GetWeekStyle(); } },
__anonf1513b160b02() 42     { "workStateStyle", [](const CalendarComposedElement& inspector) { return inspector.GetWorkStateStyle(); } },
43 };
44 
ToJsonObject() const45 std::unique_ptr<JsonValue> CalendarComposedElement::ToJsonObject() const
46 {
47     auto resultJson = InspectorComposedElement::ToJsonObject();
48     for (const auto& value : CREATE_JSON_MAP) {
49         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
50     }
51     for (const auto& value : CREATE_CALENDAR_THEME_JASON) {
52         resultJson->Put(value.first.c_str(), value.second(*this));
53     }
54     return resultJson;
55 }
56 
GetShowLunar() const57 std::string CalendarComposedElement::GetShowLunar() const
58 {
59     auto renderCalendar = GetRenderCalendar();
60     auto showLunar = renderCalendar ? renderCalendar->GetCalendarDataAdapter()->ShowLunar() : false;
61     return ConvertBoolToString(showLunar);
62 }
63 
GetShowHoliday() const64 std::string CalendarComposedElement::GetShowHoliday() const
65 {
66     auto renderCalendar = GetRenderCalendar();
67     auto showHoliday = renderCalendar ? renderCalendar->GetShowHoliday() : true;
68     return ConvertBoolToString(showHoliday);
69 }
70 
GetNeedSlide() const71 std::string CalendarComposedElement::GetNeedSlide() const
72 {
73     auto renderCalendar = GetRenderCalendar();
74     auto needSlide = renderCalendar ? renderCalendar->GetNeedSlide() : false;
75     return ConvertBoolToString(needSlide);
76 }
77 
GetStartOfWeek() const78 std::string CalendarComposedElement::GetStartOfWeek() const
79 
80 {
81     auto renderCalendar = GetRenderCalendar();
82     auto startDayOfWeek = renderCalendar ? renderCalendar->GetStartDayOfWeek() : 6;
83     return ConvertWeekToString(std::to_string(startDayOfWeek));
84 }
85 
GetOffDay() const86 std::string CalendarComposedElement::GetOffDay() const
87 {
88     auto renderCalendar = GetRenderCalendar();
89     auto offday = renderCalendar ? renderCalendar->GetOffdays() : "";
90     return ConvertOffDayToString(offday);
91 }
92 
GetCurrentDayStyle() const93 std::unique_ptr<JsonValue> CalendarComposedElement::GetCurrentDayStyle() const
94 {
95     auto renderCalendar = GetRenderCalendar();
96     return renderCalendar ? ConvertThemeToDayStyle(renderCalendar->GetCalendarTheme()) : nullptr;
97 }
98 
GetNonCurrentDayStyle() const99 std::unique_ptr<JsonValue> CalendarComposedElement::GetNonCurrentDayStyle() const
100 {
101     auto renderCalendar = GetRenderCalendar();
102     return renderCalendar ? ConvertThemeToNonCurrentDayStyle(renderCalendar->GetCalendarTheme()) : nullptr;
103 }
104 
GetTodayStyle() const105 std::unique_ptr<JsonValue> CalendarComposedElement::GetTodayStyle() const
106 {
107     auto renderCalendar = GetRenderCalendar();
108     return renderCalendar ? ConvertThemeToTodayStyle(renderCalendar->GetCalendarTheme()) : nullptr;
109 }
110 
GetWeekStyle() const111 std::unique_ptr<JsonValue> CalendarComposedElement::GetWeekStyle() const
112 {
113     auto renderCalendar = GetRenderCalendar();
114     return renderCalendar ? ConvertThemeToWeekStyle(renderCalendar->GetCalendarTheme()) : nullptr;
115 }
116 
GetWorkStateStyle() const117 std::unique_ptr<JsonValue> CalendarComposedElement::GetWorkStateStyle() const
118 {
119     auto renderCalendar = GetRenderCalendar();
120     return renderCalendar ? ConvertThemeToWorkStateStyle(renderCalendar->GetCalendarTheme()) : nullptr;
121 }
122 
GetAxis() const123 std::string CalendarComposedElement::GetAxis() const
124 {
125     auto renderCalendar = GetRenderCalendar();
126     auto axis = renderCalendar ? renderCalendar->GetAxis() : Axis::HORIZONTAL;
127     switch (axis) {
128         case Axis::VERTICAL:
129             return "Axis.Vertical";
130         case Axis::HORIZONTAL:
131         default:
132             return "Axis.Horizontal";
133     }
134     return ConvertAxisToString(axis);
135 }
136 
ConvertWeekToString(std::string week) const137 std::string CalendarComposedElement::ConvertWeekToString(std::string week) const
138 {
139     if (week == "0") {
140         return "Mon";
141     } else if (week == "1") {
142         return "Tue";
143     } else if (week == "2") {
144         return "Wed";
145     } else if (week == "3") {
146         return "Thur";
147     } else if (week == "4") {
148         return "Fri";
149     } else if (week == "5") {
150         return "Sat";
151     } else if (week == "6") {
152         return "Sun";
153     } else {
154         return "Sun";
155     }
156 }
157 
ConvertOffDayToString(std::string offDay) const158 std::string CalendarComposedElement::ConvertOffDayToString(std::string offDay) const
159 {
160     std::string result = "Sun|Sat";
161     if (!offDay.empty()) {
162         offDay = offDay.substr(0, offDay.size() - 1);
163         auto data = SplitString(offDay, ",");
164         result = ConvertOffDayFamily(data);
165     }
166     return result;
167 }
168 
SplitString(const std::string & s,const std::string & c) const169 std::vector<std::string> CalendarComposedElement::SplitString(const std::string& s, const std::string& c) const
170 {
171     std::vector<std::string> v;
172     std::string::size_type pos1, pos2;
173     pos1 = 0;
174     pos2 = s.find(c);
175     while (std::string::npos != pos2) {
176         v.push_back(s.substr(pos1, pos2 - pos1));
177         pos1 = pos2 + c.size();
178         pos2 = s.find(c, pos1);
179     }
180     if (pos1 != s.length()) {
181         v.push_back(s.substr(pos1));
182     }
183     return v;
184 }
185 
ConvertOffDayFamily(const std::vector<std::string> & data) const186 std::string CalendarComposedElement::ConvertOffDayFamily(const std::vector<std::string>& data) const
187 {
188     std::string result = "";
189     for (const auto& item : data) {
190         result += ConvertWeekToString(item);
191         result += "|";
192     }
193     result = result.substr(0, result.size() ? result.size() - 1 : 0);
194     return result;
195 }
196 
GetRenderCalendar() const197 RefPtr<RenderCalendar> CalendarComposedElement::GetRenderCalendar() const
198 {
199     auto node = GetInspectorNode(CalendarElement::TypeId());
200     if (node) {
201         while (!AceType::DynamicCast<RenderCalendar>(node)) {
202             node = node->GetChildren().front();
203         }
204         return AceType::DynamicCast<RenderCalendar>(node);
205     }
206     return nullptr;
207 }
208 
ConvertThemeToDayStyle(const CalendarThemeStructure & theme) const209 std::unique_ptr<JsonValue> CalendarComposedElement::ConvertThemeToDayStyle(const CalendarThemeStructure& theme) const
210 {
211     Color dayColor = theme.dayColor;
212     Color lunarColor = theme.lunarColor;
213     Color markLunarColor = theme.markLunarColor;
214     Dimension dayFontSize = theme.dayFontSize;
215     Dimension lunarDayFontSize = theme.lunarDayFontSize;
216     Dimension dayHeight = theme.dayHeight;
217     Dimension dayWidth = theme.dayWidth;
218     Dimension gregorianCalendarHeight = theme.gregorianCalendarHeight;
219     Dimension dayYAxisOffset = theme.dayYAxisOffset;
220     Dimension lunarDayYAxisOffset = theme.lunarDayYAxisOffset;
221     Dimension underscoreXAxisOffset = theme.underscoreXAxisOffset;
222     Dimension underscoreYAxisOffset = theme.underscoreYAxisOffset;
223     Dimension scheduleMarkerXAxisOffset = theme.scheduleMarkerXAxisOffset;
224     Dimension scheduleMarkerYAxisOffset = theme.scheduleMarkerYAxisOffset;
225     auto dayStyle = JsonUtil::Create(true);
226     dayStyle->Put("dayColor", ConvertColorToString(dayColor).c_str());
227     dayStyle->Put("lunarColor", ConvertColorToString(lunarColor).c_str());
228     dayStyle->Put("markLunarColor", ConvertColorToString(markLunarColor).c_str());
229     dayStyle->Put("dayFontSize", std::to_string(dayFontSize.Value()).c_str());
230     dayStyle->Put("lunarDayFontSize", std::to_string(lunarDayFontSize.Value()).c_str());
231     dayStyle->Put("dayHeight", std::to_string(dayHeight.Value()).c_str());
232     dayStyle->Put("dayWidth", std::to_string(dayWidth.Value()).c_str());
233     dayStyle->Put("gregorianCalendarHeight", std::to_string(gregorianCalendarHeight.Value()).c_str());
234     dayStyle->Put("dayYAxisOffset", std::to_string(dayYAxisOffset.Value()).c_str());
235     dayStyle->Put("lunarDayYAxisOffset", std::to_string(lunarDayYAxisOffset.Value()).c_str());
236     dayStyle->Put("underscoreXAxisOffset", std::to_string(underscoreXAxisOffset.Value()).c_str());
237     dayStyle->Put("underscoreYAxisOffset", std::to_string(underscoreYAxisOffset.Value()).c_str());
238     dayStyle->Put("scheduleMarkerXAxisOffset", std::to_string(scheduleMarkerXAxisOffset.Value()).c_str());
239     dayStyle->Put("scheduleMarkerYAxisOffset", std::to_string(scheduleMarkerYAxisOffset.Value()).c_str());
240     return dayStyle;
241 }
242 
ConvertThemeToNonCurrentDayStyle(const CalendarThemeStructure & theme) const243 std::unique_ptr<JsonValue> CalendarComposedElement::ConvertThemeToNonCurrentDayStyle(
244     const CalendarThemeStructure& theme) const
245 {
246     Color nonCurrentMonthDayColor = theme.nonCurrentMonthDayColor;
247     Color nonCurrentMonthLunarColor = theme.nonCurrentMonthLunarColor;
248     Color nonCurrentMonthWorkDayMarkColor = theme.nonCurrentMonthWorkDayMarkColor;
249     Color nonCurrentMonthOffDayMarkColor = theme.nonCurrentMonthOffDayMarkColor;
250     auto nonCurrentDayStyle = JsonUtil::Create(true);
251     nonCurrentDayStyle->Put("nonCurrentMonthDayColor", ConvertColorToString(nonCurrentMonthDayColor).c_str());
252     nonCurrentDayStyle->Put("nonCurrentMonthLunarColor", ConvertColorToString(nonCurrentMonthLunarColor).c_str());
253     nonCurrentDayStyle->Put(
254         "nonCurrentMonthWorkDayMarkColor", ConvertColorToString(nonCurrentMonthWorkDayMarkColor).c_str());
255     nonCurrentDayStyle->Put(
256         "nonCurrentMonthOffDayMarkColor", ConvertColorToString(nonCurrentMonthOffDayMarkColor).c_str());
257     return nonCurrentDayStyle;
258 }
259 
ConvertThemeToTodayStyle(const CalendarThemeStructure & theme) const260 std::unique_ptr<JsonValue> CalendarComposedElement::ConvertThemeToTodayStyle(const CalendarThemeStructure& theme) const
261 {
262     Color focusedDayColor = theme.focusedDayColor;
263     Color focusedLunarColor = theme.focusedLunarColor;
264     Color focusedAreaBackgroundColor = theme.focusedAreaBackgroundColor;
265     Dimension focusedAreaRadius = theme.focusedAreaRadius;
266     auto todayStyle = JsonUtil::Create(true);
267     todayStyle->Put("focusedDayColor", ConvertColorToString(focusedDayColor).c_str());
268     todayStyle->Put("focusedLunarColor", ConvertColorToString(focusedLunarColor).c_str());
269     todayStyle->Put("focusedAreaBackgroundColor", ConvertColorToString(focusedAreaBackgroundColor).c_str());
270     todayStyle->Put("focusedAreaRadius", std::to_string(focusedAreaRadius.Value()).c_str());
271     return todayStyle;
272 }
273 
ConvertThemeToWeekStyle(const CalendarThemeStructure & theme) const274 std::unique_ptr<JsonValue> CalendarComposedElement::ConvertThemeToWeekStyle(const CalendarThemeStructure& theme) const
275 {
276     Color weekColor = theme.weekColor;
277     Color weekendDayColor = theme.weekendDayColor;
278     Color weekendLunarColor = theme.weekendLunarColor;
279     Dimension weekFontSize = theme.weekFontSize;
280     Dimension weekHeight = theme.weekHeight;
281     Dimension weekWidth = theme.weekWidth;
282     Dimension weekAndDayRowSpace = theme.weekAndDayRowSpace;
283     auto weekStyle = JsonUtil::Create(true);
284     weekStyle->Put("weekColor", ConvertColorToString(weekColor).c_str());
285     weekStyle->Put("weekendDayColor", ConvertColorToString(weekendDayColor).c_str());
286     weekStyle->Put("weekendLunarColor", ConvertColorToString(weekendLunarColor).c_str());
287     weekStyle->Put("weekFontSize", std::to_string(weekFontSize.Value()).c_str());
288     weekStyle->Put("weekHeight", std::to_string(weekHeight.Value()).c_str());
289     weekStyle->Put("weekWidth", std::to_string(weekWidth.Value()).c_str());
290     weekStyle->Put("weekAndDayRowSpace", std::to_string(weekAndDayRowSpace.Value()).c_str());
291     return weekStyle;
292 }
293 
ConvertThemeToWorkStateStyle(const CalendarThemeStructure & theme) const294 std::unique_ptr<JsonValue> CalendarComposedElement::ConvertThemeToWorkStateStyle(
295     const CalendarThemeStructure& theme) const
296 {
297     Color workDayMarkColor = theme.workDayMarkColor;
298     Color offDayMarkColor = theme.offDayMarkColor;
299     Dimension workDayMarkSize = theme.workDayMarkSize;
300     Dimension offDayMarkSize = theme.offDayMarkSize;
301     Dimension workStateWidth = theme.workStateWidth;
302     Dimension workStateHorizontalMovingDistance = theme.workStateHorizontalMovingDistance;
303     Dimension workStateVerticalMovingDistance = theme.workStateVerticalMovingDistance;
304     auto workStateStyle = JsonUtil::Create(true);
305     workStateStyle->Put("workDayMarkColor", ConvertColorToString(workDayMarkColor).c_str());
306     workStateStyle->Put("offDayMarkColor", ConvertColorToString(offDayMarkColor).c_str());
307     workStateStyle->Put("workDayMarkSize", std::to_string(workDayMarkSize.Value()).c_str());
308     workStateStyle->Put("offDayMarkSize", std::to_string(offDayMarkSize.Value()).c_str());
309     workStateStyle->Put("workStateWidth", std::to_string(workStateWidth.Value()).c_str());
310     workStateStyle->Put(
311         "workStateHorizontalMovingDistance", std::to_string(workStateHorizontalMovingDistance.Value()).c_str());
312     workStateStyle->Put(
313         "workStateVerticalMovingDistance", std::to_string(workStateVerticalMovingDistance.Value()).c_str());
314     return workStateStyle;
315 }
316 
317 } // namespace OHOS::Ace::V2
318