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/js_menu_item_group.h"
17 
18 #include "core/components_ng/base/view_stack_processor.h"
19 #include "core/components_ng/pattern/menu/menu_item_group/menu_item_group_view.h"
20 
21 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)22 void JSMenuItemGroup::Create(const JSCallbackInfo& info)
23 {
24     if (Container::IsCurrentUseNewPipeline()) {
25         NG::MenuItemGroupView::Create();
26 
27         if (info.Length() < 1 || !info[0]->IsObject()) {
28             return;
29         }
30         auto obj = JSRef<JSObject>::Cast(info[0]);
31         auto headerProp = obj->GetProperty("header");
32         if (!headerProp.IsEmpty()) {
33             if (headerProp->IsFunction()) {
34                 RefPtr<NG::UINode> header;
35                 {
36                     auto headerBuilderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(headerProp));
37                     CHECK_NULL_VOID(headerBuilderFunc);
38                     NG::ScopedViewStackProcessor builderViewStackProcessor;
39                     headerBuilderFunc->Execute();
40                     header = NG::ViewStackProcessor::GetInstance()->Finish();
41                     CHECK_NULL_VOID(header);
42                 }
43                 NG::MenuItemGroupView::SetHeader(header);
44             } else {
45                 std::string headerStr;
46                 if (!ParseJsString(headerProp, headerStr)) {
47                     return;
48                 }
49                 NG::MenuItemGroupView::SetHeader(headerStr);
50             }
51         }
52         auto footerProp = obj->GetProperty("footer");
53         if (!footerProp.IsEmpty()) {
54             if (footerProp->IsFunction()) {
55                 RefPtr<NG::UINode> footer;
56                 {
57                     auto footerBuilderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(footerProp));
58                     CHECK_NULL_VOID(footerBuilderFunc);
59                     NG::ScopedViewStackProcessor builderViewStackProcessor;
60                     footerBuilderFunc->Execute();
61                     footer = NG::ViewStackProcessor::GetInstance()->Finish();
62                     CHECK_NULL_VOID(footer);
63                 }
64                 NG::MenuItemGroupView::SetFooter(footer);
65             } else {
66                 std::string footerStr;
67                 if (!ParseJsString(footerProp, footerStr)) {
68                     return;
69                 }
70                 NG::MenuItemGroupView::SetFooter(footerStr);
71             }
72         }
73         return;
74     }
75 }
76 
JSBind(BindingTarget globalObj)77 void JSMenuItemGroup::JSBind(BindingTarget globalObj)
78 {
79     JSClass<JSMenuItemGroup>::Declare("MenuItemGroup");
80     MethodOptions opt = MethodOptions::NONE;
81     JSClass<JSMenuItemGroup>::StaticMethod("create", &JSMenuItemGroup::Create, opt);
82     JSClass<JSMenuItemGroup>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
83     JSClass<JSMenuItemGroup>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
84     JSClass<JSMenuItemGroup>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
85     JSClass<JSMenuItemGroup>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
86     JSClass<JSMenuItemGroup>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
87     JSClass<JSMenuItemGroup>::InheritAndBind<JSViewAbstract>(globalObj);
88 }
89 } // namespace OHOS::Ace::Framework