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 "js_intention_code.h"
17 
18 #include "key_event.h"
19 #include "mmi_log.h"
20 #include "napi_constants.h"
21 #include "util_napi.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "JsIntentionCode"
25 
26 namespace OHOS {
27 namespace MMI {
28 
GetNapiInt32(napi_env env,int32_t code)29 napi_value JsIntentionCode::GetNapiInt32(napi_env env, int32_t code)
30 {
31     CALL_DEBUG_ENTER;
32     napi_value intentionCode = nullptr;
33     CHKRP(napi_create_int32(env, code, &intentionCode), CREATE_INT32);
34     return intentionCode;
35 }
36 
EnumClassConstructor(napi_env env,napi_callback_info info)37 napi_value JsIntentionCode::EnumClassConstructor(napi_env env, napi_callback_info info)
38 {
39     CALL_DEBUG_ENTER;
40     size_t argc = 0;
41     napi_value args[1] = { 0 };
42     napi_value ret = nullptr;
43     void *data = nullptr;
44     CHKRP(napi_get_cb_info(env, info, &argc, args, &ret, &data), GET_CB_INFO);
45     return ret;
46 }
47 
Export(napi_env env,napi_value exports)48 napi_value JsIntentionCode::Export(napi_env env, napi_value exports)
49 {
50     CALL_DEBUG_ENTER;
51     napi_property_descriptor desc[] = {
52         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_UNKNOWN", GetNapiInt32(env, KeyEvent::INTENTION_UNKNOWN)),
53         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_UP", GetNapiInt32(env, KeyEvent::INTENTION_UP)),
54         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_DOWN", GetNapiInt32(env, KeyEvent::INTENTION_DOWN)),
55         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_LEFT", GetNapiInt32(env, KeyEvent::INTENTION_LEFT)),
56         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_RIGHT", GetNapiInt32(env, KeyEvent::INTENTION_RIGHT)),
57         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_SELECT", GetNapiInt32(env, KeyEvent::INTENTION_SELECT)),
58         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_ESCAPE", GetNapiInt32(env, KeyEvent::INTENTION_ESCAPE)),
59         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_BACK", GetNapiInt32(env, KeyEvent::INTENTION_BACK)),
60         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_FORWARD", GetNapiInt32(env, KeyEvent::INTENTION_FORWARD)),
61         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_MENU", GetNapiInt32(env, KeyEvent::INTENTION_MENU)),
62         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_PAGE_UP", GetNapiInt32(env, KeyEvent::INTENTION_PAGE_UP)),
63         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_PAGE_DOWN", GetNapiInt32(env, KeyEvent::INTENTION_PAGE_DOWN)),
64         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_ZOOM_OUT", GetNapiInt32(env, KeyEvent::INTENTION_ZOOM_OUT)),
65         DECLARE_NAPI_STATIC_PROPERTY("INTENTION_ZOOM_IN", GetNapiInt32(env, KeyEvent::INTENTION_ZOOM_IN)),
66     };
67 
68     napi_value result = nullptr;
69     CHKRP(napi_define_class(env, "IntentionCode", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
70         sizeof(desc) / sizeof(*desc), desc, &result), DEFINE_CLASS);
71     CHKRP(napi_set_named_property(env, exports, "IntentionCode", result), SET_NAMED_PROPERTY);
72     return exports;
73 }
74 } // namespace MMI
75 } // namespace OHOS