diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 4e42006..388cdd6 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -2171,6 +2171,7 @@ function node_library_info() { 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), + array('system', 'drupal.dialog'), ), ); $libraries['drupal.content_types'] = array( diff --git a/core/modules/node/node.preview.js b/core/modules/node/node.preview.js index 3be74e2..df4181b 100644 --- a/core/modules/node/node.preview.js +++ b/core/modules/node/node.preview.js @@ -1,26 +1,51 @@ +/** + * @file + * Defines Javascript behaviors for node previews. + */ + (function ($, Drupal) { "use strict"; /** - * Disabling all links (except local fragment identifiers such as href="#frag") - * in node previews to prevent users from leaving the page. + * Provides a modal confirmation before leaving a node preview. */ Drupal.behaviors.nodePreviewDestroyLinks = { attach: function (context) { - var $preview = $(context).find('.node').once('node-preview'); - if ($preview.length) { - $preview.on('click.preview', 'a:not([href^=#])', function (e) { - e.preventDefault(); - }); - } + // Display a modal confirmation for all links within preview, except + // for local fragment identifiers such as href="#frag". + $(context).find('.node').on('click.preview', 'a:not([href^=#])', function (event) { + // Only confirm leaving previews when left-clicking and user is not + // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard) + // or SHIFT key. + if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) { + event.preventDefault(); + var $previewDialog = $('
') + .text(Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?')) + .appendTo('body'); + Drupal.dialog($previewDialog, { + title: Drupal.t('Leave preview?'), + buttons: [ + { + text: Drupal.t('Cancel'), + click: function () { + $(this).dialog('close'); + } + }, + { + text: Drupal.t('Leave preview'), + click: function () { + window.top.location.href = event.target.href; + } + } + ] + }).showModal(); + } + }); }, detach: function (context, settings, trigger) { if (trigger === 'unload') { - var $preview = $(context).find('.node').removeOnce('node-preview'); - if ($preview.length) { - $preview.off('click.preview'); - } + $(context).find('.node').off('click.preview', 'a:not([href^=#])'); } } };