Drupal investigation

node.preview.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @file
  3. * Preview behaviors.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Disables all non-relevant links in node previews.
  9. *
  10. * Destroys links (except local fragment identifiers such as href="#frag") in
  11. * node previews to prevent users from leaving the page.
  12. *
  13. * @type {Drupal~behavior}
  14. *
  15. * @prop {Drupal~behaviorAttach} attach
  16. * Attaches confirmation prompt for clicking links in node preview mode.
  17. * @prop {Drupal~behaviorDetach} detach
  18. * Detaches confirmation prompt for clicking links in node preview mode.
  19. */
  20. Drupal.behaviors.nodePreviewDestroyLinks = {
  21. attach: function (context) {
  22. function clickPreviewModal(event) {
  23. // Only confirm leaving previews when left-clicking and user is not
  24. // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard)
  25. // or SHIFT key.
  26. if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
  27. event.preventDefault();
  28. var $previewDialog = $('<div>' + Drupal.theme('nodePreviewModal') + '</div>').appendTo('body');
  29. Drupal.dialog($previewDialog, {
  30. title: Drupal.t('Leave preview?'),
  31. buttons: [
  32. {
  33. text: Drupal.t('Cancel'),
  34. click: function () {
  35. $(this).dialog('close');
  36. }
  37. }, {
  38. text: Drupal.t('Leave preview'),
  39. click: function () {
  40. window.top.location.href = event.target.href;
  41. }
  42. }
  43. ]
  44. }).showModal();
  45. }
  46. }
  47. var $preview = $(context).find('.content').once('node-preview');
  48. if ($(context).find('.node-preview-container').length) {
  49. $preview.on('click.preview', 'a:not([href^=#], #edit-backlink, #toolbar-administration a)', clickPreviewModal);
  50. }
  51. },
  52. detach: function (context, settings, trigger) {
  53. if (trigger === 'unload') {
  54. var $preview = $(context).find('.content').removeOnce('node-preview');
  55. if ($preview.length) {
  56. $preview.off('click.preview');
  57. }
  58. }
  59. }
  60. };
  61. /**
  62. * Switch view mode.
  63. *
  64. * @type {Drupal~behavior}
  65. *
  66. * @prop {Drupal~behaviorAttach} attach
  67. * Attaches automatic submit on `formUpdated.preview` events.
  68. */
  69. Drupal.behaviors.nodePreviewSwitchViewMode = {
  70. attach: function (context) {
  71. var $autosubmit = $(context).find('[data-drupal-autosubmit]').once('autosubmit');
  72. if ($autosubmit.length) {
  73. $autosubmit.on('formUpdated.preview', function () {
  74. $(this.form).trigger('submit');
  75. });
  76. }
  77. }
  78. };
  79. /**
  80. * Theme function for node preview modal.
  81. *
  82. * @return {string}
  83. * Markup for the node preview modal.
  84. */
  85. Drupal.theme.nodePreviewModal = function () {
  86. return '<p>' +
  87. Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?') +
  88. '</p><small class="description">' +
  89. Drupal.t('CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.') + '</small>';
  90. };
  91. })(jQuery, Drupal);