1 /*
2 * Copyright (c) 2021-2023 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_indexer.h"
17
18 #include "base/geometry/dimension.h"
19 #include "base/log/ace_scoring_log.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
22 #include "bridge/declarative_frontend/jsview/js_scroller.h"
23 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
24 #include "bridge/declarative_frontend/jsview/models/indexer_model_impl.h"
25 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_indexer_theme.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components/common/properties/decoration.h"
28 #include "core/components/common/properties/text_style.h"
29 #include "core/components/indexer/indexer_theme.h"
30 #include "core/components_ng/pattern/indexer/indexer_model_ng.h"
31
32 namespace OHOS::Ace {
33 std::unique_ptr<IndexerModel> IndexerModel::instance_ = nullptr;
34 std::mutex IndexerModel::mutex_;
GetInstance()35 IndexerModel* IndexerModel::GetInstance()
36 {
37 if (!instance_) {
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (!instance_) {
40 #ifdef NG_BUILD
41 instance_.reset(new NG::IndexerModelNG());
42 #else
43 if (Container::IsCurrentUseNewPipeline()) {
44 instance_.reset(new NG::IndexerModelNG());
45 } else {
46 instance_.reset(new Framework::IndexerModelImpl());
47 }
48 #endif
49 }
50 }
51 return instance_.get();
52 }
53 } // namespace OHOS::Ace
54
55 namespace OHOS::Ace::Framework {
56 namespace {
57 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
58 const std::vector<V2::AlignStyle> ALIGN_STYLE = { V2::AlignStyle::LEFT, V2::AlignStyle::RIGHT, V2::AlignStyle::START,
59 V2::AlignStyle::END };
60 const std::vector<NG::AlignStyle> NG_ALIGN_STYLE = { NG::AlignStyle::LEFT, NG::AlignStyle::RIGHT, NG::AlignStyle::START,
61 NG::AlignStyle::END };
62 constexpr Dimension DEFAULT_ITEM_SIZE = 16.0_vp;
63 constexpr double ZERO_RADIUS = 0.0;
64 constexpr double POPUP_ITEM_DEFAULT_RADIUS = 24.0;
65 constexpr double ITEM_DEFAULT_RADIUS = 8.0;
66 constexpr double RADIUS_OFFSET = 4.0;
67 }; // namespace
68
ParseIndexerSelectedObject(const JSCallbackInfo & info,const JSRef<JSVal> & changeEventVal,bool isMethodProp=false)69 void JSIndexer::ParseIndexerSelectedObject(
70 const JSCallbackInfo& info, const JSRef<JSVal>& changeEventVal, bool isMethodProp = false)
71 {
72 CHECK_NULL_VOID(changeEventVal->IsFunction());
73 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(changeEventVal));
74 auto changeEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const int32_t selected) {
75 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
76 ACE_SCORING_EVENT("Indexer.SelectedChangeEvent");
77 auto newJSVal = JSRef<JSVal>::Make(ToJSValue(selected));
78 func->ExecuteJS(1, &newJSVal);
79 };
80
81 if (isMethodProp) {
82 IndexerModel::GetInstance()->SetChangeEvent(changeEvent);
83 } else {
84 IndexerModel::GetInstance()->SetCreatChangeEvent(changeEvent);
85 }
86 }
87
CreateArc(const JSCallbackInfo & args)88 void JSIndexer::CreateArc(const JSCallbackInfo& args)
89 {
90 Create(args, true);
91 }
92
Create(const JSCallbackInfo & args)93 void JSIndexer::Create(const JSCallbackInfo& args)
94 {
95 Create(args, false);
96 }
97
Create(const JSCallbackInfo & args,bool isArc)98 void JSIndexer::Create(const JSCallbackInfo& args, bool isArc)
99 {
100 if (args.Length() < 1 || !args[0]->IsObject()) {
101 return;
102 }
103 size_t length = 0;
104 int32_t selectedVal = 0;
105 std::vector<std::string> indexerArray;
106 JSRef<JSObject> paramObj = JSRef<JSObject>::Cast(args[0]);
107 JSRef<JSVal> arrayVal = paramObj->GetProperty("arrayValue");
108 if (arrayVal->IsArray()) {
109 JSRef<JSArray> jsArray = JSRef<JSArray>::Cast(arrayVal);
110 length = jsArray->Length();
111 for (size_t i = 0; i < length; i++) {
112 auto value = jsArray->GetValueAt(i);
113 if (value->IsString()) {
114 indexerArray.emplace_back(value->ToString());
115 }
116 }
117 }
118
119 JSRef<JSVal> selectedProperty = paramObj->GetProperty("selected");
120 if (selectedProperty->IsNumber()) {
121 selectedVal = selectedProperty->ToNumber<int32_t>();
122 IndexerModel::GetInstance()->Create(indexerArray, selectedVal, isArc);
123 JSIndexerTheme::ApplyTheme();
124 JSRef<JSVal> changeEventVal = paramObj->GetProperty("$selected");
125 ParseIndexerSelectedObject(args, changeEventVal);
126 } else if (!isArc && length > 0 && selectedProperty->IsObject()) {
127 JSRef<JSObject> selectedObj = JSRef<JSObject>::Cast(selectedProperty);
128 auto selectedValueProperty = selectedObj->GetProperty("value");
129 if (selectedValueProperty->IsNumber()) {
130 selectedVal = selectedValueProperty->ToNumber<int32_t>();
131 }
132 IndexerModel::GetInstance()->Create(indexerArray, selectedVal, isArc);
133 JSIndexerTheme::ApplyTheme();
134 JSRef<JSVal> changeEventVal = selectedObj->GetProperty("changeEvent");
135 if (!changeEventVal.IsEmpty()) {
136 if (!changeEventVal->IsUndefined() && changeEventVal->IsFunction()) {
137 ParseIndexerSelectedObject(args, changeEventVal);
138 }
139 return;
140 }
141
142 args.ReturnSelf();
143 } else {
144 IndexerModel::GetInstance()->Create(indexerArray, selectedVal, isArc);
145 JSIndexerTheme::ApplyTheme();
146 }
147 }
148
SetSelectedColor(const JSCallbackInfo & args)149 void JSIndexer::SetSelectedColor(const JSCallbackInfo& args)
150 {
151 if (args.Length() < 1) {
152 return;
153 }
154 IndexerModel::GetInstance()->SetSelectedColor(PaseColor(args));
155 }
156
SetColor(const JSCallbackInfo & args)157 void JSIndexer::SetColor(const JSCallbackInfo& args)
158 {
159 if (args.Length() < 1) {
160 return;
161 }
162 IndexerModel::GetInstance()->SetColor(PaseColor(args));
163 }
164
SetPopupColor(const JSCallbackInfo & args)165 void JSIndexer::SetPopupColor(const JSCallbackInfo& args)
166 {
167 if (args.Length() < 1) {
168 return;
169 }
170 IndexerModel::GetInstance()->SetPopupColor(PaseColor(args));
171 }
172
SetSelectedBackgroundColor(const JSCallbackInfo & args)173 void JSIndexer::SetSelectedBackgroundColor(const JSCallbackInfo& args)
174 {
175 if (args.Length() < 1) {
176 return;
177 }
178 IndexerModel::GetInstance()->SetSelectedBackgroundColor(PaseColor(args));
179 }
180
SetPopupBackground(const JSCallbackInfo & args)181 void JSIndexer::SetPopupBackground(const JSCallbackInfo& args)
182 {
183 if (args.Length() < 1) {
184 return;
185 }
186 IndexerModel::GetInstance()->SetPopupBackground(PaseColor(args));
187 }
188
SetUsingPopup(bool state)189 void JSIndexer::SetUsingPopup(bool state)
190 {
191 IndexerModel::GetInstance()->SetUsingPopup(state);
192 }
193
SetSelectedFont(const JSCallbackInfo & args)194 void JSIndexer::SetSelectedFont(const JSCallbackInfo& args)
195 {
196 if (args.Length() < 1) {
197 return;
198 }
199 std::optional<Dimension> fontSize;
200 std::optional<FontWeight> fontWeight;
201 std::optional<std::vector<std::string>> fontFamily;
202 std::optional<FontStyle> fontStyle;
203 if (args[0]->IsObject()) {
204 GetFontContent(args, fontSize, fontWeight, fontFamily, fontStyle);
205 }
206 IndexerModel::GetInstance()->SetSelectedFont(fontSize, fontWeight, fontFamily, fontStyle);
207 }
208
SetPopupFont(const JSCallbackInfo & args)209 void JSIndexer::SetPopupFont(const JSCallbackInfo& args)
210 {
211 if (args.Length() < 1) {
212 return;
213 }
214 std::optional<Dimension> fontSize;
215 std::optional<FontWeight> fontWeight;
216 std::optional<std::vector<std::string>> fontFamily;
217 std::optional<FontStyle> fontStyle;
218 if (args[0]->IsObject()) {
219 GetFontContent(args, fontSize, fontWeight, fontFamily, fontStyle);
220 }
221 IndexerModel::GetInstance()->SetPopupFont(fontSize, fontWeight, fontFamily, fontStyle);
222 }
223
SetFont(const JSCallbackInfo & args)224 void JSIndexer::SetFont(const JSCallbackInfo& args)
225 {
226 if (args.Length() < 1) {
227 return;
228 }
229 std::optional<Dimension> fontSize;
230 std::optional<FontWeight> fontWeight;
231 std::optional<std::vector<std::string>> fontFamily;
232 std::optional<FontStyle> fontStyle;
233 if (args[0]->IsObject()) {
234 GetFontContent(args, fontSize, fontWeight, fontFamily, fontStyle);
235 }
236 IndexerModel::GetInstance()->SetFont(fontSize, fontWeight, fontFamily, fontStyle);
237 }
238
JsOnSelected(const JSCallbackInfo & args)239 void JSIndexer::JsOnSelected(const JSCallbackInfo& args)
240 {
241 if (args[0]->IsFunction()) {
242 auto onSelected = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
243 const int32_t selected) {
244 JAVASCRIPT_EXECUTION_SCOPE(execCtx);
245 auto params = ConvertToJSValues(selected);
246 func->Call(JSRef<JSObject>(), params.size(), params.data());
247 };
248 IndexerModel::GetInstance()->SetOnSelected(onSelected);
249 }
250 }
251
JsOnRequestPopupData(const JSCallbackInfo & args)252 void JSIndexer::JsOnRequestPopupData(const JSCallbackInfo& args)
253 {
254 if (args[0]->IsFunction()) {
255 auto requestPopupData = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
256 const int32_t selected) {
257 std::vector<std::string> popupData;
258 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, popupData);
259 auto params = ConvertToJSValues(selected);
260 JSRef<JSArray> result = func->Call(JSRef<JSObject>(), params.size(), params.data());
261 if (result.IsEmpty()) {
262 return popupData;
263 }
264
265 if (!result->IsArray()) {
266 return popupData;
267 }
268
269 for (size_t i = 0; i < result->Length(); i++) {
270 if (result->GetValueAt(i)->IsString()) {
271 auto item = result->GetValueAt(i);
272 popupData.emplace_back(item->ToString());
273 }
274 }
275 return popupData;
276 };
277 IndexerModel::GetInstance()->SetOnRequestPopupData(requestPopupData);
278 }
279 }
280
JsOnPopupSelected(const JSCallbackInfo & args)281 void JSIndexer::JsOnPopupSelected(const JSCallbackInfo& args)
282 {
283 if (args[0]->IsFunction()) {
284 auto onPopupSelected = [execCtx = args.GetExecutionContext(), func = JSRef<JSFunc>::Cast(args[0])](
285 const int32_t selected) {
286 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
287 auto params = ConvertToJSValues(selected);
288 func->Call(JSRef<JSObject>(), params.size(), params.data());
289 };
290 IndexerModel::GetInstance()->SetOnPopupSelected(onPopupSelected);
291 }
292 }
293
GetFontContent(const JSCallbackInfo & args,std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)294 void JSIndexer::GetFontContent(const JSCallbackInfo& args, std::optional<Dimension>& fontSize,
295 std::optional<FontWeight>& fontWeight, std::optional<std::vector<std::string>>& fontFamily,
296 std::optional<FontStyle>& fontStyle)
297 {
298 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
299 JSRef<JSVal> size = obj->GetProperty("size");
300 CalcDimension fontSizeData;
301 if (ParseJsDimensionFp(size, fontSizeData) && !fontSizeData.IsNegative() &&
302 fontSizeData.Unit() != DimensionUnit::PERCENT) {
303 fontSize = fontSizeData;
304 }
305
306 JSRef<JSVal> weight = obj->GetProperty("weight");
307 if (weight->IsString() || weight->IsNumber()) {
308 fontWeight = ConvertStrToFontWeight(weight->ToString());
309 }
310
311 JSRef<JSVal> family = obj->GetProperty("family");
312 std::vector<std::string> fontFamilies;
313 if (ParseJsFontFamilies(family, fontFamilies)) {
314 fontFamily = fontFamilies;
315 }
316
317 JSRef<JSVal> style = obj->GetProperty("style");
318 if (style->IsNumber()) {
319 int32_t value = style->ToNumber<int32_t>();
320 if (value >= 0 && value < static_cast<int32_t>(FONT_STYLES.size())) {
321 fontStyle = FONT_STYLES[value];
322 }
323 }
324 }
325
SetItemSize(const JSCallbackInfo & args)326 void JSIndexer::SetItemSize(const JSCallbackInfo& args)
327 {
328 if (args.Length() < 1) {
329 return;
330 }
331 CalcDimension itemSize;
332 if (ParseJsDimensionVp(args[0], itemSize) && GreatNotEqual(itemSize.Value(), 0.0) &&
333 itemSize.Unit() != DimensionUnit::PERCENT) {
334 IndexerModel::GetInstance()->SetItemSize(itemSize);
335 return;
336 }
337 IndexerModel::GetInstance()->SetItemSize(DEFAULT_ITEM_SIZE);
338 }
339
SetAlignStyle(const JSCallbackInfo & args)340 void JSIndexer::SetAlignStyle(const JSCallbackInfo& args)
341 {
342 if (args.Length() < 1) {
343 return;
344 }
345 int32_t value = Container::IsCurrentUseNewPipeline() ? static_cast<int32_t>(NG::AlignStyle::END)
346 : static_cast<int32_t>(V2::AlignStyle::END);
347 auto alignValue = -1;
348 if (args[0]->IsNumber()) {
349 alignValue = args[0]->ToNumber<int32_t>();
350 }
351 if (alignValue >= 0 && alignValue < static_cast<int32_t>(ALIGN_STYLE.size())) {
352 value = alignValue;
353 }
354 IndexerModel::GetInstance()->SetAlignStyle(value);
355 CalcDimension popupHorizontalSpace(-1.0);
356 if (args.Length() > 1) {
357 ParseJsDimensionVp(args[1], popupHorizontalSpace);
358 }
359 IndexerModel::GetInstance()->SetPopupHorizontalSpace(popupHorizontalSpace);
360 }
361
SetSelected(const JSCallbackInfo & args)362 void JSIndexer::SetSelected(const JSCallbackInfo& args)
363 {
364 if (args.Length() < 1) {
365 return;
366 }
367 int32_t selected = 0;
368 auto selectedVal = args[0];
369 JSRef<JSVal> changeEventVal;
370 if (selectedVal->IsObject()) {
371 JSRef<JSObject> obj = JSRef<JSObject>::Cast(selectedVal);
372 selectedVal = obj->GetProperty("value");
373 changeEventVal = obj->GetProperty("$value");
374 } else if (args.Length() > 1) {
375 changeEventVal = args[1];
376 }
377 if (ParseJsInteger<int32_t>(selectedVal, selected)) {
378 IndexerModel::GetInstance()->SetSelected(selected);
379 }
380 ParseIndexerSelectedObject(args, changeEventVal, true);
381 }
382
SetPopupPosition(const JSCallbackInfo & args)383 void JSIndexer::SetPopupPosition(const JSCallbackInfo& args)
384 {
385 std::optional<Dimension> xOpt;
386 std::optional<Dimension> yOpt;
387 if (args[0]->IsObject()) {
388 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
389 CalcDimension x;
390 CalcDimension y;
391 JSRef<JSVal> xVal = obj->GetProperty("x");
392 JSRef<JSVal> yVal = obj->GetProperty("y");
393 if ((xVal->IsString() && StringUtils::StringToCalcDimensionNG(xVal->ToString(), x, false)) ||
394 (!xVal->IsString() && JSViewAbstract::ParseJsDimensionVp(xVal, x))) {
395 xOpt = x;
396 }
397 if ((yVal->IsString() && StringUtils::StringToCalcDimensionNG(yVal->ToString(), y, false)) ||
398 (!yVal->IsString() && JSViewAbstract::ParseJsDimensionVp(yVal, y))) {
399 yOpt = y;
400 }
401 } else if (Container::LessThanAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
402 return;
403 }
404 IndexerModel::GetInstance()->SetPopupPositionX(xOpt);
405 IndexerModel::GetInstance()->SetPopupPositionY(yOpt);
406 }
407
SetPopupSelectedColor(const JSCallbackInfo & args)408 void JSIndexer::SetPopupSelectedColor(const JSCallbackInfo& args)
409 {
410 if (args.Length() < 1) {
411 return;
412 }
413 IndexerModel::GetInstance()->SetPopupSelectedColor(PaseColor(args));
414 }
415
SetPopupUnselectedColor(const JSCallbackInfo & args)416 void JSIndexer::SetPopupUnselectedColor(const JSCallbackInfo& args)
417 {
418 if (args.Length() < 1) {
419 return;
420 }
421 IndexerModel::GetInstance()->SetPopupUnselectedColor(PaseColor(args));
422 }
423
SetPopupItemFont(const JSCallbackInfo & args)424 void JSIndexer::SetPopupItemFont(const JSCallbackInfo& args)
425 {
426 CalcDimension fontSize;
427 std::string weight;
428 if (args[0]->IsObject()) {
429 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
430 JSRef<JSVal> size = obj->GetProperty("size");
431 if (!size->IsNull()) {
432 CalcDimension fontSizeData;
433 if (ParseJsDimensionFp(size, fontSizeData) && !fontSizeData.IsNegative() &&
434 fontSizeData.Unit() != DimensionUnit::PERCENT) {
435 fontSize = fontSizeData;
436 }
437 }
438
439 auto jsWeight = obj->GetProperty("weight");
440 if (!jsWeight->IsNull()) {
441 if (jsWeight->IsNumber()) {
442 weight = std::to_string(jsWeight->ToNumber<int32_t>());
443 } else {
444 ParseJsString(jsWeight, weight);
445 }
446 }
447 }
448 IndexerModel::GetInstance()->SetFontSize(fontSize);
449 IndexerModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight, FontWeight::MEDIUM));
450 }
451
SetPopupItemBackgroundColor(const JSCallbackInfo & args)452 void JSIndexer::SetPopupItemBackgroundColor(const JSCallbackInfo& args)
453 {
454 if (args.Length() < 1) {
455 return;
456 }
457 IndexerModel::GetInstance()->SetPopupItemBackground(PaseColor(args));
458 }
459
PaseColor(const JSCallbackInfo & args)460 std::optional<Color> JSIndexer::PaseColor(const JSCallbackInfo& args)
461 {
462 std::optional<Color> colorOpt;
463 Color color;
464 if (ParseJsColor(args[0], color)) {
465 colorOpt = color;
466 }
467 return colorOpt;
468 }
469
SetAutoCollapse(const JSCallbackInfo & args)470 void JSIndexer::SetAutoCollapse(const JSCallbackInfo& args)
471 {
472 bool state = true;
473 if (args[0]->IsBoolean()) {
474 state = args[0]->ToBoolean();
475 }
476 IndexerModel::GetInstance()->SetAutoCollapse(state);
477 }
478
SetPopupItemBorderRadius(const JSCallbackInfo & args)479 void JSIndexer::SetPopupItemBorderRadius(const JSCallbackInfo& args)
480 {
481 auto radius = Dimension(ZERO_RADIUS, DimensionUnit::VP);
482 auto popupRadius = Dimension(ZERO_RADIUS, DimensionUnit::VP);
483 if (args.Length() > 0 && args[0]->IsNumber()) {
484 auto radiusValue = args[0]->ToNumber<double>();
485 if (radiusValue >= 0) {
486 radius.SetValue(radiusValue);
487 radius.SetUnit(DimensionUnit::VP);
488 popupRadius.SetValue(radiusValue + RADIUS_OFFSET);
489 popupRadius.SetUnit(DimensionUnit::VP);
490 }
491 } else {
492 radius.SetValue(POPUP_ITEM_DEFAULT_RADIUS);
493 radius.SetUnit(DimensionUnit::VP);
494 popupRadius.SetValue(radius.Value() + RADIUS_OFFSET);
495 popupRadius.SetUnit(DimensionUnit::VP);
496 }
497 IndexerModel::GetInstance()->SetPopupItemBorderRadius(radius);
498 IndexerModel::GetInstance()->SetPopupBorderRadius(popupRadius);
499 }
500
SetItemBorderRadius(const JSCallbackInfo & args)501 void JSIndexer::SetItemBorderRadius(const JSCallbackInfo& args)
502 {
503 auto radius = Dimension(ZERO_RADIUS, DimensionUnit::VP);
504 auto indexerRadius = Dimension(ZERO_RADIUS, DimensionUnit::VP);
505 if (args.Length() > 0 && args[0]->IsNumber()) {
506 auto radiusValue = args[0]->ToNumber<double>();
507 if (radiusValue >= 0) {
508 radius.SetValue(radiusValue);
509 radius.SetUnit(DimensionUnit::VP);
510 indexerRadius.SetValue(radiusValue + RADIUS_OFFSET);
511 indexerRadius.SetUnit(DimensionUnit::VP);
512 }
513 } else {
514 radius.SetValue(ITEM_DEFAULT_RADIUS);
515 radius.SetUnit(DimensionUnit::VP);
516 indexerRadius.SetValue(radius.Value() + RADIUS_OFFSET);
517 indexerRadius.SetUnit(DimensionUnit::VP);
518 }
519 IndexerModel::GetInstance()->SetItemBorderRadius(radius);
520 IndexerModel::GetInstance()->SetIndexerBorderRadius(indexerRadius);
521 }
522
SetPopupBackgroundBlurStyle(const JSCallbackInfo & args)523 void JSIndexer::SetPopupBackgroundBlurStyle(const JSCallbackInfo& args)
524 {
525 if (args.Length() < 1) {
526 return;
527 }
528
529 BlurStyleOption styleOption;
530 if (args[0]->IsNumber()) {
531 auto blurStyle = args[0]->ToNumber<int32_t>();
532 if (blurStyle >= static_cast<int>(BlurStyle::NO_MATERIAL) &&
533 blurStyle <= static_cast<int>(BlurStyle::COMPONENT_ULTRA_THICK)) {
534 styleOption.blurStyle = static_cast<BlurStyle>(blurStyle);
535 } else {
536 styleOption.blurStyle = BlurStyle::COMPONENT_REGULAR;
537 }
538 } else {
539 styleOption.blurStyle = BlurStyle::COMPONENT_REGULAR;
540 }
541 IndexerModel::GetInstance()->SetPopupBackgroundBlurStyle(styleOption);
542 }
543
SetPopupTitleBackground(const JSCallbackInfo & args)544 void JSIndexer::SetPopupTitleBackground(const JSCallbackInfo& args)
545 {
546 if (args.Length() < 1) {
547 return;
548 }
549 IndexerModel::GetInstance()->SetPopupTitleBackground(PaseColor(args));
550 }
551
SetWidth(const JSCallbackInfo & args)552 void JSIndexer::SetWidth(const JSCallbackInfo& args)
553 {
554 JSViewAbstract::JsWidth(args);
555 if (args[0]->IsString() && args[0]->ToString() == "auto") {
556 IndexerModel::GetInstance()->SetAdaptiveWidth(true);
557 } else {
558 IndexerModel::GetInstance()->SetAdaptiveWidth(false);
559 }
560 }
561
SetEnableHapticFeedback(const JSCallbackInfo & args)562 void JSIndexer::SetEnableHapticFeedback(const JSCallbackInfo& args)
563 {
564 bool state = true;
565 if (args.Length() > 0 && args[0]->IsBoolean()) {
566 state = args[0]->ToBoolean();
567 }
568 IndexerModel::GetInstance()->SetEnableHapticFeedback(state);
569 }
570
JSBind(BindingTarget globalObj)571 void JSIndexer::JSBind(BindingTarget globalObj)
572 {
573 MethodOptions opt = MethodOptions::NONE;
574 JSClass<JSIndexer>::Declare("AlphabetIndexer");
575 JSClass<JSIndexer>::StaticMethod("create", &JSIndexer::Create);
576 JSClass<JSIndexer>::StaticMethod("createArc", &JSIndexer::CreateArc);
577 // API7 onSelected deprecated
578 JSClass<JSIndexer>::StaticMethod("onSelected", &JSIndexer::JsOnSelected);
579 JSClass<JSIndexer>::StaticMethod("onSelect", &JSIndexer::JsOnSelected);
580 JSClass<JSIndexer>::StaticMethod("color", &JSIndexer::SetColor, opt);
581 JSClass<JSIndexer>::StaticMethod("selectedColor", &JSIndexer::SetSelectedColor, opt);
582 JSClass<JSIndexer>::StaticMethod("popupColor", &JSIndexer::SetPopupColor, opt);
583 JSClass<JSIndexer>::StaticMethod("selectedBackgroundColor", &JSIndexer::SetSelectedBackgroundColor, opt);
584 JSClass<JSIndexer>::StaticMethod("popupBackground", &JSIndexer::SetPopupBackground, opt);
585 JSClass<JSIndexer>::StaticMethod("usingPopup", &JSIndexer::SetUsingPopup, opt);
586 JSClass<JSIndexer>::StaticMethod("selectedFont", &JSIndexer::SetSelectedFont);
587 JSClass<JSIndexer>::StaticMethod("font", &JSIndexer::SetFont);
588 JSClass<JSIndexer>::StaticMethod("popupFont", &JSIndexer::SetPopupFont);
589 JSClass<JSIndexer>::StaticMethod("itemSize", &JSIndexer::SetItemSize, opt);
590 JSClass<JSIndexer>::StaticMethod("alignStyle", &JSIndexer::SetAlignStyle, opt);
591 JSClass<JSIndexer>::StaticMethod("onRequestPopupData", &JSIndexer::JsOnRequestPopupData, opt);
592 JSClass<JSIndexer>::StaticMethod("selected", &JSIndexer::SetSelected, opt);
593 JSClass<JSIndexer>::StaticMethod("popupPosition", &JSIndexer::SetPopupPosition, opt);
594 JSClass<JSIndexer>::StaticMethod("popupSelectedColor", &JSIndexer::SetPopupSelectedColor, opt);
595 JSClass<JSIndexer>::StaticMethod("popupUnselectedColor", &JSIndexer::SetPopupUnselectedColor, opt);
596 JSClass<JSIndexer>::StaticMethod("popupItemFont", &JSIndexer::SetPopupItemFont);
597 JSClass<JSIndexer>::StaticMethod("popupItemBackgroundColor", &JSIndexer::SetPopupItemBackgroundColor, opt);
598 JSClass<JSIndexer>::StaticMethod("autoCollapse", &JSIndexer::SetAutoCollapse, opt);
599 JSClass<JSIndexer>::StaticMethod("popupItemBorderRadius", &JSIndexer::SetPopupItemBorderRadius);
600 JSClass<JSIndexer>::StaticMethod("itemBorderRadius", &JSIndexer::SetItemBorderRadius);
601 JSClass<JSIndexer>::StaticMethod("popupBackgroundBlurStyle", &JSIndexer::SetPopupBackgroundBlurStyle);
602 JSClass<JSIndexer>::StaticMethod("popupTitleBackground", &JSIndexer::SetPopupTitleBackground, opt);
603 JSClass<JSIndexer>::StaticMethod("width", &JSIndexer::SetWidth);
604 JSClass<JSIndexer>::StaticMethod("enableHapticFeedback", &JSIndexer::SetEnableHapticFeedback, opt);
605 // keep compatible, need remove after
606 JSClass<JSIndexer>::StaticMethod("onPopupSelected", &JSIndexer::JsOnPopupSelected, opt);
607 JSClass<JSIndexer>::StaticMethod("onPopupSelect", &JSIndexer::JsOnPopupSelected, opt);
608 JSClass<JSIndexer>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
609 JSClass<JSIndexer>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
610 JSClass<JSIndexer>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
611 JSClass<JSIndexer>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
612 JSClass<JSIndexer>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
613 JSClass<JSIndexer>::InheritAndBind<JSViewAbstract>(globalObj);
614 }
615 } // namespace OHOS::Ace::Framework
616