Phase 4: Begin JavaScript API compatibility layer for Bootstrap 5

- Added bootstrap-utils.ts as a temporary compatibility layer:
  - Clear documentation that this is temporary and will be removed after migration
  - Support for modals, toasts, tooltips, and dropdowns
  - Ensures consistent migration path for Bootstrap 5 JavaScript API changes
- Updated key files to use the compatibility layer:
  - Changed alert.ts to use BootstrapUtils for modals and toasts
  - Changed sharing.ts to use BootstrapUtils for tooltips and dropdowns
- Added plan to remove the compatibility layer in Phase 12 after migration
- Updated migration plan documentation to track progress

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Matt Godbolt
2025-04-17 14:50:47 -05:00
co-authored by Claude
parent f53ee0c36d
commit e2eaadfda3
4 changed files with 376 additions and 19 deletions
+20 -6
View File
@@ -79,11 +79,16 @@ allows for:
## Phase 4: JavaScript API Compatibility Layer
- [ ] Create a Bootstrap compatibility utility module to abstract component initialization
- [ ] This will help transition from jQuery-based initialization to native JS
- [ ] It will also make future jQuery removal easier if desired
- [ ] Define methods for each component type (Modal, Dropdown, Toast, etc.)
- [ ] Implement both jQuery and native JS paths depending on configuration
- [x] Create a temporary Bootstrap compatibility utility module to abstract component initialization
- [x] Implement a hybrid approach with `bootstrap-utils.ts` as a compatibility layer
- [x] Mark clearly as temporary code to be removed after migration is complete
- [x] Define methods for each component type (Modal, Dropdown, Toast, etc.)
- [x] Update component initialization in key files:
- [x] widgets/alert.ts (modals and toasts)
- [x] sharing.ts (modals, tooltips, and dropdowns)
- [ ] compiler-picker-popup.ts (modals)
- [ ] load-save.ts (modals)
- [ ] Other files with Bootstrap component initialization
- [ ] Test the compatibility layer with basic components
### Notes for Human Testers (Phase 4)
@@ -185,10 +190,13 @@ allows for:
- [ ] Document custom component implementations
- [ ] Note any deprecated features or changes in functionality
## Phase 12: Optional jQuery Removal (Future Work)
## Phase 12: Optional jQuery Removal and Cleanup (Future Work)
- [ ] Create plan for jQuery removal (if desired)
- [ ] Identify non-Bootstrap jQuery usage that would need refactoring
- [ ] Remove the temporary `bootstrap-utils.ts` compatibility layer
- [ ] Replace all uses with direct Bootstrap 5 API calls
- [ ] Document the native Bootstrap 5 API for future reference
- [ ] Note: This would be a separate effort after the Bootstrap migration is stable
## Notes for Implementation
@@ -231,5 +239,11 @@ allows for:
- data-dismiss → data-bs-dismiss
- Identified that data-ride and data-spy attributes are not used in the codebase
- Testing component functionality pending
- Phase 4 in progress:
- Created temporary `bootstrap-utils.ts` compatibility layer to abstract Bootstrap 5 APIs
- Updated alert.ts to use BootstrapUtils for modals and toasts
- Updated sharing.ts to use BootstrapUtils for tooltips, modals, and dropdowns
- Created plan to eventually remove compatibility layer in Phase 12
- Further component updates and testing pending
This plan will be updated as progress is made, with each completed step marked accordingly.
+325
View File
@@ -0,0 +1,325 @@
// Copyright (c) 2025, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
/**
* TEMPORARY COMPATIBILITY LAYER
*
* This module provides utilities to help transition from Bootstrap 4's jQuery-based API
* to Bootstrap 5's vanilla JavaScript API. This is intended as a temporary solution
* during the migration from Bootstrap 4 to 5 and should be removed once the migration
* is complete.
*
* The goal is to minimize changes throughout the codebase by centralizing the Bootstrap
* API changes in this file, while still allowing for gradual migration to direct API calls.
*
* @deprecated This module should be removed after the Bootstrap 5 migration is complete.
*/
import $ from 'jquery';
// Import Bootstrap JS
import 'bootstrap';
// Type definitions for Bootstrap 5 objects
declare global {
interface Window {
bootstrap: {
Modal: ModalClass;
Dropdown: DropdownClass;
Toast: ToastClass;
Tooltip: TooltipClass;
Popover: PopoverClass;
Tab: TabClass;
Collapse: CollapseClass;
};
}
interface ModalClass {
new (element: HTMLElement, options?: any): ModalInstance;
getInstance(element: HTMLElement): ModalInstance | null;
}
interface ModalInstance {
show(): void;
hide(): void;
toggle(): void;
dispose(): void;
}
interface DropdownClass {
new (element: HTMLElement, options?: any): DropdownInstance;
getInstance(element: HTMLElement): DropdownInstance | null;
}
interface DropdownInstance {
show(): void;
hide(): void;
toggle(): void;
dispose(): void;
}
interface ToastClass {
new (element: HTMLElement, options?: any): ToastInstance;
getInstance(element: HTMLElement): ToastInstance | null;
}
interface ToastInstance {
show(): void;
hide(): void;
dispose(): void;
}
interface TooltipClass {
new (element: HTMLElement, options?: any): TooltipInstance;
getInstance(element: HTMLElement): TooltipInstance | null;
}
interface TooltipInstance {
show(): void;
hide(): void;
toggle(): void;
dispose(): void;
}
interface PopoverClass {
new (element: HTMLElement, options?: any): PopoverInstance;
getInstance(element: HTMLElement): PopoverInstance | null;
}
interface PopoverInstance {
show(): void;
hide(): void;
toggle(): void;
dispose(): void;
}
interface TabClass {
new (element: HTMLElement): TabInstance;
getInstance(element: HTMLElement): TabInstance | null;
}
interface TabInstance {
show(): void;
dispose(): void;
}
interface CollapseClass {
new (element: HTMLElement, options?: any): CollapseInstance;
getInstance(element: HTMLElement): CollapseInstance | null;
}
interface CollapseInstance {
show(): void;
hide(): void;
toggle(): void;
dispose(): void;
}
}
export class BootstrapUtils {
/**
* Initialize a modal
* @param elementOrSelector Element or selector for the modal
* @param options Modal options
* @returns Modal instance
*/
static initModal(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Modal(element, options);
}
/**
* Show a modal
* @param elementOrSelector Element or selector for the modal
*/
static showModal(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const modal = window.bootstrap.Modal.getInstance(element) || new window.bootstrap.Modal(element);
modal.show();
}
/**
* Hide a modal
* @param elementOrSelector Element or selector for the modal
*/
static hideModal(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const modal = window.bootstrap.Modal.getInstance(element);
if (modal) modal.hide();
}
/**
* Initialize a toast
* @param elementOrSelector Element or selector for the toast
* @param options Toast options
* @returns Toast instance
*/
static initToast(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Toast(element, options);
}
/**
* Show a toast
* @param elementOrSelector Element or selector for the toast
*/
static showToast(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const toast = window.bootstrap.Toast.getInstance(element) || new window.bootstrap.Toast(element);
toast.show();
}
/**
* Hide a toast
* @param elementOrSelector Element or selector for the toast
*/
static hideToast(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const toast = window.bootstrap.Toast.getInstance(element);
if (toast) toast.hide();
}
/**
* Initialize a dropdown
* @param elementOrSelector Element or selector for the dropdown
* @param options Dropdown options
* @returns Dropdown instance
*/
static initDropdown(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Dropdown(element, options);
}
/**
* Show a dropdown
* @param elementOrSelector Element or selector for the dropdown
*/
static showDropdown(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const dropdown = window.bootstrap.Dropdown.getInstance(element) || new window.bootstrap.Dropdown(element);
dropdown.show();
}
/**
* Hide a dropdown
* @param elementOrSelector Element or selector for the dropdown
*/
static hideDropdown(elementOrSelector: string | HTMLElement | JQuery): void {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return;
const dropdown = window.bootstrap.Dropdown.getInstance(element);
if (dropdown) dropdown.hide();
}
/**
* Initialize a tooltip
* @param elementOrSelector Element or selector for the tooltip
* @param options Tooltip options
* @returns Tooltip instance
*/
static initTooltip(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Tooltip(element, options);
}
/**
* Initialize a popover
* @param elementOrSelector Element or selector for the popover
* @param options Popover options
* @returns Popover instance
*/
static initPopover(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Popover(element, options);
}
/**
* Initialize a tab
* @param elementOrSelector Element or selector for the tab
* @returns Tab instance
*/
static initTab(elementOrSelector: string | HTMLElement | JQuery): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Tab(element);
}
/**
* Initialize a collapse
* @param elementOrSelector Element or selector for the collapse
* @param options Collapse options
* @returns Collapse instance
*/
static initCollapse(elementOrSelector: string | HTMLElement | JQuery, options?: any): any {
const element = BootstrapUtils.getElement(elementOrSelector);
if (!element) return null;
return new window.bootstrap.Collapse(element, options);
}
/**
* Helper method to get an HTMLElement from various input types
* @param elementOrSelector Element, jQuery object, or selector
* @returns HTMLElement or null
*/
private static getElement(elementOrSelector: string | HTMLElement | JQuery): HTMLElement | null {
if (!elementOrSelector) return null;
if (typeof elementOrSelector === 'string') {
return document.querySelector(elementOrSelector as string);
}
if (elementOrSelector instanceof HTMLElement) {
return elementOrSelector;
}
if (elementOrSelector instanceof $) {
return (elementOrSelector as JQuery)[0] as HTMLElement;
}
return null;
}
}
+15 -5
View File
@@ -26,6 +26,7 @@ import ClipboardJS from 'clipboard';
import GoldenLayout from 'golden-layout';
import $ from 'jquery';
import _ from 'underscore';
import {BootstrapUtils} from './bootstrap-utils.js';
import {sessionThenLocalStorage} from './local.js';
import {options} from './options.js';
import * as url from './url.js';
@@ -269,7 +270,7 @@ export class Sharing {
event.stopPropagation();
this.copyLinkTypeToClipboard(type);
// As we prevented bubbling, the dropdown won't close by itself. We need to trigger it manually
this.share.dropdown('hide');
BootstrapUtils.hideDropdown(this.share);
}
}
@@ -307,15 +308,24 @@ export class Sharing {
}
private displayTooltip(where: JQuery, message: string): void {
where.tooltip('dispose');
where.tooltip({
// First dispose any existing tooltip
const tooltipEl = where[0];
const existingTooltip = window.bootstrap.Tooltip.getInstance(tooltipEl);
if (existingTooltip) {
existingTooltip.dispose();
}
// Create and show new tooltip
const tooltip = BootstrapUtils.initTooltip(tooltipEl, {
placement: 'bottom',
trigger: 'manual',
title: message,
});
where.tooltip('show');
tooltip.show();
// Manual triggering of tooltips does not hide them automatically. This timeout ensures they do
setTimeout(() => where.tooltip('hide'), 1500);
setTimeout(() => tooltip.hide(), 1500);
}
private openShareModalForType(type: LinkType): void {
+16 -8
View File
@@ -24,6 +24,7 @@
import $ from 'jquery';
import {BootstrapUtils} from '../bootstrap-utils.js';
import {AlertAskOptions, AlertEnterTextOptions, AlertNotifyOptions} from './alert.interfaces.js';
export class Alert {
@@ -57,7 +58,8 @@ export class Alert {
modal.toggleClass('error-alert', isError === true);
modal.find('.modal-title').html(title);
modal.find('.modal-body').html(body);
modal.modal();
BootstrapUtils.showModal(modal);
if (onClose) {
modal.off('hidden.bs.modal');
modal.on('hidden.bs.modal', onClose);
@@ -86,7 +88,8 @@ export class Alert {
modal.off('hidden.bs.modal');
modal.on('hidden.bs.modal', askOptions.onClose);
}
modal.modal();
BootstrapUtils.showModal(modal);
return modal;
}
@@ -119,21 +122,26 @@ export class Alert {
</div>
`);
container.append(newElement);
newElement.toast({
const toastOptions = {
autohide: autoDismiss,
delay: dismissTime,
});
};
BootstrapUtils.initToast(newElement, toastOptions);
if (group !== '') {
if (collapseSimilar) {
// Only collapsing if a group has been specified
const old = container.find(`[data-group="${group}"]`);
old.toast('hide');
old.remove();
old.each((_, element) => {
BootstrapUtils.hideToast(element);
$(element).remove();
});
}
newElement.attr('data-group', group);
}
onBeforeShow(newElement);
newElement.toast('show');
BootstrapUtils.showToast(newElement);
}
/**
@@ -181,7 +189,7 @@ export class Alert {
modal.on('shown.bs.modal', () => {
answerEdit.trigger('focus');
});
modal.modal();
BootstrapUtils.showModal(modal);
return modal;
}
}