Skip to content

Commit

Permalink
[pdf] Write forms actions
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita committed Oct 19, 2023
1 parent c0661e1 commit b27d77e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 17 deletions.
28 changes: 19 additions & 9 deletions pdf/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -2045,22 +2045,32 @@ var CPresentation = CPresentation || function(){};
* Note: This method used by forms actions.
* @memberof CPDFDoc
* @param {CBaseField[]} aNames - array with forms names to reset. If param is undefined or array is empty then resets all forms.
* @param {boolean} bAllExcept - reset all fields except aNames
* @typeofeditors ["PDF"]
*/
CPDFDoc.prototype.ResetForms = function(aNames) {
CPDFDoc.prototype.ResetForms = function(aNames, bAllExcept) {
let oActionsQueue = this.GetActionsQueue();
let oThis = this;

if (aNames.length > 0) {
aNames.forEach(function(name) {
let aFields = oThis.GetFields(name);
if (aFields.length > 0)
AscCommon.History.Clear()

aFields.forEach(function(field) {
field.Reset();
if (bAllExcept) {
for (let nField = 0; nField < this.widgets.length; nField++) {
let oField = this.widgets[nField];
if (aNames.includes(oField.GetFullName()) == false)
oField.Reset();
}
}
else {
aNames.forEach(function(name) {
let aFields = oThis.GetFields(name);
if (aFields.length > 0)
AscCommon.History.Clear()

aFields.forEach(function(field) {
field.Reset();
});
});
});
}
}
else {
this.widgets.forEach(function(field) {
Expand Down
64 changes: 61 additions & 3 deletions pdf/src/forms/base/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@
oAction.SetField(this);
break;
case AscPDF.ACTIONS_TYPES.ResetForm:
oAction = new AscPDF.CActionReset(aFields, aActionsInfo[i]["Fields"]);
oAction = new AscPDF.CActionReset(aActionsInfo[i]["Fields"], Boolean(aActionsInfo[i]["Flags"]));
aActions.push(oAction);
oAction.SetField(this);
break;
Expand Down Expand Up @@ -569,7 +569,61 @@

return null;
};
CBaseField.prototype.GetListActions = function() {
let aActions = [];

let oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.MouseUp);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.MouseDown);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.MouseEnter);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.MouseExit);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.OnFocus);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.OnBlur);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.Keystroke);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.Validate);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.Calculate);
if (oAction) {
aActions.push(oAction);
}

oAction = this.GetTrigger(AscPDF.FORMS_TRIGGERS_TYPES.Format);
if (oAction) {
aActions.push(oAction);
}

return aActions;
};
CBaseField.prototype.GetDocument = function() {
return this._doc;
};
Expand Down Expand Up @@ -1981,9 +2035,13 @@
memory.WriteString(sName);
}

// actions to do
memory.WriteLong(0);
// actions
let aActions = this.GetListActions();
memory.WriteLong(aActions.length);

for (let i = 0; i < aActions.length; i++) {
aActions[i].WriteToBinary(memory);
}
};

// for format
Expand Down
102 changes: 97 additions & 5 deletions pdf/src/forms/formActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,50 @@
CFormTrigger.prototype.GetActions = function() {
return this.Actions;
};
CFormTrigger.prototype.GetType = function() {
return this.type;
};
CFormTrigger.prototype.WriteToBinary = function(memory) {
let nType = this.GetType();
switch (nType) {
case AscPDF.FORMS_TRIGGERS_TYPES.MouseUp:
memory.WriteString("A");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.MouseDown:
memory.WriteString("D");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.MouseEnter:
memory.WriteString("E");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.MouseExit:
memory.WriteString("X");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.OnFocus:
memory.WriteString("Fo");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.OnBlur:
memory.WriteString("Bl");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.Keystroke:
memory.WriteString("K");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.Validate:
memory.WriteString("V");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.Calculate:
memory.WriteString("C");
break;
case AscPDF.FORMS_TRIGGERS_TYPES.Format:
memory.WriteString("F");
break;
}

for (let i = 0; i < this.Actions.length; i++) {
this.Actions[i].WriteToBinary(memory);
if (this.Actions[i + 1])
memory.WriteByte(1);
}
};

function CActionBase(nType) {
this.type = nType;
Expand Down Expand Up @@ -327,6 +371,11 @@
oActionsQueue.Continue();
};

CActionNamed.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());
memory.WriteString(this.GetName());
};

function CActionURI(sURI) {
CActionBase.call(this, ACTIONS_TYPES.URI);
this.uri = sURI;
Expand All @@ -347,13 +396,20 @@

editor.sendEvent("asc_onOpenLinkPdfForm", this.uri, this.OpenLink.bind(this), oActionsQueue.Continue.bind(oActionsQueue));
};

CActionURI.prototype.GetURI = function() {
return this.uri;
};
CActionURI.prototype.OpenLink = function() {
window.open(this.uri, "_blank");

this.field.GetDocument().GetActionsQueue().Continue();
};

CActionURI.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());
memory.WriteString(this.GetURI());
};

function CActionHideShow(bHidden, aFieldsNames) {
CActionBase.call(this, ACTIONS_TYPES.HideShow);
this.hidden = bHidden;
Expand All @@ -377,15 +433,30 @@
oDoc.HideShowForms(this.hidden, this.names);
};

function CActionReset(aFieldsNames) {
CActionHideShow.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());
if (this.hidden)
memory.WriteLong(1);
else
memory.WriteLong(0);

if (this.names && this.names.length != 0) {
memory.WriteLong(this.names.length);
for (let i = 0; i < this.names.length; i++) {
memory.WriteString(this.names[i]);
}
}
};

function CActionReset(aFieldsNames, bAllExcept) {
CActionBase.call(this, ACTIONS_TYPES.Reset);
this.names = aFieldsNames;
this.names = aFieldsNames;
this.bAllExcept = bAllExcept;
};
CActionReset.prototype = Object.create(CActionBase.prototype);
CActionReset.prototype.constructor = CActionReset;

CActionReset.prototype.Do = function() {
let oViewer = editor.getDocumentRenderer();
let oDoc = this.field.GetDocument();
let oActionsQueue = oDoc.GetActionsQueue();

Expand All @@ -395,7 +466,23 @@
if (this.triggerType == FORMS_TRIGGERS_TYPES.OnFocus && this.field != oDoc.activeForm)
oActionsQueue.Continue();

oDoc.ResetForms(this.names);
oDoc.ResetForms(this.names, this.bAllExcept);
};

CActionReset.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());

if (this.bAllExcept)
memory.WriteLong(1);
else
memory.WriteLong(0);

if (this.names && this.names.length != 0) {
memory.WriteLong(this.names.length);
for (let i = 0; i < this.names.length; i++) {
memory.WriteString(this.names[i]);
}
}
};

function CActionRunScript(script) {
Expand Down Expand Up @@ -450,6 +537,11 @@
console.log(err);
}
};

CActionRunScript.prototype.WriteToBinary = function(memory) {
memory.WriteByte(this.GetType());
memory.WriteString(this.script);
};

function EvalScript(str, oParentDoc) {
let aArgsNamesToDelete = [
Expand Down

0 comments on commit b27d77e

Please sign in to comment.