1 /*
2  * Copyright (c) 2021-2024 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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_TEXTPICKER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_TEXTPICKER_H
18 
19 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
20 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
21 #include "core/components/picker/picker_text_component.h"
22 #include "core/components_ng/pattern/picker/picker_type_define.h"
23 #include "core/components_ng/pattern/text_picker/textpicker_event_hub.h"
24 
25 namespace OHOS::Ace::Framework {
26 
27 enum class PickerStyle {
28     INLINE = 0,
29     BLOCK,
30     FADE,
31 };
32 
33 struct ParseTextArrayParam {
34     std::vector<NG::RangeContent> result;
35     uint32_t kind = 0;
36     uint32_t selected = 0;
37     std::string value;
38     std::vector<NG::TextCascadePickerOptions> options;
39     std::vector<uint32_t> selecteds;
40     std::vector<std::string> values;
41     JSRef<JSVal> valueChangeEventVal;
42     JSRef<JSVal> selectedChangeEventVal;
43 };
44 
45 class JSTextPickerParser : public JSViewAbstract {
46 public:
47     static bool ParseIconTextArray(const JSRef<JSObject>& paramObject, std::vector<NG::RangeContent>& result,
48         uint32_t& kind, uint32_t& selected);
49     static bool ParseTextArray(const JSRef<JSObject>& paramObject, ParseTextArrayParam& param);
50     static void ParseTextStyle(const JSRef<JSObject>& paramObj, NG::PickerTextStyle& textStyle, const std::string& pos);
51     static bool ParseMultiTextArray(const JSRef<JSObject>& paramObj, ParseTextArrayParam& param);
52     static bool ParseCascadeTextArray(const JSRef<JSObject>& paramObj, std::vector<uint32_t>& selecteds,
53         std::vector<std::string>& values, NG::TextCascadePickerOptionsAttr& attr);
54     static bool GenerateCascadeOptions(const JSRef<JSArray>& array,
55         std::vector<NG::TextCascadePickerOptions>& options);
56     static bool GenerateCascadeOptionsInternal(const JSRef<JSObject>& jsObj,
57         std::vector<NG::TextCascadePickerOptions>& options);
58     static bool ParseInternalArray(const JSRef<JSArray>& jsRangeValue, std::vector<uint32_t>& selecteds,
59         std::vector<std::string>& values, uint32_t index, bool isHasSelectAttr);
60     static void SetSelectedValues(std::vector<uint32_t>& selectedValues, std::vector<std::string>& values,
61         uint32_t index, bool isHasSelectAttr, std::vector<std::string>& resultStr);
62     static bool ParseMultiTextArrayValue(const JsiRef<JsiValue>& jsValue, ParseTextArrayParam& param);
63     static bool ParseMultiTextArraySelect(const JsiRef<JsiValue>& jsSelectedValue, ParseTextArrayParam& param);
64     static void ParseMultiTextArraySelectInternal(const std::vector<NG::TextCascadePickerOptions>& options,
65         const std::vector<std::string>& values, std::vector<uint32_t>& selecteds);
66     static bool ParseMultiTextArrayRange(const JSRef<JSArray>& jsRangeValue,
67         std::vector<NG::TextCascadePickerOptions>& options);
68     static bool ParseMultiTextArrayRangeInternal(const JSRef<JSVal>& value,
69         std::vector<NG::TextCascadePickerOptions>& options);
70     static void ParseMultiTextArraySelectArrayInternal(const std::vector<NG::TextCascadePickerOptions>& options,
71         std::vector<uint32_t>& selecteds);
72     static void ParseMultiTextArrayValueInternal(const std::vector<NG::TextCascadePickerOptions>& options,
73         std::vector<std::string>& values);
74     static void ParseMultiTextArrayValueSingleInternal(
75         const std::vector<NG::TextCascadePickerOptions>& options, const std::string& value,
76         std::vector<std::string>& values);
77     static void IsUserDefinedFontFamily(const std::string& pos);
78 };
79 
80 class JSTextPicker : public JSViewAbstract {
81 public:
82     static void Create(const JSCallbackInfo& info);
83     static void JSBind(BindingTarget globalObj);
84 
85     static void SetDefaultPickerItemHeight(const JSCallbackInfo& info);
86     static void SetCanLoop(const JSCallbackInfo& info);
87 
88     static void OnAccept(const JSCallbackInfo& info);
89     static void OnCancel(const JSCallbackInfo& info);
90     static void OnChange(const JSCallbackInfo& info);
91     static void OnScrollStop(const JSCallbackInfo& info);
92     static void SetDisappearTextStyle(const JSCallbackInfo& info);
93     static void SetTextStyle(const JSCallbackInfo& info);
94     static void SetSelectedTextStyle(const JSCallbackInfo& info);
95     static void SetSelectedIndex(const JSCallbackInfo& info);
96     static void PickerBackgroundColor(const JSCallbackInfo& info);
97     static void SetDivider(const JSCallbackInfo& info);
98     static void SetGradientHeight(const JSCallbackInfo& info);
99     static void JsOpacity(const JSCallbackInfo& info);
100 private:
101     static size_t ProcessCascadeOptionDepth(const NG::TextCascadePickerOptions& option);
102     static void ProcessCascadeSelected(const std::vector<NG::TextCascadePickerOptions>& options,
103         uint32_t index, std::vector<uint32_t>& selecteds);
104     static bool ProcessCascadeOptions(const JSRef<JSObject>& paramObjec,
105         std::vector<NG::TextCascadePickerOptions>& options, std::vector<uint32_t>& selecteds,
106         std::vector<std::string>& values, NG::TextCascadePickerOptionsAttr& attr);
107     static void SetSelectedIndexMulti(const JsiRef<JsiValue>& jsSelectedValue);
108     static void SetSelectedIndexMultiInternal(uint32_t count, std::vector<NG::TextCascadePickerOptions>& options,
109         std::vector<uint32_t>& selecteds);
110     static void SetSelectedInternal(uint32_t count, std::vector<NG::TextCascadePickerOptions>& options,
111         std::vector<uint32_t>& selecteds);
112     static void SetSelectedIndexSingle(const JsiRef<JsiValue>& jsSelectedValue);
113     static bool ProcessSingleRangeValue(const JSRef<JSObject>& paramObjec, ParseTextArrayParam& param);
114     static void SetSelectedIndexSingleInternal(const std::vector<NG::TextCascadePickerOptions>& options,
115         uint32_t count, uint32_t& selectedValue, std::vector<uint32_t>& selectedValues);
116     static void CreateMulti(const RefPtr<PickerTheme>& theme, const std::vector<std::string>& values,
117         const std::vector<uint32_t>& selectedValues, const NG::TextCascadePickerOptionsAttr& attr,
118         const std::vector<NG::TextCascadePickerOptions>& options);
119     static bool CheckDividerValue(const Dimension &dimension);
120 };
121 
122 class JSTextPickerDialog {
123 public:
124     static void JSBind(BindingTarget globalObj);
125     static void Show(const JSCallbackInfo& info);
126     static void TextPickerDialogShow(const JSRef<JSObject>& paramObj,
127         const std::map<std::string, NG::DialogTextEvent>& dialogEvent,
128         const std::map<std::string, NG::DialogGestureEvent>& dialogCancelEvent);
129     static std::map<std::string, NG::DialogTextEvent> DialogEvent(const JSCallbackInfo& info);
130     static std::map<std::string, NG::DialogGestureEvent> DialogCancelEvent(const JSCallbackInfo& info);
131 
132 private:
133     static void ParseText(RefPtr<PickerTextComponent>& component, const JSRef<JSObject>& paramObj);
134     static void AddEvent(RefPtr<PickerTextComponent>& picker, const JSCallbackInfo& info);
135     static bool ParseShowData(const JSRef<JSObject>& paramObj, NG::TextPickerSettingData& settingData);
136     static void ParseTextProperties(const JSRef<JSObject>& paramObj, NG::PickerTextProperties& result);
137     static void ParseShowDataMultiContent(const std::vector<NG::TextCascadePickerOptions>& options,
138         const std::vector<uint32_t>& selecteds, const std::vector<std::string>& values,
139         NG::TextCascadePickerOptionsAttr& attr, NG::TextPickerSettingData& settingData);
140     static bool ParseShowDataAttribute(const JSRef<JSObject>& paramObject,
141         NG::TextPickerSettingData& settingData);
142     static bool ParseCanLoop(const JSRef<JSObject>& paramObject,
143         bool& canLoop);
144     static bool ParseShowDataOptions(
145         const JSRef<JSObject>& paramObject, ParseTextArrayParam& param, NG::TextCascadePickerOptionsAttr& attr);
146 };
147 } // namespace OHOS::Ace::Framework
148 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_TEXTPICKER_H
149