• 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::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
69     { "EventType", g_eventType},
70     { "RecurrenceFrequency", g_recurrenceFrequency},
71 };
72 
73 static const std::map<std::string_view, const std::vector<struct JsEnumString>&> g_stringEnumClassMap = {
74     { "CalendarType", g_calendarTypeKey },
75     { "ServiceType", g_serviceType },
76 };
77 
JsEnumIntInit(napi_env env,napi_value exports)78 napi_value CalendarEnumNapi::JsEnumIntInit(napi_env env, napi_value exports)
79 {
80     for (const auto &enumClass : g_intEnumClassMap) {
81         auto &enumClassName = enumClass.first;
82         auto &enumItemVec = enumClass.second;
83         int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
84         std::vector<napi_value> value;
85         value.resize(vecSize);
86         for (int32_t index = 0; index < vecSize; ++index) {
87             napi_create_int32(env, enumItemVec[index].enumInt, &value[index]);
88         }
89 
90         std::vector<napi_property_descriptor> property;
91         property.resize(vecSize);
92         for (int32_t index = 0; index < vecSize; ++index) {
93             property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
94                 enumItemVec[index].enumName.data(), value[index]);
95         }
96 
97         auto constructor = [](napi_env env, napi_callback_info info) {
98             napi_value jsThis = nullptr;
99             napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
100             return jsThis;
101         };
102 
103         napi_value result = nullptr;
104         napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
105             nullptr, property.size(), property.data(), &result);
106         CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
107 
108         status = napi_set_named_property(env, exports, enumClassName.data(), result);
109         CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
110     }
111     return exports;
112 }
113 
JsEnumStringInit(napi_env env,napi_value exports)114 napi_value CalendarEnumNapi::JsEnumStringInit(napi_env env, napi_value exports)
115 {
116     for (auto it = g_stringEnumClassMap.begin(); it != g_stringEnumClassMap.end(); it++) {
117         auto &enumClassName = it->first;
118         auto &enumItemVec = it->second;
119         int32_t vecSize = static_cast<int32_t>(enumItemVec.size());
120         std::vector<napi_value> value;
121         value.resize(vecSize);
122         for (int32_t index = 0; index < vecSize; ++index) {
123             napi_create_string_utf8(env, enumItemVec[index].enumString.data(), NAPI_AUTO_LENGTH, &value[index]);
124         }
125 
126         std::vector<napi_property_descriptor> property;
127         property.resize(vecSize);
128         for (int32_t index = 0; index < vecSize; ++index) {
129             property[index] = napi_property_descriptor DECLARE_NAPI_STATIC_PROPERTY(
130                 enumItemVec[index].enumName.data(), value[index]);
131         }
132 
133         auto constructor = [](napi_env env, napi_callback_info info) {
134             napi_value jsThis = nullptr;
135             napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
136             return jsThis;
137         };
138 
139         napi_value result = nullptr;
140         napi_status status = napi_define_class(env, enumClassName.data(), NAPI_AUTO_LENGTH, constructor,
141             nullptr, property.size(), property.data(), &result);
142         CHECK_RETURN(status == napi_ok, "Failed to define enum", nullptr);
143 
144         status = napi_set_named_property(env, exports, enumClassName.data(), result);
145         CHECK_RETURN(status == napi_ok, "Failed to set result", nullptr);
146     }
147     return exports;
148 }
149 
Init(napi_env env,napi_value exports)150 napi_value CalendarEnumNapi::Init(napi_env env, napi_value exports)
151 {
152     JsEnumIntInit(env, exports);
153     JsEnumStringInit(env, exports);
154     return exports;
155 }
156 } // namespace CalendarApi