• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "calendar_enum_napi.h"
17 #include <map>
18 #include <vector>
19 #include <string_view>
20 #include "calendar_define.h"
21 #include "napi_queue.h"
22 
23 
24 namespace OHOS::CalendarApi {
25 struct JsEnumInt {
26     std::string_view enumName;
27     int32_t enumInt;
28 };
29 
30 struct JsEnumString {
31     std::string_view enumName;
32     std::string_view enumString;
33 };
34 
35 
36 static const std::vector<struct JsEnumInt> g_eventType = {
37     { "IMPORTANT", EventType::Important },
38     { "NORMAL", EventType::Normal },
39 };
40 
41 static const std::vector<struct JsEnumInt> g_recurrenceFrequency = {
42     { "YEARLY", RecurrenceType::YEARLY },
43     { "MONTHLY", RecurrenceType::MONTHLY },
44     { "WEEKLY", RecurrenceType::WEEKLY },
45     { "DAILY", RecurrenceType::DAILY },
46 };
47 
48 static const std::vector<struct JsEnumString> g_calendarTypeKey = {
49     { "LOCAL", "local" },
50     { "EMAIL", "email" },
51     { "BIRTHDAY", "birthday" },
52     { "CALDAV", "caldav" },
53     { "SUBSCRIBED", "subscribed" },
54 };
55 
56 static const std::vector<struct JsEnumString> g_serviceType = {
57     { "MEETING", "Meeting" },
58     { "WATCHING", "Watching" },
59     { "REPAYMENT", "Repayment" },
60     { "LIVE", "Live" },
61     { "SHOPPING", "Shopping" },
62     { "TRIP", "Trip" },
63     { "CLASS", "Class" },
64     { "SPORTS_EVENTS", "SportsEvents" },
65     { "SPORTS_EXERCISE", "SportsExercise" },
66 };
67 
68 static const std::vector<struct JsEnumString> g_attendeeRole = {
69     { "ORGANIZER", "organizer" },
70     { "PARTICIPANT", "participant" },
71 };
72 
73 static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
74     { "EventType", g_eventType},
75     { "RecurrenceFrequency", g_recurrenceFrequency},
76 };
77 
78 static const std::map<std::string_view, const std::vector<struct JsEnumString>&> g_stringEnumClassMap = {
79     { "CalendarType", g_calendarTypeKey },
80     { "ServiceType", g_serviceType },
81     { "AttendeeRole", g_attendeeRole },
82 };
83 
JsEnumIntInit(napi_env env,napi_value exports)84 napi_value CalendarEnumNapi::JsEnumIntInit(napi_env env, napi_value exports)
85 {
86     for (const auto &enumClass : g_intEnumClassMap) {
87         auto &enumClassName = enumClass.first;
88         auto &enumItemVec = enumClass.second;
89         int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
90         std::vector<napi_value> value;
91         value.resize(vecSize);
92         for (int32_t index = 0; index < vecSize; ++index) {
93             napi_create_int32(env, enumItemVec[index].enumInt, &value[index]);
94         }
95 
96         std::vector<napi_property_descriptor> property;
97         property.resize(vecSize);
98         for (int32_t index = 0; index < vecSize; ++index) {
99             property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
100                 enumItemVec[index].enumName.data(), value[index]);
101         }
102 
103         auto constructor = [](napi_env env, napi_callback_info info) {
104             napi_value jsThis = nullptr;
105             napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
106             return jsThis;
107         };
108 
109         napi_value result = nullptr;
110         napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
111             nullptr, property.size(), property.data(), &result);
112         CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
113 
114         status = napi_set_named_property(env, exports, enumClassName.data(), result);
115         CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
116     }
117     return exports;
118 }
119 
JsEnumStringInit(napi_env env,napi_value exports)120 napi_value CalendarEnumNapi::JsEnumStringInit(napi_env env, napi_value exports)
121 {
122     for (auto it = g_stringEnumClassMap.begin(); it != g_stringEnumClassMap.end(); it++) {
123         auto &enumClassName = it->first;
124         auto &enumItemVec = it->second;
125         int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
126         std::vector<napi_value> value;
127         value.resize(vecSize);
128         for (int32_t index = 0; index < vecSize; ++index) {
129             napi_create_string_utf8(env, enumItemVec[index].enumString.data(), NAPI_AUTO_LENGTH, &value[index]);
130         }
131 
132         std::vector<napi_property_descriptor> property;
133         property.resize(vecSize);
134         for (int32_t index = 0; index < vecSize; ++index) {
135             property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
136                 enumItemVec[index].enumName.data(), value[index]);
137         }
138 
139         auto constructor = [](napi_env env, napi_callback_info info) {
140             napi_value jsThis = nullptr;
141             napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
142             return jsThis;
143         };
144 
145         napi_value result = nullptr;
146         napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
147             nullptr, property.size(), property.data(), &result);
148         CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
149 
150         status = napi_set_named_property(env, exports, enumClassName.data(), result);
151         CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
152     }
153     return exports;
154 }
155 
Init(napi_env env,napi_value exports)156 napi_value CalendarEnumNapi::Init(napi_env env, napi_value exports)
157 {
158     JsEnumIntInit(env, exports);
159     JsEnumStringInit(env, exports);
160     return exports;
161 }
162 } // namespace CalendarApi