• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_INDICATOR_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_INDICATOR_H
18 
19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
20 #include "frameworks/core/components_ng/base/ui_node.h"
21 #include "frameworks/core/components_ng/pattern/swiper_indicator/indicator_common/indicator_controller.h"
22 
23 namespace OHOS::Ace::Framework {
24 
25 class JSIndicator : public JSViewAbstract {
26 public:
27     static void JSBind(BindingTarget globalObj);
28     static void Create(const JSCallbackInfo& info);
29 
30 protected:
31     static void SetLoop(const JSCallbackInfo& info);
32     static void SetOnChange(const JSCallbackInfo& info);
33     static void SetInitialIndex(const JSCallbackInfo& info);
34     static void SetStyle(const JSCallbackInfo& info);
35     static void SetCount(const JSCallbackInfo& info);
36     static void SetVertical(const JSCallbackInfo& info);
37     static SwiperParameters GetDotIndicatorInfo(const JSRef<JSObject>& obj);
38     static SwiperDigitalParameters GetDigitIndicatorInfo(const JSRef<JSObject>& obj);
39     static std::optional<Dimension> ParseIndicatorDimension(const JSRef<JSVal>& value);
40     static std::optional<Dimension> ParseIndicatorBottom(const JSRef<JSVal>& bottomValue, bool hasIgnoreSize);
41     static void GetFontContent(const JSRef<JSVal>& font, bool isSelected, SwiperDigitalParameters& digitalParameters);
42     static void SetDotIndicatorInfo(const JSRef<JSObject>& obj, SwiperParameters& swiperParameters,
43         const RefPtr<SwiperIndicatorTheme>& swiperIndicatorTheme);
44 };
45 
46 class JSIndicatorController final : public Referenced {
47 public:
48     JSIndicatorController() = default;
49     ~JSIndicatorController() override = default;
50     static void JSBind(BindingTarget globalObj);
51     static void Constructor(const JSCallbackInfo& args);
52     static void Destructor(JSIndicatorController* scroller);
53 
ShowNext(const JSCallbackInfo & args)54     void ShowNext(const JSCallbackInfo& args)
55     {
56         ContainerScope scope(instanceId_);
57         if (controller_) {
58             controller_->ShowNext();
59         }
60     }
61 
ShowPrevious(const JSCallbackInfo & args)62     void ShowPrevious(const JSCallbackInfo& args)
63     {
64         ContainerScope scope(instanceId_);
65         if (controller_) {
66             controller_->ShowPrevious();
67         }
68     }
69 
70     void ChangeIndex(const JSCallbackInfo& args);
SetController(const RefPtr<NG::IndicatorController> & controller,WeakPtr<NG::UINode> & indicatorNode)71     void SetController(const RefPtr<NG::IndicatorController>& controller, WeakPtr<NG::UINode>& indicatorNode)
72     {
73         controller_ = controller;
74         indicatorNode_ = indicatorNode;
75         hasController_ = true;
76         if (hasSwiperNode_) {
77             controller_->SetSwiperNode(swiperNode_, indicatorNode_);
78         }
79     }
80 
GetController()81     RefPtr<NG::IndicatorController> GetController()
82     {
83         return controller_;
84     }
85 
SetInstanceId(int32_t id)86     void SetInstanceId(int32_t id)
87     {
88         instanceId_ = id;
89     }
90 
SetSwiperNode(WeakPtr<NG::UINode> & node)91     void SetSwiperNode(WeakPtr<NG::UINode>& node)
92     {
93         swiperNode_ = node;
94         hasSwiperNode_ = true;
95         if (hasController_) {
96             controller_->SetSwiperNode(swiperNode_, indicatorNode_);
97         }
98     }
99 
ResetSwiperNode()100     void ResetSwiperNode()
101     {
102         swiperNode_ = nullptr;
103         auto controller = GetController();
104         if (hasController_ && GetController()) {
105             controller->ResetSwiperNode();
106         }
107     }
108 
109 private:
110     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
111     RefPtr<NG::IndicatorController> controller_;
112     bool hasSwiperNode_ = false;
113     bool hasController_ = false;
114     WeakPtr<NG::UINode> swiperNode_;
115     WeakPtr<NG::UINode> indicatorNode_;
116     ACE_DISALLOW_COPY_AND_MOVE(JSIndicatorController);
117 };
118 
119 } // namespace OHOS::Ace::Framework
120 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_INDICATOR_H