• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         return;
26     }
27     NG::MenuItemGroupView::Create();
28     if (info.Length() < 1 || !info[0]->IsObject()) {
29         return;
30     }
31     auto obj = JSRef<JSObject>::Cast(info[0]);
32     auto headerProp = obj->GetProperty("header");
33     if (!headerProp.IsEmpty()) {
34         if (headerProp->IsFunction()) {
35             RefPtr<NG::UINode> header;
36             {
37                 auto headerBuilderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(headerProp));
38                 CHECK_NULL_VOID(headerBuilderFunc);
39                 NG::ScopedViewStackProcessor builderViewStackProcessor;
40                 headerBuilderFunc->Execute();
41                 header = NG::ViewStackProcessor::GetInstance()->Finish();
42                 CHECK_NULL_VOID(header);
43             }
44             NG::MenuItemGroupView::SetHeader(header);
45         } else {
46             if (!ParseJsHeaderString(headerProp)) {
47                 return;
48             }
49         }
50     }
51     auto footerProp = obj->GetProperty("footer");
52     if (!footerProp.IsEmpty()) {
53         if (footerProp->IsFunction()) {
54             RefPtr<NG::UINode> footer;
55             {
56                 auto footerBuilderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(footerProp));
57                 CHECK_NULL_VOID(footerBuilderFunc);
58                 NG::ScopedViewStackProcessor builderViewStackProcessor;
59                 footerBuilderFunc->Execute();
60                 footer = NG::ViewStackProcessor::GetInstance()->Finish();
61                 CHECK_NULL_VOID(footer);
62             }
63             NG::MenuItemGroupView::SetFooter(footer);
64         } else {
65             if (!ParseJsFooterString(footerProp)) {
66                 return;
67             }
68         }
69     }
70 }
71 
ParseJsHeaderString(const JSRef<JSVal> & headerProp)72 bool JSMenuItemGroup::ParseJsHeaderString(const JSRef<JSVal>& headerProp)
73 {
74     std::string headerStr;
75     RefPtr<ResourceObject> resObj;
76     if (!ParseJsString(headerProp, headerStr, resObj)) {
77         return false;
78     }
79     NG::MenuItemGroupView::SetHeader(headerStr);
80     if (SystemProperties::ConfigChangePerform()) {
81         NG::MenuItemGroupView::CreateWithStringResourceObj(resObj, NG::MenuItemGroupStringType::HEADER);
82     }
83     return true;
84 }
85 
ParseJsFooterString(const JSRef<JSVal> & footerProp)86 bool JSMenuItemGroup::ParseJsFooterString(const JSRef<JSVal>& footerProp)
87 {
88     std::string footerStr;
89     RefPtr<ResourceObject> resObj;
90     if (!ParseJsString(footerProp, footerStr, resObj)) {
91         return false;
92     }
93     NG::MenuItemGroupView::SetFooter(footerStr);
94     if (SystemProperties::ConfigChangePerform()) {
95         NG::MenuItemGroupView::CreateWithStringResourceObj(resObj, NG::MenuItemGroupStringType::FOOTER);
96     }
97     return true;
98 }
99 
JSBind(BindingTarget globalObj)100 void JSMenuItemGroup::JSBind(BindingTarget globalObj)
101 {
102     JSClass<JSMenuItemGroup>::Declare("MenuItemGroup");
103     MethodOptions opt = MethodOptions::NONE;
104     JSClass<JSMenuItemGroup>::StaticMethod("create", &JSMenuItemGroup::Create, opt);
105     JSClass<JSMenuItemGroup>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
106     JSClass<JSMenuItemGroup>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
107     JSClass<JSMenuItemGroup>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
108     JSClass<JSMenuItemGroup>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
109     JSClass<JSMenuItemGroup>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
110     JSClass<JSMenuItemGroup>::InheritAndBind<JSViewAbstract>(globalObj);
111 }
112 } // namespace OHOS::Ace::Framework
113