Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dragging Feature #660

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/DebugBar/Resources/debugbar.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* Hide debugbar when printing a page */
div.phpdebugbar {
display: none;
}
@media print {
div.phpdebugbar {
display: none;
Expand Down
84 changes: 84 additions & 0 deletions src/DebugBar/Resources/debugbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,88 @@ if (typeof(PhpDebugBar) == 'undefined') {

});

// ============= Handling Dragging Behavior ============
var storedPosX = localStorage.getItem('phpdebugbarPositionX') || 0;
var screenWidth = $(window).width();

$(document).on('click', '.phpdebugbar', function () {
let isClosed = $('.phpdebugbar').hasClass('phpdebugbar-closed');
if (!isClosed) {
devzkhalil marked this conversation as resolved.
Show resolved Hide resolved
// Save current position to localStorage when closing the debugbar
storedPosX = localStorage.getItem('phpdebugbarPositionX');
devzkhalil marked this conversation as resolved.
Show resolved Hide resolved
$('.phpdebugbar').css('left', '0px');
} else {
// Restore saved position from localStorage when opening the debugbar
localStorage.setItem('phpdebugbarPositionX', storedPosX);
$('.phpdebugbar').css('left', storedPosX + 'px');
}
});

$(document).ready(function () {
devzkhalil marked this conversation as resolved.
Show resolved Hide resolved
// Check local storage for saved position
var savedPosX = localStorage.getItem('phpdebugbarPositionX');
var phpdebugbarIsOpen = localStorage.getItem('phpdebugbar-open');

if (savedPosX !== null && phpdebugbarIsOpen == 0 && screenWidth > savedPosX) {
$('.phpdebugbar').css('left', savedPosX + 'px');
devzkhalil marked this conversation as resolved.
Show resolved Hide resolved
}

// Make the phpdebugbar visible after setting the position
$('div.phpdebugbar').css('display', 'block');

function startDragging(e) {
// Prevent text selection while dragging
e.preventDefault();

// Track initial mouse position and element position
var initialMouseX = e.type === 'mousedown' ? e.clientX : e.touches[0].clientX;
var initialPosX = $('.phpdebugbar').position().left;

// Get the width of the element and the screen
var elementWidth = $('.phpdebugbar').outerWidth();

function doDrag(e) {
// Calculate the change in mouse position
var clientX = e.type === 'mousemove' ? e.clientX : e.touches[0].clientX;
var deltaX = clientX - initialMouseX;

// Update the position of the element
var newPosX = initialPosX + deltaX;

// Ensure the new position is within screen boundaries
if (newPosX < 0) {
newPosX = 0;
} else if (newPosX + elementWidth > screenWidth) {
newPosX = screenWidth - elementWidth;
}

let isClosed = $('.phpdebugbar').hasClass('phpdebugbar-closed');
if (!isClosed) {
$('.phpdebugbar').css('left', '0px');
return;
}

$('.phpdebugbar').css('left', newPosX + 'px');
}

function stopDragging() {
// Unbind the move and up/end events
$(document).off('mousemove.drag touchmove.drag');
$(document).off('mouseup.drag touchend.drag');

// Save the new position to local storage
var finalPosX = $('.phpdebugbar').position().left;
localStorage.setItem('phpdebugbarPositionX', finalPosX);
}

// Bind the move and up/end events
$(document).on('mousemove.drag touchmove.drag', doDrag);
$(document).on('mouseup.drag touchend.drag', stopDragging);
}

// Bind the down/start events
$(document).on('mousedown touchstart', '.phpdebugbar', startDragging);
});
// ============= End Handling Dragging Behavior ============

})(PhpDebugBar.$);