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 #include "frameworks/bridge/common/dom/dom_option.h"
17
18 #include "frameworks/bridge/common/dom/dom_reflect_map.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20
21 namespace OHOS::Ace::Framework {
22 namespace {
23
24 const char MENU_OPTION_SHOW_TYPE[] = "show";
25 const char MENU_OPTION_POPUP_TYPE[] = "popup";
26
27 }
28
DOMOption(NodeId nodeId,const std::string & nodeName)29 DOMOption::DOMOption(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
30 {
31 selectOptionComponent_ = AceType::MakeRefPtr<OptionComponent>();
32 selectOptionComponent_->SetId(std::to_string(nodeId));
33 }
34
InitializeStyle()35 void DOMOption::InitializeStyle()
36 {
37 ResetInitializedStyle();
38 }
39
ResetInitializedStyle()40 void DOMOption::ResetInitializedStyle()
41 {
42 selectOptionComponent_->InitTheme(GetThemeManager());
43 theme_ = GetTheme<SelectTheme>();
44 if (theme_) {
45 selectOptionComponent_->SetClickedColor(theme_->GetClickedColor());
46 selectOptionComponent_->SetSelectedColor(theme_->GetSelectedColor());
47 selectOptionComponent_->SetSelectedBackgroundColor(theme_->GetSelectedColor());
48 selectOptionComponent_->SetFontColor(theme_->GetFontColor());
49 selectOptionComponent_->SetFontSize(theme_->GetFontSize());
50 selectOptionComponent_->SetFontWeight(theme_->GetFontWeight());
51 selectOptionComponent_->SetFontFamily(theme_->GetFontFamily());
52 selectOptionComponent_->SetTextDecoration(theme_->GetTextDecoration());
53 selectOptionComponent_->SetAllowScale(theme_->IsAllowScale());
54 }
55 }
56
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)57 bool DOMOption::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
58 {
59 #if defined(PREVIEW)
60 if (attr.first != DOM_OPTION_CONTENT) {
61 attrs_.push_back(attr);
62 }
63 #endif
64 if (attr.first == DOM_OPTION_SELECTED) {
65 selectOptionComponent_->SetSelected(StringToBool(attr.second));
66 return true;
67 }
68
69 if (attr.first == DOM_OPTION_VALUE) {
70 selectOptionComponent_->SetValue(attr.second);
71 return true;
72 }
73
74 if (attr.first == DOM_OPTION_ICON) {
75 if (!icon_) {
76 icon_ = AceType::MakeRefPtr<ImageComponent>(attr.second);
77 } else {
78 icon_->SetSrc(attr.second);
79 }
80 selectOptionComponent_->SetIcon(icon_);
81 return true;
82 }
83
84 if (attr.first == DOM_OPTION_ACTION) {
85 if (attr.second == MENU_OPTION_SHOW_TYPE) {
86 selectOptionComponent_->SetShowInNavigationBar(ShowInNavigationBar::SHOW);
87 } else if (attr.second == MENU_OPTION_POPUP_TYPE) {
88 selectOptionComponent_->SetShowInNavigationBar(ShowInNavigationBar::POPUP);
89 }
90 return true;
91 }
92
93 if (attr.first == DOM_OPTION_CONTENT) {
94 if (!content_) {
95 content_ = AceType::MakeRefPtr<TextComponent>(attr.second);
96 } else {
97 content_->SetData(attr.second);
98 }
99 selectOptionComponent_->SetText(content_);
100 return true;
101 }
102
103 if (attr.first == DOM_DISABLED) {
104 selectOptionComponent_->SetDisabled(StringToBool(attr.second));
105 return true;
106 }
107
108 if (attr.first == DOM_SHOW) {
109 selectOptionComponent_->SetVisible(StringToBool(attr.second));
110 return true;
111 }
112
113 if (attr.first == DOM_FOCUSABLE) {
114 selectOptionComponent_->SetFocusable(StringToBool(attr.second));
115 return true;
116 }
117
118 return false;
119 }
120
SetSpecializedStyle(const std::pair<std::string,std::string> & style)121 bool DOMOption::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
122 {
123 #if defined(PREVIEW)
124 styles_.push_back(style);
125 #endif
126 if (style.first == DOM_TEXTAREA_COLOR) {
127 selectOptionComponent_->SetFontColor(ParseColor(style.second));
128 return true;
129 }
130
131 if (style.first == DOM_TEXTAREA_FONT_SIZE) {
132 selectOptionComponent_->SetFontSize(ParseDimension(style.second));
133 return true;
134 }
135
136 if (style.first == DOM_TEXTAREA_FONT_WEIGHT) {
137 selectOptionComponent_->SetFontWeight(ConvertStrToFontWeight(style.second));
138 return true;
139 }
140
141 if (style.first == DOM_TEXTAREA_FONT_FAMILY) {
142 selectOptionComponent_->SetFontFamily(style.second);
143 return true;
144 }
145
146 if (style.first == DOM_OPTION_TEXT_DECORATION) {
147 selectOptionComponent_->SetTextDecoration(ConvertStrToTextDecoration(style.second));
148 return true;
149 }
150
151 if (style.first == DOM_TEXT_ALLOW_SCALE) {
152 selectOptionComponent_->SetAllowScale(StringToBool(style.second));
153 return true;
154 }
155
156 return false;
157 }
158
PrepareSpecializedComponent()159 void DOMOption::PrepareSpecializedComponent()
160 {
161 selectOptionComponent_->SetTextDirection((IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR));
162 }
163
164 #if defined(PREVIEW)
OnSetStyleFinished()165 void DOMOption::OnSetStyleFinished()
166 {
167 std::reverse(std::begin(attrs_), std::end(attrs_));
168 std::reverse(std::begin(styles_), std::end(styles_));
169 selectOptionComponent_->SetAttr(attrs_);
170 selectOptionComponent_->SetStyle(styles_);
171 }
172 #endif
173 } // namespace OHOS::Ace::Framework
174