// * pixel offsets all over the place // * via cleanup // * popup dialogs // * scales /*extern $, console */ // the map data structure - updated via JSON from the server var map = { valid: 0 }; var mapfile = ''; var lastserial = 0; var linkmdown = false; var dragmdown = false; var addedVia = false; var dragstart = { x: -1, y: -1 }; var dragstop = { x: -1, y: -1 }; var dragitem = ''; var dragoffset = { x: 0, y: 0 }; var interactmode = ''; // we queue these up to make our one-at-a-time AJAX call work //var AJAXRequest = { // params: {}, // send: function() // { // } //}; //function printfire() { // if (document.createEvent) // { // printfire.args = arguments; // var ev = document.createEvent("Events"); // ev.initEvent("printfire", false, true ); // dispatchEvent(ev); // } //} function openmap(mapname) { console.log('Opening map: ' + mapname); mapfile = mapname; $('#welcome').hide(); $('#filepicker').hide(); $('#toolbar').show(); $('#themap').show(); $('#busy').hide(); $('img.mapnode').remove(); $('img.mapvia').remove(); $('#filename').html(mapname); console.log('Refreshing map: ' + mapname); map_refresh(); } function showpicker() { $('#welcome').hide(); $('#filepicker').show(); $('#toolbar').hide(); $('#themap').hide(); $('#busy').show(); $('#filelist').empty(); $('#filelist').append( '
Fetching File List...
';
}
$('#filelist').append('
');
existing = $('img#' + via_id);
}
newx = origin_x + vs[i][0] - 5;
newy = origin_y + vs[i][1] - 5;
existing.css({
position: 'absolute',
left: newx + "px",
top: newy + "px",
'z-index': 30
});
console.log("created " + via_id + ' at ' + newx + ',' + newy);
}
}
}
}
reapplyDraggableEvents();
$('#busy').hide();
}
function map_refresh()
{
console.log('Fetching JSON.');
$.getJSON("editor-backend.php", {
map: mapfile,
"cmd": "dump_map",
"serial": lastserial
}, function(json)
{
console.log('Inside JSON function');
if (json.valid === 1) {
map = json;
console.log('Loaded JSON for ' + mapfile + ' - about to syncmap()');
syncmap();
console.log('Map synced. Fetching bg image data.');
$('#existingdata').attr('src', json.map.mapcache + "?s=" + json.serial);
lastserial = json.serial;
// $('#existingdata').attr('src',json.map.mapcache);
$.get('editor-backend.php', {
map: mapfile,
cmd: "imagemap"
}, function(cont)
{
$('map#weathermap_imap').empty();
// $('map#weathermap_imap').html(cont);
// Getting medieval here - jQuery doesn't seem to be interested in Imagemaps??
var oldskool = document.getElementById('weathermap_imap');
oldskool.innerHTML = cont;
reapplyLinkEvents();
});
} else {
console.log('Failed to get good JSON');
alert('Got invalid JSON map data');
}
console.log('Leaving JSON function');
});
console.log('Done with map_refresh()');
}
function getMousePosition(event)
{
var x = event.pageX || (event.clientX
+ (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
var y = event.pageY || (event.clientY
+ (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
return {
x: x,
y: y
};
}
function reapplyLinkEvents()
{
// unbind, then re-apply all events for links.
// is this actually necessary?
$('area[@id^=LINK:]').unbind();
$('area[@id^=LINK:]').mousedown(function(ev)
{
console.log('LINK mousedown');
linkmdown = true;
// retrieve positioning properties
var pos = getMousePosition(ev);
dragstart.x = pos.x;
dragstart.y = pos.y;
dragitem = $(this).attr('id');
return(false);
});
$('area[@id^=LINK:]').mousemove(function()
{
if (linkmdown) {
return(false);
}
return(true);
});
$('area[@id^=LINK:]').mouseup(function(ev)
{
// verify if we've moved much since the down - if we did, it's not a click
var pos = getMousePosition(ev);
dragstop.x = pos.x;
dragstop.y = pos.y;
var dx = Math.abs(dragstop.x - dragstart.x);
var dy = Math.abs(dragstop.y - dragstart.y);
if ((dx + dy) < 4) {
// treat this is a click - we're still inside the link, and we didn't move far
alert('click on ' + dragitem);
} else {
dragitem = '';
dragstart = {
x: -1,
y: -1
};
dragstop = {
x: -1,
y: -1
};
if (addedVia) {
$('#newvia').remove();
addedVia = false;
}
}
console.log('LINK mouseup');
linkmdown = false;
});
}
function reapplyDraggableEvents()
{
var origin_x, origin_y;
$('.draggable').unbind();
$('.draggable').mousedown(function(ev)
{
dragmdown = true;
console.log('mousedown');
var pos = getMousePosition(ev);
dragstart.x = pos.x;
dragstart.y = pos.y;
dragitem = $(this).attr('id');
var w = $(this).width();
var h = $(this).height();
var t = parseInt($(this).css('top'), 10);
var l = parseInt($(this).css('left'), 10);
dragoffset.x = -(w / 2);
dragoffset.y = -(h / 2);
return(false);
});
$('.draggable').mousemove(function(ev) { });
$('.draggable').mouseup(function(ev)
{
console.log('mouseup');
dragmdown = false;
// verify if we've moved much since the down - if we did, it's not a click
var pos = getMousePosition(ev);
dragstop.x = pos.x;
dragstop.y = pos.y;
var dx = Math.abs(dragstop.x - dragstart.x);
var dy = Math.abs(dragstop.y - dragstart.y);
if ((dx + dy) < 4) {
// treat this is a click - we're still inside the link, and we didn't move far
alert('click on ' + dragitem);
} else {
if (dragitem.slice(0, 8) === 'mapnode_') {
origin_x = parseInt($('#existingdata').css('left'), 10);
origin_y = parseInt($('#existingdata').css('top'), 10);
dragitem = dragitem.slice(8, dragitem.length);
$.getJSON('editor-backend.php', {
map: mapfile,
cmd: "move_node",
x: pos.x - origin_x,
y: pos.y - origin_y,
nodename: dragitem
}, function() { map_refresh(); });
}
if (dragitem.slice(0, 7) === 'mapvia_') {
origin_x = parseInt($('#existingdata').css('left'), 10);
origin_y = parseInt($('#existingdata').css('top'), 10);
dragitem = dragitem.slice(7, dragitem.length);
$.getJSON('editor-backend.php', {
map: mapfile,
cmd: "move_via",
x: pos.x - origin_x,
y: pos.y - origin_y,
vianame: dragitem
}, function() { map_refresh(); });
}
dragitem = '';
dragstart = {
x: -1,
y: -1
};
dragstop = {
x: -1,
y: -1
};
addedvia = false;
}
return(false);
});
}
function nodeadd()
{
console.log('Node Add - Initial');
interactmode = 'nodeadd';
$('#existingdata').click(function(ev)
{
var x = ev.pageX - parseInt($('#existingdata').css('left'), 10);
var y = ev.pageY - parseInt($('#existingdata').css('top'), 10);
console.log('Ready to add a new node at ' + x + ',' + y);
return(false);
});
}
function linkadd() { console.log('Link Add - Initial'); }
$(document).ready(function()
{
$("body").ajaxStart(function() { $(this).css({
border: 'red 2px solid'
}); });
$("body").ajaxStop(function() { $(this).css({
border: 'none'
}); });
$('#welcome').show();
$('#filepicker').hide();
$('#toolbar').hide();
$('#themap').hide();
$('#busy').hide();
$('#welcome').click(function() {
showpicker(); });
// handle the release, which may not be over the original object anymore
$(document).mouseup(function(ev)
{
if (linkmdown === true) {
console.log("LINK mouseup");
// retrieve positioning properties
var pos = getMousePosition(ev);
dragstop.x = pos.x;
dragstop.y = pos.y;
linkmdown = false;
// $('#log').append('That was a drag. ');
if (addedVia) {
// give the temporary VIA a better name
var now = new Date();
$('#newvia').attr('class', 'deadvia');
$('#newvia').attr('id', 'via_' + now.getTime());
addedVia = false;
console.log("Solidified ephemeral VIA");
reapplyDraggableEvents();
reapplyLinkEvents();
// XXX - do something to tell the editor serverside
var origin_x = parseInt($('#existingdata').css('left'), 10);
var origin_y = parseInt($('#existingdata').css('top'), 10);
var linkname = dragitem.slice(5, dragitem.length);
$.getJSON('editor-backend.php', {
map: mapfile,
cmd: "add_via",
x: pos.x - origin_x,
y: pos.y - origin_y,
linkname: linkname,
startx: dragstart.x,
starty: dragstart.y
}, function() { map_refresh(); });
}
}
});
$(document).mousemove(function(ev)
{
var theItem;
if (linkmdown || dragmdown) {
var pos = getMousePosition(ev);
if (dragmdown) {
theItem = $('#' + dragitem);
}
if (linkmdown) {
theItem = $('#newvia');
if (!addedVia) {
console.log("Created ephemeral VIA");
// we just left the reservation, and we're still dragging.
// - time to create a little marker
$('#nodecontainer').append(
'
');
theItem = $('#newvia');
dragoffset.x = -theItem.width() / 2;
dragoffset.y = -theItem.height() / 2;
addedVia = true;
}
}
var x = pos.x + dragoffset.x;
var y = pos.y + dragoffset.y;
theItem.css({
left: x,
top: y
});
}
});
$('#btn_refresh').click(function() { map_refresh(); });
$('#btn_selectfile').click(function() { showpicker(); });
$('#btn_addnode').click(function() { nodeadd(); });
$('#btn_addlink').click(function() { linkadd(); });
reapplyDraggableEvents();
reapplyLinkEvents();
});