1 /*
2 * Copyright (c) 2021-2022 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 "bridge/declarative_frontend/jsview/js_select.h"
17
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21
22 #include "base/log/ace_scoring_log.h"
23 #include "base/utils/utils.h"
24 #include "bridge/common/utils/utils.h"
25 #include "bridge/declarative_frontend/engine/functions/js_function.h"
26 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
27 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
28 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
29 #include "core/components/option/option_component.h"
30 #include "core/components/select/select_component.h"
31 #include "core/components/select/select_theme.h"
32 #include "core/components_ng/pattern/select/select_view.h"
33 #include "core/components_v2/inspector/inspector_constants.h"
34 #include "core/pipeline/pipeline_base.h"
35
36 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)37 void JSSelect::Create(const JSCallbackInfo& info)
38 {
39 if (info.Length() < 0) {
40 return;
41 }
42
43 if (Container::IsCurrentUseNewPipeline()) {
44 if (info[0]->IsArray()) {
45 auto paramArray = JSRef<JSArray>::Cast(info[0]);
46 size_t size = paramArray->Length();
47 std::vector<NG::SelectParam> params(size);
48 for (size_t i = 0; i < size; i++) {
49 std::string value;
50 std::string icon;
51
52 auto indexObject = JSRef<JSObject>::Cast(paramArray->GetValueAt(i));
53 auto selectValue = indexObject->GetProperty("value");
54 auto selectIcon = indexObject->GetProperty("icon");
55 if (!ParseJsString(selectValue, value)) {
56 LOGE("selectValue is null");
57 return;
58 }
59 if (!ParseJsMedia(selectIcon, icon)) {
60 LOGI("selectIcon is null");
61 }
62
63 params[i] = { value, icon };
64 }
65 NG::SelectView::Create(params);
66 }
67 return;
68 }
69
70 auto selectTheme = GetTheme<SelectTheme>();
71 auto selectComponent = AceType::MakeRefPtr<SelectComponent>();
72 selectComponent->SetTheme(selectTheme);
73
74 auto tipText = AceType::MakeRefPtr<TextComponent>("");
75 selectComponent->SetTipText(tipText);
76
77 if (info[0]->IsArray()) {
78 auto paramArray = JSRef<JSArray>::Cast(info[0]);
79 size_t size = paramArray->Length();
80 for (size_t i = 0; i < size; i++) {
81 std::string value;
82 std::string icon;
83
84 auto indexObject = JSRef<JSObject>::Cast(paramArray->GetValueAt(i));
85 auto selectValue = indexObject->GetProperty("value");
86 auto selectIcon = indexObject->GetProperty("icon");
87 if (!ParseJsString(selectValue, value)) {
88 LOGE("selectValue is null");
89 return;
90 }
91 if (!ParseJsMedia(selectIcon, icon)) {
92 LOGE("selectValue is null");
93 }
94
95 auto optionTheme = GetTheme<SelectTheme>();
96 if (!optionTheme) {
97 LOGE("JSSelect: Get option theme is null.");
98 continue;
99 }
100
101 auto optionComponent = AceType::MakeRefPtr<OHOS::Ace::OptionComponent>(optionTheme);
102 auto textComponent = AceType::MakeRefPtr<OHOS::Ace::TextComponent>(value);
103 if (!icon.empty()) {
104 optionComponent->SetIcon(AceType::MakeRefPtr<OHOS::Ace::ImageComponent>(icon));
105 }
106 optionComponent->SetTheme(optionTheme);
107 optionComponent->SetText(textComponent);
108 optionComponent->SetTextStyle(optionTheme->GetTitleStyle());
109 optionComponent->SetSelectedTextStyle(optionTheme->GetTitleStyle());
110 optionComponent->SetSelectedBackgroundColor(optionTheme->GetSelectedColor());
111 optionComponent->SetValue(value);
112 selectComponent->AppendSelectOption(optionComponent);
113 }
114 }
115
116 ViewStackProcessor::GetInstance()->ClaimElementId(selectComponent);
117 ViewStackProcessor::GetInstance()->Push(selectComponent);
118 }
119
JSBind(BindingTarget globalObj)120 void JSSelect::JSBind(BindingTarget globalObj)
121 {
122 JSClass<JSSelect>::Declare("Select");
123 MethodOptions opt = MethodOptions::NONE;
124 JSClass<JSSelect>::StaticMethod("create", &JSSelect::Create, opt);
125
126 JSClass<JSSelect>::StaticMethod("selected", &JSSelect::Selected, opt);
127 JSClass<JSSelect>::StaticMethod("value", &JSSelect::Value, opt);
128 JSClass<JSSelect>::StaticMethod("font", &JSSelect::Font, opt);
129 JSClass<JSSelect>::StaticMethod("fontColor", &JSSelect::FontColor, opt);
130 JSClass<JSSelect>::StaticMethod("selectedOptionBgColor", &JSSelect::SelectedOptionBgColor, opt);
131 JSClass<JSSelect>::StaticMethod("selectedOptionFont", &JSSelect::SelectedOptionFont, opt);
132 JSClass<JSSelect>::StaticMethod("selectedOptionFontColor", &JSSelect::SelectedOptionFontColor, opt);
133 JSClass<JSSelect>::StaticMethod("optionBgColor", &JSSelect::OptionBgColor, opt);
134 JSClass<JSSelect>::StaticMethod("optionFont", &JSSelect::OptionFont, opt);
135 JSClass<JSSelect>::StaticMethod("optionFontColor", &JSSelect::OptionFontColor, opt);
136 JSClass<JSSelect>::StaticMethod("onSelect", &JSSelect::OnSelected, opt);
137 // API7 onSelected deprecated
138 JSClass<JSSelect>::StaticMethod("onSelected", &JSSelect::OnSelected, opt);
139 JSClass<JSSelect>::StaticMethod("width", &JSSelect::JsWidth);
140 JSClass<JSSelect>::StaticMethod("height", &JSSelect::JsHeight);
141 JSClass<JSSelect>::StaticMethod("size", &JSSelect::JsSize);
142 JSClass<JSSelect>::StaticMethod("padding", &JSSelect::JsPadding);
143 JSClass<JSSelect>::StaticMethod("paddingTop", &JSSelect::SetPaddingTop, opt);
144 JSClass<JSSelect>::StaticMethod("paddingBottom", &JSSelect::SetPaddingBottom, opt);
145 JSClass<JSSelect>::StaticMethod("paddingLeft", &JSSelect::SetPaddingLeft, opt);
146 JSClass<JSSelect>::StaticMethod("paddingRight", &JSSelect::SetPaddingRight, opt);
147
148 JSClass<JSSelect>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
149 JSClass<JSSelect>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
150 JSClass<JSSelect>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
151 JSClass<JSSelect>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
152 JSClass<JSSelect>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
153 JSClass<JSSelect>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
154 JSClass<JSSelect>::Inherit<JSViewAbstract>();
155 JSClass<JSSelect>::Bind(globalObj);
156 }
157
Selected(int value)158 void JSSelect::Selected(int value)
159 {
160 if (Container::IsCurrentUseNewPipeline()) {
161 if (value <= 0) {
162 value = 0;
163 }
164 NG::SelectView::SetSelected(value);
165 return;
166 }
167
168 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
169 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
170 if (!selectComponent) {
171 LOGE("search component error");
172 return;
173 }
174 auto popup = selectComponent->GetPopup();
175 if (!popup) {
176 LOGE("popup is null");
177 return;
178 }
179 auto option = popup->GetSelectOptions();
180 if (value < 0 || value >= static_cast<int32_t>(option.size())) {
181 LOGE("Input selected index error, use the default value");
182 value = 0;
183 }
184
185 auto tipText = selectComponent->GetTipText();
186 auto optionComponent = selectComponent->GetSelectOption(value);
187 if (!optionComponent) {
188 LOGE("optionComponent is null");
189 return;
190 }
191 optionComponent->SetSelected(true);
192
193 auto optionText = optionComponent->GetText();
194 if (!optionText) {
195 return;
196 }
197 if (!selectComponent->HasSetTipText()) {
198 tipText->SetData(optionText->GetData());
199 }
200 }
201
Value(const std::string & value)202 void JSSelect::Value(const std::string& value)
203 {
204 if (Container::IsCurrentUseNewPipeline()) {
205 NG::SelectView::SetValue(value);
206 return;
207 }
208 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
209 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
210 if (!selectComponent) {
211 LOGE("search component error");
212 return;
213 }
214 auto tipText = selectComponent->GetTipText();
215 if (!value.empty()) {
216 selectComponent->SetTipText(true);
217 }
218 tipText->SetData(value);
219 }
220
Font(const JSCallbackInfo & info)221 void JSSelect::Font(const JSCallbackInfo& info)
222 {
223 if (!info[0]->IsObject()) {
224 return;
225 }
226
227 auto param = JSRef<JSObject>::Cast(info[0]);
228
229 if (Container::IsCurrentUseNewPipeline()) {
230 auto size = param->GetProperty("size");
231 if (!size->IsNull()) {
232 Dimension fontSize;
233 if (ParseJsDimensionFp(size, fontSize)) {
234 NG::SelectView::SetFontSize(fontSize);
235 }
236 }
237 std::string weight;
238 auto fontWeight = param->GetProperty("weight");
239 if (!fontWeight->IsNull()) {
240 if (fontWeight->IsNumber()) {
241 weight = std::to_string(fontWeight->ToNumber<int32_t>());
242 } else {
243 ParseJsString(fontWeight, weight);
244 }
245 NG::SelectView::SetFontWeight(ConvertStrToFontWeight(weight));
246 }
247
248 auto family = param->GetProperty("family");
249 if (!family->IsNull() && family->IsString()) {
250 auto familyVal = family->ToString();
251 NG::SelectView::SetFontFamily(ConvertStrToFontFamilies(familyVal));
252 }
253
254 auto style = param->GetProperty("style");
255 if (!style->IsNull() && style->IsNumber()) {
256 auto styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
257 NG::SelectView::SetItalicFontStyle(styleVal);
258 }
259 return;
260 }
261
262 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
263 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
264 if (!selectComponent) {
265 LOGE("search component error");
266 return;
267 }
268
269 auto size = param->GetProperty("size");
270 TextStyle textStyle = selectComponent->GetSelectStyle();
271 if (!size->IsNull()) {
272 Dimension fontSize;
273 if (ParseJsDimensionFp(size, fontSize)) {
274 textStyle.SetFontSize(fontSize);
275 }
276 }
277
278 std::string weight;
279 auto fontWeight = param->GetProperty("weight");
280 if (!fontWeight->IsNull()) {
281 if (fontWeight->IsNumber()) {
282 weight = std::to_string(fontWeight->ToNumber<int32_t>());
283 } else {
284 ParseJsString(fontWeight, weight);
285 }
286 textStyle.SetFontWeight(ConvertStrToFontWeight(weight));
287 }
288
289 auto family = param->GetProperty("family");
290 if (!family->IsNull() && family->IsString()) {
291 auto familyVal = family->ToString();
292 textStyle.SetFontFamilies(ConvertStrToFontFamilies(familyVal));
293 }
294
295 auto style = param->GetProperty("style");
296 if (!style->IsNull() && style->IsNumber()) {
297 FontStyle styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
298 textStyle.SetFontStyle(styleVal);
299 }
300 selectComponent->SetSelectStyle(std::move(textStyle));
301 }
302
FontColor(const JSCallbackInfo & info)303 void JSSelect::FontColor(const JSCallbackInfo& info)
304 {
305 if (info.Length() < 1) {
306 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
307 return;
308 }
309
310 Color textColor;
311 if (!ParseJsColor(info[0], textColor)) {
312 if (info[0]->IsNull() || info[0]->IsUndefined()) {
313 auto pipeline = PipelineBase::GetCurrentContext();
314 CHECK_NULL_VOID_NOLOG(pipeline);
315 auto theme = pipeline->GetTheme<SelectTheme>();
316 CHECK_NULL_VOID_NOLOG(theme);
317 textColor = theme->GetFontColor();
318 } else {
319 return;
320 }
321 }
322
323 if (Container::IsCurrentUseNewPipeline()) {
324 NG::SelectView::SetFontColor(textColor);
325 return;
326 }
327
328 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
329 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
330 if (!selectComponent) {
331 LOGE("search component error");
332 return;
333 }
334 auto textStyle = selectComponent->GetSelectStyle();
335 textStyle.SetTextColor(textColor);
336 selectComponent->SetSelectStyle(std::move(textStyle));
337 }
338
SelectedOptionBgColor(const JSCallbackInfo & info)339 void JSSelect::SelectedOptionBgColor(const JSCallbackInfo& info)
340 {
341 if (info.Length() < 1) {
342 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
343 return;
344 }
345 Color bgColor;
346 if (!ParseJsColor(info[0], bgColor)) {
347 if (info[0]->IsUndefined() || info[0]->IsNull()) {
348 auto pipeline = PipelineBase::GetCurrentContext();
349 CHECK_NULL_VOID_NOLOG(pipeline);
350 auto theme = pipeline->GetTheme<SelectTheme>();
351 CHECK_NULL_VOID_NOLOG(theme);
352 bgColor = theme->GetSelectedColor();
353 } else {
354 return;
355 }
356 }
357
358 if (Container::IsCurrentUseNewPipeline()) {
359 NG::SelectView::SetSelectedOptionBgColor(bgColor);
360 return;
361 }
362
363 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
364 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
365 if (!selectComponent) {
366 LOGE("search component error");
367 return;
368 }
369 auto popup = selectComponent->GetPopup();
370 if (!popup) {
371 LOGE("popup is null");
372 return;
373 }
374 auto option = popup->GetSelectOptions();
375 for (auto& optionItem : option) {
376 if (optionItem) {
377 optionItem->SetSelectedBackgroundColor(bgColor);
378 }
379 }
380 }
381
SelectedOptionFont(const JSCallbackInfo & info)382 void JSSelect::SelectedOptionFont(const JSCallbackInfo& info)
383 {
384 if (!info[0]->IsObject()) {
385 return;
386 }
387 auto param = JSRef<JSObject>::Cast(info[0]);
388
389 if (Container::IsCurrentUseNewPipeline()) {
390 auto size = param->GetProperty("size");
391 if (!size->IsNull()) {
392 Dimension fontSize;
393 if (ParseJsDimensionFp(size, fontSize)) {
394 NG::SelectView::SetSelectedOptionFontSize(fontSize);
395 }
396 }
397 std::string weight;
398 auto fontWeight = param->GetProperty("weight");
399 if (!fontWeight->IsNull()) {
400 if (fontWeight->IsNumber()) {
401 weight = std::to_string(fontWeight->ToNumber<int32_t>());
402 } else {
403 ParseJsString(fontWeight, weight);
404 }
405 NG::SelectView::SetSelectedOptionFontWeight(ConvertStrToFontWeight(weight));
406 }
407
408 auto family = param->GetProperty("family");
409 if (!family->IsNull() && family->IsString()) {
410 auto familyVal = family->ToString();
411 NG::SelectView::SetSelectedOptionFontFamily(ConvertStrToFontFamilies(familyVal));
412 }
413
414 auto style = param->GetProperty("style");
415 if (!style->IsNull() && style->IsNumber()) {
416 auto styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
417 NG::SelectView::SetSelectedOptionItalicFontStyle(styleVal);
418 }
419 return;
420 }
421
422 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
423 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
424 if (!selectComponent) {
425 LOGE("search component error");
426 return;
427 }
428 auto popup = selectComponent->GetPopup();
429 if (!popup) {
430 LOGE("popup is null");
431 return;
432 }
433 auto option = popup->GetSelectOptions();
434 if (info.Length() < 1) {
435 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
436 return;
437 }
438 for (auto& optionItem : option) {
439 auto size = param->GetProperty("size");
440 TextStyle textStyle = optionItem->GetSelectedTextStyle();
441
442 if (!size->IsNull()) {
443 Dimension fontSize;
444 if (ParseJsDimensionFp(size, fontSize)) {
445 textStyle.SetFontSize(fontSize);
446 }
447 }
448
449 std::string weight;
450 auto fontWeight = param->GetProperty("weight");
451 if (!fontWeight->IsNull()) {
452 if (fontWeight->IsNumber()) {
453 weight = std::to_string(fontWeight->ToNumber<int32_t>());
454 } else {
455 ParseJsString(fontWeight, weight);
456 }
457 textStyle.SetFontWeight(ConvertStrToFontWeight(weight));
458 }
459
460 auto family = param->GetProperty("family");
461 if (!family->IsNull() && family->IsString()) {
462 auto familyVal = family->ToString();
463 textStyle.SetFontFamilies(ConvertStrToFontFamilies(familyVal));
464 }
465
466 auto style = param->GetProperty("style");
467 if (!style->IsNull() && style->IsNumber()) {
468 FontStyle styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
469 textStyle.SetFontStyle(styleVal);
470 }
471
472 optionItem->SetSelectedTextStyle(std::move(textStyle));
473 }
474 }
475
SelectedOptionFontColor(const JSCallbackInfo & info)476 void JSSelect::SelectedOptionFontColor(const JSCallbackInfo& info)
477 {
478 if (info.Length() < 1) {
479 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
480 return;
481 }
482 Color textColor;
483 if (!ParseJsColor(info[0], textColor)) {
484 return;
485 }
486
487 if (Container::IsCurrentUseNewPipeline()) {
488 NG::SelectView::SetSelectedOptionFontColor(textColor);
489 return;
490 }
491
492 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
493 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
494 if (!selectComponent) {
495 LOGE("search component error");
496 return;
497 }
498 auto popup = selectComponent->GetPopup();
499 if (!popup) {
500 LOGE("popup is null");
501 return;
502 }
503 auto option = popup->GetSelectOptions();
504 for (auto& optionItem : option) {
505 if (optionItem) {
506 TextStyle textStyle = optionItem->GetSelectedTextStyle();
507 textStyle.SetTextColor(textColor);
508 optionItem->SetSelectedTextStyle(textStyle);
509 }
510 }
511 }
512
OptionBgColor(const JSCallbackInfo & info)513 void JSSelect::OptionBgColor(const JSCallbackInfo& info)
514 {
515 if (info.Length() < 1) {
516 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
517 return;
518 }
519 Color bgColor;
520 if (!ParseJsColor(info[0], bgColor)) {
521 return;
522 }
523
524 if (Container::IsCurrentUseNewPipeline()) {
525 NG::SelectView::SetOptionBgColor(bgColor);
526 return;
527 }
528
529 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
530 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
531 if (!selectComponent) {
532 LOGE("search component error");
533 return;
534 }
535 auto popup = selectComponent->GetPopup();
536 if (!popup) {
537 LOGE("popup is null");
538 return;
539 }
540 auto option = popup->GetSelectOptions();
541 for (auto& optionItem : option) {
542 if (optionItem) {
543 optionItem->SetBackgroundColor(bgColor);
544 }
545 }
546 }
547
OptionFont(const JSCallbackInfo & info)548 void JSSelect::OptionFont(const JSCallbackInfo& info)
549 {
550 if (!info[0]->IsObject()) {
551 return;
552 }
553 auto param = JSRef<JSObject>::Cast(info[0]);
554
555 if (Container::IsCurrentUseNewPipeline()) {
556 auto size = param->GetProperty("size");
557 if (!size->IsNull()) {
558 Dimension fontSize;
559 if (ParseJsDimensionFp(size, fontSize)) {
560 NG::SelectView::SetOptionFontSize(fontSize);
561 }
562 }
563 std::string weight;
564 auto fontWeight = param->GetProperty("weight");
565 if (!fontWeight->IsNull()) {
566 if (fontWeight->IsNumber()) {
567 weight = std::to_string(fontWeight->ToNumber<int32_t>());
568 } else {
569 ParseJsString(fontWeight, weight);
570 }
571 NG::SelectView::SetOptionFontWeight(ConvertStrToFontWeight(weight));
572 }
573
574 auto family = param->GetProperty("family");
575 if (!family->IsNull() && family->IsString()) {
576 auto familyVal = family->ToString();
577 NG::SelectView::SetOptionFontFamily(ConvertStrToFontFamilies(familyVal));
578 }
579
580 auto style = param->GetProperty("style");
581 if (!style->IsNull() && style->IsNumber()) {
582 auto styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
583 NG::SelectView::SetOptionItalicFontStyle(styleVal);
584 }
585 return;
586 }
587
588 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
589 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
590 if (!selectComponent) {
591 LOGE("search component error");
592 return;
593 }
594 auto popup = selectComponent->GetPopup();
595 if (!popup) {
596 LOGE("popup is null");
597 return;
598 }
599 auto option = popup->GetSelectOptions();
600 if (info.Length() < 1) {
601 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
602 return;
603 }
604 for (auto& optionItem : option) {
605 TextStyle textStyle = optionItem->GetTextStyle();
606 auto size = param->GetProperty("size");
607 if (!size->IsNull()) {
608 Dimension fontSize;
609 if (ParseJsDimensionFp(size, fontSize)) {
610 textStyle.SetFontSize(fontSize);
611 }
612 }
613
614 std::string weight;
615 auto fontWeight = param->GetProperty("weight");
616 if (!fontWeight->IsNull()) {
617 if (fontWeight->IsNumber()) {
618 weight = std::to_string(fontWeight->ToNumber<int32_t>());
619 } else {
620 ParseJsString(fontWeight, weight);
621 }
622 textStyle.SetFontWeight(ConvertStrToFontWeight(weight));
623 }
624
625 auto family = param->GetProperty("family");
626 if (!family->IsNull() && family->IsString()) {
627 auto familyVal = family->ToString();
628 textStyle.SetFontFamilies(ConvertStrToFontFamilies(familyVal));
629 }
630
631 auto style = param->GetProperty("style");
632 if (!style->IsNull() && style->IsNumber()) {
633 FontStyle styleVal = static_cast<FontStyle>(style->ToNumber<int32_t>());
634 textStyle.SetFontStyle(styleVal);
635 }
636
637 optionItem->SetTextStyle(std::move(textStyle));
638 }
639 }
640
OptionFontColor(const JSCallbackInfo & info)641 void JSSelect::OptionFontColor(const JSCallbackInfo& info)
642 {
643 if (info.Length() < 1) {
644 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
645 return;
646 }
647 Color textColor;
648 if (!ParseJsColor(info[0], textColor)) {
649 return;
650 }
651
652 if (Container::IsCurrentUseNewPipeline()) {
653 NG::SelectView::SetOptionFontColor(textColor);
654 return;
655 }
656
657 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
658 auto selectComponent = AceType::DynamicCast<SelectComponent>(component);
659 if (!selectComponent) {
660 LOGE("search component error");
661 return;
662 }
663 auto popup = selectComponent->GetPopup();
664 if (!popup) {
665 LOGE("popup is null");
666 return;
667 }
668 auto option = popup->GetSelectOptions();
669 for (auto& optionItem : option) {
670 if (optionItem) {
671 TextStyle textStyle = optionItem->GetTextStyle();
672 textStyle.SetTextColor(textColor);
673 optionItem->SetTextStyle(textStyle);
674 }
675 }
676 }
677
OnSelected(const JSCallbackInfo & info)678 void JSSelect::OnSelected(const JSCallbackInfo& info)
679 {
680 if (Container::IsCurrentUseNewPipeline()) {
681 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
682 auto onSelect = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
683 int32_t index, const std::string& value) {
684 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
685 ACE_SCORING_EVENT("Select.onSelect");
686 JSRef<JSVal> params[2];
687 params[0] = JSRef<JSVal>::Make(ToJSValue(index));
688 params[1] = JSRef<JSVal>::Make(ToJSValue(value));
689 func->ExecuteJS(2, params);
690 };
691 NG::SelectView::SetOnSelect(onSelect);
692 } else if (!JSViewBindEvent(&SelectComponent::SetOnSelected, info)) {
693 LOGE("Failed to bind event");
694 }
695 info.ReturnSelf();
696 }
697
JsWidth(const JSCallbackInfo & info)698 void JSSelect::JsWidth(const JSCallbackInfo& info)
699 {
700 if (Container::IsCurrentUseNewPipeline()) {
701 JSViewAbstract::JsWidth(info);
702 return;
703 }
704 if (info.Length() < 1) {
705 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
706 return;
707 }
708
709 Width(info[0]);
710 }
711
Width(const JSRef<JSVal> & jsValue)712 void JSSelect::Width(const JSRef<JSVal>& jsValue)
713 {
714 Dimension value;
715 if (!ParseJsDimensionVp(jsValue, value)) {
716 return;
717 }
718 auto stack = ViewStackProcessor::GetInstance();
719 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
720 if (selectComponent) {
721 selectComponent->SetWidth(value);
722 }
723 }
724
JsHeight(const JSCallbackInfo & info)725 void JSSelect::JsHeight(const JSCallbackInfo& info)
726 {
727 if (Container::IsCurrentUseNewPipeline()) {
728 JSViewAbstract::JsHeight(info);
729 return;
730 }
731 if (info.Length() < 1) {
732 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
733 return;
734 }
735
736 Height(info[0]);
737 }
738
Height(const JSRef<JSVal> & jsValue)739 void JSSelect::Height(const JSRef<JSVal>& jsValue)
740 {
741 Dimension value;
742 if (!ParseJsDimensionVp(jsValue, value)) {
743 return;
744 }
745 auto stack = ViewStackProcessor::GetInstance();
746 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
747 if (selectComponent) {
748 selectComponent->SetHeight(value);
749 }
750 }
751
JsSize(const JSCallbackInfo & info)752 void JSSelect::JsSize(const JSCallbackInfo& info)
753 {
754 if (info.Length() < 1) {
755 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
756 return;
757 }
758
759 if (!info[0]->IsObject()) {
760 LOGE("arg is not Object or String.");
761 return;
762 }
763
764 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
765 Width(sizeObj->GetProperty("width"));
766 Height(sizeObj->GetProperty("height"));
767 }
768
JsPadding(const JSCallbackInfo & info)769 void JSSelect::JsPadding(const JSCallbackInfo& info)
770 {
771 if (!info[0]->IsString() && !info[0]->IsNumber() && !info[0]->IsObject()) {
772 LOGE("arg is not a string, number or object.");
773 return;
774 }
775
776 if (Container::IsCurrentUseNewPipeline()) {
777 JSViewAbstract::JsPadding(info);
778 return;
779 }
780
781 if (info[0]->IsObject()) {
782 auto stack = ViewStackProcessor::GetInstance();
783 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
784 if (!selectComponent) {
785 LOGE("search component error");
786 return;
787 }
788 auto argsPtrItem = JsonUtil::ParseJsonString(info[0]->ToString());
789 if (!argsPtrItem || argsPtrItem->IsNull()) {
790 LOGE("Js Parse object failed. argsPtr is null. %s", info[0]->ToString().c_str());
791 return;
792 }
793 if (argsPtrItem->Contains("top")) {
794 Dimension topDimen = Dimension(0.0, DimensionUnit::VP);
795 if (ParseJsonDimensionVp(argsPtrItem->GetValue("top"), topDimen)) {
796 selectComponent->SetTopPadding(topDimen);
797 }
798 }
799 if (argsPtrItem->Contains("left")) {
800 Dimension leftDimen = Dimension(0.0, DimensionUnit::VP);
801 if (ParseJsonDimensionVp(argsPtrItem->GetValue("left"), leftDimen)) {
802 selectComponent->SetLeftPadding(leftDimen);
803 }
804 }
805 if (argsPtrItem->Contains("right")) {
806 Dimension rightDimen = Dimension(0.0, DimensionUnit::VP);
807 if (ParseJsonDimensionVp(argsPtrItem->GetValue("right"), rightDimen)) {
808 selectComponent->SetRightPadding(rightDimen);
809 }
810 }
811 if (argsPtrItem->Contains("bottom")) {
812 Dimension bottomDimen = Dimension(0.0, DimensionUnit::VP);
813 if (ParseJsonDimensionVp(argsPtrItem->GetValue("bottom"), bottomDimen)) {
814 selectComponent->SetBottomPadding(bottomDimen);
815 }
816 }
817 }
818 Dimension length;
819 if (ParseJsDimensionVp(info[0], length)) {
820 auto stack = ViewStackProcessor::GetInstance();
821 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
822 if (selectComponent) {
823 selectComponent->SetLeftPadding(length);
824 selectComponent->SetTopPadding(length);
825 selectComponent->SetRightPadding(length);
826 selectComponent->SetBottomPadding(length);
827 }
828 }
829 }
830
SetPaddingLeft(const JSCallbackInfo & info)831 void JSSelect::SetPaddingLeft(const JSCallbackInfo& info)
832 {
833 if (Container::IsCurrentUseNewPipeline()) {
834 JSViewAbstract::SetPaddingLeft(info);
835 return;
836 }
837 if (info.Length() < 1) {
838 LOGE("The arg is wrong, it is supposed to have at least 1 argument");
839 return;
840 }
841 Dimension value;
842 if (!ParseJsDimensionVp(info[0], value)) {
843 return;
844 }
845 auto stack = ViewStackProcessor::GetInstance();
846 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
847 if (selectComponent) {
848 selectComponent->SetLeftPadding(value);
849 }
850 }
851
SetPaddingTop(const JSCallbackInfo & info)852 void JSSelect::SetPaddingTop(const JSCallbackInfo& info)
853 {
854 if (Container::IsCurrentUseNewPipeline()) {
855 JSViewAbstract::SetPaddingTop(info);
856 return;
857 }
858 if (info.Length() < 1) {
859 LOGE("The arg is wrong, it is supposed to have at least 1 argument");
860 return;
861 }
862 Dimension value;
863 if (!ParseJsDimensionVp(info[0], value)) {
864 return;
865 }
866 auto stack = ViewStackProcessor::GetInstance();
867 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
868 if (selectComponent) {
869 selectComponent->SetTopPadding(value);
870 }
871 }
872
SetPaddingRight(const JSCallbackInfo & info)873 void JSSelect::SetPaddingRight(const JSCallbackInfo& info)
874 {
875 if (Container::IsCurrentUseNewPipeline()) {
876 JSViewAbstract::SetPaddingRight(info);
877 return;
878 }
879 if (info.Length() < 1) {
880 LOGE("The arg is wrong, it is supposed to have at least 1 argument");
881 return;
882 }
883 Dimension value;
884 if (!ParseJsDimensionVp(info[0], value)) {
885 return;
886 }
887 auto stack = ViewStackProcessor::GetInstance();
888 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
889 if (selectComponent) {
890 selectComponent->SetRightPadding(value);
891 }
892 }
893
SetPaddingBottom(const JSCallbackInfo & info)894 void JSSelect::SetPaddingBottom(const JSCallbackInfo& info)
895 {
896 if (Container::IsCurrentUseNewPipeline()) {
897 JSViewAbstract::SetPaddingBottom(info);
898 return;
899 }
900 if (info.Length() < 1) {
901 LOGE("The arg is wrong, it is supposed to have at least 1 argument");
902 return;
903 }
904 Dimension value;
905 if (!ParseJsDimensionVp(info[0], value)) {
906 return;
907 }
908 auto stack = ViewStackProcessor::GetInstance();
909 auto selectComponent = AceType::DynamicCast<SelectComponent>(stack->GetMainComponent());
910 if (selectComponent) {
911 selectComponent->SetBottomPadding(value);
912 }
913 }
914 } // namespace OHOS::Ace::Framework
915