2014-02-12 09:38:38 +00:00
|
|
|
/*globals svgEditor, svgCanvas, $*/
|
|
|
|
/*jslint vars: true, eqeq: true*/
|
2010-02-08 21:26:54 +00:00
|
|
|
/*
|
|
|
|
* ext-arrows.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-02-19 12:22:18 +00:00
|
|
|
svgEditor.addExtension('Arrows', function(S) {
|
|
|
|
var svgcontent = S.svgcontent,
|
|
|
|
addElem = S.addSvgElementFromJson,
|
|
|
|
nonce = S.nonce,
|
|
|
|
randomize_ids = S.randomize_ids,
|
2014-02-12 09:38:38 +00:00
|
|
|
selElems, pathdata,
|
2013-02-19 12:22:18 +00:00
|
|
|
lang_list = {
|
|
|
|
'en':[
|
|
|
|
{'id': 'arrow_none', 'textContent': 'No arrow' }
|
2010-02-08 21:26:54 +00:00
|
|
|
],
|
2013-02-19 12:22:18 +00:00
|
|
|
'fr':[
|
|
|
|
{'id': 'arrow_none', 'textContent': 'Sans flèche' }
|
2010-02-08 21:26:54 +00:00
|
|
|
]
|
2013-02-19 12:22:18 +00:00
|
|
|
},
|
2014-02-12 09:38:38 +00:00
|
|
|
arrowprefix,
|
2013-02-19 12:22:18 +00:00
|
|
|
prefix = 'se_arrow_';
|
2010-02-27 22:55:53 +00:00
|
|
|
|
2013-02-19 12:22:18 +00:00
|
|
|
function setArrowNonce(window, n) {
|
|
|
|
randomize_ids = true;
|
|
|
|
arrowprefix = prefix + n + '_';
|
|
|
|
pathdata.fw.id = arrowprefix + 'fw';
|
|
|
|
pathdata.bk.id = arrowprefix + 'bk';
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsetArrowNonce(window) {
|
|
|
|
randomize_ids = false;
|
|
|
|
arrowprefix = prefix;
|
|
|
|
pathdata.fw.id = arrowprefix + 'fw';
|
|
|
|
pathdata.bk.id = arrowprefix + 'bk';
|
|
|
|
}
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
|
|
|
|
svgCanvas.bind('setnonce', setArrowNonce);
|
|
|
|
svgCanvas.bind('unsetnonce', unsetArrowNonce);
|
|
|
|
|
|
|
|
if (randomize_ids) {
|
|
|
|
arrowprefix = prefix + nonce + '_';
|
|
|
|
} else {
|
|
|
|
arrowprefix = prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
pathdata = {
|
|
|
|
fw: {d: 'm0,0l10,5l-10,5l5,-5l-5,-5z', refx: 8, id: arrowprefix + 'fw'},
|
|
|
|
bk: {d: 'm10,0l-10,5l10,5l-5,-5l5,-5z', refx: 2, id: arrowprefix + 'bk'}
|
|
|
|
};
|
|
|
|
|
2013-02-19 12:22:18 +00:00
|
|
|
function getLinked(elem, attr) {
|
|
|
|
var str = elem.getAttribute(attr);
|
2014-02-12 09:38:38 +00:00
|
|
|
if(!str) {return null;}
|
2013-02-19 12:22:18 +00:00
|
|
|
var m = str.match(/\(\#(.*)\)/);
|
|
|
|
if(!m || m.length !== 2) {
|
|
|
|
return null;
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
return S.getElem(m[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function showPanel(on) {
|
|
|
|
$('#arrow_panel').toggle(on);
|
|
|
|
if(on) {
|
|
|
|
var el = selElems[0];
|
|
|
|
var end = el.getAttribute('marker-end');
|
|
|
|
var start = el.getAttribute('marker-start');
|
|
|
|
var mid = el.getAttribute('marker-mid');
|
|
|
|
var val;
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (end && start) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'both';
|
2014-02-12 09:38:38 +00:00
|
|
|
} else if (end) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'end';
|
2014-02-12 09:38:38 +00:00
|
|
|
} else if (start) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'start';
|
2014-02-12 09:38:38 +00:00
|
|
|
} else if (mid) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'mid';
|
2014-02-12 09:38:38 +00:00
|
|
|
if (mid.indexOf('bk') !== -1) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'mid_bk';
|
|
|
|
}
|
|
|
|
}
|
2010-02-27 22:55:53 +00:00
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (!start && !mid && !end) {
|
2013-02-19 12:22:18 +00:00
|
|
|
val = 'none';
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
$('#arrow_list').val(val);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetMarker() {
|
|
|
|
var el = selElems[0];
|
|
|
|
el.removeAttribute('marker-start');
|
|
|
|
el.removeAttribute('marker-mid');
|
|
|
|
el.removeAttribute('marker-end');
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMarker(dir, type, id) {
|
|
|
|
// TODO: Make marker (or use?) per arrow type, since refX can be different
|
|
|
|
id = id || arrowprefix + dir;
|
|
|
|
|
|
|
|
var marker = S.getElem(id);
|
|
|
|
var data = pathdata[dir];
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (type == 'mid') {
|
2013-02-19 12:22:18 +00:00
|
|
|
data.refx = 5;
|
|
|
|
}
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (!marker) {
|
2013-02-19 12:22:18 +00:00
|
|
|
marker = addElem({
|
|
|
|
'element': 'marker',
|
|
|
|
'attr': {
|
|
|
|
'viewBox': '0 0 10 10',
|
|
|
|
'id': id,
|
|
|
|
'refY': 5,
|
|
|
|
'markerUnits': 'strokeWidth',
|
|
|
|
'markerWidth': 5,
|
|
|
|
'markerHeight': 5,
|
|
|
|
'orient': 'auto',
|
|
|
|
'style': 'pointer-events:none' // Currently needed for Opera
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
});
|
|
|
|
var arrow = addElem({
|
|
|
|
'element': 'path',
|
|
|
|
'attr': {
|
|
|
|
'd': data.d,
|
|
|
|
'fill': '#000000'
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
});
|
|
|
|
marker.appendChild(arrow);
|
|
|
|
S.findDefs().appendChild(marker);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
marker.setAttribute('refX', data.refx);
|
|
|
|
|
|
|
|
return marker;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setArrow() {
|
|
|
|
var type = this.value;
|
|
|
|
resetMarker();
|
|
|
|
|
2014-02-12 09:38:38 +00:00
|
|
|
if (type == 'none') {
|
2013-02-19 12:22:18 +00:00
|
|
|
return;
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-19 12:22:18 +00:00
|
|
|
// Set marker on element
|
|
|
|
var dir = 'fw';
|
2014-02-12 09:38:38 +00:00
|
|
|
if (type == 'mid_bk') {
|
2013-02-19 12:22:18 +00:00
|
|
|
type = 'mid';
|
|
|
|
dir = 'bk';
|
2014-02-12 09:38:38 +00:00
|
|
|
} else if (type == 'both') {
|
2013-02-19 12:22:18 +00:00
|
|
|
addMarker('bk', type);
|
|
|
|
svgCanvas.changeSelectedAttribute('marker-start', 'url(#' + pathdata.bk.id + ')');
|
|
|
|
type = 'end';
|
|
|
|
dir = 'fw';
|
|
|
|
} else if (type == 'start') {
|
|
|
|
dir = 'bk';
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
addMarker(dir, type);
|
|
|
|
svgCanvas.changeSelectedAttribute('marker-' + type, 'url(#' + pathdata[dir].id + ')');
|
|
|
|
S.call('changed', selElems);
|
|
|
|
}
|
|
|
|
|
|
|
|
function colorChanged(elem) {
|
|
|
|
var color = elem.getAttribute('stroke');
|
|
|
|
var mtypes = ['start', 'mid', 'end'];
|
|
|
|
var defs = S.findDefs();
|
|
|
|
|
|
|
|
$.each(mtypes, function(i, type) {
|
|
|
|
var marker = getLinked(elem, 'marker-'+type);
|
2014-02-12 09:38:38 +00:00
|
|
|
if(!marker) {return;}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
var cur_color = $(marker).children().attr('fill');
|
|
|
|
var cur_d = $(marker).children().attr('d');
|
|
|
|
var new_marker = null;
|
2014-02-12 09:38:38 +00:00
|
|
|
if(cur_color === color) {return;}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
var all_markers = $(defs).find('marker');
|
|
|
|
// Different color, check if already made
|
|
|
|
all_markers.each(function() {
|
|
|
|
var attrs = $(this).children().attr(['fill', 'd']);
|
|
|
|
if(attrs.fill === color && attrs.d === cur_d) {
|
|
|
|
// Found another marker with this color and this path
|
|
|
|
new_marker = this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!new_marker) {
|
|
|
|
// Create a new marker with this color
|
|
|
|
var last_id = marker.id;
|
|
|
|
var dir = last_id.indexOf('_fw') !== -1?'fw':'bk';
|
|
|
|
|
|
|
|
new_marker = addMarker(dir, type, arrowprefix + dir + all_markers.length);
|
|
|
|
|
|
|
|
$(new_marker).children().attr('fill', color);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
$(elem).attr('marker-'+type, 'url(#' + new_marker.id + ')');
|
|
|
|
|
|
|
|
// Check if last marker can be removed
|
|
|
|
var remove = true;
|
|
|
|
$(S.svgcontent).find('line, polyline, path, polygon').each(function() {
|
|
|
|
var elem = this;
|
|
|
|
$.each(mtypes, function(j, mtype) {
|
|
|
|
if($(elem).attr('marker-' + mtype) === 'url(#' + marker.id + ')') {
|
2014-02-12 09:38:38 +00:00
|
|
|
remove = false;
|
|
|
|
return remove;
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-12 09:38:38 +00:00
|
|
|
if(!remove) {return false;}
|
2010-02-08 21:26:54 +00:00
|
|
|
});
|
2013-02-19 12:22:18 +00:00
|
|
|
|
|
|
|
// Not found, so can safely remove
|
|
|
|
if(remove) {
|
|
|
|
$(marker).remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: 'Arrows',
|
|
|
|
context_tools: [{
|
|
|
|
type: 'select',
|
|
|
|
panel: 'arrow_panel',
|
|
|
|
title: 'Select arrow type',
|
|
|
|
id: 'arrow_list',
|
|
|
|
options: {
|
|
|
|
none: 'No arrow',
|
|
|
|
end: '---->',
|
|
|
|
start: '<----',
|
|
|
|
both: '<--->',
|
|
|
|
mid: '-->--',
|
|
|
|
mid_bk: '--<--'
|
2010-02-08 21:26:54 +00:00
|
|
|
},
|
2013-02-19 12:22:18 +00:00
|
|
|
defval: 'none',
|
|
|
|
events: {
|
|
|
|
change: setArrow
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
callback: function() {
|
|
|
|
$('#arrow_panel').hide();
|
|
|
|
// Set ID so it can be translated in locale file
|
|
|
|
$('#arrow_list option')[0].id = 'connector_no_arrow';
|
|
|
|
},
|
|
|
|
addLangData: function(lang) {
|
|
|
|
return {
|
|
|
|
data: lang_list[lang]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
selectedChanged: function(opts) {
|
|
|
|
// Use this to update the current selected elements
|
|
|
|
selElems = opts.elems;
|
|
|
|
|
|
|
|
var i = selElems.length;
|
|
|
|
var marker_elems = ['line', 'path', 'polyline', 'polygon'];
|
|
|
|
while(i--) {
|
|
|
|
var elem = selElems[i];
|
2014-02-12 09:38:38 +00:00
|
|
|
if(elem && $.inArray(elem.tagName, marker_elems) !== -1) {
|
2013-02-19 12:22:18 +00:00
|
|
|
if(opts.selectedElement && !opts.multiselected) {
|
|
|
|
showPanel(true);
|
2010-02-08 21:26:54 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
2010-02-08 21:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-19 12:22:18 +00:00
|
|
|
},
|
|
|
|
elementChanged: function(opts) {
|
|
|
|
var elem = opts.elems[0];
|
|
|
|
if(elem && (
|
|
|
|
elem.getAttribute('marker-start') ||
|
|
|
|
elem.getAttribute('marker-mid') ||
|
|
|
|
elem.getAttribute('marker-end')
|
|
|
|
)) {
|
|
|
|
// var start = elem.getAttribute('marker-start');
|
|
|
|
// var mid = elem.getAttribute('marker-mid');
|
|
|
|
// var end = elem.getAttribute('marker-end');
|
|
|
|
// Has marker, so see if it should match color
|
|
|
|
colorChanged(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-02-08 21:26:54 +00:00
|
|
|
});
|