From 7fddb75fc463cc465c023a8cb2d146e4709d084d Mon Sep 17 00:00:00 2001 From: Leopold Relota Date: Mon, 10 Jun 2024 14:08:38 +0000 Subject: [PATCH 1/2] delegate event handlers --- assets/javascripts/bootstrap/carousel.js | 6 +++--- assets/javascripts/bootstrap/collapse.js | 2 +- assets/javascripts/bootstrap/dropdown.js | 14 ++++++++++---- assets/javascripts/bootstrap/modal.js | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/assets/javascripts/bootstrap/carousel.js b/assets/javascripts/bootstrap/carousel.js index a5fcac3170..2ed0a80b1c 100644 --- a/assets/javascripts/bootstrap/carousel.js +++ b/assets/javascripts/bootstrap/carousel.js @@ -232,9 +232,9 @@ e.preventDefault() } - $(document) - .on('click.bs.carousel.data-api', '[data-slide]', clickHandler) - .on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler) + $('[data-slide], [data-slide-to]') + .on('click.bs.carousel.data-api', clickHandler) + .on('click.bs.carousel.data-api', clickHandler) $(window).on('load', function () { $('[data-ride="carousel"]').each(function () { diff --git a/assets/javascripts/bootstrap/collapse.js b/assets/javascripts/bootstrap/collapse.js index 2cd5997fd5..ae650a03d8 100644 --- a/assets/javascripts/bootstrap/collapse.js +++ b/assets/javascripts/bootstrap/collapse.js @@ -197,7 +197,7 @@ // COLLAPSE DATA-API // ================= - $(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function (e) { + $('[data-toggle="collapse"]').on('click.bs.collapse.data-api', function (e) { var $this = $(this) if (!$this.attr('data-target')) e.preventDefault() diff --git a/assets/javascripts/bootstrap/dropdown.js b/assets/javascripts/bootstrap/dropdown.js index 4ded850121..0f79843535 100644 --- a/assets/javascripts/bootstrap/dropdown.js +++ b/assets/javascripts/bootstrap/dropdown.js @@ -157,9 +157,15 @@ $(document) .on('click.bs.dropdown.data-api', clearMenus) - .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() }) - .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle) - .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown) - .on('keydown.bs.dropdown.data-api', '.dropdown-menu', Dropdown.prototype.keydown) + + $('.dropdown form') + .on('click.bs.dropdown.data-api', function (e) { e.stopPropagation() }) + + $('.dropdown-menu') + .on('keydown.bs.dropdown.data-api', Dropdown.prototype.keydown) + + $(toggle) + .on('click.bs.dropdown.data-api', Dropdown.prototype.toggle) + .on('keydown.bs.dropdown.data-api', Dropdown.prototype.keydown) }(jQuery); diff --git a/assets/javascripts/bootstrap/modal.js b/assets/javascripts/bootstrap/modal.js index b9eca494fe..cee0af0c5d 100644 --- a/assets/javascripts/bootstrap/modal.js +++ b/assets/javascripts/bootstrap/modal.js @@ -335,7 +335,7 @@ // MODAL DATA-API // ============== - $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) { + $('[data-toggle="modal"]').on('click.bs.modal.data-api', function (e) { var $this = $(this) var href = $this.attr('href') var target = $this.attr('data-target') || From 6222b7c52ba1d2274286300e36e9e0513f73c0f0 Mon Sep 17 00:00:00 2001 From: Leopold Relota Date: Tue, 11 Jun 2024 08:47:12 +0000 Subject: [PATCH 2/2] init clearMenues when drop is opened --- assets/javascripts/bootstrap/dropdown.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/bootstrap/dropdown.js b/assets/javascripts/bootstrap/dropdown.js index 0f79843535..8f436a8f45 100644 --- a/assets/javascripts/bootstrap/dropdown.js +++ b/assets/javascripts/bootstrap/dropdown.js @@ -156,7 +156,14 @@ // =================================== $(document) - .on('click.bs.dropdown.data-api', clearMenus) + .on('show.bs.dropdown', function(e) { + $(document) + .on('click.bs.dropdown.data-api', clearMenus) + }) + .on('hide.bs.dropdown', function(e) { + $(document) + .off('click.bs.dropdown.data-api', clearMenus) + }) $('.dropdown form') .on('click.bs.dropdown.data-api', function (e) { e.stopPropagation() })