1 /*
2 * Copyright (c) 2021 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/js_frontend/engine/jsi/jsi_input_bridge.h"
17
18 namespace OHOS::Ace::Framework {
19 namespace {
20
GetInputOption(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject,InputOption & inputOption)21 void GetInputOption(
22 const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject, InputOption& inputOption)
23 {
24 if (!valObject->IsObject(runtime)) {
25 LOGE("none found attrs");
26 return;
27 }
28 shared_ptr<JsValue> properties;
29 int32_t len = 0;
30 if (!valObject->GetPropertyNames(runtime, properties, len)) {
31 LOGE("Get property names failed when get input option.");
32 return;
33 }
34 for (int32_t i = 0; i < len; i++) {
35 const auto& key = properties->GetElement(runtime, i);
36 std::string keyStr = key->ToString(runtime);
37 if (keyStr.empty()) {
38 continue;
39 }
40 const auto& val = valObject->GetProperty(runtime, key);
41 if (val->IsNumber(runtime) || val->IsBoolean(runtime) || val->IsString(runtime)) {
42 std::string valStr = val->ToString(runtime);
43 if (valStr.empty()) {
44 continue;
45 }
46 if (strcmp(keyStr.c_str(), DOM_INPUT_OPTION_ICON) == 0) {
47 inputOption.image = valStr;
48 } else if (strcmp(keyStr.c_str(), DOM_INPUT_OPTION_CONTENT) == 0) {
49 inputOption.text = valStr;
50 }
51 }
52 }
53 }
54
55 } // namespace
56
ParseInputOptions(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valArray)57 void JsiInputBridge::ParseInputOptions(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valArray)
58 {
59 shared_ptr<JsValue> properties;
60 int32_t len = 0;
61 if (!valArray->GetPropertyNames(runtime, properties, len)) {
62 LOGE("Get property names failed when parse input options.");
63 return;
64 }
65 for (int32_t i = 0; i < len; ++i) {
66 const auto& itemKey = properties->GetElement(runtime, i);
67 const auto& itemVal = valArray->GetProperty(runtime, itemKey);
68 InputOption inputOption;
69 if (itemVal->IsObject(runtime)) {
70 GetInputOption(runtime, itemVal, inputOption);
71 inputOptions_.push_back(inputOption);
72 }
73 }
74 }
75
76 } // namespace OHOS::Ace::Framework
77