2010-02-09 21:46:18 +00:00
|
|
|
/*
|
|
|
|
* ext-closepath.js
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-02-09 21:46:18 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This extension adds a simple button to the contextual panel for paths
|
|
|
|
// The button toggles whether the path is open or closed
|
2013-02-16 11:19:25 +00:00
|
|
|
svgEditor.addExtension('ClosePath', function() {
|
|
|
|
var selElems,
|
|
|
|
updateButton = function(path) {
|
|
|
|
var seglist = path.pathSegList,
|
|
|
|
closed = seglist.getItem(seglist.numberOfItems - 1).pathSegType == 1,
|
|
|
|
showbutton = closed ? '#tool_openpath' : '#tool_closepath',
|
|
|
|
hidebutton = closed ? '#tool_closepath' : '#tool_openpath';
|
|
|
|
$(hidebutton).hide();
|
|
|
|
$(showbutton).show();
|
|
|
|
},
|
|
|
|
showPanel = function(on) {
|
|
|
|
$('#closepath_panel').toggle(on);
|
|
|
|
if (on) {
|
2010-02-09 21:46:18 +00:00
|
|
|
var path = selElems[0];
|
2013-02-16 11:19:25 +00:00
|
|
|
if (path) updateButton(path);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
toggleClosed = function() {
|
|
|
|
var path = selElems[0];
|
|
|
|
if (path) {
|
|
|
|
var seglist = path.pathSegList,
|
|
|
|
last = seglist.numberOfItems - 1;
|
|
|
|
// is closed
|
|
|
|
if (seglist.getItem(last).pathSegType == 1) {
|
|
|
|
seglist.removeItem(last);
|
|
|
|
} else {
|
|
|
|
seglist.appendItem(path.createSVGPathSegClosePath());
|
2010-02-09 21:46:18 +00:00
|
|
|
}
|
2013-02-16 11:19:25 +00:00
|
|
|
updateButton(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: 'ClosePath',
|
|
|
|
svgicons: 'extensions/closepath_icons.svg',
|
|
|
|
buttons: [{
|
|
|
|
id: 'tool_openpath',
|
|
|
|
type: 'context',
|
|
|
|
panel: 'closepath_panel',
|
|
|
|
title: 'Open path',
|
|
|
|
events: {
|
|
|
|
click: function() {
|
|
|
|
toggleClosed();
|
2010-02-19 16:55:08 +00:00
|
|
|
}
|
2013-02-16 11:19:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'tool_closepath',
|
|
|
|
type: 'context',
|
|
|
|
panel: 'closepath_panel',
|
|
|
|
title: 'Close path',
|
|
|
|
events: {
|
|
|
|
click: function() {
|
|
|
|
toggleClosed();
|
2010-02-19 16:55:08 +00:00
|
|
|
}
|
2013-02-16 11:19:25 +00:00
|
|
|
}
|
|
|
|
}],
|
|
|
|
callback: function() {
|
|
|
|
$('#closepath_panel').hide();
|
|
|
|
},
|
|
|
|
selectedChanged: function(opts) {
|
|
|
|
selElems = opts.elems;
|
|
|
|
var i = selElems.length;
|
|
|
|
while (i--) {
|
|
|
|
var elem = selElems[i];
|
|
|
|
if (elem && elem.tagName == 'path') {
|
|
|
|
if (opts.selectedElement && !opts.multiselected) {
|
|
|
|
showPanel(true);
|
2010-02-09 21:46:18 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
|
|
|
}
|
2013-02-16 11:19:25 +00:00
|
|
|
} else {
|
|
|
|
showPanel(false);
|
2010-02-09 21:46:18 +00:00
|
|
|
}
|
2010-02-19 22:40:04 +00:00
|
|
|
}
|
2013-02-16 11:19:25 +00:00
|
|
|
}
|
|
|
|
};
|
2010-02-09 21:46:18 +00:00
|
|
|
});
|