Organize the code.

This commit is contained in:
mdtrooper 2016-04-04 13:53:37 +02:00
parent 989cc2cc92
commit a2cbc4d867
1 changed files with 155 additions and 130 deletions

View File

@ -41,6 +41,7 @@ MapController.prototype._start_flag_multiple_selection = false;
MapController.prototype._flag_multiple_selection = false; MapController.prototype._flag_multiple_selection = false;
MapController.prototype._cache_files = {}; MapController.prototype._cache_files = {};
MapController.prototype._last_event = null; MapController.prototype._last_event = null;
MapController.prototype._keys_pressed = [];
MapController.prototype._last_mouse_position = null; MapController.prototype._last_mouse_position = null;
MapController.prototype._relationship_in_progress = false; MapController.prototype._relationship_in_progress = false;
MapController.prototype._relationship_in_progress_type = null; MapController.prototype._relationship_in_progress_type = null;
@ -1325,28 +1326,17 @@ MapController.prototype.resize_node = function(item, handler, delta_x, delta_y)
self.move_arrow(item["graph_id"]); self.move_arrow(item["graph_id"]);
} }
/** MapController.prototype.key_is_pressed = function(key) {
* Function init_events
* Return boolean
* This function init click events in the map
*/
MapController.prototype.init_events = function() {
var self = this; var self = this;
d3.select("body") if (typeof(self._keys_pressed[key]) == "undefined")
.on("keydown", function() { return false;
// ctrl key else
if (d3.event.keyCode === CONTROL_KEY) { return self._keys_pressed[key];
self._start_flag_multiple_selection = true; }
self.multiple_selection_start();
} MapController.prototype.events_for_nodes = function() {
}) var self = this;
.on("keyup", function() {
if (d3.event.keyCode === CONTROL_KEY) {
self.multiple_selection_end();
self._last_event = "multiple_selection_end";
}
});
var node_menu = [ var node_menu = [
{ {
@ -1395,29 +1385,6 @@ MapController.prototype.init_events = function() {
} }
]; ];
var map_menu = [
{
title: 'Add fictional node',
action: function(elm, d, i) {
self.add_fictional_node();
}
},
{
title: 'Edit map',
action: function(elm, d, i) {
self.editMap(self, elm);
}
},
{
title: 'Save map',
action: function(elm, d, i) {
console.log('Save map!!');
}
}
];
d3.select("#map").on("contextmenu", d3.contextMenu(map_menu));
d3.selectAll(".node") d3.selectAll(".node")
.on("mouseover", function(d) { .on("mouseover", function(d) {
self.select_node(d['graph_id'], "over"); self.select_node(d['graph_id'], "over");
@ -1463,6 +1430,151 @@ MapController.prototype.init_events = function() {
d3.selectAll(".draggable").call(drag); d3.selectAll(".draggable").call(drag);
/**
* Function dragstarted
* Return void
*/
function dragstarted(d) {
if (d3.event.sourceEvent.button == 0) {
d3.event.sourceEvent.stopPropagation();
d3.event.sourceEvent.preventDefault();
}
if (self._relationship_in_progress) {
return;
}
if ($("#node_" + d['graph_id']).hasClass("tooltipstered")) {
$("#node_" + d['graph_id']).tooltipster('destroy');
}
self.remove_resize_square();
var status_selection = self.get_status_selection_node(d['graph_id']);
if (status_selection.indexOf("select") == -1) {
self.remove_selection_nodes();
}
self.select_node(d['graph_id'], "select");
}
/**
* Function dragged
* Return void
*/
function dragged(d) {
var delta_x = d3.event.dx;
var delta_y = d3.event.dy;
self.last_event = "drag";
$.each(nodes, function(i, node) {
if (node.type != ITEM_TYPE_AGENT_NETWORKMAP)
return 1; // Continue
var status_selection =
self.get_status_selection_node(node.graph_id);
if (status_selection.indexOf("select") == -1) {
return 1; // Continue
}
var d3_node = d3.select(self._target + " #node_" + node.graph_id);
var transform = d3.transform(d3_node.attr("transform"));
transform.translate[0] += delta_x;
transform.translate[1] += delta_y;
nodes[i].x = transform.translate[0];
nodes[i].y = transform.translate[1];
d3.select(".minimap #node_" + node.graph_id)
.attr("transform", transform.toString());
d3_node.attr("transform", transform.toString());
self.move_arrow(node.graph_id);
});
}
/**
* Function dragended
* Return void
*/
function dragended(d) {
if (self._last_event != "contextmenu") {
self._last_event = null;
self.select_node(d['graph_id'], "off");
if ($("#node_" + d['graph_id']).hasClass("tooltipstered")) {
$("#node_" + d['graph_id']).tooltipster('destroy');
}
self.remove_resize_square();
}
}
}
/**
* Function init_events
* Return boolean
* This function init click events in the map
*/
MapController.prototype.init_events = function() {
var self = this;
self.events_for_nodes();
d3.select("body")
.on("keydown", function() {
// ctrl key
if (d3.event.keyCode === CONTROL_KEY) {
self._start_flag_multiple_selection = true;
self.multiple_selection_start();
self._keys_pressed[CONTROL_KEY] = true;
}
})
.on("keyup", function() {
if (d3.event.keyCode === CONTROL_KEY) {
self.multiple_selection_end();
self._last_event = "multiple_selection_end";
self._keys_pressed[CONTROL_KEY] = false;
}
});
var map_menu = [
{
title: 'Add fictional node',
action: function(elm, d, i) {
self.add_fictional_node();
}
},
{
title: 'Edit map',
action: function(elm, d, i) {
self.editMap(self, elm);
}
},
{
title: 'Save map',
action: function(elm, d, i) {
console.log('Save map!!');
}
}
];
d3.select("#map").on("contextmenu", d3.contextMenu(map_menu));
d3.select(self._target + " svg").on("mousedown", d3.select(self._target + " svg").on("mousedown",
function() { function() {
if (self._start_flag_multiple_selection) { if (self._start_flag_multiple_selection) {
@ -1565,93 +1677,6 @@ MapController.prototype.init_events = function() {
}); });
} }
}); });
/**
* Function dragstarted
* Return void
*/
function dragstarted(d) {
if (d3.event.sourceEvent.button == 0) {
d3.event.sourceEvent.stopPropagation();
d3.event.sourceEvent.preventDefault();
}
if (self._relationship_in_progress) {
return;
}
if ($("#node_" + d['graph_id']).hasClass("tooltipstered")) {
$("#node_" + d['graph_id']).tooltipster('destroy');
}
self.remove_resize_square();
var status_selection = self.get_status_selection_node(d['graph_id']);
if (status_selection.indexOf("select") == -1) {
self.remove_selection_nodes();
}
self.select_node(d['graph_id'], "select");
}
/**
* Function dragged
* Return void
*/
function dragged(d) {
var delta_x = d3.event.dx;
var delta_y = d3.event.dy;
self.last_event = "drag";
$.each(nodes, function(i, node) {
if (node.type != ITEM_TYPE_AGENT_NETWORKMAP)
return 1; // Continue
var status_selection =
self.get_status_selection_node(node.graph_id);
if (status_selection.indexOf("select") == -1) {
return 1; // Continue
}
var d3_node = d3.select(self._target + " #node_" + node.graph_id);
var transform = d3.transform(d3_node.attr("transform"));
transform.translate[0] += delta_x;
transform.translate[1] += delta_y;
nodes[i].x = transform.translate[0];
nodes[i].y = transform.translate[1];
d3.select(".minimap #node_" + node.graph_id)
.attr("transform", transform.toString());
d3_node.attr("transform", transform.toString());
self.move_arrow(node.graph_id);
});
}
/**
* Function dragended
* Return void
*/
function dragended(d) {
if (self._last_event != "contextmenu") {
self._last_event = null;
self.select_node(d['graph_id'], "off");
if ($("#node_" + d['graph_id']).hasClass("tooltipstered")) {
$("#node_" + d['graph_id']).tooltipster('destroy');
}
self.remove_resize_square();
}
}
} }
/** /**