1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fxjs/xfa/cjx_form.h"
8
9 #include <vector>
10
11 #include "fxjs/cfxjse_engine.h"
12 #include "fxjs/cfxjse_value.h"
13 #include "fxjs/js_resources.h"
14 #include "xfa/fxfa/cxfa_eventparam.h"
15 #include "xfa/fxfa/cxfa_ffnotify.h"
16 #include "xfa/fxfa/parser/cxfa_arraynodelist.h"
17 #include "xfa/fxfa/parser/cxfa_document.h"
18 #include "xfa/fxfa/parser/cxfa_form.h"
19
20 const CJX_MethodSpec CJX_Form::MethodSpecs[] = {
21 {"execCalculate", execCalculate_static},
22 {"execInitialize", execInitialize_static},
23 {"execValidate", execValidate_static},
24 {"formNodes", formNodes_static},
25 {"recalculate", recalculate_static},
26 {"remerge", remerge_static}};
27
CJX_Form(CXFA_Form * form)28 CJX_Form::CJX_Form(CXFA_Form* form) : CJX_Model(form) {
29 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs));
30 }
31
~CJX_Form()32 CJX_Form::~CJX_Form() {}
33
formNodes(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)34 CJS_Return CJX_Form::formNodes(
35 CJS_V8* runtime,
36 const std::vector<v8::Local<v8::Value>>& params) {
37 if (params.size() != 1)
38 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
39
40 CXFA_Node* pDataNode = ToNode(runtime->ToXFAObject(params[0]));
41 if (!pDataNode)
42 return CJS_Return(JSGetStringFromID(JSMessage::kValueError));
43
44 std::vector<CXFA_Node*> formItems;
45 CXFA_ArrayNodeList* pFormNodes = new CXFA_ArrayNodeList(GetDocument());
46 pFormNodes->SetArrayNodeList(formItems);
47
48 CFXJSE_Value* value =
49 GetDocument()->GetScriptContext()->GetJSValueFromMap(pFormNodes);
50 if (!value)
51 return CJS_Return(runtime->NewNull());
52 return CJS_Return(value->DirectGetValue().Get(runtime->GetIsolate()));
53 }
54
remerge(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)55 CJS_Return CJX_Form::remerge(CJS_V8* runtime,
56 const std::vector<v8::Local<v8::Value>>& params) {
57 if (!params.empty())
58 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
59
60 GetDocument()->DoDataRemerge(true);
61 return CJS_Return(true);
62 }
63
execInitialize(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)64 CJS_Return CJX_Form::execInitialize(
65 CJS_V8* runtime,
66 const std::vector<v8::Local<v8::Value>>& params) {
67 if (!params.empty())
68 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
69
70 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
71 if (pNotify)
72 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize);
73 return CJS_Return(true);
74 }
75
recalculate(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)76 CJS_Return CJX_Form::recalculate(
77 CJS_V8* runtime,
78 const std::vector<v8::Local<v8::Value>>& params) {
79 CXFA_EventParam* pEventParam =
80 GetDocument()->GetScriptContext()->GetEventParam();
81 if (pEventParam->m_eType == XFA_EVENT_Calculate ||
82 pEventParam->m_eType == XFA_EVENT_InitCalculate) {
83 return CJS_Return(true);
84 }
85 if (params.size() != 1)
86 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
87
88 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
89 if (!pNotify || runtime->ToInt32(params[0]) != 0)
90 return CJS_Return(true);
91
92 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate);
93 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate);
94 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Ready, true);
95 return CJS_Return(true);
96 }
97
execCalculate(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)98 CJS_Return CJX_Form::execCalculate(
99 CJS_V8* runtime,
100 const std::vector<v8::Local<v8::Value>>& params) {
101 if (!params.empty())
102 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
103
104 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
105 if (pNotify)
106 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate);
107 return CJS_Return(true);
108 }
109
execValidate(CJS_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)110 CJS_Return CJX_Form::execValidate(
111 CJS_V8* runtime,
112 const std::vector<v8::Local<v8::Value>>& params) {
113 if (params.size() != 0)
114 return CJS_Return(JSGetStringFromID(JSMessage::kParamError));
115
116 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
117 if (!pNotify)
118 return CJS_Return(runtime->NewBoolean(false));
119
120 int32_t iRet =
121 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate);
122 return CJS_Return(runtime->NewBoolean(iRet != XFA_EVENTERROR_Error));
123 }
124