Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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 (
27           event.button === 0 &&
28           !event.altKey &&
29           !event.ctrlKey &&
30           !event.metaKey &&
31           !event.shiftKey
32         ) {
33           event.preventDefault();
34           const $previewDialog = $(
35             `<div>${Drupal.theme('nodePreviewModal')}</div>`,
36           ).appendTo('body');
37           Drupal.dialog($previewDialog, {
38             title: Drupal.t('Leave preview?'),
39             buttons: [
40               {
41                 text: Drupal.t('Cancel'),
42                 click() {
43                   $(this).dialog('close');
44                 },
45               },
46               {
47                 text: Drupal.t('Leave preview'),
48                 click() {
49                   window.top.location.href = event.target.href;
50                 },
51               },
52             ],
53           }).showModal();
54         }
55       }
56
57       const $preview = $(context).once('node-preview');
58       if ($(context).find('.node-preview-container').length) {
59         $preview.on(
60           'click.preview',
61           'a:not([href^="#"], .node-preview-container a)',
62           clickPreviewModal,
63         );
64       }
65     },
66     detach(context, settings, trigger) {
67       if (trigger === 'unload') {
68         const $preview = $(context)
69           .find('.content')
70           .removeOnce('node-preview');
71         if ($preview.length) {
72           $preview.off('click.preview');
73         }
74       }
75     },
76   };
77
78   /**
79    * Switch view mode.
80    *
81    * @type {Drupal~behavior}
82    *
83    * @prop {Drupal~behaviorAttach} attach
84    *   Attaches automatic submit on `formUpdated.preview` events.
85    */
86   Drupal.behaviors.nodePreviewSwitchViewMode = {
87     attach(context) {
88       const $autosubmit = $(context)
89         .find('[data-drupal-autosubmit]')
90         .once('autosubmit');
91       if ($autosubmit.length) {
92         $autosubmit.on('formUpdated.preview', function() {
93           $(this.form).trigger('submit');
94         });
95       }
96     },
97   };
98
99   /**
100    * Theme function for node preview modal.
101    *
102    * @return {string}
103    *   Markup for the node preview modal.
104    */
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.',
110     )}</small>`;
111   };
112 })(jQuery, Drupal);