linkitDialog.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file
  3. * Linkit tinymce dialog helper.
  4. */
  5. (function ($) {
  6. // Abort if Drupal.linkit is not defined.
  7. if (typeof Drupal.linkit === 'undefined') {
  8. return ;
  9. }
  10. Drupal.linkit.registerDialogHelper('tinymce', {
  11. init : function() {},
  12. /**
  13. * Prepare the dialog after init.
  14. */
  15. afterInit : function () {
  16. var editor = Drupal.settings.linkit.currentInstance.editor;
  17. var element, link;
  18. // Restore the selection if the browser is IE.
  19. if (tinymce.isIE) {
  20. editor.selection.moveToBookmark(editor.windowManager.bookmark);
  21. }
  22. // If we have selected a link element, lets populate the fields in the
  23. // dialog with the values from that link element.
  24. if (element = editor.dom.getParent(editor.selection.getNode(), 'A')) {
  25. link = {
  26. path: editor.dom.getAttrib(element, 'href'),
  27. attributes: {}
  28. };
  29. // Get all attributes that have fields in the modal.
  30. var additionalAttributes = Drupal.linkit.additionalAttributes();
  31. // Add attributes to the link object, but only those that are enabled in Linkit.
  32. tinymce.each(additionalAttributes, function(attribute) {
  33. var value = editor.dom.getAttrib(element, attribute);
  34. if (value) {
  35. link.attributes[attribute] = value;
  36. }
  37. });
  38. }
  39. // Populate the fields.
  40. Drupal.linkit.populateFields(link);
  41. },
  42. /**
  43. * Insert the link into the editor.
  44. *
  45. * @param {Object} link
  46. * The link object.
  47. */
  48. insertLink : function(data) {
  49. var editor = Drupal.settings.linkit.currentInstance.editor,
  50. element = editor.dom.getParent(editor.selection.getNode(), 'A');
  51. // Restore the selection if the browser is IE.
  52. if (tinymce.isIE) {
  53. editor.selection.moveToBookmark(editor.windowManager.bookmark);
  54. }
  55. // Set undo begin point.
  56. editor.execCommand("mceBeginUndoLevel");
  57. data.attributes.href = data.path;
  58. // No link element selected, create a new anchor element.
  59. if (element == null) {
  60. // If there is no selection, lets inser a new element.
  61. if (editor.selection.isCollapsed()) {
  62. var content = (Drupal.settings.linkit.currentInstance.linkContent) ? Drupal.settings.linkit.currentInstance.linkContent : data.path;
  63. editor.execCommand('mceInsertContent', false,
  64. editor.dom.createHTML('a', data.attributes, content));
  65. } else {
  66. editor.execCommand("mceInsertLink", false, data.attributes);
  67. }
  68. }
  69. // We are editing an existing link, so just overwrite the attributes.
  70. else {
  71. editor.dom.setAttribs(element, data.attributes);
  72. }
  73. // Don't move caret if selection was image
  74. if(element != null) {
  75. if (element.childNodes.length != 1 || element.firstChild.nodeName != 'IMG') {
  76. editor.focus();
  77. editor.selection.select(element);
  78. editor.selection.collapse(0);
  79. // Restore the selection if the browser is IE.
  80. if (tinymce.isIE) {
  81. editor.selection.moveToBookmark(editor.windowManager.bookmark);
  82. }
  83. }
  84. }
  85. // Set undo end point.
  86. editor.execCommand("mceEndUndoLevel");
  87. }
  88. });
  89. })(jQuery);