• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_picker_base.h"
17 
18 #include "base/utils/linear_map.h"
19 #include "base/utils/string_utils.h"
20 #include "base/utils/utils.h"
21 #include "frameworks/bridge/common/dom/dom_reflect_map.h"
22 #include "frameworks/bridge/common/utils/utils.h"
23 
24 namespace OHOS::Ace::Framework {
25 namespace {
26 
27 const PickerDate DEFAULT_PICKER_DATE(1970, 1, 1);
28 const PickerDate DEFAULT_END_PICKER_DATE(2100, 12, 31);
29 constexpr char PICKER_DATE_SPLITER = '-';
30 const PickerTime DEFAULT_PICKER_TIME(0, 0, 0);
31 constexpr char PICKER_TIME_SPLITER = ':';
32 
33 } // namespace
34 
DOMPickerBase(NodeId nodeId,const std::string & nodeName,bool hasValue)35 DOMPickerBase::DOMPickerBase(NodeId nodeId, const std::string& nodeName, bool hasValue)
36     : DOMNode(nodeId, nodeName), hasValue_(hasValue)
37 {
38     if (!hasValue_) {
39         return;
40     }
41     valuePickerChild_ = AceType::MakeRefPtr<PickerValueComponent>([weak = WeakClaim(this)]() {
42         auto refPtr = weak.Upgrade();
43         if (refPtr) {
44             refPtr->HandleClickCallback();
45         }
46     });
47     pickerId_ = nodeId;
48 }
49 
InitializeStyle()50 void DOMPickerBase::InitializeStyle()
51 {
52     ResetInitializedStyle();
53 }
54 
ResetInitializedStyle()55 void DOMPickerBase::ResetInitializedStyle()
56 {
57     auto theme = GetTheme<PickerTheme>();
58     if (!theme) {
59         return;
60     }
61     theme_ = theme->clone();
62     if (!theme_ || !valuePickerChild_) {
63         return;
64     }
65     valuePickerChild_->SetTheme(theme_);
66 }
67 
GetSpecializedComponent()68 RefPtr<Component> DOMPickerBase::GetSpecializedComponent()
69 {
70     if (!hasValue_) {
71         return basePickerChild_;
72     }
73     return valuePickerChild_;
74 }
75 
HandleClickCallback() const76 void DOMPickerBase::HandleClickCallback() const
77 {
78     if (!hasValue_) {
79         return;
80     }
81     ShowDialog();
82 }
83 
ShowDialog() const84 void DOMPickerBase::ShowDialog() const
85 {
86     if (!basePickerChild_) {
87         LOGW("not implementation now.");
88         return;
89     }
90     auto context = GetPipelineContext().Upgrade();
91     if (!context) {
92         LOGE("Pipeline context not found error!");
93         return;
94     }
95     auto pageStack = context->GetLastStack();
96     if (!pageStack) {
97         LOGE("Page stack not found error!");
98         return;
99     }
100     basePickerChild_->ShowDialog(pageStack);
101 }
102 
GetPickerDate(const std::string & strDate,PickerDate & outDate) const103 bool DOMPickerBase::GetPickerDate(const std::string& strDate, PickerDate& outDate) const
104 {
105     std::string strValue;
106     std::stringstream streamDate(strDate);
107     if (!std::getline(streamDate, strValue, PICKER_DATE_SPLITER)) {
108         return false;
109     }
110     outDate.SetYear(StringUtils::StringToInt(strValue));
111     if (!std::getline(streamDate, strValue, PICKER_DATE_SPLITER)) {
112         return false;
113     }
114     outDate.SetMonth(StringUtils::StringToInt(strValue));
115     if (!std::getline(streamDate, strValue, PICKER_DATE_SPLITER)) {
116         return false;
117     }
118     outDate.SetDay(StringUtils::StringToInt(strValue));
119     return true;
120 }
121 
GetPickerTime(const std::string & strDate,PickerTime & outTime) const122 bool DOMPickerBase::GetPickerTime(const std::string& strDate, PickerTime& outTime) const
123 {
124     std::string strValue;
125     std::stringstream streamDate(strDate);
126     if (!std::getline(streamDate, strValue, PICKER_TIME_SPLITER)) {
127         return false;
128     }
129     outTime.SetHour(StringUtils::StringToInt(strValue));
130     if (!std::getline(streamDate, strValue, PICKER_TIME_SPLITER)) {
131         return false;
132     }
133     outTime.SetMinute(StringUtils::StringToInt(strValue));
134     // the format time hh:mm is supported, so the time should be set
135     if (std::getline(streamDate, strValue)) {
136         outTime.SetSecond(StringUtils::StringToInt(strValue));
137     }
138     return true;
139 }
140 
GetPickerDateTime(const std::string & strDate,PickerDateTime & outDateTime) const141 bool DOMPickerBase::GetPickerDateTime(const std::string& strDate, PickerDateTime& outDateTime) const
142 {
143     std::vector<std::string> strValues;
144     StringUtils::StringSpliter(strDate, PICKER_DATE_SPLITER, strValues);
145     PickerDate outDate = outDateTime.GetDate();
146     PickerTime outTime = outDateTime.GetTime();
147     if (strValues.size() == 4) { // MM-dd-hh-mm
148         outDate.SetMonth(StringUtils::StringToInt(strValues[0]));
149         outDate.SetDay(StringUtils::StringToInt(strValues[1]));
150         outTime.SetHour(StringUtils::StringToInt(strValues[2]));
151         outTime.SetMinute(StringUtils::StringToInt(strValues[3]));
152     } else if (strValues.size() == 5) { // yyyy-MM-dd-hh-mm
153         outDate.SetYear(StringUtils::StringToInt(strValues[0]));
154         outDate.SetMonth(StringUtils::StringToInt(strValues[1]));
155         outDate.SetDay(StringUtils::StringToInt(strValues[2]));
156         outTime.SetHour(StringUtils::StringToInt(strValues[3]));
157         outTime.SetMinute(StringUtils::StringToInt(strValues[4]));
158     } else {
159         LOGE("input date time is invalidate.");
160         return false;
161     }
162     outDateTime.SetDate(outDate);
163     outDateTime.SetTime(outTime);
164     return true;
165 }
166 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)167 bool DOMPickerBase::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
168 {
169     if (attr.first == DOM_VALUE) {
170         if (!hasValue_) {
171             LOGE("picker view has no attribute of value.");
172             return false;
173         }
174         std::string strValue = attr.second;
175         strValue.erase(
176             std::remove_if(strValue.begin(), strValue.end(), [](char c) { return c == DOM_PICKER_SPLIT_ARRAY; }),
177             strValue.end());
178         valuePickerChild_->SetText(strValue);
179         return true;
180     }
181 
182     if (attr.first == DOM_TYPE) {
183         type_ = attr.second;
184         return true;
185     }
186 
187     static const LinearMapNode<void (*)(DOMPickerBase&, const std::string&)> pickerAttrOperators[] = {
188         { DOM_PICKER_COLUMN_COUNT, [](DOMPickerBase& picker, const std::string& val) { picker.SetColumnCount(val); } },
189         { DOM_PICKER_CONTAIN_SECOND, [](DOMPickerBase& picker, const std::string& val) { picker.SetHasSecond(val); } },
190         { DOM_END, [](DOMPickerBase& picker, const std::string& val) { picker.SetEnd(val); } },
191         { DOM_PICKER_HOUR24, [](DOMPickerBase& picker, const std::string& val) { picker.SetHour24(val); } },
192         { DOM_PICKER_PREFIX, [](DOMPickerBase& picker, const std::string& val) { picker.SetPrefix(val); } },
193         { DOM_PICKER_SUFFIX, [](DOMPickerBase& picker, const std::string& val) { picker.SetSuffix(val); } },
194         { DOM_PICKER_SHOW_LUNAR, [](DOMPickerBase& picker, const std::string& val) { picker.SetShowLunar(val); } },
195         { DOM_PICKER_LUNAR, [](DOMPickerBase& picker, const std::string& val) { picker.SetLunar(val); } },
196         { DOM_PICKER_RANGE, [](DOMPickerBase& picker, const std::string& val) { picker.SetRange(val); } },
197         { DOM_SELECTED, [](DOMPickerBase& picker, const std::string& val) { picker.SetSelected(val); } },
198         { DOM_START, [](DOMPickerBase& picker, const std::string& val) { picker.SetStart(val); } },
199         { DOM_PICKER_VIBRATE, [](DOMPickerBase& picker, const std::string& val) { picker.SetVibrate(val); } },
200     };
201     auto it = BinarySearchFindIndex(pickerAttrOperators, ArraySize(pickerAttrOperators), attr.first.c_str());
202     if (it != -1) {
203         if (!basePickerChild_) {
204             storeAttributes_.emplace_back(attr);
205             return true;
206         }
207         pickerAttrOperators[it].value(*this, attr.second);
208         return true;
209     }
210     return false;
211 }
212 
AddSpecializedEvent(int32_t pageId,const std::string & event)213 bool DOMPickerBase::AddSpecializedEvent(int32_t pageId, const std::string& event)
214 {
215     static const LinearMapNode<void (*)(RefPtr<PickerBaseComponent>&, const EventMarker&)> pickerEventOperators[] = {
216         { DOM_CANCEL,
217             [](RefPtr<PickerBaseComponent>& picker, const EventMarker& event) { picker->SetOnCancel(event); } },
218         { DOM_CHANGE,
219             [](RefPtr<PickerBaseComponent>& picker, const EventMarker& event) { picker->SetOnChange(event); } },
220         { DOM_PICKER_COLUMN_CHANGE,
221             [](RefPtr<PickerBaseComponent>& picker, const EventMarker& event) { picker->SetOnColumnChange(event); } },
222     };
223     auto it = BinarySearchFindIndex(pickerEventOperators, ArraySize(pickerEventOperators), event.c_str());
224     if (it != -1) {
225         if (!basePickerChild_) {
226             storeEvents_.emplace_back(std::make_pair(pageId, event));
227             return true;
228         }
229         pickerEventOperators[it].value(basePickerChild_, EventMarker(GetNodeIdForEvent(), event, pageId));
230         return true;
231     }
232     return false;
233 }
234 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)235 bool DOMPickerBase::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
236 {
237     if (!hasValue_) {
238         return SetOptionStyleOperators(style);
239     }
240     return SetTextStyleOperators(style);
241 }
242 
SetOptionStyleOperators(const std::pair<std::string,std::string> & style)243 bool DOMPickerBase::SetOptionStyleOperators(const std::pair<std::string, std::string>& style)
244 {
245     static const LinearMapNode<void (*)(const DOMPickerBase&,
246         const std::string&, TextStyle&, TextStyle&, TextStyle&, TextStyle&)>
247         optionStyleOperators[] = {
248             { DOM_TEXT_COLOR, [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle&,
249                               TextStyle&, TextStyle&) { normal.SetTextColor(node.ParseColor(val)); } },
250             { DOM_PICKER_DISAPPEAR_COLOR, [](const DOMPickerBase& node, const std::string& val,
251                 TextStyle&, TextStyle&, TextStyle&, TextStyle& style) { style.SetTextColor(node.ParseColor(val)); } },
252             { DOM_PICKER_DISAPPEAR_FONT_SIZE, [](const DOMPickerBase& node, const std::string& val,
253                 TextStyle&, TextStyle&, TextStyle&, TextStyle& style) {
254                 style.SetFontSize(node.ParseDimension(val));
255                 style.SetAdaptMaxFontSize(node.ParseDimension(val)); } },
256             { DOM_PICKER_FOCUS_COLOR, [](const DOMPickerBase& node, const std::string& val, TextStyle&, TextStyle&,
257                                       TextStyle& focus, TextStyle&) { focus.SetTextColor(node.ParseColor(val)); } },
258             { DOM_PICKER_FOCUS_SIZE,
259                 [](const DOMPickerBase& node, const std::string& val,
260                     TextStyle&, TextStyle&, TextStyle& focus, TextStyle&) {
261                     focus.SetFontSize(node.ParseDimension(val));
262                     focus.SetAdaptMaxFontSize(node.ParseDimension(val));
263                 } },
264             { DOM_TEXT_FONT_FAMILY,
265                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& select,
266                     TextStyle& focus, TextStyle& disappear) {
267                     normal.SetFontFamilies(node.ParseFontFamilies(val));
268                     select.SetFontFamilies(node.ParseFontFamilies(val));
269                     focus.SetFontFamilies(node.ParseFontFamilies(val));
270                     disappear.SetFontFamilies(node.ParseFontFamilies(val));
271                 } },
272             { DOM_TEXT_FONT_SIZE,
273                 [](const DOMPickerBase& node, const std::string& val,
274                     TextStyle& normal, TextStyle&, TextStyle&, TextStyle&) {
275                     normal.SetFontSize(node.ParseDimension(val));
276                     normal.SetAdaptMaxFontSize(node.ParseDimension(val));
277                 } },
278             { DOM_PICKER_SELECT_COLOR, [](const DOMPickerBase& node, const std::string& val,
279                                        TextStyle&, TextStyle& select,
280                                        TextStyle&, TextStyle&) { select.SetTextColor(node.ParseColor(val)); } },
281             { DOM_PICKER_SELECT_SIZE,
282                 [](const DOMPickerBase& node, const std::string& val,
283                     TextStyle&, TextStyle& select, TextStyle&, TextStyle&) {
284                     select.SetFontSize(node.ParseDimension(val));
285                     select.SetAdaptMaxFontSize(node.ParseDimension(val));
286                 } },
287         };
288     auto styleIter = BinarySearchFindIndex(optionStyleOperators, ArraySize(optionStyleOperators), style.first.c_str());
289     if (styleIter != -1) {
290         if (!basePickerChild_) {
291             storeStyles_.emplace_back(style);
292             return true;
293         }
294         auto theme = basePickerChild_->GetTheme();
295         auto normalStyle = theme->GetOptionStyle(false, false);
296         auto selectStyle = theme->GetOptionStyle(true, false);
297         auto focusStyle = theme->GetOptionStyle(true, true);
298         auto disappearStyle = theme->GetDisappearOptionStyle();
299         optionStyleOperators[styleIter].value(*this, style.second,
300             normalStyle, selectStyle, focusStyle, disappearStyle);
301         theme->SetOptionStyle(false, false, normalStyle);
302         theme->SetOptionStyle(true, false, selectStyle);
303         theme->SetOptionStyle(true, true, focusStyle);
304         theme->SetDisappearOptionStyle(disappearStyle);
305         return true;
306     }
307     return false;
308 }
309 
SetTextBackgoundColor(const std::pair<std::string,std::string> & style)310 bool DOMPickerBase::SetTextBackgoundColor(const std::pair<std::string, std::string>& style)
311 {
312     if (style.first != DOM_BACKGROUND_COLOR) {
313         return false;
314     }
315     auto decoration = theme_->GetOptionDecoration(false);
316     if (!decoration) {
317         return false;
318     }
319     decoration->SetBackgroundColor(ParseColor(style.second));
320     return true;
321 }
322 
SetTextStyleOperators(const std::pair<std::string,std::string> & style)323 bool DOMPickerBase::SetTextStyleOperators(const std::pair<std::string, std::string>& style)
324 {
325     static const LinearMapNode<void (*)(const DOMPickerBase&, const std::string&, TextStyle&, TextStyle&)>
326         textStyleOperators[] = {
327             { DOM_TEXT_ALLOW_SCALE,
328                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
329                     normal.SetAllowScale(StringToBool(val));
330                     focus.SetAllowScale(StringToBool(val));
331                 } },
332             { DOM_TEXT_FONT_FAMILY,
333                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
334                     normal.SetFontFamilies(node.ParseFontFamilies(val));
335                     focus.SetFontFamilies(node.ParseFontFamilies(val));
336                 } },
337             { DOM_TEXT_FONT_SIZE,
338                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
339                     normal.SetFontSize(node.ParseDimension(val));
340                     normal.SetAdaptMaxFontSize(node.ParseDimension(val));
341                     focus.SetFontSize(node.ParseDimension(val));
342                     focus.SetAdaptMaxFontSize(node.ParseDimension(val));
343                 } },
344             { DOM_TEXT_FONT_STYLE,
345                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
346                     normal.SetFontStyle(ConvertStrToFontStyle(val));
347                     focus.SetFontStyle(ConvertStrToFontStyle(val));
348                 } },
349             { DOM_TEXT_FONT_WEIGHT,
350                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
351                     normal.SetFontWeight(ConvertStrToFontWeight(val));
352                     focus.SetFontWeight(ConvertStrToFontWeight(val));
353                 } },
354             { DOM_TEXT_LETTER_SPACING,
355                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
356                     normal.SetLetterSpacing(node.ParseDimension(val));
357                     focus.SetLetterSpacing(node.ParseDimension(val));
358                 } },
359             { DOM_TEXT_LINE_HEIGHT,
360                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
361                     normal.SetLineHeight(node.ParseLineHeight(val));
362                     focus.SetLineHeight(node.ParseLineHeight(val));
363                 } },
364             { DOM_PICKER_TEXT_COLOR,
365                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
366                     normal.SetTextColor(node.ParseColor(val));
367                     focus.SetTextColor(node.ParseColor(val));
368                 } },
369             { DOM_TEXT_DECORATION,
370                 [](const DOMPickerBase& node, const std::string& val, TextStyle& normal, TextStyle& focus) {
371                     normal.SetTextDecoration(ConvertStrToTextDecoration(val));
372                     focus.SetTextDecoration(ConvertStrToTextDecoration(val));
373                 } },
374         };
375     auto operatorIter = BinarySearchFindIndex(textStyleOperators, ArraySize(textStyleOperators), style.first.c_str());
376     if (operatorIter != -1) {
377         auto normalStyle = theme_->GetOptionStyle(false, false);
378         auto focusStyle = theme_->GetOptionStyle(true, true);
379         textStyleOperators[operatorIter].value(*this, style.second, normalStyle, focusStyle);
380         theme_->SetOptionStyle(false, false, normalStyle);
381         theme_->SetOptionStyle(true, false, normalStyle);
382         theme_->SetOptionStyle(true, true, focusStyle);
383         return true;
384     }
385     if (SetColumnHeight(style)) {
386         return true;
387     }
388     if (SetTextBackgoundColor(style)) {
389         return true;
390     }
391     return false;
392 }
393 
CallSpecializedMethod(const std::string & method,const std::string & args)394 void DOMPickerBase::CallSpecializedMethod(const std::string& method, const std::string& args)
395 {
396     if (!basePickerChild_) {
397         LOGW("not implementation now.");
398         return;
399     }
400     if (method == DOM_ROTATION) {
401         auto controller = basePickerChild_->GetRotationController();
402         if (controller) {
403             LOGD("Rotation focus picker view");
404             controller->RequestRotation(true);
405         }
406     }
407     if (!hasValue_) {
408         return;
409     }
410     if (method == DOM_SHOW) {
411         ShowDialog();
412     }
413 }
414 
PrepareSpecializedComponent()415 void DOMPickerBase::PrepareSpecializedComponent()
416 {
417     if (valuePickerChild_) {
418         valuePickerChild_->SetTextDirection((IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR));
419     }
420     if (CreatePicker()) {
421         for (const auto& attribute : storeAttributes_) {
422             SetSpecializedAttr(attribute);
423         }
424         for (const auto& oneEvent : storeEvents_) {
425             AddSpecializedEvent(oneEvent.first, oneEvent.second);
426         }
427         for (const auto& oneStyle : storeStyles_) {
428             SetSpecializedStyle(oneStyle);
429         }
430     }
431     storeAttributes_.clear();
432     storeEvents_.clear();
433     storeStyles_.clear();
434 }
435 
CompositeComponents()436 void DOMPickerBase::CompositeComponents()
437 {
438     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
439         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
440         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
441         // check invalidate of start and end.
442         const auto& start = picker->GetStartDate();
443         const auto& end = picker->GetEndDate();
444         if (start.GetYear() > end.GetYear() ||
445             (start.GetYear() == end.GetYear() && start.GetMonth() > end.GetMonth()) ||
446             (start.GetYear() == end.GetYear() && start.GetMonth() == end.GetMonth() && start.GetDay() > end.GetDay())) {
447             // invalidate => use default start date and end date.
448             picker->SetStartDate(DEFAULT_PICKER_DATE); // default start date is 1970-1-1 from FA document.
449             picker->SetEndDate(DEFAULT_END_PICKER_DATE); // default end date is 2100-12-31 from FA document.
450         }
451     }
452     if (IsRightToLeft()) {
453         SetAlignment(Alignment::CENTER_RIGHT);
454     }
455     DOMNode::CompositeComponents();
456 }
457 
CreatePicker()458 bool DOMPickerBase::CreatePicker()
459 {
460     // The type cannot be dynamically changed.
461     if (basePickerChild_) {
462         return true;
463     }
464 
465     // Operator map for type.
466     static const std::unordered_map<std::string, void (*)(RefPtr<PickerBaseComponent>&)> pickerOperators = {
467         { DOM_PICKER_TYPE_TIME,
468             [](RefPtr<PickerBaseComponent>& pickerBase) {
469                 pickerBase = AceType::MakeRefPtr<PickerTimeComponent>(); } },
470         { DOM_PICKER_TYPE_DATE,
471             [](RefPtr<PickerBaseComponent>& pickerBase) {
472                 pickerBase = AceType::MakeRefPtr<PickerDateComponent>(); } },
473         { DOM_PICKER_TYPE_TEXT,
474             [](RefPtr<PickerBaseComponent>& pickerBase) {
475                 pickerBase = AceType::MakeRefPtr<PickerTextComponent>(); } },
476         { DOM_PICKER_TYPE_MULTITEXT,
477             [](RefPtr<PickerBaseComponent>& pickerBase) {
478                 pickerBase = AceType::MakeRefPtr<PickerMultiTextComponent>();
479             } },
480         { DOM_PICKER_TYPE_DATETIME,
481             [](RefPtr<PickerBaseComponent>& pickerBase) {
482                 pickerBase = AceType::MakeRefPtr<PickerDateTimeComponent>();
483             } },
484     };
485 
486     auto operatorIter = pickerOperators.find(type_);
487     if (operatorIter != pickerOperators.end()) {
488         operatorIter->second(basePickerChild_);
489     } else {
490         basePickerChild_ = AceType::MakeRefPtr<PickerTextComponent>();
491         type_ = DOM_PICKER_TYPE_TEXT;
492     }
493 
494     if (basePickerChild_) {
495         auto theme = GetTheme<PickerTheme>();
496         basePickerChild_->SetTheme(theme->clone());
497         basePickerChild_->SetTextDirection((IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR));
498         basePickerChild_->SetIsDialog(hasValue_);
499         basePickerChild_->SetNodeId(GetNodeId());
500 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
501         basePickerChild_->SetPickerBaseId(pickerId_);
502 #endif
503         return true;
504     }
505 
506     LOGE("unsupported type: %{private}s.", type_.c_str());
507     return false;
508 }
509 
SetColumnHeight(const std::pair<std::string,std::string> & style)510 bool DOMPickerBase::SetColumnHeight(const std::pair<std::string, std::string>& style)
511 {
512     if (style.first != DOM_PICKER_COLUMN_HEIGHT) {
513         return false;
514     }
515 
516     if (!hasValue_) {
517         return false;
518     }
519 
520     if (!basePickerChild_) {
521         storeStyles_.emplace_back(style);
522         return false;
523     }
524 
525     basePickerChild_->SetColumnHeight(StringToDimension(style.second));
526     return true;
527 }
528 
SetStart(const std::string & value)529 bool DOMPickerBase::SetStart(const std::string& value)
530 {
531     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
532         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
533         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
534         auto date = DEFAULT_PICKER_DATE;
535         if (GetPickerDate(value, date)) {
536             picker->SetStartDate(date);
537         } else {
538             LOGE("the value of picker date is invalid.");
539         }
540 
541         return true;
542     }
543 
544     return false;
545 }
546 
SetEnd(const std::string & value)547 bool DOMPickerBase::SetEnd(const std::string& value)
548 {
549     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
550         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
551         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
552         auto date = DEFAULT_PICKER_DATE;
553         if (GetPickerDate(value, date)) {
554             picker->SetEndDate(date);
555         } else {
556             LOGE("input value of picker date is invalid.");
557         }
558 
559         return true;
560     }
561 
562     return false;
563 }
564 
SetSelected(const std::string & value)565 bool DOMPickerBase::SetSelected(const std::string& value)
566 {
567     return (SetTextSelected(value) || SetDateTimeSelected(value));
568 }
569 
SetTextSelected(const std::string & value)570 bool DOMPickerBase::SetTextSelected(const std::string& value)
571 {
572     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TEXT &&
573         AceType::InstanceOf<PickerTextComponent>(basePickerChild_)) {
574         auto picker = AceType::DynamicCast<PickerTextComponent>(basePickerChild_);
575         picker->SetSelected(StringUtils::StringToInt(value));
576         return true;
577     }
578 
579     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_MULTITEXT &&
580         AceType::InstanceOf<PickerMultiTextComponent>(basePickerChild_)) {
581         auto picker = AceType::DynamicCast<PickerMultiTextComponent>(basePickerChild_);
582         std::vector<std::string> out;
583         StringUtils::StringSpliter(value, DOM_PICKER_SPLIT_ARRAY, out);
584         std::vector<uint32_t> selectedIndexes;
585         for (uint32_t index = 0; index < out.size(); ++index) {
586             selectedIndexes.emplace_back(StringUtils::StringToInt(out[index]));
587         }
588         picker->SetSelected(selectedIndexes);
589         return true;
590     }
591 
592     return false;
593 }
594 
SetDateTimeSelected(const std::string & value)595 bool DOMPickerBase::SetDateTimeSelected(const std::string& value)
596 {
597     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
598         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
599         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
600         auto date = DEFAULT_PICKER_DATE;
601         if (GetPickerDate(value, date)) {
602             picker->SetSelectedDate(date);
603         } else {
604             LOGE("input value of picker date is invalid.");
605         }
606 
607         return true;
608     }
609 
610     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TIME &&
611         AceType::InstanceOf<PickerTimeComponent>(basePickerChild_)) {
612         auto picker = AceType::DynamicCast<PickerTimeComponent>(basePickerChild_);
613         auto time = DEFAULT_PICKER_TIME;
614         if (GetPickerTime(value, time)) {
615             picker->SetSelectedTime(time);
616         } else {
617             LOGE("input value of picker time is invalid.");
618         }
619         return true;
620     }
621 
622     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATETIME &&
623         AceType::InstanceOf<PickerDateTimeComponent>(basePickerChild_)) {
624         auto picker = AceType::DynamicCast<PickerDateTimeComponent>(basePickerChild_);
625         auto dateTime = PickerDateTime::Current();
626         if (GetPickerDateTime(value, dateTime)) {
627             picker->SetSelectedDateTime(dateTime);
628         } else {
629             LOGE("input value of picker date time is invalid.");
630         }
631 
632         return true;
633     }
634 
635     return false;
636 }
637 
SetHasSecond(const std::string & value)638 bool DOMPickerBase::SetHasSecond(const std::string& value)
639 {
640     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TIME &&
641         AceType::InstanceOf<PickerTimeComponent>(basePickerChild_)) {
642         auto picker = AceType::DynamicCast<PickerTimeComponent>(basePickerChild_);
643         picker->SetHasSecond((value == "true")); // bool attribute's value is "true" and "false".
644         return true;
645     }
646 
647     return false;
648 }
649 
SetRange(const std::string & value)650 bool DOMPickerBase::SetRange(const std::string& value)
651 {
652     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TEXT &&
653         AceType::InstanceOf<PickerTextComponent>(basePickerChild_)) {
654         auto picker = AceType::DynamicCast<PickerTextComponent>(basePickerChild_);
655         std::vector<std::string> out;
656         StringUtils::StringSpliter(value, DOM_PICKER_SPLIT_ARRAY, out);
657         picker->SetRange(out);
658         return true;
659     }
660 
661     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_MULTITEXT &&
662         AceType::InstanceOf<PickerMultiTextComponent>(basePickerChild_)) {
663         auto picker = AceType::DynamicCast<PickerMultiTextComponent>(basePickerChild_);
664         std::vector<std::string> out;
665         StringUtils::StringSpliter(value, DOM_PICKER_SPLIT_ARRAY, out);
666         std::vector<std::vector<std::string>> arrayRange;
667         for (uint32_t index = 0; index < out.size(); ++index) {
668             std::vector<std::string> textRange;
669             StringUtils::StringSpliter(out[index], DOM_PICKER_SPLIT_ITEM, textRange);
670             arrayRange.emplace_back(textRange);
671         }
672         picker->SetRange(arrayRange);
673         return true;
674     }
675 
676     return false;
677 }
678 
SetColumnCount(const std::string & value)679 bool DOMPickerBase::SetColumnCount(const std::string& value)
680 {
681     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_MULTITEXT &&
682         AceType::InstanceOf<PickerMultiTextComponent>(basePickerChild_)) {
683         auto picker = AceType::DynamicCast<PickerMultiTextComponent>(basePickerChild_);
684         picker->SetColumnCount(StringUtils::StringToInt(value));
685         return true;
686     }
687 
688     return false;
689 }
690 
SetHour24(const std::string & value)691 bool DOMPickerBase::SetHour24(const std::string& value)
692 {
693     if (basePickerChild_ && (type_ == DOM_PICKER_TYPE_DATETIME || type_ == DOM_PICKER_TYPE_TIME) &&
694         AceType::InstanceOf<PickerTimeComponent>(basePickerChild_)) {
695         auto picker = AceType::DynamicCast<PickerTimeComponent>(basePickerChild_);
696         if (value == "24") {
697             picker->SetHour24(true);
698         } else if (value == "12") {
699             picker->SetHour24(false);
700         } else {
701             LOGE("value of hour24 is invalid.");
702         }
703         return true;
704     }
705 
706     return false;
707 }
708 
SetShowLunar(const std::string & value)709 bool DOMPickerBase::SetShowLunar(const std::string& value)
710 {
711     static const char* FALSE = "false";
712     static const char* TRUE = "true";
713 
714     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATETIME &&
715         AceType::InstanceOf<PickerDateTimeComponent>(basePickerChild_)) {
716         auto picker = AceType::DynamicCast<PickerDateTimeComponent>(basePickerChild_);
717         if (value == TRUE) {
718             picker->SetShowLunar(true);
719         } else if (value == FALSE) {
720             picker->SetShowLunar(false);
721         } else {
722             LOGE("value of show lunar is invalid.");
723         }
724         return true;
725     }
726 
727     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
728         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
729         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
730         if (value == TRUE) {
731             picker->SetShowLunar(true);
732         } else if (value == FALSE) {
733             picker->SetShowLunar(false);
734         } else {
735             LOGE("value of show lunar is invalid.");
736         }
737         return true;
738     }
739 
740     return false;
741 }
742 
SetLunar(const std::string & value)743 bool DOMPickerBase::SetLunar(const std::string& value)
744 {
745     static const char* FALSE = "false";
746     static const char* TRUE = "true";
747 
748     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATETIME &&
749         AceType::InstanceOf<PickerDateTimeComponent>(basePickerChild_)) {
750         auto picker = AceType::DynamicCast<PickerDateTimeComponent>(basePickerChild_);
751         if (value == TRUE) {
752             picker->SetHasLunar(true);
753         } else if (value == FALSE) {
754             picker->SetHasLunar(false);
755         } else {
756             LOGE("value of lunar is invalid.");
757         }
758         return true;
759     }
760 
761     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_DATE &&
762         AceType::InstanceOf<PickerDateComponent>(basePickerChild_)) {
763         auto picker = AceType::DynamicCast<PickerDateComponent>(basePickerChild_);
764         if (value == TRUE) {
765             picker->SetHasLunar(true);
766         } else if (value == FALSE) {
767             picker->SetHasLunar(false);
768         } else {
769             LOGE("value of lunar is invalid.");
770         }
771         return true;
772     }
773 
774     return false;
775 }
776 
SetPrefix(const std::string & value)777 bool DOMPickerBase::SetPrefix(const std::string& value)
778 {
779     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TEXT &&
780         AceType::InstanceOf<PickerTextComponent>(basePickerChild_)) {
781         auto picker = AceType::DynamicCast<PickerTextComponent>(basePickerChild_);
782         picker->SetPrefix(value);
783         return true;
784     }
785     return false;
786 }
787 
SetSuffix(const std::string & value)788 bool DOMPickerBase::SetSuffix(const std::string& value)
789 {
790     if (basePickerChild_ && type_ == DOM_PICKER_TYPE_TEXT &&
791         AceType::InstanceOf<PickerTextComponent>(basePickerChild_)) {
792         auto picker = AceType::DynamicCast<PickerTextComponent>(basePickerChild_);
793         picker->SetSuffix(value);
794         return true;
795     }
796     return false;
797 }
798 
SetVibrate(const std::string & value)799 bool DOMPickerBase::SetVibrate(const std::string& value)
800 {
801     static const char* FALSE = "false";
802     static const char* TRUE = "true";
803 
804     if (basePickerChild_) {
805         if (value == TRUE) {
806             basePickerChild_->SetNeedVibrate(true);
807         } else if (value == FALSE) {
808             basePickerChild_->SetNeedVibrate(false);
809         } else {
810             LOGE("value of lunar is invalid.");
811         }
812         return true;
813     }
814     return false;
815 }
816 
817 } // namespace OHOS::Ace::Framework
818