6 (function ($, Drupal) {
8 * Disables all non-relevant links in node previews.
10 * Destroys links (except local fragment identifiers such as href="#frag") in
11 * node previews to prevent users from leaving the page.
13 * @type {Drupal~behavior}
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.
20 Drupal.behaviors.nodePreviewDestroyLinks = {
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)
26 if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
27 event.preventDefault();
28 const $previewDialog = $(`<div>${Drupal.theme('nodePreviewModal')}</div>`).appendTo('body');
29 Drupal.dialog($previewDialog, {
30 title: Drupal.t('Leave preview?'),
33 text: Drupal.t('Cancel'),
35 $(this).dialog('close');
38 text: Drupal.t('Leave preview'),
40 window.top.location.href = event.target.href;
48 const $preview = $(context).find('.content').once('node-preview');
49 if ($(context).find('.node-preview-container').length) {
50 $preview.on('click.preview', 'a:not([href^=#], #edit-backlink, #toolbar-administration a)', clickPreviewModal);
53 detach(context, settings, trigger) {
54 if (trigger === 'unload') {
55 const $preview = $(context).find('.content').removeOnce('node-preview');
56 if ($preview.length) {
57 $preview.off('click.preview');
66 * @type {Drupal~behavior}
68 * @prop {Drupal~behaviorAttach} attach
69 * Attaches automatic submit on `formUpdated.preview` events.
71 Drupal.behaviors.nodePreviewSwitchViewMode = {
73 const $autosubmit = $(context).find('[data-drupal-autosubmit]').once('autosubmit');
74 if ($autosubmit.length) {
75 $autosubmit.on('formUpdated.preview', function () {
76 $(this.form).trigger('submit');
83 * Theme function for node preview modal.
86 * Markup for the node preview modal.
88 Drupal.theme.nodePreviewModal = function () {
90 Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?')
91 }</p><small class="description">${
92 Drupal.t('CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.')}</small>`;