1 /*
2  * Copyright (c) 2021 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 <string>
17 
18 #include "bridge/declarative_frontend/jsview/models/for_each_model_impl.h"
19 
20 #include "base/memory/referenced.h"
21 #include "bridge/declarative_frontend/engine/functions/js_foreach_function.h"
22 #include "bridge/declarative_frontend/view_stack_processor.h"
23 #include "core/components/foreach/for_each_component.h"
24 #include "core/components_part_upd/foreach/foreach_component.h"
25 #include "core/components_part_upd/foreach/foreach_element.h"
26 #include "core/components_v2/common/element_proxy.h"
27 
28 namespace OHOS::Ace::Framework {
29 
30 
Create(const std::string & compilerGenId,const OHOS::Ace::ForEachFunc & ForEachFunc)31 void ForEachModelImpl::Create(const std::string& compilerGenId, const OHOS::Ace::ForEachFunc& ForEachFunc)
32 {
33     if (compilerGenId == "") {
34         LOGE("Missing ForEach id");
35         return;
36     }
37     if (!ForEachFunc.idGenFunc_ || !ForEachFunc.itemGenFunc_) {
38         LOGE("for each func is nullptr");
39         return;
40     }
41 
42     auto* viewStack = ViewStackProcessor::GetInstance();
43     std::string viewId = viewStack->ProcessViewId(compilerGenId);
44     viewStack->Push(AceType::MakeRefPtr<ForEachComponent>(viewId, "ForEach"));
45 
46     std::vector<std::string> keys = ForEachFunc.idGenFunc_();
47     for (size_t i = 0; i < keys.size(); i++) {
48         keys[i].insert(0, "-");
49         keys[i].insert(0, compilerGenId);
50         viewStack->PushKey(keys[i]);
51 
52         viewStack->Push(AceType::MakeRefPtr<MultiComposedComponent>(viewStack->GetKey(), "ForEachItem"));
53         ForEachFunc.itemGenFunc_(i);
54         viewStack->PopContainer();
55         viewStack->PopKey();
56     }
57 }
58 
Create()59 void ForEachModelImpl::Create()
60 {
61     LOGE("Create (no params) unsupported by ForEachModelImpl");
62 }
63 
Pop()64 void ForEachModelImpl::Pop()
65 {
66     ViewStackProcessor::GetInstance()->PopContainer();
67 }
68 
GetCurrentIdList(int32_t nodeId)69 std::list<std::string> ForEachModelImpl::GetCurrentIdList(int32_t nodeId)
70 {
71     LOGE("GetCurrentIdList unsupported by ForEachModelImpl");
72     return {};
73 }
74 
SetNewIds(std::list<std::string> && newIds)75 void ForEachModelImpl::SetNewIds(std::list<std::string>&& newIds)
76 {
77     LOGE("SetNewIds unsupported by ForEachModelImpl");
78 }
79 
CreateNewChildStart(const std::string & id)80 void ForEachModelImpl::CreateNewChildStart(const std::string& id)
81 {
82     LOGE("CreateNewChildStart unsupported by ForEachModelImpl");
83 }
84 
CreateNewChildFinish(const std::string & id)85 void ForEachModelImpl::CreateNewChildFinish(const std::string& id)
86 {
87     LOGE("CreateNewChildFinish unsupported by ForEachModelImpl");
88 }
89 
90 } // namespace OHOS::Ace::Framework
91