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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SWIPER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SWIPER_H
18 
19 #include "core/components_ng/pattern/swiper/swiper_model.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
21 
22 namespace OHOS::Ace::Framework {
23 
24 class JSSwiper : public JSContainerBase {
25 public:
26     static void JSBind(BindingTarget globalObj);
27     static void Create(const JSCallbackInfo& info);
28 
29 protected:
30     static void SetIndicatorInteractive(const JSCallbackInfo& info);
31     static void SetAutoPlay(bool autoPlay);
32     static void SetDuration(const JSCallbackInfo& info);
33     static void SetIndex(const JSCallbackInfo& info);
34     static void SetInterval(const JSCallbackInfo& info);
35     static void SetLoop(const JSCallbackInfo& info);
36     static void SetVertical(bool isVertical);
37     static void SetIndicator(const JSCallbackInfo& info);
38     static void SetWidth(const JSCallbackInfo& info);
39     static void SetHeight(const JSCallbackInfo& info);
40     static void SetWidth(const JSRef<JSVal>& jsValue);
41     static void SetHeight(const JSRef<JSVal>& jsValue);
42     static void SetSize(const JSCallbackInfo& info);
43     static void SetIndicatorStyle(const JSCallbackInfo& info);
44     static void SetItemSpace(const JSCallbackInfo& info);
45     static void SetPreviousMargin(const JSCallbackInfo& info);
46     static void SetNextMargin(const JSCallbackInfo& info);
47     static void SetDisplayMode(int32_t index);
48     static void SetEffectMode(const JSCallbackInfo& info);
49     static void SetDisplayCount(const JSCallbackInfo& info);
50     static void SetCachedCount(const JSCallbackInfo& info);
51     static void SetEnabled(const JSCallbackInfo& info);
52     static void SetDisableSwipe(bool disableSwipe);
53     static void SetCurve(const JSCallbackInfo& info);
54     static void SetOnChange(const JSCallbackInfo& info);
55     static void SetOnAnimationStart(const JSCallbackInfo& info);
56     static void SetOnAnimationEnd(const JSCallbackInfo& info);
57     static void SetOnGestureSwipe(const JSCallbackInfo& info);
58     static void SetOnClick(const JSCallbackInfo& info);
59     static void JsRemoteMessage(const JSCallbackInfo& info);
60     static void GetFontContent(const JSRef<JSVal>& font, bool isSelected, SwiperDigitalParameters& digitalParameters);
61     static void SetDisplayArrow(const JSCallbackInfo& info);
62     static SwiperParameters GetDotIndicatorInfo(const JSRef<JSObject>& obj);
63     static void SetDotIndicatorInfo(const JSRef<JSObject>& obj, SwiperParameters& swiperParameters,
64         const RefPtr<SwiperIndicatorTheme>& swiperIndicatorTheme);
65     static SwiperDigitalParameters GetDigitIndicatorInfo(const JSRef<JSObject>& obj);
66     static std::optional<Dimension> ParseIndicatorDimension(const JSRef<JSVal>& value);
67     static void SetIsIndicatorCustomSize(const Dimension& dimPosition, bool parseOk);
68     static bool GetArrowInfo(const JSRef<JSObject>& obj, SwiperArrowParameters& swiperArrowParameters);
69     static void SetNestedScroll(const JSCallbackInfo& info);
70     static void SetCustomContentTransition(const JSCallbackInfo& info);
71     static void SetOnContentDidScroll(const JSCallbackInfo& info);
72     static bool ParseLengthMetricsToDimension(const JSRef<JSVal>& jsValue, CalcDimension& result);
73     static void SetIndicatorController(const JSCallbackInfo& info);
74 };
75 
76 class JSSwiperController final : public Referenced {
77 public:
78     JSSwiperController() = default;
79     ~JSSwiperController() override = default;
80 
81     static void JSBind(BindingTarget globalObj);
82     static void Constructor(const JSCallbackInfo& args);
83     static void Destructor(JSSwiperController* scroller);
84 
SwipeTo(const JSCallbackInfo& args)85     void SwipeTo(const JSCallbackInfo& args)
86     {
87         ContainerScope scope(instanceId_);
88         if (args.Length() < 1 || !args[0]->IsNumber()) {
89             LOGE("Param is not valid");
90             return;
91         }
92         if (controller_) {
93             controller_->SwipeTo(args[0]->ToNumber<int32_t>());
94         }
95     }
96 
ShowNext(const JSCallbackInfo& args)97     void ShowNext(const JSCallbackInfo& args)
98     {
99         ContainerScope scope(instanceId_);
100         if (controller_) {
101             controller_->ShowNext();
102         }
103     }
104 
ShowPrevious(const JSCallbackInfo& args)105     void ShowPrevious(const JSCallbackInfo& args)
106     {
107         ContainerScope scope(instanceId_);
108         if (controller_) {
109             controller_->ShowPrevious();
110         }
111     }
112 
113     void ChangeIndex(const JSCallbackInfo& args);
114 
115     void FinishAnimation(const JSCallbackInfo& args);
116     void PreloadItems(const JSCallbackInfo& args);
117 
SetController(const RefPtr<SwiperController>& controller)118     void SetController(const RefPtr<SwiperController>& controller)
119     {
120         controller_ = controller;
121     }
122 
SetInstanceId(int32_t id)123     void SetInstanceId(int32_t id)
124     {
125         instanceId_ = id;
126     }
127 
128 private:
129     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
130     RefPtr<SwiperController> controller_;
131 
132     ACE_DISALLOW_COPY_AND_MOVE(JSSwiperController);
133 };
134 
135 } // namespace OHOS::Ace::Framework
136 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SWIPER_H
137