2018-05-16 00:53:27 +00:00
|
|
|
/* eslint-disable no-var */
|
|
|
|
/* globals svgEditor, svgCanvas, $ */
|
2010-02-08 21:26:54 +00:00
|
|
|
/*
|
|
|
|
* ext-connector.js
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-02-08 21:26:54 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
*
|
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
svgEditor.addExtension('Connector', function (S) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var svgcontent = S.svgcontent,
|
|
|
|
svgroot = S.svgroot,
|
|
|
|
getNextId = S.getNextId,
|
|
|
|
getElem = S.getElem,
|
|
|
|
addElem = S.addSvgElementFromJson,
|
|
|
|
selManager = S.selectorManager,
|
2010-03-23 01:27:20 +00:00
|
|
|
curConfig = svgEditor.curConfig,
|
2010-03-11 21:07:13 +00:00
|
|
|
started = false,
|
2018-05-16 00:53:27 +00:00
|
|
|
startX,
|
|
|
|
startY,
|
|
|
|
curLine,
|
|
|
|
startElem,
|
|
|
|
endElem,
|
2010-03-11 21:07:13 +00:00
|
|
|
connections = [],
|
2018-05-16 00:53:27 +00:00
|
|
|
connSel = '.se_connector',
|
|
|
|
seNs,
|
|
|
|
// connect_str = '-SE_CONNECT-',
|
2014-02-12 09:38:38 +00:00
|
|
|
selElems = [],
|
|
|
|
elData = $.data;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
var langList = {
|
|
|
|
'en': [
|
|
|
|
{'id': 'mode_connect', 'title': 'Connect two objects'}
|
2010-03-11 21:07:13 +00:00
|
|
|
],
|
2018-05-16 00:53:27 +00:00
|
|
|
'fr': [
|
|
|
|
{'id': 'mode_connect', 'title': 'Connecter deux objets'}
|
2010-03-11 21:07:13 +00:00
|
|
|
]
|
|
|
|
};
|
2014-02-12 09:38:38 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
function getBBintersect (x, y, bb, offset) {
|
|
|
|
if (offset) {
|
2014-02-12 09:38:38 +00:00
|
|
|
offset -= 0;
|
|
|
|
bb = $.extend({}, bb);
|
|
|
|
bb.width += offset;
|
|
|
|
bb.height += offset;
|
2018-05-16 00:53:27 +00:00
|
|
|
bb.x -= offset / 2;
|
|
|
|
bb.y -= offset / 2;
|
2014-02-12 09:38:38 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
var midX = bb.x + bb.width / 2;
|
|
|
|
var midY = bb.y + bb.height / 2;
|
|
|
|
var lenX = x - midX;
|
|
|
|
var lenY = y - midY;
|
|
|
|
|
|
|
|
var slope = Math.abs(lenY / lenX);
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
var ratio;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
if (slope < bb.height / bb.width) {
|
|
|
|
ratio = (bb.width / 2) / Math.abs(lenX);
|
2014-02-12 09:38:38 +00:00
|
|
|
} else {
|
2018-05-16 00:53:27 +00:00
|
|
|
ratio = (bb.height / 2) / Math.abs(lenY);
|
2014-02-12 09:38:38 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
return {
|
2018-05-16 00:53:27 +00:00
|
|
|
x: midX + lenX * ratio,
|
|
|
|
y: midY + lenY * ratio
|
2014-02-12 09:38:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
function getOffset (side, line) {
|
|
|
|
var giveOffset = !!line.getAttribute('marker-' + side);
|
|
|
|
// var giveOffset = $(line).data(side+'_off');
|
2010-06-17 17:57:24 +00:00
|
|
|
|
|
|
|
// TODO: Make this number (5) be based on marker width/height
|
|
|
|
var size = line.getAttribute('stroke-width') * 5;
|
2018-05-16 00:53:27 +00:00
|
|
|
return giveOffset ? size : 0;
|
2010-06-17 17:57:24 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
function showPanel (on) {
|
|
|
|
var connRules = $('#connector_rules');
|
|
|
|
if (!connRules.length) {
|
|
|
|
connRules = $('<style id="connector_rules"></style>').appendTo('head');
|
|
|
|
}
|
|
|
|
connRules.text(!on ? '' : '#tool_clone, #tool_topath, #tool_angle, #xy_panel { display: none !important; }');
|
2010-03-11 21:07:13 +00:00
|
|
|
$('#connector_panel').toggle(on);
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
function setPoint (elem, pos, x, y, setMid) {
|
2014-02-12 09:38:38 +00:00
|
|
|
var i, pts = elem.points;
|
2010-03-11 21:07:13 +00:00
|
|
|
var pt = svgroot.createSVGPoint();
|
|
|
|
pt.x = x;
|
|
|
|
pt.y = y;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (pos === 'end') { pos = pts.numberOfItems - 1; }
|
2010-03-11 21:07:13 +00:00
|
|
|
// TODO: Test for this on init, then use alt only if needed
|
|
|
|
try {
|
|
|
|
pts.replaceItem(pt, pos);
|
2018-05-16 00:53:27 +00:00
|
|
|
} catch (err) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Should only occur in FF which formats points attr as "n,n n,n", so just split
|
2018-05-16 00:53:27 +00:00
|
|
|
var ptArr = elem.getAttribute('points').split(' ');
|
|
|
|
for (i = 0; i < ptArr.length; i++) {
|
2014-02-12 09:38:38 +00:00
|
|
|
if (i === pos) {
|
2018-05-16 00:53:27 +00:00
|
|
|
ptArr[i] = x + ',' + y;
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
elem.setAttribute('points', ptArr.join(' '));
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
if (setMid) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Add center point
|
2018-05-16 00:53:27 +00:00
|
|
|
var ptStart = pts.getItem(0);
|
|
|
|
var ptEnd = pts.getItem(pts.numberOfItems - 1);
|
|
|
|
setPoint(elem, 1, (ptEnd.x + ptStart.x) / 2, (ptEnd.y + ptStart.y) / 2);
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
function updateLine (diffX, diffY) {
|
2010-06-17 17:57:24 +00:00
|
|
|
// Update line with element
|
|
|
|
var i = connections.length;
|
2018-05-16 00:53:27 +00:00
|
|
|
while (i--) {
|
2010-06-17 17:57:24 +00:00
|
|
|
var conn = connections[i];
|
|
|
|
var line = conn.connector;
|
2018-05-16 00:53:27 +00:00
|
|
|
// var elem = conn.elem;
|
|
|
|
|
|
|
|
var pre = conn.is_start ? 'start' : 'end';
|
|
|
|
// var sw = line.getAttribute('stroke-width') * 5;
|
|
|
|
|
2010-06-17 17:57:24 +00:00
|
|
|
// Update bbox for this element
|
2018-05-16 00:53:27 +00:00
|
|
|
var bb = elData(line, pre + '_bb');
|
|
|
|
bb.x = conn.start_x + diffX;
|
|
|
|
bb.y = conn.start_y + diffY;
|
|
|
|
elData(line, pre + '_bb', bb);
|
|
|
|
|
|
|
|
var altPre = conn.is_start ? 'end' : 'start';
|
|
|
|
|
2010-06-17 17:57:24 +00:00
|
|
|
// Get center pt of connected element
|
2018-05-16 00:53:27 +00:00
|
|
|
var bb2 = elData(line, altPre + '_bb');
|
|
|
|
var srcX = bb2.x + bb2.width / 2;
|
|
|
|
var srcY = bb2.y + bb2.height / 2;
|
|
|
|
|
2010-06-17 17:57:24 +00:00
|
|
|
// Set point of element being moved
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt = getBBintersect(srcX, srcY, bb, getOffset(pre, line)); // $(line).data(pre+'_off')?sw:0
|
2014-02-12 09:38:38 +00:00
|
|
|
setPoint(line, conn.is_start ? 0 : 'end', pt.x, pt.y, true);
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-06-17 17:57:24 +00:00
|
|
|
// Set point of connected element
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt2 = getBBintersect(pt.x, pt.y, elData(line, altPre + '_bb'), getOffset(altPre, line));
|
2014-02-12 09:38:38 +00:00
|
|
|
setPoint(line, conn.is_start ? 'end' : 0, pt2.x, pt2.y, true);
|
2010-06-17 17:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
function findConnectors (elems) {
|
2014-02-12 09:38:38 +00:00
|
|
|
var i;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!elems) { elems = selElems; }
|
|
|
|
var connectors = $(svgcontent).find(connSel);
|
2010-03-11 21:07:13 +00:00
|
|
|
connections = [];
|
2010-02-08 21:26:54 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Loop through connectors to see if one is connected to the element
|
2018-05-16 00:53:27 +00:00
|
|
|
connectors.each(function () {
|
|
|
|
var addThis;
|
2014-02-12 09:38:38 +00:00
|
|
|
function add () {
|
|
|
|
if ($.inArray(this, elems) !== -1) {
|
|
|
|
// Pretend this element is selected
|
2018-05-16 00:53:27 +00:00
|
|
|
addThis = true;
|
2014-02-12 09:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 12:37:49 +00:00
|
|
|
|
|
|
|
// Grab the ends
|
|
|
|
var parts = [];
|
|
|
|
['start', 'end'].forEach(function (pos, i) {
|
2018-01-22 09:08:40 +00:00
|
|
|
var key = 'c_' + pos;
|
|
|
|
var part = elData(this, key);
|
2018-05-16 00:53:27 +00:00
|
|
|
if (part == null) {
|
2018-01-18 12:37:49 +00:00
|
|
|
part = document.getElementById(
|
2018-01-19 15:08:35 +00:00
|
|
|
this.attributes['se:connector'].value.split(' ')[i]
|
2018-01-18 12:37:49 +00:00
|
|
|
);
|
2018-05-16 00:53:27 +00:00
|
|
|
elData(this, 'c_' + pos, part.id);
|
|
|
|
elData(this, pos + '_bb', svgCanvas.getStrokedBBox([part]));
|
2018-01-22 09:01:59 +00:00
|
|
|
} else part = document.getElementById(part);
|
2018-01-18 12:37:49 +00:00
|
|
|
parts.push(part);
|
2018-01-19 15:08:35 +00:00
|
|
|
}.bind(this));
|
2018-01-18 12:37:49 +00:00
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var cElem = parts[i];
|
2018-01-18 12:37:49 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
addThis = false;
|
2010-03-11 21:07:13 +00:00
|
|
|
// The connected element might be part of a selected group
|
2018-05-16 00:53:27 +00:00
|
|
|
$(cElem).parents().each(add);
|
|
|
|
|
|
|
|
if (!cElem || !cElem.parentNode) {
|
2010-03-11 21:07:13 +00:00
|
|
|
$(this).remove();
|
|
|
|
continue;
|
2010-02-18 15:42:10 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
if ($.inArray(cElem, elems) !== -1 || addThis) {
|
|
|
|
var bb = svgCanvas.getStrokedBBox([cElem]);
|
2010-03-11 21:07:13 +00:00
|
|
|
connections.push({
|
2018-05-16 00:53:27 +00:00
|
|
|
elem: cElem,
|
2010-03-11 21:07:13 +00:00
|
|
|
connector: this,
|
|
|
|
is_start: (i === 0),
|
|
|
|
start_x: bb.x,
|
|
|
|
start_y: bb.y
|
2018-05-16 00:53:27 +00:00
|
|
|
});
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
function updateConnectors (elems) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Updates connector lines based on selected elements
|
|
|
|
// Is not used on mousemove, as it runs getStrokedBBox every time,
|
|
|
|
// which isn't necessary there.
|
2014-02-12 09:38:38 +00:00
|
|
|
var i, j;
|
2010-06-17 17:57:24 +00:00
|
|
|
findConnectors(elems);
|
2014-02-12 09:38:38 +00:00
|
|
|
if (connections.length) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Update line with element
|
2014-02-12 09:38:38 +00:00
|
|
|
i = connections.length;
|
|
|
|
while (i--) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var conn = connections[i];
|
|
|
|
var line = conn.connector;
|
|
|
|
var elem = conn.elem;
|
2010-02-08 21:26:54 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
// var sw = line.getAttribute('stroke-width') * 5;
|
|
|
|
var pre = conn.is_start ? 'start' : 'end';
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Update bbox for this element
|
|
|
|
var bb = svgCanvas.getStrokedBBox([elem]);
|
|
|
|
bb.x = conn.start_x;
|
|
|
|
bb.y = conn.start_y;
|
2018-05-16 00:53:27 +00:00
|
|
|
elData(line, pre + '_bb', bb);
|
|
|
|
/* var addOffset = */ elData(line, pre + '_off');
|
|
|
|
|
|
|
|
var altPre = conn.is_start ? 'end' : 'start';
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Get center pt of connected element
|
2018-05-16 00:53:27 +00:00
|
|
|
var bb2 = elData(line, altPre + '_bb');
|
|
|
|
var srcX = bb2.x + bb2.width / 2;
|
|
|
|
var srcY = bb2.y + bb2.height / 2;
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Set point of element being moved
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt = getBBintersect(srcX, srcY, bb, getOffset(pre, line));
|
2014-02-12 09:38:38 +00:00
|
|
|
setPoint(line, conn.is_start ? 0 : 'end', pt.x, pt.y, true);
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Set point of connected element
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt2 = getBBintersect(pt.x, pt.y, elData(line, altPre + '_bb'), getOffset(altPre, line));
|
2014-02-12 09:38:38 +00:00
|
|
|
setPoint(line, conn.is_start ? 'end' : 0, pt2.x, pt2.y, true);
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Update points attribute manually for webkit
|
2014-02-12 09:38:38 +00:00
|
|
|
if (navigator.userAgent.indexOf('AppleWebKit') !== -1) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var pts = line.points;
|
|
|
|
var len = pts.numberOfItems;
|
2018-05-16 00:53:27 +00:00
|
|
|
var ptArr = [];
|
2014-02-12 09:38:38 +00:00
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
pt = pts.getItem(j);
|
2018-05-16 00:53:27 +00:00
|
|
|
ptArr[j] = pt.x + ',' + pt.y;
|
2014-02-12 09:38:38 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
line.setAttribute('points', ptArr.join(' '));
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Do once
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
2010-03-11 21:07:13 +00:00
|
|
|
var gse = svgCanvas.groupSelectedElements;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
svgCanvas.groupSelectedElements = function () {
|
|
|
|
svgCanvas.removeFromSelection($(connSel).toArray());
|
2010-12-01 17:54:11 +00:00
|
|
|
return gse.apply(this, arguments);
|
2014-02-12 09:38:38 +00:00
|
|
|
};
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
var mse = svgCanvas.moveSelectedElements;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
svgCanvas.moveSelectedElements = function () {
|
2010-08-17 18:09:50 +00:00
|
|
|
var cmd = mse.apply(this, arguments);
|
2010-03-11 21:07:13 +00:00
|
|
|
updateConnectors();
|
2010-08-17 18:09:50 +00:00
|
|
|
return cmd;
|
2014-02-12 09:38:38 +00:00
|
|
|
};
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
seNs = svgCanvas.getEditorNS();
|
2010-03-11 21:07:13 +00:00
|
|
|
}());
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Do on reset
|
2018-05-16 00:53:27 +00:00
|
|
|
function init () {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Make sure all connectors have data set
|
2018-05-16 00:53:27 +00:00
|
|
|
$(svgcontent).find('*').each(function () {
|
|
|
|
var conn = this.getAttributeNS(seNs, 'connector');
|
|
|
|
if (conn) {
|
|
|
|
this.setAttribute('class', connSel.substr(1));
|
|
|
|
var connData = conn.split(' ');
|
|
|
|
var sbb = svgCanvas.getStrokedBBox([getElem(connData[0])]);
|
|
|
|
var ebb = svgCanvas.getStrokedBBox([getElem(connData[1])]);
|
|
|
|
$(this).data('c_start', connData[0])
|
|
|
|
.data('c_end', connData[1])
|
2010-03-11 21:07:13 +00:00
|
|
|
.data('start_bb', sbb)
|
|
|
|
.data('end_bb', ebb);
|
|
|
|
svgCanvas.getEditorNS(true);
|
|
|
|
}
|
|
|
|
});
|
2018-05-16 00:53:27 +00:00
|
|
|
// updateConnectors();
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2014-02-12 09:38:38 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
// $(svgroot).parent().mousemove(function (e) {
|
|
|
|
// // if (started
|
|
|
|
// // || svgCanvas.getMode() !== 'connector'
|
|
|
|
// // || e.target.parentNode.parentNode !== svgcontent) return;
|
|
|
|
//
|
|
|
|
// console.log('y')
|
|
|
|
// // if (e.target.parentNode.parentNode === svgcontent) {
|
|
|
|
// //
|
|
|
|
// // }
|
|
|
|
// });
|
2014-02-12 09:38:38 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
return {
|
2018-05-16 00:53:27 +00:00
|
|
|
name: 'Connector',
|
|
|
|
svgicons: svgEditor.curConfig.imgPath + 'conn.svg',
|
2010-03-11 21:07:13 +00:00
|
|
|
buttons: [{
|
2018-05-16 00:53:27 +00:00
|
|
|
id: 'mode_connect',
|
|
|
|
type: 'mode',
|
|
|
|
icon: svgEditor.curConfig.imgPath + 'cut.png',
|
|
|
|
title: 'Connect two objects',
|
2010-03-11 21:07:13 +00:00
|
|
|
includeWith: {
|
|
|
|
button: '#tool_line',
|
|
|
|
isDefault: false,
|
|
|
|
position: 1
|
2010-02-08 21:26:54 +00:00
|
|
|
},
|
2010-03-11 21:07:13 +00:00
|
|
|
events: {
|
2018-05-16 00:53:27 +00:00
|
|
|
'click': function () {
|
|
|
|
svgCanvas.setMode('connector');
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}],
|
2018-05-16 00:53:27 +00:00
|
|
|
addLangData: function (lang) {
|
2010-03-11 21:07:13 +00:00
|
|
|
return {
|
2018-05-16 00:53:27 +00:00
|
|
|
data: langList[lang]
|
2010-03-11 21:07:13 +00:00
|
|
|
};
|
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
mouseDown: function (opts) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var e = opts.event;
|
2018-05-16 00:53:27 +00:00
|
|
|
startX = opts.start_x;
|
|
|
|
startY = opts.start_y;
|
2010-03-11 21:07:13 +00:00
|
|
|
var mode = svgCanvas.getMode();
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
if (mode === 'connector') {
|
|
|
|
if (started) { return; }
|
|
|
|
|
|
|
|
var mouseTarget = e.target;
|
|
|
|
|
|
|
|
var parents = $(mouseTarget).parents();
|
|
|
|
|
|
|
|
if ($.inArray(svgcontent, parents) !== -1) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Connectable element
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// If child of foreignObject, use parent
|
2018-05-16 00:53:27 +00:00
|
|
|
var fo = $(mouseTarget).closest('foreignObject');
|
|
|
|
startElem = fo.length ? fo[0] : mouseTarget;
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Get center of source element
|
2018-05-16 00:53:27 +00:00
|
|
|
var bb = svgCanvas.getStrokedBBox([startElem]);
|
|
|
|
var x = bb.x + bb.width / 2;
|
|
|
|
var y = bb.y + bb.height / 2;
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
started = true;
|
2018-05-16 00:53:27 +00:00
|
|
|
curLine = addElem({
|
|
|
|
'element': 'polyline',
|
|
|
|
'attr': {
|
|
|
|
'id': getNextId(),
|
|
|
|
'points': (x + ',' + y + ' ' + x + ',' + y + ' ' + startX + ',' + startY),
|
|
|
|
'stroke': '#' + curConfig.initStroke.color,
|
|
|
|
'stroke-width': (!startElem.stroke_width || startElem.stroke_width === 0) ? curConfig.initStroke.width : startElem.stroke_width,
|
|
|
|
'fill': 'none',
|
|
|
|
'opacity': curConfig.initStroke.opacity,
|
|
|
|
'style': 'pointer-events:none'
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-16 00:53:27 +00:00
|
|
|
elData(curLine, 'start_bb', bb);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
return {
|
|
|
|
started: true
|
|
|
|
};
|
2014-02-12 09:38:38 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
if (mode === 'select') {
|
2010-03-11 21:07:13 +00:00
|
|
|
findConnectors();
|
|
|
|
}
|
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
mouseMove: function (opts) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var zoom = svgCanvas.getZoom();
|
2018-05-16 00:53:27 +00:00
|
|
|
// var e = opts.event;
|
|
|
|
var x = opts.mouse_x / zoom;
|
|
|
|
var y = opts.mouse_y / zoom;
|
|
|
|
|
|
|
|
var diffX = x - startX,
|
|
|
|
diffY = y - startY;
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
var mode = svgCanvas.getMode();
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
if (mode === 'connector' && started) {
|
|
|
|
// var sw = curLine.getAttribute('stroke-width') * 3;
|
2010-03-11 21:07:13 +00:00
|
|
|
// Set start point (adjusts based on bb)
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt = getBBintersect(x, y, elData(curLine, 'start_bb'), getOffset('start', curLine));
|
|
|
|
startX = pt.x;
|
|
|
|
startY = pt.y;
|
|
|
|
|
|
|
|
setPoint(curLine, 0, pt.x, pt.y, true);
|
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Set end point
|
2018-05-16 00:53:27 +00:00
|
|
|
setPoint(curLine, 'end', x, y, true);
|
|
|
|
} else if (mode === 'select') {
|
2010-03-11 21:07:13 +00:00
|
|
|
var slen = selElems.length;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
while (slen--) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var elem = selElems[slen];
|
|
|
|
// Look for selected connector elements
|
2018-05-16 00:53:27 +00:00
|
|
|
if (elem && elData(elem, 'c_start')) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Remove the "translate" transform given to move
|
|
|
|
svgCanvas.removeFromSelection([elem]);
|
|
|
|
svgCanvas.getTransformList(elem).clear();
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
if (connections.length) {
|
|
|
|
updateLine(diffX, diffY);
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
mouseUp: function (opts) {
|
|
|
|
// var zoom = svgCanvas.getZoom();
|
2010-03-11 21:07:13 +00:00
|
|
|
var e = opts.event,
|
2018-05-16 00:53:27 +00:00
|
|
|
// x = opts.mouse_x / zoom,
|
|
|
|
// y = opts.mouse_y / zoom,
|
|
|
|
mouseTarget = e.target;
|
|
|
|
|
|
|
|
if (svgCanvas.getMode() === 'connector') {
|
|
|
|
var fo = $(mouseTarget).closest('foreignObject');
|
|
|
|
if (fo.length) { mouseTarget = fo[0]; }
|
|
|
|
|
|
|
|
var parents = $(mouseTarget).parents();
|
|
|
|
|
|
|
|
if (mouseTarget === startElem) {
|
2010-06-16 13:16:00 +00:00
|
|
|
// Start line through click
|
|
|
|
started = true;
|
|
|
|
return {
|
|
|
|
keep: true,
|
|
|
|
element: null,
|
|
|
|
started: started
|
2014-02-12 09:38:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if ($.inArray(svgcontent, parents) === -1) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Not a valid target element, so remove line
|
2018-05-16 00:53:27 +00:00
|
|
|
$(curLine).remove();
|
2010-03-11 21:07:13 +00:00
|
|
|
started = false;
|
|
|
|
return {
|
|
|
|
keep: false,
|
|
|
|
element: null,
|
|
|
|
started: started
|
2014-02-12 09:38:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
// Valid end element
|
2018-05-16 00:53:27 +00:00
|
|
|
endElem = mouseTarget;
|
|
|
|
|
|
|
|
var startId = startElem.id, endId = endElem.id;
|
|
|
|
var connStr = startId + ' ' + endId;
|
|
|
|
var altStr = endId + ' ' + startId;
|
2014-02-12 09:38:38 +00:00
|
|
|
// Don't create connector if one already exists
|
2018-05-16 00:53:27 +00:00
|
|
|
var dupe = $(svgcontent).find(connSel).filter(function () {
|
|
|
|
var conn = this.getAttributeNS(seNs, 'connector');
|
|
|
|
if (conn === connStr || conn === altStr) { return true; }
|
2014-02-12 09:38:38 +00:00
|
|
|
});
|
2018-05-16 00:53:27 +00:00
|
|
|
if (dupe.length) {
|
|
|
|
$(curLine).remove();
|
2010-03-11 21:07:13 +00:00
|
|
|
return {
|
2014-02-12 09:38:38 +00:00
|
|
|
keep: false,
|
|
|
|
element: null,
|
|
|
|
started: false
|
|
|
|
};
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
var bb = svgCanvas.getStrokedBBox([endElem]);
|
|
|
|
|
|
|
|
var pt = getBBintersect(startX, startY, bb, getOffset('start', curLine));
|
|
|
|
setPoint(curLine, 'end', pt.x, pt.y, true);
|
|
|
|
$(curLine)
|
|
|
|
.data('c_start', startId)
|
|
|
|
.data('c_end', endId)
|
|
|
|
.data('end_bb', bb);
|
|
|
|
seNs = svgCanvas.getEditorNS(true);
|
|
|
|
curLine.setAttributeNS(seNs, 'se:connector', connStr);
|
|
|
|
curLine.setAttribute('class', connSel.substr(1));
|
|
|
|
curLine.setAttribute('opacity', 1);
|
|
|
|
svgCanvas.addToSelection([curLine]);
|
2014-02-12 09:38:38 +00:00
|
|
|
svgCanvas.moveToBottomSelectedElement();
|
2018-05-16 00:53:27 +00:00
|
|
|
selManager.requestSelector(curLine).showGrips(false);
|
2014-02-12 09:38:38 +00:00
|
|
|
started = false;
|
|
|
|
return {
|
|
|
|
keep: true,
|
2018-05-16 00:53:27 +00:00
|
|
|
element: curLine,
|
2014-02-12 09:38:38 +00:00
|
|
|
started: started
|
|
|
|
};
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
selectedChanged: function (opts) {
|
2010-09-10 19:17:06 +00:00
|
|
|
// TODO: Find better way to skip operations if no connectors are in use
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!$(svgcontent).find(connSel).length) { return; }
|
|
|
|
|
|
|
|
if (svgCanvas.getMode() === 'connector') {
|
2010-09-09 17:38:33 +00:00
|
|
|
svgCanvas.setMode('select');
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Use this to update the current selected elements
|
|
|
|
selElems = opts.elems;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
var i = selElems.length;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
while (i--) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var elem = selElems[i];
|
2018-05-16 00:53:27 +00:00
|
|
|
if (elem && elData(elem, 'c_start')) {
|
2010-03-11 21:07:13 +00:00
|
|
|
selManager.requestSelector(elem).showGrips(false);
|
2018-05-16 00:53:27 +00:00
|
|
|
if (opts.selectedElement && !opts.multiselected) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// TODO: Set up context tools and hide most regular line tools
|
|
|
|
showPanel(true);
|
2010-02-08 21:26:54 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2010-06-17 17:57:24 +00:00
|
|
|
updateConnectors();
|
2010-03-11 21:07:13 +00:00
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
elementChanged: function (opts) {
|
2010-03-11 21:07:13 +00:00
|
|
|
var elem = opts.elems[0];
|
2014-02-12 09:38:38 +00:00
|
|
|
if (elem && elem.tagName === 'svg' && elem.id === 'svgcontent') {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Update svgcontent (can change on import)
|
|
|
|
svgcontent = elem;
|
|
|
|
init();
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2010-03-11 21:07:13 +00:00
|
|
|
// Has marker, so change offset
|
2014-02-12 09:38:38 +00:00
|
|
|
var start;
|
|
|
|
if (elem && (
|
2018-05-16 00:53:27 +00:00
|
|
|
elem.getAttribute('marker-start') ||
|
|
|
|
elem.getAttribute('marker-mid') ||
|
|
|
|
elem.getAttribute('marker-end')
|
2010-03-11 21:07:13 +00:00
|
|
|
)) {
|
2018-05-16 00:53:27 +00:00
|
|
|
start = elem.getAttribute('marker-start');
|
|
|
|
var mid = elem.getAttribute('marker-mid');
|
|
|
|
var end = elem.getAttribute('marker-end');
|
|
|
|
curLine = elem;
|
2010-03-11 21:07:13 +00:00
|
|
|
$(elem)
|
2018-05-16 00:53:27 +00:00
|
|
|
.data('start_off', !!start)
|
|
|
|
.data('end_off', !!end);
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (elem.tagName === 'line' && mid) {
|
2010-03-11 21:07:13 +00:00
|
|
|
// Convert to polyline to accept mid-arrow
|
2018-05-16 00:53:27 +00:00
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
var x1 = Number(elem.getAttribute('x1'));
|
|
|
|
var x2 = Number(elem.getAttribute('x2'));
|
|
|
|
var y1 = Number(elem.getAttribute('y1'));
|
|
|
|
var y2 = Number(elem.getAttribute('y2'));
|
2010-03-11 21:07:13 +00:00
|
|
|
var id = elem.id;
|
2018-05-16 00:53:27 +00:00
|
|
|
|
|
|
|
var midPt = (' ' + ((x1 + x2) / 2) + ',' + ((y1 + y2) / 2) + ' ');
|
2010-03-11 21:07:13 +00:00
|
|
|
var pline = addElem({
|
2018-05-16 00:53:27 +00:00
|
|
|
'element': 'polyline',
|
|
|
|
'attr': {
|
|
|
|
'points': (x1 + ',' + y1 + midPt + x2 + ',' + y2),
|
|
|
|
'stroke': elem.getAttribute('stroke'),
|
|
|
|
'stroke-width': elem.getAttribute('stroke-width'),
|
|
|
|
'marker-mid': mid,
|
|
|
|
'fill': 'none',
|
|
|
|
'opacity': elem.getAttribute('opacity') || 1
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$(elem).after(pline).remove();
|
|
|
|
svgCanvas.clearSelection();
|
|
|
|
pline.id = id;
|
|
|
|
svgCanvas.addToSelection([pline]);
|
2010-06-17 17:57:24 +00:00
|
|
|
elem = pline;
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
2010-06-17 17:57:24 +00:00
|
|
|
// Update line if it's a connector
|
2018-05-16 00:53:27 +00:00
|
|
|
if (elem.getAttribute('class') === connSel.substr(1)) {
|
2014-02-12 09:38:38 +00:00
|
|
|
start = getElem(elData(elem, 'c_start'));
|
2010-06-17 17:57:24 +00:00
|
|
|
updateConnectors([start]);
|
|
|
|
} else {
|
|
|
|
updateConnectors();
|
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
IDsUpdated: function (input) {
|
2018-01-18 12:37:49 +00:00
|
|
|
var remove = [];
|
2018-05-16 00:53:27 +00:00
|
|
|
input.elems.forEach(function (elem) {
|
|
|
|
if ('se:connector' in elem.attr) {
|
2018-01-18 12:37:49 +00:00
|
|
|
elem.attr['se:connector'] = elem.attr['se:connector'].split(' ')
|
2018-05-16 00:53:27 +00:00
|
|
|
.map(function (oldID) { return input.changes[oldID]; }).join(' ');
|
2018-01-22 09:13:43 +00:00
|
|
|
|
|
|
|
// Check validity - the field would be something like 'svg_21 svg_22', but
|
|
|
|
// if one end is missing, it would be 'svg_21' and therefore fail this test
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!/. ./.test(elem.attr['se:connector'])) {
|
2018-01-18 12:37:49 +00:00
|
|
|
remove.push(elem.attr.id);
|
2018-05-16 00:53:27 +00:00
|
|
|
}
|
2018-01-18 12:37:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return {remove: remove};
|
|
|
|
},
|
2018-05-16 00:53:27 +00:00
|
|
|
toolButtonStateUpdate: function (opts) {
|
2014-02-12 09:38:38 +00:00
|
|
|
if (opts.nostroke) {
|
2010-03-11 21:07:13 +00:00
|
|
|
if ($('#mode_connect').hasClass('tool_button_current')) {
|
2014-02-12 09:38:38 +00:00
|
|
|
svgEditor.clickSelect();
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-11 21:07:13 +00:00
|
|
|
$('#mode_connect')
|
2018-05-16 00:53:27 +00:00
|
|
|
.toggleClass('disabled', opts.nostroke);
|
2010-03-11 21:07:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|