1 /*
2 * Copyright (c) 2021-2022 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/common/dom/input/dom_checkbox_util.h"
17
18 #include "frameworks/bridge/common/utils/utils.h"
19
20 namespace OHOS::Ace::Framework {
21 namespace {
22
23 constexpr bool INPUT_CHECKED_VALUE_DEFAULT = false;
24 constexpr Dimension BOX_HOVER_RADIUS = 8.0_vp;
25
26 } // namespace
27
InitDefaultValue(const RefPtr<CheckboxComponent> & component)28 void DOMCheckboxUtil::InitDefaultValue(const RefPtr<CheckboxComponent>& component)
29 {
30 component->SetValue(INPUT_CHECKED_VALUE_DEFAULT);
31 }
32
CreateComponentAndSetChildAttr(const std::map<std::string,std::string> & attrs,DOMInput & node)33 RefPtr<CheckboxComponent> DOMCheckboxUtil::CreateComponentAndSetChildAttr(
34 const std::map<std::string, std::string>& attrs, DOMInput& node)
35 {
36 // get theme to create component
37 RefPtr<CheckboxTheme> theme = node.GetTheme<CheckboxTheme>();
38 RefPtr<CheckboxComponent> component = AceType::MakeRefPtr<CheckboxComponent>(theme);
39 // set default value
40 InitDefaultValue(component);
41 // Set default width and height to box.
42 auto boxComponent = node.GetBoxComponent();
43 if (boxComponent) {
44 if (!boxComponent->GetBackDecoration()) {
45 RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
46 backDecoration->SetBorderRadius(Radius(BOX_HOVER_RADIUS));
47 boxComponent->SetBackDecoration(backDecoration);
48 }
49 }
50 // set the default hot zone
51 auto pipelineContext = node.GetPipelineContext().Upgrade();
52 if (pipelineContext->UseLiteStyle()) {
53 component->SetHorizontalPadding(Dimension(0));
54 component->SetHotZoneVerticalPadding(Dimension(0));
55 }
56 if (LessOrEqual(node.GetWidth().Value(), 0.0) && boxComponent) {
57 node.SetWidth(theme->GetWidth());
58 boxComponent->SetWidth(theme->GetWidth().Value(), theme->GetWidth().Unit());
59 }
60 if (LessOrEqual(node.GetHeight().Value(), 0.0) && boxComponent) {
61 node.SetHeight(theme->GetHeight());
62 boxComponent->SetHeight(theme->GetHeight().Value(), theme->GetHeight().Unit());
63 }
64
65 SetChildAttr(component, attrs);
66
67 return component;
68 }
69
SetChildAttr(const RefPtr<CheckboxComponent> & component,const std::map<std::string,std::string> & attrs)70 void DOMCheckboxUtil::SetChildAttr(
71 const RefPtr<CheckboxComponent>& component, const std::map<std::string, std::string>& attrs)
72 {
73 if (!component) {
74 LOGE("fail to set child attr due to checkbox component is null");
75 return;
76 }
77 static const LinearMapNode<void (*)(const RefPtr<CheckboxComponent>&, const std::string&)> attrOperators[] = {
78 { DOM_INPUT_CHECKED, [](const RefPtr<CheckboxComponent>& component,
79 const std::string& value) { component->SetValue(StringToBool(value)); } },
80 { DOM_DISABLED, [](const RefPtr<CheckboxComponent>& component,
81 const std::string& value) { component->SetDisabled(StringToBool(value)); } },
82 };
83 for (const auto& [key, value] : attrs) {
84 auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), key.c_str());
85 if (operatorIter != -1) {
86 attrOperators[operatorIter].value(component, value);
87 }
88 }
89 }
90
AddChildEvent(const RefPtr<CheckboxComponent> & component,int32_t pageId,const std::string & nodeId,const std::vector<std::string> & events)91 void DOMCheckboxUtil::AddChildEvent(const RefPtr<CheckboxComponent>& component, int32_t pageId,
92 const std::string& nodeId, const std::vector<std::string>& events)
93 {
94 if (!component) {
95 LOGE("fail to add child event due to checkbox component is null");
96 return;
97 }
98 for (const auto& event : events) {
99 if (event == DOM_CHANGE) {
100 component->SetChangeEvent(EventMarker(nodeId, event, pageId));
101 } else if (event == DOM_CLICK) {
102 EventMarker eventMarker(nodeId, event, pageId);
103 eventMarker.SetCatchMode(false);
104 component->SetClickEvent(eventMarker);
105 } else if (event == DOM_CATCH_BUBBLE_CLICK) {
106 EventMarker eventMarker(nodeId, event, pageId);
107 eventMarker.SetCatchMode(true);
108 component->SetClickEvent(eventMarker);
109 }
110 }
111 }
112
113 } // namespace OHOS::Ace::Framework
114