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