Plugin Class
The Plugin class serves as the foundation for all plugins in RoundtableJS. It provides a structure for creating plugins that can interact with the survey at various lifecycle stages, allowing you to extend the functionality of your surveys.
Constructor
The ID of the container element where the plugin will be injected.
The position where the plugin should be injected. Can be ‘top’ or ‘bottom’.
Custom styles to apply to the plugin’s root element.
Properties
targetId
: The ID of the target container.position
: The position of the plugin (‘top’ or ‘bottom’).styles
: The styles for the plugin.pluginId
: A unique ID generated for the plugin.survey
: Reference to the survey instance (set during initialization).
Methods
initialize(survey)
Initialize the plugin with a survey instance and inject it into the DOM.
- Parameters:
survey
(Survey): The survey instance
injectPlugin()
Inject the plugin element into the DOM.
getOrCreatePluginContainer(targetContainer)
Get or create the container for plugins.
- Parameters:
targetContainer
(HTMLElement): The target container element
- Returns:
HTMLElement
: The plugin container
createPluginElement()
Create the plugin’s DOM element.
- Returns:
HTMLElement
: The created plugin element
generateContent()
Generate the content for the plugin. This method should be implemented by subclasses.
- Returns:
string
: The HTML content for the plugin
applyStyles(element)
Apply styles to the plugin element.
- Parameters:
element
(HTMLElement): The element to style
beforePageRender()
Hook called before a page is rendered. To be implemented by subclasses if needed.
afterPageRender()
Hook called after a page is rendered. To be implemented by subclasses if needed.
beforeSurveyFinish()
Hook called before the survey finishes. To be implemented by subclasses if needed.
destroy()
Remove the plugin from the DOM.
Creating a Custom Plugin
To create a custom plugin, you’ll extend the Plugin class and implement specific methods. Here’s a step-by-step guide:
-
Extend the Plugin Class
Start by creating a new class that extends Plugin:
-
Implement the Constructor
In the constructor, call the parent constructor and initialize any custom properties:
-
Implement generateContent Method
Override the generateContent method to provide the HTML content for your plugin:
-
Implement Lifecycle Methods (Optional)
Override the lifecycle methods to add your custom logic:
-
Add Custom Methods (Optional)
Include any additional methods specific to your plugin:
Example: Custom Header Plugin
This plugin adds a custom header to the survey: