1 | /*
|
---|
2 | * ext-closepath.js
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2
|
---|
5 | *
|
---|
6 | * Copyright(c) 2010 Jeff Schiller
|
---|
7 | *
|
---|
8 | */
|
---|
9 |
|
---|
10 | // This extension adds a simple button to the contextual panel for paths
|
---|
11 | // The button toggles whether the path is open or closed
|
---|
12 | methodDraw.addExtension("ClosePath", function(S) {
|
---|
13 | var selElems,
|
---|
14 | updateButton = function(path) {
|
---|
15 | var seglist = path.pathSegList,
|
---|
16 | closed = seglist.getItem(seglist.numberOfItems - 1).pathSegType==1,
|
---|
17 | showbutton = closed ? '#tool_openpath' : '#tool_closepath',
|
---|
18 | hidebutton = closed ? '#tool_closepath' : '#tool_openpath';
|
---|
19 | $(hidebutton).hide();
|
---|
20 | $(showbutton).show();
|
---|
21 | },
|
---|
22 | showPanel = function(on) {
|
---|
23 | $('#closepath_panel').toggle(on);
|
---|
24 | if (on) {
|
---|
25 | var path = selElems[0];
|
---|
26 | if (path) updateButton(path);
|
---|
27 | }
|
---|
28 | },
|
---|
29 |
|
---|
30 | toggleClosed = function() {
|
---|
31 | var path = selElems[0];
|
---|
32 | if (path) {
|
---|
33 | var seglist = path.pathSegList,
|
---|
34 | last = seglist.numberOfItems - 1;
|
---|
35 | // is closed
|
---|
36 | if(seglist.getItem(last).pathSegType == 1) {
|
---|
37 | seglist.removeItem(last);
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | seglist.appendItem(path.createSVGPathSegClosePath());
|
---|
41 | }
|
---|
42 | updateButton(path);
|
---|
43 | }
|
---|
44 | };
|
---|
45 |
|
---|
46 | return {
|
---|
47 | name: "ClosePath",
|
---|
48 | svgicons: "extensions/closepath_icons.svg",
|
---|
49 | buttons: [{
|
---|
50 | id: "tool_openpath",
|
---|
51 | type: "context",
|
---|
52 | panel: "closepath_panel",
|
---|
53 | title: "Open path",
|
---|
54 | events: {
|
---|
55 | 'click': function() {
|
---|
56 | toggleClosed();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | },
|
---|
60 | {
|
---|
61 | id: "tool_closepath",
|
---|
62 | type: "context",
|
---|
63 | panel: "closepath_panel",
|
---|
64 | title: "Close path",
|
---|
65 | events: {
|
---|
66 | 'click': function() {
|
---|
67 | toggleClosed();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }],
|
---|
71 | callback: function() {
|
---|
72 | $('#closepath_panel').hide();
|
---|
73 | },
|
---|
74 | selectedChanged: function(opts) {
|
---|
75 | selElems = opts.elems;
|
---|
76 | var i = selElems.length;
|
---|
77 |
|
---|
78 | while(i--) {
|
---|
79 | var elem = selElems[i];
|
---|
80 | if(elem && elem.tagName == 'path') {
|
---|
81 | if(opts.selectedElement && !opts.multiselected) {
|
---|
82 | showPanel(true);
|
---|
83 | } else {
|
---|
84 | showPanel(false);
|
---|
85 | }
|
---|
86 | } else {
|
---|
87 | showPanel(false);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | };
|
---|
92 | });
|
---|