Skip to content

New interface for page-level actions #3486

@j-t-1

Description

@j-t-1

Explanation

Page objects may Include an entry named AA that specifies an additional-actions dictionary that extends the set of events that can trigger the execution of an action.

Because there are many types of action (see Table 201—Action types), a good interface for adding them is needed. #3382 is for adding a JavaScript action, but this needs critiquing to ensure it is extensible for other action types.

Code Example

    def add_action(
            self,
            trigger: Literal["O", "C"] = "O",
            action_type: Literal["JavaScript"] = "JavaScript",
            action: str = ""
        ) -> None:
        r"""
        Add action which will launch on the open or close trigger event of this page.

        Args:
            trigger: "/O" or "/C", for open or close trigger event respectively.
            action_type: "JavaScript" is currently the only available action type.
            action: Your JavaScript.

        >>> output.add_action("/O", "JavaScript", 'app.alert("This is page " + this.pageNum);')
        # Example: This will display the page number when the page is opened.
        >>> output.add_action("/C", "JavaScript", 'app.alert("This is page " + this.pageNum);')
        # Example: This will display the page number when the page is closed.

        Note that this will replace any existing open or close trigger event on this page.
        Currently only an open or close event can be added, not both.
        """
        if trigger not in {"/O", "/C"}:
            raise ValueError('The trigger must be "/O" or "/C"')

        if action_type != "JavaScript":
            raise ValueError('Currently the only action_type supported is "JavaScript"')

        additional_actions = DictionaryObject()
        self[NameObject("/AA")] = additional_actions
        additional_actions[NameObject(trigger)] = DictionaryObject(
            {
                NameObject("/Type"): NameObject("/Action"),
                NameObject("/S"): NameObject("/JavaScript"),
                NameObject("/JS"): TextStringObject(f"{action}"),
            }
        )
        action_object = DecodedStreamObject()
        action_object.set_data(action.encode())

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-featureA feature requestneeds-discussionThe PR/issue needs more discussion before we can continue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions