Skip to content

Commit

Permalink
JS -- Add support for buttons
Browse files Browse the repository at this point in the history
 * radio buttons
 * checkboxes
  • Loading branch information
calixteman committed Nov 18, 2020
1 parent a06f487 commit a9fca34
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1973,18 +1973,19 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {

getFieldObject() {
let type = "button";
let value = null;
let exportValues;
if (this.data.checkBox) {
type = "checkbox";
value = this.data.fieldValue && this.data.fieldValue !== "Off";
exportValues = this.data.exportValue;
} else if (this.data.radioButton) {
type = "radiobutton";
value = this.data.fieldValue === this.data.buttonValue;
exportValues = this.data.buttonValue;
}
return {
id: this.data.id,
value,
value: this.data.fieldValue || null,
defaultValue: this.data.defaultFieldValue,
exportValues,
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
Expand Down Expand Up @@ -2065,6 +2066,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
editable: !this.data.readOnly,
name: this.data.fieldName,
rect: this.data.rect,
numItems: this.data.fieldValue.length,
multipleSelection: this.data.multiSelect,
hidden: this.data.hidden,
actions: this.data.actions,
Expand Down
144 changes: 141 additions & 3 deletions src/scripting_api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Field extends PDFObject {
this.highlight = data.highlight;
this.lineWidth = data.lineWidth;
this.multiline = data.multiline;
this.multipleSelection = data.multipleSelection;
this.multipleSelection = !!data.multipleSelection;
this.name = data.name;
this.numItems = data.numItems;
this.page = data.page;
Expand All @@ -67,14 +67,34 @@ class Field extends PDFObject {
this.textSize = data.textSize;
this.type = data.type;
this.userName = data.userName;
this.value = data.value || "";
this.valueAsString = data.valueAsString;

// Private
this._document = data.doc;
this._value = data.value || "";
this._actions = this._createActionsMap(data.actions);
}

get value() {
return this._value;
}

set value(value) {
if (!this.multipleSelection) {
this._value = value;
}
}

checkThisBox(nWidget, bCheckIt = true) {}

isBoxChecked(nWidget) {
return false;
}

isDefaultChecked(nWidget) {
return false;
}

setAction(cTrigger, cScript) {
if (typeof cTrigger !== "string" || typeof cScript !== "string") {
return;
Expand Down Expand Up @@ -138,4 +158,122 @@ class Field extends PDFObject {
}
}

export { Field };
class CheckboxField extends Field {
constructor(data) {
super(data);

this.exportValues = ["Off", this.exportValues];
this.value = this._value;
}

get value() {
return this._value;
}

set value(value) {
if (typeof value === "boolean") {
this._value = value ? this.exportValues[1] : "Off";
} else {
this._value = value === this.exportValues[1] ? value : "Off";
}
}

_isButton() {
return true;
}

_getExportValue(state) {
return state ? this.exportValues[1] : "Off";
}

isBoxChecked(nWidget) {
return this._value !== "Off";
}

isDefaultChecked(nWidget) {
return this.exportValues[1] === this.defaultValue;
}

checkThisBox(nWidget, bCheckIt = true) {
this.value = bCheckIt;
this._send({ id: this._id, value: this.value });
}
}

class RadioButtonField extends Field {
constructor(otherButtons, data) {
super(data);

this.exportValues = [this.exportValues];
this._radioIds = [this._id];
this._radioActions = [this._actions];

for (const radioData of otherButtons) {
this.exportValues.push(radioData.exportValues);
this._radioIds.push(radioData.id);
this._radioActions.push(this._createActionsMap(radioData.actions));
if (this._value === radioData.exportValues) {
this._id = radioData.id;
}
}
}

get value() {
return this._value;
}

set value(value) {
const i = this.exportValues.indexOf(value);
if (0 <= i && i < this._radioIds.length) {
this._id = this._radioIds[i];
this._value = value;
} else if (value === "Off" && this._radioIds.length === 2) {
const nextI = (1 + this._radioIds.indexOf(this._id)) % 2;
this._id = this._radioIds[nextI];
this._value = this.exportValues[nextI];
}
}

checkThisBox(nWidget, bCheckIt = true) {
if (nWidget < 0 || nWidget >= this._radioIds.length || !bCheckIt) {
return;
}

this._id = this._radioIds[nWidget];
this._value = this.exportValues[nWidget];
this._send({ id: this._id, value: this._value });
}

isBoxChecked(nWidget) {
return (
nWidget >= 0 &&
nWidget < this._radioIds.length &&
this._id === this._radioIds[nWidget]
);
}

isDefaultChecked(nWidget) {
return (
nWidget >= 0 &&
nWidget < this.exportValues.length &&
this.defaultValue === this.exportValues[nWidget]
);
}

_getExportValue(state) {
const i = this._radioIds.indexOf(this._id);
return this.exportValues[i];
}

_runActions(event) {
const i = this._radioIds.indexOf(this._id);
this._actions = this._radioActions[i];
return super._runActions(event);
}

_isButton() {
return true;
}
}

export { CheckboxField, Field, RadioButtonField };
12 changes: 10 additions & 2 deletions src/scripting_api/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* limitations under the License.
*/

import { CheckboxField, Field, RadioButtonField } from "./field.js";
import { AForm } from "./aform.js";
import { App } from "./app.js";
import { Console } from "./console.js";
import { Doc } from "./doc.js";
import { Field } from "./field.js";
import { ProxyHandler } from "./proxy.js";
import { Util } from "./util.js";
import { ZoomType } from "./constants.js";
Expand All @@ -42,7 +42,15 @@ function initSandbox({ data, extra, out, testMode = false }) {
const obj = objs[0];
obj.send = send;
obj.doc = _document.wrapped;
const field = new Field(obj);
let field;
if (obj.type === "radiobutton") {
const otherButtons = objs.slice(1);
field = new RadioButtonField(otherButtons, obj);
} else if (obj.type === "checkbox") {
field = new CheckboxField(obj);
} else {
field = new Field(obj);
}
const wrapped = new Proxy(field, proxyHandler);
doc._addField(name, wrapped);
app._objects[obj.id] = { obj: field, wrapped };
Expand Down

0 comments on commit a9fca34

Please sign in to comment.