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_form.h"
17
18 namespace OHOS::Ace::Framework {
19
DOMForm(NodeId nodeId,const std::string & nodeName)20 DOMForm::DOMForm(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
21 {
22 std::list<RefPtr<Component>> components;
23 columnContainer_ = AceType::MakeRefPtr<ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::FLEX_START, components);
24 }
25
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)26 void DOMForm::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
27 {
28 if (child) {
29 columnContainer_->InsertChild(slot, child->GetRootComponent());
30 }
31 }
32
PrepareSpecializedComponent()33 void DOMForm::PrepareSpecializedComponent() {}
34
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)35 bool DOMForm::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
36 {
37 if (attr.first == DOM_ID) {
38 formId_ = attr.second;
39 }
40 return false;
41 }
42
AddSpecializedEvent(int32_t pageId,const std::string & event)43 bool DOMForm::AddSpecializedEvent(int32_t pageId, const std::string& event)
44 {
45 if (event == DOM_FORM_EVENT_SUBMIT) {
46 onSubmit_ = AceAsyncEvent<void(const std::string&)>::Create(
47 EventMarker(GetNodeIdForEvent(), event, pageId), pipelineContext_);
48 return true;
49 } else if (event == DOM_FORM_EVENT_RESET) {
50 onReset_ = AceAsyncEvent<void()>::Create(EventMarker(GetNodeIdForEvent(), event, pageId), pipelineContext_);
51 return true;
52 }
53 return false;
54 }
55
OnChildNodeRemoved(const RefPtr<DOMNode> & child)56 void DOMForm::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
57 {
58 if (child) {
59 columnContainer_->RemoveChild(child->GetRootComponent());
60 }
61 }
62
Submit()63 void DOMForm::Submit()
64 {
65 if (!onSubmit_) {
66 return;
67 }
68 // call on the ui thread.
69 auto json = JsonUtil::Create(true);
70 for (const auto& weak : formValueList_) {
71 auto formValue = weak.Upgrade();
72 if (formValue) {
73 const auto& pair = formValue->GetFormValue();
74 if (pair.first.empty()) {
75 continue;
76 }
77 json->Put(pair.first.c_str(), pair.second.c_str());
78 }
79 }
80 auto value = JsonUtil::Create(true);
81 value->Put("value", json);
82 value->Put("formId", formId_.c_str());
83 std::string result = std::string(R"("submit",)").append(value->ToString()).append(", null");
84 onSubmit_(result);
85 }
86
Reset()87 void DOMForm::Reset()
88 {
89 // call on the ui thread.
90 for (const auto& weak : formValueList_) {
91 auto formValue = weak.Upgrade();
92 if (formValue) {
93 formValue->Reset();
94 }
95 }
96 if (onReset_) {
97 onReset_();
98 }
99 }
100
101 } // namespace OHOS::Ace::Framework
102