1 /*
2  * Copyright (c) 2023 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 "bridge/declarative_frontend/jsview/models/alert_dialog_model_impl.h"
17 
18 #include "core/components/dialog/alert_dialog_component.h"
19 
20 namespace OHOS::Ace::Framework {
SetParseButtonObj(std::function<void ()> && eventFuncImpl,ButtonInfo & buttonInfo,DialogProperties & arg,const std::string & property)21 void AlertDialogModelImpl::SetParseButtonObj(
22     std::function<void()>&& eventFuncImpl, ButtonInfo& buttonInfo, DialogProperties& arg, const std::string& property)
23 {
24     EventMarker actionId(std::move(eventFuncImpl));
25     if (property == "confirm" || property == "primaryButton") {
26         arg.primaryId = actionId;
27     } else if (property == "secondaryButton") {
28         arg.secondaryId = actionId;
29     }
30 }
31 
SetOnCancel(std::function<void ()> && eventFunc,DialogProperties & arg)32 void AlertDialogModelImpl::SetOnCancel(std::function<void()>&& eventFunc, DialogProperties& arg)
33 {
34     EventMarker cancelId(std::move(eventFunc));
35     arg.callbacks.try_emplace("cancel", cancelId);
36 }
37 
SetShowDialog(const DialogProperties & arg)38 void AlertDialogModelImpl::SetShowDialog(const DialogProperties& arg)
39 {
40     auto container = Container::Current();
41     if (container) {
42         auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
43         auto executor = container->GetTaskExecutor();
44         if (executor) {
45             executor->PostTask(
46                 [context, arg]() {
47                     if (context) {
48                         context->ShowDialog(arg, false, "AlertDialog");
49                     }
50                 },
51                 TaskExecutor::TaskType::UI, "ArkUIDialogShowAlertDialog");
52         }
53     }
54 }
55 } // namespace OHOS::Ace::Framework
56