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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_INPUT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_INPUT_H
18 
19 #include "core/components/button/button_component.h"
20 #include "core/components/common/properties/border.h"
21 #include "core/components/common/properties/input_option.h"
22 #include "core/components/checkable/checkable_component.h"
23 #include "core/pipeline/base/component.h"
24 #include "frameworks/bridge/common/dom/dom_node.h"
25 #include "frameworks/bridge/common/dom/dom_text.h"
26 #include "frameworks/bridge/common/dom/dom_type.h"
27 #include "frameworks/bridge/common/dom/form_value.h"
28 
29 namespace OHOS::Ace::Framework {
30 
31 class DOMInput final : public DOMNode, public FormValue {
32     DECLARE_ACE_TYPE(DOMInput, DOMNode, FormValue);
33 
34 public:
35     DOMInput(NodeId nodeId, const std::string& nodeName);
36     ~DOMInput() override;
37 
38     void CallSpecializedMethod(const std::string& method, const std::string& args) override;
39     void OnRequestFocus(bool shouldFocus) override;
40     RefPtr<Component> GetSpecializedComponent() override;
41 
SetInputOptions(const std::vector<InputOption> & inputOptions)42     void SetInputOptions(const std::vector<InputOption>& inputOptions)
43     {
44         inputOptions_ = inputOptions;
45     }
46 
47     bool CheckPseduo(RefPtr<Decoration> decoration) const;
48 
49 protected:
50     void OnMounted(const RefPtr<DOMNode>& parentNode) override;
51     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
52     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
53     bool AddSpecializedEvent(int32_t pageId, const std::string& event) override;
54 
55     void PrepareSpecializedComponent() override;
56 
57     void ResetInitializedStyle() override;
58 
59     void OnReset() override;
60 
61 protected:
62     void UpdateSpecializedComponent();
63 
64     void CreateSpecializedComponent();
65 
66     void UpdateSpecializedComponentStyle();
67 
68     void AddSpecializedComponentEvent();
69 
70     template<typename T, typename S>
InitCheckable()71     void InitCheckable()
72     {
73         const auto& theme = GetTheme<S>();
74         const auto& checkableComponent = AceType::DynamicCast<T>(inputChild_);
75         if (theme && checkableComponent) {
76             checkableComponent->ApplyTheme(theme);
77             SetWidth(theme->GetWidth());
78             SetHeight(theme->GetHeight());
79         }
80     }
81 
InitButton()82     void InitButton()
83     {
84         const auto& theme = GetTheme<ButtonTheme>();
85         const auto& buttonComponent = AceType::DynamicCast<ButtonComponent>(inputChild_);
86         if (theme && buttonComponent) {
87             buttonComponent->ApplyTheme(theme);
88             SetHeight(theme->GetHeight());
89         }
90     }
91 
92 private:
93     void PrepareCheckedListener();
94     void CheckSubmitAndResetType();
95     void SetFormValueListener();
96 
97     void HandlePadding(RefPtr<Component> component, RefPtr<BoxComponent> boxComponent,
98         const std::map<std::string, std::string>& styles);
99 
100     RefPtr<Component> inputChild_;
101     WeakPtr<DOMNode> formNode_;
102     std::pair<std::string, bool> type_ = { "text", false };
103 
104     std::map<std::string, std::string> inputAttrs_;
105     std::map<std::string, std::string> inputStyles_;
106     std::vector<std::string> tempEvent_;
107     EventMarker checkableChangeMarker_;
108     int32_t pageId_ = -1; // invalid id.
109     Border boxBorder_;
110     std::vector<InputOption> inputOptions_;
111 };
112 
113 } // namespace OHOS::Ace::Framework
114 
115 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_DOM_DOM_INPUT_H
116