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_rating.h"
17
18 #include "core/components/rating/rating_theme.h"
19 #include "frameworks/bridge/common/dom/dom_type.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace::Framework {
23
DOMRating(NodeId nodeId,const std::string & nodeName)24 DOMRating::DOMRating(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
25 {
26 ratingChild_ = AceType::MakeRefPtr<RatingComponent>();
27 }
28
ResetInitializedStyle()29 void DOMRating::ResetInitializedStyle()
30 {
31 SetThemeAttrs();
32 }
33
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)34 bool DOMRating::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
35 {
36 if (attr.first == DOM_RATING_INDICATOR) {
37 ratingChild_->SetIndicator(StringToBool(attr.second));
38 return true;
39 }
40 // DOMRating attributes map
41 static const std::unordered_map<std::string, void (*)(const std::string&, DOMRating&)> ratingAttrOperators = {
42 { DOM_STAR_NUM,
43 [](const std::string& val, DOMRating& rating) {
44 rating.starNum_.first = StringToInt(val);
45 rating.starNum_.second = true;
46 } },
47 { DOM_RATING_SCORE,
48 [](const std::string& val, DOMRating& rating) {
49 rating.ratingScore_.first = StringToDouble(val);
50 rating.ratingScore_.second = true;
51 } },
52 { DOM_RATING_STEP,
53 [](const std::string& val, DOMRating& rating) {
54 rating.stepSize_.first = StringToDouble(val);
55 rating.stepSize_.second = true;
56 } },
57 };
58 auto operatorIter = ratingAttrOperators.find(attr.first);
59 if (operatorIter != ratingAttrOperators.end()) {
60 operatorIter->second(attr.second, *this);
61 return true;
62 }
63
64 return false;
65 }
66
SetSpecializedStyle(const std::pair<std::string,std::string> & style)67 bool DOMRating::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
68 {
69 if (style.first == DOM_RTL_FLIP) {
70 ratingChild_->SetRtlFlip(StringToBool(style.second));
71 return true;
72 }
73 // DOMRating styles map
74 static const std::unordered_map<std::string, void (*)(const std::string&, DOMRating&)> ratingStyleOperators = {
75 { DOM_BACKGROUND_SRC,
76 [](const std::string& val, DOMRating& rating) {
77 rating.backgroundSrc_.first = val;
78 rating.backgroundSrc_.second = true;
79 } },
80 { DOM_FOREGROUND_SRC,
81 [](const std::string& val, DOMRating& rating) {
82 rating.foregroundSrc_.first = val;
83 rating.foregroundSrc_.second = true;
84 } },
85 { DOM_SECONDARY_SRC,
86 [](const std::string& val, DOMRating& rating) {
87 rating.secondarySrc_.first = val;
88 rating.secondarySrc_.second = true;
89 } },
90 };
91 auto operatorIter = ratingStyleOperators.find(style.first);
92 if (operatorIter != ratingStyleOperators.end()) {
93 operatorIter->second(style.second, *this);
94 return true;
95 }
96
97 return false;
98 }
99
AddSpecializedEvent(int32_t pageId,const std::string & event)100 bool DOMRating::AddSpecializedEvent(int32_t pageId, const std::string& event)
101 {
102 LOGD("DOMRating AddEvent");
103 if (event == DOM_CHANGE) {
104 changeEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
105 ratingChild_->SetChangeEventId(changeEventId_);
106 return true;
107 }
108 return false;
109 }
110
SetThemeAttrs()111 void DOMRating::SetThemeAttrs()
112 {
113 auto theme = GetTheme<RatingTheme>();
114 if (!theme) {
115 return;
116 }
117 if (ratingChild_->GetIndicator()) {
118 // If box's size not defined, use theme to initialize.
119 if (boxComponent_->GetHeightDimension().Value() < 0.0) {
120 SetHeight(theme->GetRatingMiniHeight());
121 boxComponent_->SetHeight(theme->GetRatingMiniHeight().Value(), theme->GetRatingMiniHeight().Unit());
122 }
123 if (boxComponent_->GetWidthDimension().Value() < 0.0) {
124 SetWidth(theme->GetRatingMiniWidth());
125 boxComponent_->SetWidth(theme->GetRatingMiniWidth().Value(), theme->GetRatingMiniWidth().Unit());
126 }
127 ratingChild_->SetPaddingVertical(Dimension());
128 ratingChild_->SetRatingScore(theme->GetRatingMiniScore());
129 ratingChild_->SetMiniResIdFromTheme(theme);
130 } else {
131 // If box's size not defined, use theme to initialize.
132 if (boxComponent_->GetHeightDimension().Value() < 0.0) {
133 SetHeight(theme->GetRatingHeight());
134 boxComponent_->SetHeight(theme->GetRatingHeight().Value(), theme->GetRatingHeight().Unit());
135 }
136 if (boxComponent_->GetWidthDimension().Value() < 0.0) {
137 SetWidth(theme->GetRatingWidth());
138 boxComponent_->SetWidth(theme->GetRatingWidth().Value(), theme->GetRatingWidth().Unit());
139 }
140 ratingChild_->SetRatingScore(theme->GetRatingScore());
141 ratingChild_->SetPaddingVertical(theme->GetPaddingVertical());
142 ratingChild_->SetResIdFromTheme(theme);
143 }
144 ratingChild_->SetStarNum(theme->GetStarNum());
145 ratingChild_->SetStepSize(theme->GetStepSize());
146 ratingChild_->SetDefaultHeight(theme->GetRatingHeight());
147 ratingChild_->SetDesignedStarAspectRatio(theme->GetDesignedStarAspectRatio());
148 ratingChild_->SetFocusBorderWidth(theme->GetFocusBorderWidth());
149 ratingChild_->SetFocusBorderRadius(theme->GetFocusBorderRadius());
150 ratingChild_->SetHoverColor(theme->GetHoverColor());
151 ratingChild_->SetStarColorActive(theme->GetStarColorActive());
152 ratingChild_->SetStarColorInactive(theme->GetStarColorInactive());
153 }
154
PrepareSpecializedComponent()155 void DOMRating::PrepareSpecializedComponent()
156 {
157 SetThemeAttrs();
158 ratingChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
159 if (starNum_.second) {
160 ratingChild_->SetStarNum(starNum_.first);
161 }
162 if (ratingScore_.second) {
163 ratingChild_->SetRatingScore(ratingScore_.first);
164 }
165 if (stepSize_.second) {
166 ratingChild_->SetStepSize(stepSize_.first);
167 }
168 if (backgroundSrc_.second) {
169 ratingChild_->SetBackgroundSrc(backgroundSrc_.first);
170 }
171 if (foregroundSrc_.second) {
172 ratingChild_->SetForegroundSrc(foregroundSrc_.first);
173 }
174 if (secondarySrc_.second) {
175 ratingChild_->SetSecondarySrc(secondarySrc_.first);
176 }
177 }
178
179 } // namespace OHOS::Ace::Framework
180