ext-closepath.js 2.1 KB

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