6 (function ($, Drupal) {
11 * Disables all non-relevant links in node previews.
13 * Destroys links (except local fragment identifiers such as href="#frag") in
14 * node previews to prevent users from leaving the page.
16 * @type {Drupal~behavior}
18 * @prop {Drupal~behaviorAttach} attach
19 * Attaches confirmation prompt for clicking links in node preview mode.
20 * @prop {Drupal~behaviorDetach} detach
21 * Detaches confirmation prompt for clicking links in node preview mode.
23 Drupal.behaviors.nodePreviewDestroyLinks = {
24 attach: function (context) {
26 function clickPreviewModal(event) {
27 // Only confirm leaving previews when left-clicking and user is not
28 // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard)
30 if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
31 event.preventDefault();
32 var $previewDialog = $('<div>' + Drupal.theme('nodePreviewModal') + '</div>').appendTo('body');
33 Drupal.dialog($previewDialog, {
34 title: Drupal.t('Leave preview?'),
37 text: Drupal.t('Cancel'),
39 $(this).dialog('close');
42 text: Drupal.t('Leave preview'),
44 window.top.location.href = event.target.href;
52 var $preview = $(context).find('.content').once('node-preview');
53 if ($(context).find('.node-preview-container').length) {
54 $preview.on('click.preview', 'a:not([href^=#], #edit-backlink, #toolbar-administration a)', clickPreviewModal);
57 detach: function (context, settings, trigger) {
58 if (trigger === 'unload') {
59 var $preview = $(context).find('.content').removeOnce('node-preview');
60 if ($preview.length) {
61 $preview.off('click.preview');
70 * @type {Drupal~behavior}
72 * @prop {Drupal~behaviorAttach} attach
73 * Attaches automatic submit on `formUpdated.preview` events.
75 Drupal.behaviors.nodePreviewSwitchViewMode = {
76 attach: function (context) {
77 var $autosubmit = $(context).find('[data-drupal-autosubmit]').once('autosubmit');
78 if ($autosubmit.length) {
79 $autosubmit.on('formUpdated.preview', function () {
80 $(this.form).trigger('submit');
87 * Theme function for node preview modal.
90 * Markup for the node preview modal.
92 Drupal.theme.nodePreviewModal = function () {
94 Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?') +
95 '</p><small class="description">' +
96 Drupal.t('CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.') + '</small>';