/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "base/memory/referenced.h" #include "core/components_ng/syntax/repeat_model_ng.h" #include "bridge/declarative_frontend/jsview/js_repeat.h" #include "bridge/declarative_frontend/jsview/js_view_common_def.h" namespace OHOS::Ace { RepeatModel* RepeatModel::GetInstance() { static NG::RepeatModelNG instance; return &instance; } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSRepeat::StartRender() { RepeatModel::GetInstance()->StartRender(); } void JSRepeat::FinishRender(const JSCallbackInfo& info) { std::list removedElmtIds; if ((info.Length() != 1) || !info[0]->IsArray()) { TAG_LOGW(AceLogTag::ACE_REPEAT, "Invalid arguments for Repeat.FinishRender"); return; } RepeatModel::GetInstance()->FinishRender(removedElmtIds); if (!removedElmtIds.size()) { return; } // convert list of removed elmtIds: std::list to JSArray JSRef jsArr = JSRef::Cast(info[0]); size_t index = jsArr->Length(); for (const auto& rmElmtId : removedElmtIds) { jsArr->SetValueAt(index++, JSRef::Make(ToJSValue(rmElmtId))); } } // signature is // fromIndex: number void JSRepeat::MoveChild(const JSCallbackInfo& info) { if ((info.Length() != 1) || !info[0]->IsNumber()) { TAG_LOGW(AceLogTag::ACE_REPEAT, "Invalid arguments for Repeat.MoveChild"); return; } const auto fromIndex = info[0]->ToNumber(); RepeatModel::GetInstance()->MoveChild(fromIndex); } // signature is // id: string // parentView? : JSView void JSRepeat::CreateNewChildStart(const JSCallbackInfo& info) { if ((info.Length() < 1) || !info[0]->IsString()) { TAG_LOGW(AceLogTag::ACE_REPEAT, "Invalid arguments for Repeat.CreateNewChildStart"); return; } const auto key = info[0]->ToString(); RepeatModel::GetInstance()->CreateNewChildStart(key); } // signature is // id: string // parentView? : JSView void JSRepeat::CreateNewChildFinish(const JSCallbackInfo& info) { if ((info.Length() < 1) || !info[0]->IsString()) { TAG_LOGW(AceLogTag::ACE_REPEAT, "Invalid arguments for Repeat.CreateNewChildFinish"); return; } const auto key = info[0]->ToString(); RepeatModel::GetInstance()->CreateNewChildFinish(key); } void JSRepeat::OnMove(const JSCallbackInfo& info) { if (info[0]->IsFunction()) { auto onMove = [execCtx = info.GetExecutionContext(), func = JSRef::Cast(info[0])] (int32_t from, int32_t to) { auto params = ConvertToJSValues(from, to); func->Call(JSRef(), params.size(), params.data()); }; RepeatModel::GetInstance()->OnMove(std::move(onMove)); } else { RepeatModel::GetInstance()->OnMove(nullptr); } } void JSRepeat::JSBind(BindingTarget globalObj) { JSClass::Declare("RepeatNative"); JSClass::StaticMethod("startRender", &JSRepeat::StartRender); JSClass::StaticMethod("finishRender", &JSRepeat::FinishRender); JSClass::StaticMethod("moveChild", &JSRepeat::MoveChild); JSClass::StaticMethod("createNewChildStart", &JSRepeat::CreateNewChildStart); JSClass::StaticMethod("createNewChildFinish", &JSRepeat::CreateNewChildFinish); JSClass::StaticMethod("onMove", &JSRepeat::OnMove); JSClass::Bind<>(globalObj); } } // namespace OHOS::Ace::Framework