• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/common/dom/dom_dialog.h"
17 
18 #include "base/log/event_report.h"
19 #include "base/utils/string_utils.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 namespace OHOS::Ace::Framework {
24 
DOMDialog(NodeId nodeId,const std::string & nodeName)25 DOMDialog::DOMDialog(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
26 {
27     dialogChild_ = AceType::MakeRefPtr<CustomDialogComponent>(std::to_string(nodeId), nodeName);
28 }
29 
PrepareSpecializedComponent()30 void DOMDialog::PrepareSpecializedComponent()
31 {
32     dialogChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
33     if (dialogWidth_.second) {
34         dialogChild_->SetWidth(dialogWidth_.first);
35     }
36     if (dialogHeight_.second) {
37         dialogChild_->SetHeight(dialogHeight_.first);
38     }
39     dialogChild_->SetIsDragable(dragable_);
40     if (display_) {
41         display_->SetVisible(isShow_ ? VisibleType::VISIBLE : VisibleType::GONE);
42     }
43 
44     if (customizedMargin_) {
45         auto margin = Edge(marginLeft_, marginTop_, marginRight_, marginBottom_);
46         dialogChild_->SetMargin(margin);
47     }
48 }
49 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)50 bool DOMDialog::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
51 {
52     static const LinearMapNode<void (*)(const std::string&, DOMDialog&)> styleOperators[] = {
53         { DOM_HEIGHT,
54             [](const std::string& val, DOMDialog& dialog) {
55                 dialog.dialogHeight_.first = dialog.ParseDimension(val);
56                 dialog.dialogHeight_.second = true;
57             } },
58         { DOM_MARGIN_BOTTOM,
59             [](const std::string& val, DOMDialog& dialog) {
60                 dialog.marginBottom_ = dialog.ParseDimension(val);
61                 dialog.customizedMargin_ = true;
62             } },
63         { DOM_MARGIN_END,
64             [](const std::string& val, DOMDialog& dialog) {
65                 if (dialog.IsRightToLeft()) {
66                     dialog.marginLeft_ = dialog.ParseDimension(val);
67                 } else {
68                     dialog.marginRight_ = dialog.ParseDimension(val);
69                 }
70                 dialog.customizedMargin_ = true;
71             } },
72         { DOM_MARGIN_LEFT,
73             [](const std::string& val, DOMDialog& dialog) {
74                 dialog.marginLeft_ = dialog.ParseDimension(val);
75                 dialog.customizedMargin_ = true;
76             } },
77         { DOM_MARGIN_RIGHT,
78             [](const std::string& val, DOMDialog& dialog) {
79                 dialog.marginRight_ = dialog.ParseDimension(val);
80                 dialog.customizedMargin_ = true;
81             } },
82         { DOM_MARGIN_START,
83             [](const std::string& val, DOMDialog& dialog) {
84                 if (dialog.IsRightToLeft()) {
85                     dialog.marginRight_ = dialog.ParseDimension(val);
86                 } else {
87                     dialog.marginLeft_ = dialog.ParseDimension(val);
88                 }
89                 dialog.customizedMargin_ = true;
90             } },
91         { DOM_MARGIN_TOP,
92             [](const std::string& val, DOMDialog& dialog) {
93                 dialog.marginTop_ = dialog.ParseDimension(val);
94                 dialog.customizedMargin_ = true;
95             } },
96         { DOM_WIDTH,
97             [](const std::string& val, DOMDialog& dialog) {
98                 dialog.dialogWidth_.first = dialog.ParseDimension(val);
99                 dialog.dialogWidth_.second = true;
100             } },
101     };
102     auto operatorIter = BinarySearchFindIndex(styleOperators, ArraySize(styleOperators), style.first.c_str());
103     if (operatorIter != -1) {
104         styleOperators[operatorIter].value(style.second, *this);
105         return true;
106     }
107     return false;
108 }
109 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)110 bool DOMDialog::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
111 {
112     if (attr.first == DOM_SHOW) {
113         isShow_ = StringToBool(attr.second);
114         return true;
115     } else if (attr.first == DOM_DIALOG_STYLE_DRAGABLE) {
116         dragable_ = StringToBool(attr.second);
117         return true;
118     }
119     return false;
120 }
121 
CallSpecializedMethod(const std::string & method,const std::string & args)122 void DOMDialog::CallSpecializedMethod(const std::string& method, const std::string& args)
123 {
124     if (method == DOM_DIALOG_METHOD_SHOW) {
125         const auto& controller = dialogChild_->GetDialogController();
126         if (!controller) {
127             return;
128         }
129         controller->ShowDialog();
130     } else if (method == DOM_DIALOG_METHOD_CLOSE) {
131         const auto& controller = dialogChild_->GetDialogController();
132         if (!controller) {
133             return;
134         }
135         controller->CloseDialog();
136     } else {
137         LOGW("no such method available.");
138     }
139 }
140 
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)141 void DOMDialog::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
142 {
143     if (!display_) {
144         display_ = AceType::MakeRefPtr<DisplayComponent>(child->GetRootComponent());
145     }
146     display_->SetVisible(isShow_ ? VisibleType::VISIBLE : VisibleType::GONE);
147     dialogChild_->SetChild(display_);
148 }
149 
OnChildNodeRemoved(const RefPtr<DOMNode> & child)150 void DOMDialog::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
151 {
152     dialogChild_->SetChild(nullptr);
153 }
154 
AddSpecializedEvent(int32_t pageId,const std::string & event)155 bool DOMDialog::AddSpecializedEvent(int32_t pageId, const std::string& event)
156 {
157     if (event == DOM_DIALOG_EVENT_CANCEL) {
158         dialogChild_->SetOnCancel(EventMarker(GetNodeIdForEvent(), event, pageId));
159         return true;
160     } else if (event == DOM_DIALOG_METHOD_SHOW) {
161         dialogChild_->SetOnShow(EventMarker(GetNodeIdForEvent(), event, pageId));
162         return true;
163     } else if (event == DOM_DIALOG_METHOD_CLOSE) {
164         dialogChild_->SetOnClose(EventMarker(GetNodeIdForEvent(), event, pageId));
165         return true;
166     }
167 
168     LOGE("unsupported event: %{public}s.", event.c_str());
169     EventReport::SendComponentException(ComponentExcepType::DIALOG_EVENT_ERR);
170     return false;
171 }
172 
CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>> & components)173 RefPtr<Component> DOMDialog::CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components)
174 {
175     auto box = AceType::MakeRefPtr<BoxComponent>();
176     box->SetChild(dialogChild_);
177     return box;
178 }
179 
180 } // namespace OHOS::Ace::Framework
181