Commit c57314a3 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT, FIX: change the way listeners are assigned

fixes https://git.goltzsche.net/caosdb/customers/glaz_awi/ext-awi/issues/32

Previously, drop listeners could be assigned multiple times. This under
some circumstances lead to the corresponding function be called multiple
times on one drop which resulted in the above mentioned bug.

Now, the named functions are directly supplied to addEventListener.
Mulitple calls no longer occur.

At the same time I made listeners uniform, i.e. all are part of the
edit_mode namespace and all are provided directly (without passing it to
e.g. set_entity_dropable)
parent 48a5ff3b
......@@ -41,6 +41,8 @@ var edit_mode = new function() {
}
}
// TODO: do all these listener need to be part of edit_mode or can they be
// declared with "var"
this.scroll_edit_panel = function(e) {
$('.caosdb-f-edit').css("top", document.documentElement.scrollTop);
}
......@@ -59,6 +61,22 @@ var edit_mode = new function() {
edit_mode.highlight(this);
}
this.property_drop_listener = function(e) {
e.preventDefault();
console.log("inn drop listener")
var entity = $(this).parent();
edit_mode.app.startEdit(entity);
edit_mode.add_dropped_property(e, edit_mode.app.entity);
}
// Define listeners for dropping record types or properties on existing entites.
this.parent_drop_listener = function(e) {
e.preventDefault();
var entity = $(this).parent();
app.startEdit(entity);
edit_mode.add_dropped_parent(e, app.entity);
}
this.add_new_property = function(entity, new_prop) {
var rt = entity.getElementsByClassName("caosdb-properties")[0];
rt.appendChild(new_prop);
......@@ -107,34 +125,35 @@ var edit_mode = new function() {
}
}
this.set_entity_dropable = function(entity, dragover, dragleave, parent_drop, property_drop) {
this.set_entity_dropable = function(entity) {
var rts = entity.getElementsByClassName("caosdb-entity-panel-body");
for (var rel of rts) {
rel.addEventListener("dragleave", dragleave);
rel.addEventListener("dragover", dragover);
rel.addEventListener("drop", property_drop);
rel.addEventListener("dragleave", edit_mode.prop_dragleave);
rel.addEventListener("dragover", edit_mode.prop_dragover);
rel.addEventListener("drop", edit_mode.property_drop_listener);
}
var headings = entity.getElementsByClassName("caosdb-entity-panel-heading");
for (var rel of headings) {
rel.addEventListener("dragleave", dragleave);
rel.addEventListener("dragover", dragover);
rel.addEventListener("drop", parent_drop);
rel.addEventListener("dragleave", edit_mode.prop_dragleave);
rel.addEventListener("dragover", edit_mode.prop_dragover);
rel.addEventListener("drop", edit_mode.parent_drop_listener);
}
}
this.unset_entity_dropable = function(entity, dragover, dragleave, parent_drop, property_drop) {
this.unset_entity_dropable = function(entity) {
var rts = entity.getElementsByClassName("caosdb-entity-panel-body");
for (var rel of rts) {
rel.removeEventListener("dragleave", dragleave);
rel.removeEventListener("dragover", dragover);
rel.removeEventListener("drop", property_drop);
rel.removeEventListener("dragleave", edit_mode.prop_dragleave);
rel.removeEventListener("dragover", edit_mode.prop_dragover);
rel.removeEventListener("drop", edit_mode.property_drop_listener);
}
var headings = entity.getElementsByClassName("caosdb-entity-panel-heading");
for (var rel of headings) {
rel.removeEventListener("dragleave", dragleave);
rel.removeEventListener("dragover", dragover);
rel.removeEventListener("drop", parent_drop);
rel.removeEventListener("dragleave", edit_mode.prop_dragleave);
rel.removeEventListener("dragover", edit_mode.prop_dragover);
rel.removeEventListener("drop", edit_mode.parent_drop_listener);
}
}
......@@ -548,8 +567,6 @@ var edit_mode = new function() {
}
}
/*
* Initialize the edit mode and create a state machine.
*/
......@@ -619,19 +636,6 @@ var edit_mode = new function() {
}, edit_mode.handle_error);
});
// Define listeners for dropping record types or properties on existing entites.
var parent_drop_listener = function(e) {
e.preventDefault();
var entity = $(this).parent();
app.startEdit(entity);
edit_mode.add_dropped_parent(e, app.entity);
}
var property_drop_listener = function(e) {
e.preventDefault();
var entity = $(this).parent();
app.startEdit(entity);
edit_mode.add_dropped_property(e, app.entity);
}
// Define the error handler for the state machine.
app.errorHandler = function(fn) {
......@@ -649,7 +653,7 @@ var edit_mode = new function() {
new_buttons.attr("disabled", false);
$('.caosdb-entity-panel').each(function(index) {
let entity = this;
edit_mode.set_entity_dropable(entity, edit_mode.prop_dragover, edit_mode.prop_dragleave, parent_drop_listener, property_drop_listener);
edit_mode.set_entity_dropable(entity);
if (typeof getEntityID(entity) == "undefined" || getEntityID(entity) == '') {
// no id -> insert
edit_mode.add_start_edit_button(entity, () => {
......@@ -667,7 +671,7 @@ var edit_mode = new function() {
app.entity = entities[0];
if (edit_mode.has_errors(app.entity)) {
hintMessages.hintMessages(app.entity);
edit_mode.set_entity_dropable(app.entity, edit_mode.prop_dragover, edit_mode.prop_dragleave, parent_drop_listener, property_drop_listener);
edit_mode.set_entity_dropable(app.entity);
edit_mode.init_actions_panels();
// only add edit button, because a deletion would fail again
edit_mode.add_start_edit_button(app.entity, () => {
......@@ -709,7 +713,7 @@ var edit_mode = new function() {
app.errorHandler(() => {
// remove event listeners, add the save button an so on
$('.caosdb-entity-panel').each(function(index) {
edit_mode.unset_entity_dropable(this, edit_mode.prop_dragover, edit_mode.prop_dragleave, parent_drop_listener, property_drop_listener);
edit_mode.unset_entity_dropable(this);
edit_mode.remove_start_edit_button(this);
edit_mode.remove_new_record_button(this);
edit_mode.remove_delete_button(this);
......@@ -823,7 +827,7 @@ var edit_mode = new function() {
app.waiting = createWaitingNotification("Please wait.");
$(app.waiting).hide();
app.init();
// We need some refactoring: there ist the variable editApp which is
// TODO: We need some refactoring: there ist the variable editApp which is
// set with this return value. I think app is a bad name as it contains
// the state machine. Alos, the state machine should be either passed
// around or be global (edit_mode.app).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment