From 2ffb1b9024d3a04a6a7913192942ceebb196f27d Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 28 Oct 2015 12:59:32 +0100 Subject: refactoring --- derivation-editor.js | 666 +++++++++++++++++++++++++-------------------------- 1 file changed, 333 insertions(+), 333 deletions(-) (limited to 'derivation-editor.js') diff --git a/derivation-editor.js b/derivation-editor.js index 1adf2ac..f01a285 100644 --- a/derivation-editor.js +++ b/derivation-editor.js @@ -1,56 +1,58 @@ /* - * global vars and data + * global vars and configuration * */ -var r, +var DE_paper, // objects - shapes_by_id = {}, - shapes = [], - target_shapes = [], - texts = [], - connections = {}, - id = 0, - next_grid = 0, - // layout - margin = 30, - padding = margin/3, - xbegin = 80, - ybegin = 5, - box_height = 30, - line_margin = 80, - ysource = ybegin, - ytarget = ysource+line_margin; - font_size = 10, - font_width = -1, - stroke_width = 1, - stroke_width_hi = 3, - align_stroke = "#eee", - align_stroke_hi = "#000", - text_att = { "fill": "#000", "stroke": "none", "text-anchor": "start", - "font-size": font_size, "font-family": "Times New Roman" }, - shape_att = { "fill": "#eee", "stroke": "#000", "fill-opacity": 0, - "stroke-width": stroke_width } + DE_shapes_by_id = {}, + DE_shapes = [], + DE_target_shapes = [], + DE_texts = [], + DE_connections = {}, + DE_id = 0, + DE_next_grid = 0, + // ui + DE_ui_margin = 30, + DE_ui_padding = DE_ui_margin/3, + DE_ui_xbegin = 80, + DE_ui_ybegin = 5, + DE_ui_box_height = 30, + DE_ui_line_margin = 80, + DE_ui_ysource = DE_ui_ybegin, + DE_ui_ytarget = DE_ui_ysource+DE_ui_line_margin; + DE_ui_font_size = 10, + DE_ui_font_width = -1, + DE_ui_stroke_width = 1, + DE_ui_stroke_width_hi = 3, + DE_ui_align_stroke = "#eee", + DE_ui_align_stroke_hi = "#000", + DE_ui_text_att = { "fill": "#000", "stroke": "none", + "text-anchor": "start", "font-size": DE_ui_font_size, + "font-family": "Times New Roman" }, + DE_ui_shape_att = { "fill": "#eee", "stroke": "#000", "fill-opacity": 0, + "stroke-width": DE_ui_stroke_width } // dragging - cur_drag = null, - new_pos = -1, - old_pos = -1; + DE_cur_drag = null, + DE_new_pos = -1, + DE_old_pos = -1; // connecting - connect_mode = false, - connect_mode_shape = null, - new_conns = [], + DE_connect_mode = false, + DE_connect_mode_shape = null, + DE_new_conns = [], // editing - cur_ed = null, - cur_ed_shape = null, - edit_mode = false, + DE_cur_ed = null, + DE_cur_ed_shape = null, + DE_edit_mode = false, // removing - rm_shape = null, + DE_rm_shape = null, // data - source = null, - target = null, - align = null; + DE_data_source = null, + DE_data_target = null, + DE_data_align = null; -/* - * connection +/****************************************************************************** + * + * connections/links * */ Raphael.fn.connection = function (obj1, obj2, line, bg) @@ -75,34 +77,37 @@ Raphael.fn.connection = function (obj1, obj2, line, bg) } else { return { bg: bg && bg.split && this.path(path).attr({stroke: bg.split("|")[0], fill: "none", "stroke-width": bg.split("|")[1] || 3}), - line: this.path(path).attr({stroke: align_stroke, fill: "none"}), + line: this.path(path).attr({stroke: DE_ui_align_stroke, fill: "none"}), from: obj1, to: obj2 }; } -}; -var conn_str = function (obj1, obj2) +} + +var DE_conn_str = function (obj1, obj2) { return obj1["id_"]+"-"+obj2["id_"]; } -var make_conn = function(obj1, obj2) + +var DE_make_conn = function(obj1, obj2) { - connections[conn_str(obj1,obj2)] = r.connection(obj1, obj2); - if (connect_mode) { - new_conns.push(connections[conn_str(obj1,obj2)]); - connections[conn_str(obj1,obj2)].line.attr({"stroke":align_stroke_hi,"stroke-width":stroke_width_hi}); + DE_connections[DE_conn_str(obj1,obj2)] = DE_paper.connection(obj1, obj2); + if (DE_connect_mode) { + DE_new_conns.push(DE_connections[DE_conn_str(obj1,obj2)]); + DE_connections[DE_conn_str(obj1,obj2)].line.attr({"stroke":DE_ui_align_stroke_hi,"stroke-width":DE_ui_stroke_width_hi}); } -}, -rm_conn = function(id1, id2) +} + +var DE_rm_conn = function (id1, id2) { var b = false; - for (var i=0; i obj["grid_tmp_"]) { - // right -> left - if (cur_drag.getBBox().width < obj.getBBox().width && - cur_drag.getBBox().x > (obj.getBBox().x+obj.getBBox().width/1000)) { // ignored tolerance, when - return; // dragging onto shapes - } - att = { x: obj.attr("x")+cur_drag.getBBox().width+(margin-2*padding) }; - obj.attr(att); - att = { x: obj.pair.attr("x")+cur_drag.getBBox().width+(margin-2*padding) }; - obj.pair.attr(att); - } else { - // left -> right - if (cur_drag.getBBox().width < obj.getBBox().width && - cur_drag.getBBox().x < (obj.getBBox().x+obj.getBBox().width/1000)) { - return; - } - att = { x: obj.attr("x")-(cur_drag.getBBox().width+(margin-2*padding)) }; - obj.attr(att); - att = { x: obj.pair.attr("x")-(cur_drag.getBBox().width+(margin-2*padding)) }; - obj.pair.attr(att); - } - // grid pos - new_pos = obj["grid_tmp_"]; - var tmp_pos = cur_drag["grid_tmp_"]; - cur_drag["grid_tmp_"] = obj["grid_tmp_"]; - obj["grid_tmp_"] = tmp_pos; -}, -debug = function () { - var s = ""; - for (var i=0; i del_grid) { - target_shapes[i]["grid_"] -= 1; + DE_target_shapes[i]["grid_"] -= 1; } if (g > max) max = g; } - next_grid = g; - if (!next_grid) // empty - next_grid = 0; - cur_drag = null; + DE_next_grid = g; + if (!DE_next_grid) // empty + DE_next_grid = 0; + DE_cur_drag = null; snap_to_grid(true); return; } @@ -264,41 +222,85 @@ up = function () { this.pair.animate({"fill-opacity": 0}, 500); snap_to_grid(true); -}, -snap_to_grid = function (anim=false) +} + +/****************************************************************************** + * + * snap-to-grid + * + */ +var DE_collide = function (obj) +{ + // not a shape + if (!obj["id_"] || obj.type!="rect") + return; + // remove + if (obj["rm_shape_"]) { + DE_cur_drag["delete_me_"] = true; + return; + } + if (DE_cur_drag["grid_tmp_"] > obj["grid_tmp_"]) { + // right -> left + if (DE_cur_drag.getBBox().width < obj.getBBox().width && + DE_cur_drag.getBBox().x > (obj.getBBox().x+obj.getBBox().width/1000)) { // ignored tolerance, when + return; // dragging onto shapes + } + att = { x: obj.attr("x")+DE_cur_drag.getBBox().width+(DE_ui_margin-2*DE_ui_padding) }; + obj.attr(att); + att = { x: obj.pair.attr("x")+DE_cur_drag.getBBox().width+(DE_ui_margin-2*DE_ui_padding) }; + obj.pair.attr(att); + } else { + // left -> right + if (DE_cur_drag.getBBox().width < obj.getBBox().width && + DE_cur_drag.getBBox().x < (obj.getBBox().x+obj.getBBox().width/1000)) { + return; + } + att = { x: obj.attr("x")-(DE_cur_drag.getBBox().width+(DE_ui_margin-2*DE_ui_padding)) }; + obj.attr(att); + att = { x: obj.pair.attr("x")-(DE_cur_drag.getBBox().width+(DE_ui_margin-2*DE_ui_padding)) }; + obj.pair.attr(att); + } + // grid pos + DE_new_pos = obj["grid_tmp_"]; + var tmp_pos = DE_cur_drag["grid_tmp_"]; + DE_cur_drag["grid_tmp_"] = obj["grid_tmp_"]; + obj["grid_tmp_"] = tmp_pos; +} + +var snap_to_grid = function (anim=false) { // just x coord, y is fixed in drag - var d = xbegin; + var d = DE_ui_xbegin; // just target objs - target_shapes.sort(function(a, b) { + DE_target_shapes.sort(function(a, b) { return a["grid_"]-b["grid_"]; }); // switch - if (cur_drag) { // fix glitch when calling from add_obj() and up() - cur_drag["grid_"] = new_pos; - cur_id = cur_drag["id_"]; - if (new_pos > old_pos) { + if (DE_cur_drag) { // fix glitch when calling from DE_add_object() and up() + DE_cur_drag["grid_"] = DE_new_pos; + cur_id = DE_cur_drag["id_"]; + if (DE_new_pos > DE_old_pos) { // left -> right - for (var i=0; i < target_shapes.length; i++) { - pos = target_shapes[i]["grid_"]; - id_ = target_shapes[i]["id_"]; + for (var i=0; i < DE_target_shapes.length; i++) { + pos = DE_target_shapes[i]["grid_"]; + id_ = DE_target_shapes[i]["id_"]; if (id_ == cur_id) continue; - if (pos >= old_pos && pos <= new_pos) { - target_shapes[i]["grid_"] -= 1; + if (pos >= DE_old_pos && pos <= DE_new_pos) { + DE_target_shapes[i]["grid_"] -= 1; } else { continue; } } - } else if (new_pos < old_pos) { + } else if (DE_new_pos < DE_old_pos) { // right -> left - for (var i=0; i < target_shapes.length; i++) { - pos = target_shapes[i]["grid_"]; - id_ = target_shapes[i]["id_"]; + for (var i=0; i < DE_target_shapes.length; i++) { + pos = DE_target_shapes[i]["grid_"]; + id_ = DE_target_shapes[i]["id_"]; if (id_ == cur_id) continue; - if (pos >= new_pos && pos <= old_pos) { - target_shapes[i]["grid_"] += 1; + if (pos >= DE_new_pos && pos <= DE_old_pos) { + DE_target_shapes[i]["grid_"] += 1; } else { continue; } @@ -306,12 +308,12 @@ snap_to_grid = function (anim=false) } } // ^ fix glitch // sort by grid pos - target_shapes.sort(function(a, b) { + DE_target_shapes.sort(function(a, b) { return a["grid_"]-b["grid_"]; }); // fix box layout - for (var i = 0; i < target_shapes.length; i++) { - var obj = target_shapes[i]; + for (var i = 0; i < DE_target_shapes.length; i++) { + var obj = DE_target_shapes[i]; if (!obj || !obj.attrs) { // removed return; } @@ -321,126 +323,137 @@ snap_to_grid = function (anim=false) } else { obj.attr(att); } - att = { x: obj.getBBox().x+padding }; + att = { x: obj.getBBox().x+DE_ui_padding }; if (anim) { obj.pair.animate(att,125); } else { obj.pair.attr(att); } - d += obj.getBBox().width+(margin-2*padding); + d += obj.getBBox().width+(DE_ui_margin-2*DE_ui_padding); // fix tmp grid obj["grid_tmp_"] = obj["grid_"]; } - for (key in connections) { - r.connection(connections[key]); + for (key in DE_connections) { + DE_paper.connection(DE_connections[key]); } -}; +} -/* - * objs +var DE_debug_snap_to_grid = function () { + var s = ""; + for (var i=0; i max) { + for (var i=0; i < DE_shapes.length; i++) { + if (DE_shapes[i]["grid_"] > max) { max_idx = i; - max = shapes[i]["grid_"]; + max = DE_shapes[i]["grid_"]; } } - if (!shapes[max_idx]) { - make_obj(xbegin+padding, "X", "target"); + if (!DE_shapes[max_idx]) { + DE_make_obj(DE_ui_xbegin+DE_ui_padding, "X", "target"); } else { - make_obj(shapes[max_idx].getBBox().x2+(margin-padding), + DE_make_obj(DE_shapes[max_idx].getBBox().x2+(DE_ui_margin-DE_ui_padding), "X", "target"); } - r.setSize(r.width+target_shapes[target_shapes.length-1].getBBox().width+margin, r.height); + DE_paper.setSize(DE_paper.width+DE_target_shapes[DE_target_shapes.length-1].getBBox().width+DE_ui_margin, DE_paper.height); - cur_drag = null; + DE_cur_drag = null; snap_to_grid(true); -}, -make_objs = function (a, type) +} + +var DE_make_objs = function (a, type) { for (var i=0; i < a.length; i++) { var x = 0; if (i == 0) { - x = xbegin+padding; + x = DE_ui_xbegin+DE_ui_padding; } else { - x = margin+texts[texts.length-1].getBBox().x2; + x = DE_ui_margin+DE_texts[DE_texts.length-1].getBBox().x2; } - make_obj(x, a[i], type); + DE_make_obj(x, a[i], type); } }; -/* - * data +/****************************************************************************** + * + * extract data from ui * */ -var extract_data = function () +var DE_extract_data = function () { el = document.getElementById("data"); d = {}; @@ -535,23 +551,23 @@ var extract_data = function () d["align"] = []; // target var ids = []; - target_shapes.sort(function(a, b) { + DE_target_shapes.sort(function(a, b) { return a["grid_"]-b["grid_"]; }); - for (var i=0; i