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_ACE_ENGINE_FRAMEWORKS_BRIDGE_COMMON_DOM_FORM_VALUE_H
17 #define FOUNDATION_ACE_ACE_ENGINE_FRAMEWORKS_BRIDGE_COMMON_DOM_FORM_VALUE_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 
23 namespace OHOS::Ace::Framework {
24 
25 class FormValue : public virtual AceType {
DECLARE_ACE_TYPE(FormValue,AceType)26     DECLARE_ACE_TYPE(FormValue, AceType)
27 
28 public:
29     std::pair<std::string, std::string> GetFormValue()
30     {
31         if (isCheckbox_) {
32             if (!isChecked_) {
33                 return { "", "" };
34             }
35             if (value_.empty()) {
36                 return { name_, "on" };
37             }
38         }
39         if (isRadio_) {
40             if (isChecked_) {
41                 return { name_, value_ };
42             }
43             return { "", "" };
44         }
45         return { name_, value_ };
46     }
Reset()47     void Reset()
48     {
49         value_ = originValue_;
50         isChecked_ = originChecked_;
51         OnReset();
52     }
53 
GetName()54     const std::string& GetName() const
55     {
56         return name_;
57     }
SetName(const std::string & name)58     void SetName(const std::string& name)
59     {
60         name_ = name;
61     }
GetOriginValue()62     const std::string& GetOriginValue() const
63     {
64         return originValue_;
65     }
SetOriginValue(const std::string & originValue)66     void SetOriginValue(const std::string& originValue)
67     {
68         originValue_ = originValue;
69         value_ = originValue;
70     }
GetValue()71     const std::string& GetValue() const
72     {
73         return value_;
74     }
SetValue(const std::string & value)75     void SetValue(const std::string& value)
76     {
77         value_ = value;
78         if (isRadio_) {
79             if (value_ == originValue_) {
80                 isChecked_ = true;
81             } else {
82                 isChecked_ = false;
83             }
84         }
85     }
IsChecked()86     bool IsChecked() const
87     {
88         return isChecked_;
89     }
SetChecked(bool checked)90     void SetChecked(bool checked)
91     {
92         isChecked_ = checked;
93     }
IsCheckbox()94     bool IsCheckbox() const
95     {
96         return isCheckbox_;
97     }
SetIsCheckbox(bool isCheckbox)98     void SetIsCheckbox(bool isCheckbox)
99     {
100         isCheckbox_ = isCheckbox;
101     }
IsOriginChecked()102     bool IsOriginChecked() const
103     {
104         return originChecked_;
105     }
SetOriginChecked(bool originChecked)106     void SetOriginChecked(bool originChecked)
107     {
108         originChecked_ = originChecked;
109         isChecked_ = originChecked;
110     }
IsRadio()111     bool IsRadio() const
112     {
113         return isRadio_;
114     }
SetIsRadio(bool isRadio)115     void SetIsRadio(bool isRadio)
116     {
117         isRadio_ = isRadio;
118     }
119 
120 protected:
OnReset()121     virtual void OnReset() {}
122 
123 private:
124     std::string name_;
125     std::string originValue_;
126     std::string value_;
127     bool isCheckbox_ = false;
128     bool isRadio_ = false;
129     bool isChecked_ = false;
130     bool originChecked_ = false;
131 };
132 
133 } // namespace OHOS::Ace::Framework
134 
135 #endif // FOUNDATION_ACE_ACE_ENGINE_FRAMEWORKS_BRIDGE_COMMON_DOM_FORM_VALUE_H
136