ext-closepath.js 2.0 KB

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