1 /*
2 * Copyright (c) 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 "core/components_ng/pattern/stepper/stepper_node.h"
17
18 namespace OHOS::Ace::NG {
19
GetOrCreateStepperNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)20 RefPtr<StepperNode> StepperNode::GetOrCreateStepperNode(
21 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
22 {
23 auto stepperNode = ElementRegister::GetInstance()->GetSpecificItemById<StepperNode>(nodeId);
24 if (stepperNode) {
25 if (stepperNode->GetTag() == tag) {
26 return stepperNode;
27 }
28 ElementRegister::GetInstance()->RemoveItemSilently(nodeId);
29 auto parent = stepperNode->GetParent();
30 if (parent) {
31 parent->RemoveChild(stepperNode);
32 }
33 }
34
35 auto pattern = patternCreator ? patternCreator() : AceType::MakeRefPtr<Pattern>();
36 stepperNode = AceType::MakeRefPtr<StepperNode>(tag, nodeId, pattern, false);
37 stepperNode->InitializePatternAndContext();
38 ElementRegister::GetInstance()->AddUINode(stepperNode);
39 return stepperNode;
40 }
41
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)42 void StepperNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
43 {
44 int32_t swiperId = GetSwiperId();
45 auto swiperNode = GetChildAtIndex(GetChildIndexById(swiperId));
46 if (swiperNode) {
47 child->MountToParent(swiperNode, slot);
48 }
49 }
50
DeleteChildFromGroup(int32_t slot)51 void StepperNode::DeleteChildFromGroup(int32_t slot)
52 {
53 int32_t swiperId = GetSwiperId();
54 auto swiper = GetChildAtIndex(GetChildIndexById(swiperId));
55 CHECK_NULL_VOID(swiper);
56 swiper->RemoveChildAtIndex(slot);
57 }
58
59 } // namespace OHOS::Ace::NG