/* * global vars and data * */ var shapes_by_id = {}, r, curDrag = null, curEd = null, curEdShape = null, shapes = [], target_shapes = [], texts = [], connections = {}, margin = 30, padding = margin/3, xbegin = 80, ybegin = 5, box_height = 50, line_margin = 150, ysource = ybegin, ytarget = ysource+line_margin; font_size = 20, font_width = -1, id = 0, connect_mode = false, connect_mode_shape = null, edit_mode = false, rm_shape = null, 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": 1 } source = ["Das", "ist ein", "kleines", "Haus", "gewesen", "."], // data target = ["This", "has been", "a", "small", "house", "."], // ^ align = [0, 1, 3, 4, 1, 5], new_pos = -1, old_pos = -1; /* * connection * */ Raphael.fn.connection = function (obj1, obj2, line, bg) { if (obj1.line && obj1.from && obj1.to) { line = obj1; obj1 = line.from; obj2 = line.to; } if (!obj1.getBBox() || !obj2.getBBox()) return; var bb1 = obj1.getBBox(), bb2 = obj2.getBBox(), x1 = bb1.x+bb1.width/2, y1 = bb1.y+bb1.height, x2 = bb2.x+bb2.width/2, y2 = bb2.y, path = ["M", x1, y1, "L", x2, y2]; if (line && line.line) { line.bg && line.bg.attr({path: path}); line.line.attr({path: path}); } 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: "#000", fill: "none"}), from: obj1, to: obj2 }; } }; var conn_str = function (obj1, obj2) { return obj1["id_"]+"-"+obj2["id_"]; } var make_conn = function(obj1, obj2) { connections[conn_str(obj1,obj2)] = r.connection(obj1, obj2); }, rm_conn = function(id1, id2) { var b = false; for (var i=0; i obj["grid_tmp_"]) { // right -> left /*if (curDrag.getBBox().width < obj.getBBox().width && curDrag.getBBox().x > (obj.getBBox().x+obj.getBBox().width/3)) { return; }*/ att = { x: obj.attr("x")+curDrag.getBBox().width+(margin-2*padding) }; obj.attr(att); att = { x: obj.pair.attr("x")+curDrag.getBBox().width+(margin-2*padding) }; obj.pair.attr(att); } else { // left -> right /*if (curDrag.getBBox().width < obj.getBBox().width && curDrag.getBBox().x < (obj.getBBox().x+obj.getBBox().width/3)) { return; }*/ att = { x: obj.attr("x")-(curDrag.getBBox().width+(margin-2*padding)) }; obj.attr(att); att = { x: obj.pair.attr("x")-(curDrag.getBBox().width+(margin-2*padding)) }; obj.pair.attr(att); } // switch grid pos new_pos = obj["grid_tmp_"]; var tmpx = curDrag["grid_tmp_"]; curDrag["grid_tmp_"] = obj["grid_tmp_"]; obj["grid_tmp_"] = tmpx; //snap_to_grid(true,true); }, debug = function () { var s = ""; for (var i=0; iold_pos)) { target_shapes[i]["grid_"] += 1; } else if (b && new_pos old_pos) { for (var i=0; i < target_shapes.length; i++) { pos = target_shapes[i]["grid_"]; id = target_shapes[i]["id_"]; if (id == cur_id) continue; if (pos >= old_pos && pos <= new_pos) { target_shapes[i]["grid_"] -= 1; } else { continue; } } } else if (new_pos < old_pos) { for (var i=0; i < target_shapes.length; i++) { pos = target_shapes[i]["grid_"]; id = target_shapes[i]["id_"]; if (id == cur_id) continue; if (pos >= new_pos && pos <= old_pos) { target_shapes[i]["grid_"] += 1; } else { continue; } } } debug1(); target_shapes.sort(function(a, b) { return a["grid_"]-b["grid_"]; }); for (var i = 0; i < target_shapes.length; i++) { var obj = target_shapes[i]; if (!obj || !obj.attrs) { // removed return; } att = { x:d }; if (anim) { obj.attr(att); } else { obj.attr(att); } att = { x: obj.getBBox().x+padding }; if (anim) { obj.pair.animate(att,125); } else { obj.pair.attr(att); } d += obj.getBBox().width+(margin-2*padding); } for (key in connections) { r.connection(connections[key]); } }; /* * objs * */ var make_obj = function(x, text, type) { var y; if (type == "source") { y = ysource; } else if (type == "target") { y = ytarget; } // make text obj texts.push(r.text(x, y+(box_height/2), text).attr(text_att)); // make shape obj var x_shape = texts[texts.length-1].getBBox().x-padding, x_width = texts[texts.length-1].getBBox().width+(2*padding); shapes.push(r.rect(x_shape, y, x_width, box_height, 5).attr(shape_att)); tx = texts[texts.length-1]; sh = shapes[shapes.length-1]; // fix z-index tx.toBack(); sh.toFront(); // pair text/shape tx.pair = shapes[shapes.length-1]; sh.pair = texts[texts.length-1]; // meta sh["type_"] = type; sh["id_"] = id; shapes_by_id[id] = sh; if (type == "target") { sh.drag(move, dragger, up).onDragOver( function(obj) { collide(obj); }) sh.attr({ cursor: "move" }); tx.drag(move, dragger, up); sh["grid_"] = id; sh["grid_tmp_"] = id; sh.click(function() { if (connect_mode) { if (connections[conn_str(connect_mode_shape,this)]) { rm_conn(connect_mode_shape["id_"], this["id_"]); } else { make_conn(connect_mode_shape, this); } connect_mode_shape.attr({"fill-opacity": 0}); connect_mode = false; connect_mode_shape = null; } }); target_shapes.push(sh); // inline text editing r.inlineTextEditing(tx); sh.dblclick(function(){ if (edit_mode) return; edit_mode = true; this.pair.toFront(); curEd = this.pair; curEdShape = this; var input = curEd.inlineTextEditing.startEditing(); input.addEventListener("keypress", function(e) { if (e.keyCode==27||e.keyCode==37||e.keyCode==38||e.keyCode==39||e.keyCode==40) { // esc, arrows, backspace return; } else if (e.keyCode == 8) { // backspace curEdShape.animate({width:curEdShape.getBBox().width-font_width},125); setTimeout(function(){snap_to_grid(true);},125); } else if (e.keyCode == 13) { // enter e.preventDefault(); curEd.inlineTextEditing.stopEditing(); curEdShape.toFront(); curEd.toBack(); curEdShape.animate({width:curEd.getBBox().width+(margin-padding)},125); setTimeout(function(){snap_to_grid(true);},125); edit_mode = false; } else { curEdShape.animate({width:(this.value.length*font_width)+2*font_width+2*padding},25); setTimeout(function(){ snap_to_grid(true); r.setSize(r.width+font_width, r.height); },25); } }); input.addEventListener("blur", function(e) { curEd.inlineTextEditing.stopEditing(); curEdShape.toFront(); curEd.toBack(); curEdShape.animate({width:curEd.getBBox().width+(margin-padding)},125); setTimeout(function(){snap_to_grid(true);},125); edit_mode = false; }, true); }); } else if (type == "source") { sh.click(function() { if (connect_mode) { if (this != connect_mode_shape) return; this.animate({"fill-opacity": 0}, 250); connect_mode = false; connect_mode_shape = null; } else { this.animate({"fill-opacity": .5}, 250); connect_mode = true; connect_mode_shape = this; } }); } id++; }, add_obj = function() { var max=0, max_idx=-1; for (var i=0; i < shapes.length; i++) { if (shapes[i]["grid_"] > max) { max_idx = i; max = shapes[i]["grid_"]; } } if (!shapes[max_idx]) { make_obj(xbegin+padding, "X", "target", 0); } else { make_obj(shapes[max_idx].getBBox().x2+(margin-padding), "NEW", "target", max+1); } r.setSize(r.width+target_shapes[target_shapes.length-1].getBBox().width+margin, r.height); snap_to_grid(true); }, make_objs = function (a, type) { for (var i=0; i < a.length; i++) { var x = 0; if (i == 0) { x = xbegin+padding; } else { x = margin+texts[texts.length-1].getBBox().x2; } make_obj(x, a[i], type); } }; /* * data * */ var extract_data = function () { el = document.getElementById("data"); d = {}; d["source"] = []; d["target"] = []; d["align"] = []; // target var ids = []; target_shapes.sort(function(a, b) { return a["grid_"]-b["grid_"]; }); for (var i=0; i