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)
33 event.preventDefault();
34 const $previewDialog = $(
35 `<div>${Drupal.theme('nodePreviewModal')}</div>`,
37 Drupal.dialog($previewDialog, {
38 title: Drupal.t('Leave preview?'),
41 text: Drupal.t('Cancel'),
43 $(this).dialog('close');
47 text: Drupal.t('Leave preview'),
49 window.top.location.href = event.target.href;
57 const $preview = $(context).once('node-preview');
58 if ($(context).find('.node-preview-container').length) {
61 'a:not([href^="#"], .node-preview-container a)',
66 detach(context, settings, trigger) {
67 if (trigger === 'unload') {
68 const $preview = $(context)
70 .removeOnce('node-preview');
71 if ($preview.length) {
72 $preview.off('click.preview');
81 * @type {Drupal~behavior}
83 * @prop {Drupal~behaviorAttach} attach
84 * Attaches automatic submit on `formUpdated.preview` events.
86 Drupal.behaviors.nodePreviewSwitchViewMode = {
88 const $autosubmit = $(context)
89 .find('[data-drupal-autosubmit]')
91 if ($autosubmit.length) {
92 $autosubmit.on('formUpdated.preview', function() {
93 $(this.form).trigger('submit');
100 * Theme function for node preview modal.
103 * Markup for the node preview modal.
105 Drupal.theme.nodePreviewModal = function() {
106 return `<p>${Drupal.t(
107 'Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?',
108 )}</p><small class="description">${Drupal.t(
109 'CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.',