7#include "fxjs/xfa/cjx_instancemanager.h"
13#include "fxjs/js_resources.h"
14#include "fxjs/xfa/cfxjse_engine.h"
15#include "third_party/base/check_op.h"
16#include "third_party/base/containers/span.h"
17#include "v8/include/v8-object.h"
18#include "v8/include/v8-primitive.h"
19#include "xfa/fxfa/cxfa_ffdoc.h"
20#include "xfa/fxfa/cxfa_ffnotify.h"
21#include "xfa/fxfa/parser/cxfa_document.h"
22#include "xfa/fxfa/parser/cxfa_instancemanager.h"
23#include "xfa/fxfa/parser/cxfa_occur.h"
24#include "xfa/fxfa/parser/cxfa_subform.h"
27 {
"addInstance", addInstance_static},
28 {
"insertInstance", insertInstance_static},
29 {
"moveInstance", moveInstance_static},
30 {
"removeInstance", removeInstance_static},
31 {
"setInstances", setInstances_static}};
33CJX_InstanceManager::CJX_InstanceManager(CXFA_InstanceManager* mgr)
44int32_t CJX_InstanceManager::SetInstances(
v8::Isolate* pIsolate,
46 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
48 if (iDesired < iMin) {
54 if (iMax >= 0 && iDesired > iMax) {
60 if (iDesired == iCount)
63 if (iDesired < iCount) {
65 WideString wsInstanceName = WideString(
68 : wsInstManagerName.Last(wsInstManagerName.GetLength() - 1));
69 uint32_t dInstanceNameHash =
70 FX_HashCode_GetW(wsInstanceName.AsStringView());
80 while (iCount > iDesired) {
81 CXFA_Node* pRemoveInstance = pPrevSibling->GetNextSibling();
88 GetXFANode()->RemoveItem(pRemoveInstance,
true);
93 while (iCount < iDesired) {
98 GetXFANode()->InsertItem(pNewInstance, iCount, iCount,
false);
108 GetDocument()->GetLayoutProcessor()->SetHasChangedContainer();
116 if (iFrom > iCount || iTo > iCount - 1) {
120 if (iFrom < 0 || iTo < 0 || iFrom == iTo)
124 if (!pMoveInstance) {
129 GetXFANode()->RemoveItem(pMoveInstance,
false);
130 GetXFANode()->InsertItem(pMoveInstance, iTo, iCount - 1,
true);
131 GetDocument()->GetLayoutProcessor()->SetHasChangedContainer();
137 pdfium::span<v8::Local<v8::Value>> params) {
142 if (params.size() != 2)
145 int32_t iFrom = runtime->ToInt32(params[0]);
146 int32_t iTo = runtime->ToInt32(params[1]);
165CJS_Result CJX_InstanceManager::removeInstance(
167 pdfium::span<v8::Local<v8::Value>> params) {
172 if (params.size() != 1)
175 int32_t iIndex = runtime->ToInt32(params[0]);
177 if (iIndex < 0 || iIndex >= iCount)
180 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
182 if (iCount - 1 < iMin)
186 if (!pRemoveInstance)
189 GetXFANode()->RemoveItem(pRemoveInstance,
true);
194 for (int32_t i = iIndex; i < iCount - 1; i++) {
196 if (pSubformInstance)
200 GetDocument()->GetLayoutProcessor()->SetHasChangedContainer();
206 pdfium::span<v8::Local<v8::Value>> params) {
211 if (params.size() != 1)
214 SetInstances(runtime->GetIsolate(), runtime->ToInt32(params[0]));
220 pdfium::span<v8::Local<v8::Value>> params) {
225 if (!params.empty() && params.size() != 1)
229 if (params.size() == 1)
230 fFlags = runtime->ToBoolean(params[0]);
233 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
235 if (iMax >= 0 && iCount >= iMax)
242 GetXFANode()->InsertItem(pNewInstance, iCount, iCount,
false);
247 GetDocument()->GetLayoutProcessor()->SetHasChangedContainer();
251 runtime->GetOrCreateJSBindingFromMap(pNewInstance)
);
254CJS_Result CJX_InstanceManager::insertInstance(
256 pdfium::span<v8::Local<v8::Value>> params) {
261 if (params.size() != 1 && params.size() != 2)
264 int32_t iIndex = runtime->ToInt32(params[0]);
266 if (params.size() == 2)
267 bBind = runtime->ToBoolean(params[1]);
270 if (iIndex < 0 || iIndex > iCount)
273 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
275 if (iMax >= 0 && iCount >= iMax)
282 GetXFANode()->InsertItem(pNewInstance, iIndex, iCount,
true);
287 GetDocument()->GetLayoutProcessor()->SetHasChangedContainer();
291 runtime->GetOrCreateJSBindingFromMap(pNewInstance)
);
294void CJX_InstanceManager::max(
v8::Isolate* pIsolate,
295 v8::Local<v8::Value>* pValue,
302 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
303 *pValue = fxv8::NewNumberHelper(
307void CJX_InstanceManager::min(
v8::Isolate* pIsolate,
308 v8::Local<v8::Value>* pValue,
315 CXFA_Occur* occur =
GetXFANode()->GetOccurIfExists();
316 *pValue = fxv8::NewNumberHelper(
320void CJX_InstanceManager::count(
v8::Isolate* pIsolate,
321 v8::Local<v8::Value>* pValue,
325 SetInstances(pIsolate, fxv8::ReentrantToInt32Helper(pIsolate, *pValue));
328 *pValue = fxv8::NewNumberHelper(pIsolate,
GetXFANode()->GetCount());
friend class EventParamScope
CXFA_Document * GetDocument() const
static CJS_Result Success()
static CJS_Result Failure(JSMessage id)
int32_t MoveInstance(v8::Isolate *pIsolate, int32_t iTo, int32_t iFrom)
~CJX_InstanceManager() override
bool DynamicTypeIs(TypeTag eType) const override
bool DynamicTypeIs(TypeTag eType) const override
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
void ThrowIndexOutOfBoundsException(v8::Isolate *pIsolate) const
void ThrowInvalidPropertyException(v8::Isolate *pIsolate) const
CXFA_Document * GetDocument() const
WideString GetCData(XFA_Attribute eAttr) const
CXFA_Node * GetXFANode() const
void ThrowTooManyOccurrencesException(v8::Isolate *pIsolate, const WideString &obj) const
FormType GetFormType() const
void RunNodeInitialize(CXFA_Node *pNode)
void RunSubformIndexChange(CXFA_Subform *pSubformNode)
uint32_t GetNameHash() const
CXFA_Node * GetItemIfExists(int32_t iIndex)
XFA_Element GetElementType() const
static constexpr int32_t kDefaultMax
static constexpr int32_t kDefaultMin