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