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         return;
75     }
76     static const LinearMapNode<void (*)(const RefPtr<CheckboxComponent>&, const std::string&)> attrOperators[] = {
77         { DOM_INPUT_CHECKED, [](const RefPtr<CheckboxComponent>& component,
78                              const std::string& value) { component->SetValue(StringToBool(value)); } },
79         { DOM_DISABLED, [](const RefPtr<CheckboxComponent>& component,
80                         const std::string& value) { component->SetDisabled(StringToBool(value)); } },
81     };
82     for (const auto& [key, value] : attrs) {
83         auto operatorIter = BinarySearchFindIndex(attrOperators, ArraySize(attrOperators), key.c_str());
84         if (operatorIter != -1) {
85             attrOperators[operatorIter].value(component, value);
86         }
87     }
88 }
89 
AddChildEvent(const RefPtr<CheckboxComponent> & component,int32_t pageId,const std::string & nodeId,const std::vector<std::string> & events)90 void DOMCheckboxUtil::AddChildEvent(const RefPtr<CheckboxComponent>& component, int32_t pageId,
91     const std::string& nodeId, const std::vector<std::string>& events)
92 {
93     if (!component) {
94         return;
95     }
96     for (const auto& event : events) {
97         if (event == DOM_CHANGE) {
98             component->SetChangeEvent(EventMarker(nodeId, event, pageId));
99         } else if (event == DOM_CLICK) {
100             EventMarker eventMarker(nodeId, event, pageId);
101             eventMarker.SetCatchMode(false);
102             component->SetClickEvent(eventMarker);
103         } else if (event == DOM_CATCH_BUBBLE_CLICK) {
104             EventMarker eventMarker(nodeId, event, pageId);
105             eventMarker.SetCatchMode(true);
106             component->SetClickEvent(eventMarker);
107         }
108     }
109 }
110 
111 } // namespace OHOS::Ace::Framework
112