Certainly! Here's a refactored version of the _createButton method. The refactoring focuses on reducing code duplication, enhancing readability, and modularizing the functionality by extracting common operations into helper functions.
_createButton(isToggle, options, callback) {
const button = document.createElement("button");
button.className = isToggle ? "toolbar-toggle-button" : "toolbar-button";
// Apply common attributes
this._applyAttributes(button, options.attributes);
// Initialize elements based on button type
let iconElement, labelElement;
if (isToggle) {
const initialState = Boolean(options.initialState);
button.classList.toggle("active", initialState);
// Apply state-specific attributes
this._applyAttributes(
button,
initialState ? options.activeAttributes : options.inactiveAttributes
);
// Create icon and label based on the initial state
iconElement = this._createIconElement(
initialState ? options.activeIcon : options.inactiveIcon,
options.icon
);
labelElement = this._createLabelElement(initialState, options);
} else {
// For regular buttons, create icon and label without state
iconElement = this._createIconElement(options.icon);
labelElement = this._createLabelElement(false, options);
}
// Append icon if it exists
if (iconElement) {
const iconContainer = document.createElement("span");
iconContainer.className = "toolbar-icon-container";
iconContainer.appendChild(iconElement);
button.appendChild(iconContainer);
}
// Append label
button.appendChild(labelElement);
// Set up event handlers
if (isToggle) {
const handleToggle = () => {
const newState = !button.classList.contains("active");
this._toggleButtonState(button, newState, options, iconElement, labelElement);
callback(newState);
};
button.addEventListener("click", handleToggle);
button._handleClick = handleToggle; // Reference for later removal
this.toggleButtons.push(button);
} else {
const handleClick = (event) => callback(event);
button.addEventListener("click", handleClick);
button._handleClick = handleClick; // Reference for later removal
this.buttons.push(button);
}
// Append the button to the toolbar
this.toolbar.appendChild(button);
}
Refactoring Highlights:
-
Reduced Duplication:
- The creation and appending of the
iconContainer and labelElement are handled once after determining the button type, minimizing repeated code.
-
Clear Separation of Concerns:
- Initialization of toggle-specific and regular button-specific logic is clearly separated within the
if (isToggle) block and the else block.
-
Enhanced Readability:
- Using descriptive variable names and logical structuring makes the flow easier to follow.
- Boolean casting with
Boolean(options.initialState) ensures clarity in state initialization.
-
Event Handler Setup:
- Event handlers are set up within their respective blocks, ensuring that toggle and regular buttons have appropriate behaviors without overlapping logic.
-
Consistent Attribute Application:
- Common attributes are applied first, followed by state-specific attributes for toggle buttons, maintaining a clear order of operations.
Optional Further Refactoring:
For even cleaner code, you might consider extracting some of the repeated functionalities into helper methods. For example:
-
Appending Icon and Label:
_appendIconAndLabel(button, iconElement, labelElement) {
if (iconElement) {
const iconContainer = document.createElement("span");
iconContainer.className = "toolbar-icon-container";
iconContainer.appendChild(iconElement);
button.appendChild(iconContainer);
}
button.appendChild(labelElement);
}
-
Setting Up Event Handlers:
_setupEventHandler(button, isToggle, callback, options, iconElement, labelElement) {
if (isToggle) {
const handleToggle = () => {
const newState = !button.classList.contains("active");
this._toggleButtonState(button, newState, options, iconElement, labelElement);
callback(newState);
};
button.addEventListener("click", handleToggle);
button._handleClick = handleToggle;
this.toggleButtons.push(button);
} else {
const handleClick = (event) => callback(event);
button.addEventListener("click", handleClick);
button._handleClick = handleClick;
this.buttons.push(button);
}
}
Then, your _createButton method would leverage these helpers:
_createButton(isToggle, options, callback) {
const button = document.createElement("button");
button.className = isToggle ? "toolbar-toggle-button" : "toolbar-button";
// Apply common attributes
this._applyAttributes(button, options.attributes);
let iconElement, labelElement;
if (isToggle) {
const initialState = Boolean(options.initialState);
button.classList.toggle("active", initialState);
// Apply state-specific attributes
this._applyAttributes(
button,
initialState ? options.activeAttributes : options.inactiveAttributes
);
// Create icon and label based on the initial state
iconElement = this._createIconElement(
initialState ? options.activeIcon : options.inactiveIcon,
options.icon
);
labelElement = this._createLabelElement(initialState, options);
} else {
// For regular buttons, create icon and label without state
iconElement = this._createIconElement(options.icon);
labelElement = this._createLabelElement(false, options);
}
// Append icon and label using helper
this._appendIconAndLabel(button, iconElement, labelElement);
// Set up event handlers using helper
this._setupEventHandler(button, isToggle, callback, options, iconElement, labelElement);
// Append the button to the toolbar
this.toolbar.appendChild(button);
}
This approach further modularizes the code, making each function responsible for a specific piece of functionality, thereby enhancing maintainability and scalability.