Chris@0: /**
Chris@0: * @file
Chris@0: * Preview behaviors.
Chris@0: */
Chris@0:
Chris@0: (function ($, Drupal) {
Chris@0: /**
Chris@0: * Disables all non-relevant links in node previews.
Chris@0: *
Chris@0: * Destroys links (except local fragment identifiers such as href="#frag") in
Chris@0: * node previews to prevent users from leaving the page.
Chris@0: *
Chris@0: * @type {Drupal~behavior}
Chris@0: *
Chris@0: * @prop {Drupal~behaviorAttach} attach
Chris@0: * Attaches confirmation prompt for clicking links in node preview mode.
Chris@0: * @prop {Drupal~behaviorDetach} detach
Chris@0: * Detaches confirmation prompt for clicking links in node preview mode.
Chris@0: */
Chris@0: Drupal.behaviors.nodePreviewDestroyLinks = {
Chris@0: attach(context) {
Chris@0: function clickPreviewModal(event) {
Chris@0: // Only confirm leaving previews when left-clicking and user is not
Chris@0: // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard)
Chris@0: // or SHIFT key.
Chris@0: if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
Chris@0: event.preventDefault();
Chris@0: const $previewDialog = $(`
${Drupal.theme('nodePreviewModal')}
`).appendTo('body');
Chris@0: Drupal.dialog($previewDialog, {
Chris@0: title: Drupal.t('Leave preview?'),
Chris@0: buttons: [
Chris@0: {
Chris@0: text: Drupal.t('Cancel'),
Chris@0: click() {
Chris@0: $(this).dialog('close');
Chris@0: },
Chris@0: }, {
Chris@0: text: Drupal.t('Leave preview'),
Chris@0: click() {
Chris@0: window.top.location.href = event.target.href;
Chris@0: },
Chris@0: },
Chris@0: ],
Chris@0: }).showModal();
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: const $preview = $(context).find('.content').once('node-preview');
Chris@0: if ($(context).find('.node-preview-container').length) {
Chris@0: $preview.on('click.preview', 'a:not([href^=#], #edit-backlink, #toolbar-administration a)', clickPreviewModal);
Chris@0: }
Chris@0: },
Chris@0: detach(context, settings, trigger) {
Chris@0: if (trigger === 'unload') {
Chris@0: const $preview = $(context).find('.content').removeOnce('node-preview');
Chris@0: if ($preview.length) {
Chris@0: $preview.off('click.preview');
Chris@0: }
Chris@0: }
Chris@0: },
Chris@0: };
Chris@0:
Chris@0: /**
Chris@0: * Switch view mode.
Chris@0: *
Chris@0: * @type {Drupal~behavior}
Chris@0: *
Chris@0: * @prop {Drupal~behaviorAttach} attach
Chris@0: * Attaches automatic submit on `formUpdated.preview` events.
Chris@0: */
Chris@0: Drupal.behaviors.nodePreviewSwitchViewMode = {
Chris@0: attach(context) {
Chris@0: const $autosubmit = $(context).find('[data-drupal-autosubmit]').once('autosubmit');
Chris@0: if ($autosubmit.length) {
Chris@0: $autosubmit.on('formUpdated.preview', function () {
Chris@0: $(this.form).trigger('submit');
Chris@0: });
Chris@0: }
Chris@0: },
Chris@0: };
Chris@0:
Chris@0: /**
Chris@0: * Theme function for node preview modal.
Chris@0: *
Chris@0: * @return {string}
Chris@0: * Markup for the node preview modal.
Chris@0: */
Chris@0: Drupal.theme.nodePreviewModal = function () {
Chris@0: return `${
Chris@0: Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?')
Chris@0: }
${
Chris@0: Drupal.t('CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.')}`;
Chris@0: };
Chris@0: }(jQuery, Drupal));