Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / node / node.preview.es6.js
1 /**
2  * @file
3  * Preview behaviors.
4  */
5
6 (function ($, Drupal) {
7   /**
8    * Disables all non-relevant links in node previews.
9    *
10    * Destroys links (except local fragment identifiers such as href="#frag") in
11    * node previews to prevent users from leaving the page.
12    *
13    * @type {Drupal~behavior}
14    *
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.
19    */
20   Drupal.behaviors.nodePreviewDestroyLinks = {
21     attach(context) {
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)
25         // or SHIFT key.
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?'),
31             buttons: [
32               {
33                 text: Drupal.t('Cancel'),
34                 click() {
35                   $(this).dialog('close');
36                 },
37               }, {
38                 text: Drupal.t('Leave preview'),
39                 click() {
40                   window.top.location.href = event.target.href;
41                 },
42               },
43             ],
44           }).showModal();
45         }
46       }
47
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);
51       }
52     },
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');
58         }
59       }
60     },
61   };
62
63   /**
64    * Switch view mode.
65    *
66    * @type {Drupal~behavior}
67    *
68    * @prop {Drupal~behaviorAttach} attach
69    *   Attaches automatic submit on `formUpdated.preview` events.
70    */
71   Drupal.behaviors.nodePreviewSwitchViewMode = {
72     attach(context) {
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');
77         });
78       }
79     },
80   };
81
82   /**
83    * Theme function for node preview modal.
84    *
85    * @return {string}
86    *   Markup for the node preview modal.
87    */
88   Drupal.theme.nodePreviewModal = function () {
89     return `<p>${
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>`;
93   };
94 }(jQuery, Drupal));