break.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. (function ($) {
  2. // @todo Array syntax required; 'break' is a predefined token in JavaScript.
  3. Drupal.wysiwyg.plugins['break'] = {
  4. /**
  5. * Return whether the passed node belongs to this plugin.
  6. */
  7. isNode: function(node) {
  8. return ($(node).is('img.wysiwyg-break'));
  9. },
  10. /**
  11. * Execute the button.
  12. */
  13. invoke: function(data, settings, instanceId) {
  14. if (data.format == 'html') {
  15. // Prevent duplicating a teaser break.
  16. if ($(data.node).is('img.wysiwyg-break')) {
  17. return;
  18. }
  19. var content = this._getPlaceholder(settings);
  20. }
  21. else {
  22. // Prevent duplicating a teaser break.
  23. // @todo data.content is the selection only; needs access to complete content.
  24. if (data.content.match(/<!--break-->/)) {
  25. return;
  26. }
  27. var content = '<!--break-->';
  28. }
  29. if (typeof content != 'undefined') {
  30. Drupal.wysiwyg.instances[instanceId].insert(content);
  31. }
  32. },
  33. /**
  34. * Replace all <!--break--> tags with images.
  35. */
  36. attach: function(content, settings, instanceId) {
  37. content = content.replace(/<!--break-->/g, this._getPlaceholder(settings));
  38. return content;
  39. },
  40. /**
  41. * Replace images with <!--break--> tags in content upon detaching editor.
  42. */
  43. detach: function(content, settings, instanceId) {
  44. var $content = $('<div>' + content + '</div>'); // No .outerHTML() in jQuery :(
  45. // #404532: document.createComment() required or IE will strip the comment.
  46. // #474908: IE 8 breaks when using jQuery methods to replace the elements.
  47. // @todo Add a generic implementation for all Drupal plugins for this.
  48. $.each($('img.wysiwyg-break', $content), function (i, elem) {
  49. elem.parentNode.insertBefore(document.createComment('break'), elem);
  50. elem.parentNode.removeChild(elem);
  51. });
  52. return $content.html();
  53. },
  54. /**
  55. * Helper function to return a HTML placeholder.
  56. */
  57. _getPlaceholder: function (settings) {
  58. return '<img src="' + settings.path + '/images/spacer.gif" alt="&lt;--break-&gt;" title="&lt;--break--&gt;" class="wysiwyg-break drupal-content" />';
  59. }
  60. };
  61. })(jQuery);