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