From 47ffec7a57f7a78f507a0541e3a77ab0a797cc1c Mon Sep 17 00:00:00 2001 From: Psy-Q Date: Thu, 4 Mar 2021 20:54:05 +0100 Subject: [PATCH] Reattach event listeners after #board-container is lost --- Assets/scroll.js | 76 ++++++++++++++++++++++++++---------------------- Plugin.php | 2 +- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/Assets/scroll.js b/Assets/scroll.js index cda0cbb..3e9bc42 100644 --- a/Assets/scroll.js +++ b/Assets/scroll.js @@ -1,40 +1,48 @@ -KB.on('dom.ready', function () { - function attachHorizontalScrollListeners() { - var boardContainer = $('#board-container'); - let isDown = false; - let startX; - let scrollLeftPixels; +function attachHorizontalScrollListeners() { + var boardContainer = $('#board-container'); + let isDown = false; + let startX; + let scrollLeftPixels; - boardContainer.on('mousedown', (e) => { - isDown = true; - boardContainer.addClass('horizontally-dragged'); - startX = e.pageX - boardContainer.offset().left; - scrollLeftPixels = boardContainer.scrollLeft(); - }); + boardContainer.on('mousedown', (e) => { + isDown = true; + startX = e.pageX - boardContainer.offset().left; + scrollLeftPixels = boardContainer.scrollLeft(); + }); - boardContainer.on('mouseleave', () => { - isDown = false; - boardContainer.removeClass('horizontally-dragged'); - }); + boardContainer.on('mouseleave', (e) => { + isDown = false; + }); - boardContainer.on('mouseup', () => { - isDown = false; - boardContainer.removeClass('horizontally-dragged'); - }); + boardContainer.on('mouseup', (e) => { + isDown = false; + }); - boardContainer.on('mousemove', (e) => { - if (!isDown) return; - // This is a draggable card, we don't want to also drag the background while - // dragging a card. - if ($(e.target).parents('div.task-board.draggable-item').length != 0) return; - e.preventDefault(); - const x = e.pageX - boardContainer.offset().left; - const walk = (x - startX) * 2; // Scroll speed - boardContainer.scrollLeft(scrollLeftPixels - walk); - }); - } + boardContainer.on('mousemove', (e) => { + console.log("mousemove"); + if (!isDown) return; + // This is a draggable card, we don't want to also drag the background while + // dragging a card. + if ($(e.target).parents('div.task-board.draggable-item').length != 0) return; + e.preventDefault(); + const x = e.pageX - boardContainer.offset().left; + const walk = (x - startX) * 2; // Scroll speed + boardContainer.scrollLeft(scrollLeftPixels - walk); + }); +} - if (KB.exists('#board-container')) { - attachHorizontalScrollListeners(); - } +// Only attach if we're on a page that shows the board +if (KB.exists('#board-container')) { + KB.on('dom.ready', function () { + attachHorizontalScrollListeners(); + }); +} + +// #board-container disappears after an AJAX drag/drop event, so +// we have to reattach the listeners +$(document).ajaxStop(function(){ + if (KB.exists('#board-container')) { + attachHorizontalScrollListeners(); + } }); + diff --git a/Plugin.php b/Plugin.php index 0c67a4a..28422d1 100644 --- a/Plugin.php +++ b/Plugin.php @@ -32,7 +32,7 @@ public function getPluginAuthor() public function getPluginVersion() { - return '0.2.2'; + return '0.2.3'; } public function getPluginHomepage()