Skip to content

Instantly share code, notes, and snippets.

@brantje
Created July 24, 2014 13:18
Show Gist options
  • Save brantje/359dcaf5f177789db51d to your computer and use it in GitHub Desktop.
Save brantje/359dcaf5f177789db51d to your computer and use it in GitHub Desktop.
Saving dialog positions
function make_draggable(elements) {
/* Elements is a jquery object: */
elements.draggable({
/* containment: 'parent',*/
start: function (evt, ui) {
ui.helper.css('z-index', ++zIndex);
var zoom = $('#notes').css('zoom');
pointerY = (evt.pageY - $('#notes').offset().top) / zoom - parseInt($(evt.target).css('top'));
pointerX = (evt.pageX - $('#notes').offset().left) / zoom - parseInt($(evt.target).css('left'));
},
drag: function (evt, ui) {
var zoom = $('#notes').css('zoom');
var canvasTop = $('#notes').offset().top;
var canvasLeft = $('#notes').offset().left;
var canvasHeight = $('#notes').height();
var canvasWidth = $('#notes').width();
// Fix for zoom
ui.position.top = Math.round((evt.pageY - canvasTop) / zoom - pointerY);
ui.position.left = Math.round((evt.pageX - canvasLeft) / zoom - pointerX);
// Check if element is outside canvas
if (ui.position.left < 0) ui.position.left = 0;
if (ui.position.left + $(this).width() > canvasWidth) ui.position.left = canvasWidth - $(this).width();
if (ui.position.top < 0) ui.position.top = 0;
if (ui.position.top + $(this).height() > canvasHeight) ui.position.top = canvasHeight - $(this).height();
// Finally, make sure offset aligns with position
ui.offset.top = Math.round(ui.position.top + canvasTop);
ui.offset.left = Math.round(ui.position.left + canvasLeft);
},
stop: function (e, ui) {
/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
console.log($(ui.helper).attr('id').replace('note-',''));
var d = $.base64.encode(JSON.stringify({
x: ui.position.left,
y: ui.position.top,
z: zIndex,
id: parseInt($(ui.helper).attr('id').replace('note-','')),
type: 'drag'
}));
$.get('ajax/update_position.php', {
data: d
});
}
});
}
function make_resizable(elements) {
/* Elements is a jquery object: */
elements.resizable({
/* containment: 'parent',*/
start: function (e, ui) {
ui.helper.css('z-index', ++zIndex);
},
stop: function (e, ui) {
/* Sending the height and width of the note to update_position.php via AJAX GET: */
var d = $.base64.encode(JSON.stringify({
h: ui.size.height,
w: ui.size.width,
z: zIndex,
x: ui.position.left,
y: ui.position.top,
id: parseInt(ui.helper.find('span.data').html()),
type: 'resize'
}));
$.get('ajax/update_position.php', {
data: d
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment