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_CORE_COMPONENTS_DECLARATION_INPUT_INPUT_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_INPUT_INPUT_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 
22 namespace OHOS::Ace {
23 
24 struct InputAttribute : Attribute {
25     std::pair<std::string, bool> type = { "text", true };
26 };
27 
28 class InputDeclaration : public Declaration {
29     DECLARE_ACE_TYPE(InputDeclaration, Declaration);
30 
31 public:
32     InputDeclaration() = default;
33     ~InputDeclaration() override = default;
34 
35     void InitializeStyle() override;
36 
37     void PrepareSpecializedDeclaration();
38 
GetType()39     const std::string& GetType() const
40     {
41         static std::string defaultType = "text";
42         auto& attribute = static_cast<InputAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
43         return attribute.IsValid() ? attribute.type.first : defaultType;
44     }
45 
IsTypeChanged()46     bool IsTypeChanged() const
47     {
48         auto& attribute = static_cast<InputAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
49         return attribute.IsValid() ? attribute.type.second : true;
50     }
51 
SetIsTypeChanged(bool isTypeChanged)52     void SetIsTypeChanged(bool isTypeChanged) const
53     {
54         auto& attribute = static_cast<InputAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
55         attribute.type.second = true;
56     }
57 
GetSpecializedDeclaration()58     const RefPtr<Declaration>& GetSpecializedDeclaration() const
59     {
60         return specializedDeclaration_;
61     }
62 
63 protected:
64     void InitSpecialized() override;
65     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
66     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
67     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
68     void CallSpecializedMethod(const std::string& method, const std::string& args) override;
69     void OnRequestFocus(bool shouldFocus) override;
70 
71 private:
72     void CreateSpecializedDeclaration();
73     void PrepareTextField();
74 
75     RefPtr<Declaration> specializedDeclaration_;
76     std::map<std::string, std::string> inputAttrs_;
77     std::map<std::string, std::string> inputStyles_;
78     std::map<std::string, std::string> inputEvents_;
79     int32_t pageId_ = -1; // invalid id.
80 };
81 
82 } // namespace OHOS::Ace
83 
84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_INPUT_INPUT_DECLARATION_H
85