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_badge_bridge.h"
17
18 namespace OHOS::Ace::Framework {
19
ParseBadgeConfig(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject)20 void JsiBadgeBridge::ParseBadgeConfig(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject)
21 {
22 if (!runtime) {
23 LOGE("runtime is null");
24 return;
25 }
26 if (!valObject || !valObject->IsObject(runtime)) {
27 LOGE("none found attrs");
28 return;
29 }
30 shared_ptr<JsValue> propertyNames;
31 int32_t len = 0;
32 valObject->GetPropertyNames(runtime, propertyNames, len);
33 for (auto i = 0; i < len; i++) {
34 shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
35 if (!key) {
36 LOGW("key is null. Ignoring!");
37 continue;
38 }
39 std::string keyStr = key->ToString(runtime);
40 shared_ptr<JsValue> value = valObject->GetProperty(runtime, key);
41 if (!value) {
42 LOGW("value is null. Ignoring!");
43 continue;
44 }
45 if (value->IsString(runtime) || value->IsNumber(runtime)) {
46 std::string valStr = value->ToString(runtime);
47 // static linear map must be sorted by key.
48 static const LinearMapNode<void (*)(std::string&, JsiBadgeBridge&)> badgeConfigOperators[] = {
49 { DOM_BADGE_COLOR,
50 [](std::string& valStr, JsiBadgeBridge& jsiBadgeBridge) {
51 jsiBadgeBridge.badgeConfig_.badgeColor = { Color::FromString(valStr), true };
52 } },
53 { DOM_BADGE_CIRCLE_SIZE,
54 [](std::string& valStr, JsiBadgeBridge& jsiBadgeBridge) {
55 jsiBadgeBridge.badgeConfig_.badgeSize = { StringUtils::StringToDimension(valStr), true };
56 } },
57 { DOM_BADGE_TEXT_COLOR,
58 [](std::string& valStr, JsiBadgeBridge& jsiBadgeBridge) {
59 jsiBadgeBridge.badgeConfig_.textColor = { Color::FromString(valStr), true };
60 } },
61 { DOM_BADGE_TEXT_FONT_SIZE,
62 [](std::string& valStr, JsiBadgeBridge& jsiBadgeBridge) {
63 jsiBadgeBridge.badgeConfig_.textSize = { StringUtils::StringToDimension(valStr), true };
64 } }
65 };
66 auto operatorIter =
67 BinarySearchFindIndex(badgeConfigOperators, ArraySize(badgeConfigOperators), keyStr.c_str());
68 if (operatorIter != -1) {
69 badgeConfigOperators[operatorIter].value(valStr, *this);
70 }
71 }
72 }
73 }
74
75 } // namespace OHOS::Ace::Framework
76