Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Preview behaviors.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@17
|
6 (function($, Drupal) {
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Disables all non-relevant links in node previews.
|
Chris@0
|
9 *
|
Chris@0
|
10 * Destroys links (except local fragment identifiers such as href="#frag") in
|
Chris@0
|
11 * node previews to prevent users from leaving the page.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @type {Drupal~behavior}
|
Chris@0
|
14 *
|
Chris@0
|
15 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
16 * Attaches confirmation prompt for clicking links in node preview mode.
|
Chris@0
|
17 * @prop {Drupal~behaviorDetach} detach
|
Chris@0
|
18 * Detaches confirmation prompt for clicking links in node preview mode.
|
Chris@0
|
19 */
|
Chris@0
|
20 Drupal.behaviors.nodePreviewDestroyLinks = {
|
Chris@0
|
21 attach(context) {
|
Chris@0
|
22 function clickPreviewModal(event) {
|
Chris@0
|
23 // Only confirm leaving previews when left-clicking and user is not
|
Chris@0
|
24 // pressing the ALT, CTRL, META (Command key on the Macintosh keyboard)
|
Chris@0
|
25 // or SHIFT key.
|
Chris@17
|
26 if (
|
Chris@17
|
27 event.button === 0 &&
|
Chris@17
|
28 !event.altKey &&
|
Chris@17
|
29 !event.ctrlKey &&
|
Chris@17
|
30 !event.metaKey &&
|
Chris@17
|
31 !event.shiftKey
|
Chris@17
|
32 ) {
|
Chris@0
|
33 event.preventDefault();
|
Chris@17
|
34 const $previewDialog = $(
|
Chris@17
|
35 `<div>${Drupal.theme('nodePreviewModal')}</div>`,
|
Chris@17
|
36 ).appendTo('body');
|
Chris@0
|
37 Drupal.dialog($previewDialog, {
|
Chris@0
|
38 title: Drupal.t('Leave preview?'),
|
Chris@0
|
39 buttons: [
|
Chris@0
|
40 {
|
Chris@0
|
41 text: Drupal.t('Cancel'),
|
Chris@0
|
42 click() {
|
Chris@0
|
43 $(this).dialog('close');
|
Chris@0
|
44 },
|
Chris@17
|
45 },
|
Chris@17
|
46 {
|
Chris@0
|
47 text: Drupal.t('Leave preview'),
|
Chris@0
|
48 click() {
|
Chris@0
|
49 window.top.location.href = event.target.href;
|
Chris@0
|
50 },
|
Chris@0
|
51 },
|
Chris@0
|
52 ],
|
Chris@0
|
53 }).showModal();
|
Chris@0
|
54 }
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@17
|
57 const $preview = $(context).once('node-preview');
|
Chris@0
|
58 if ($(context).find('.node-preview-container').length) {
|
Chris@17
|
59 $preview.on(
|
Chris@17
|
60 'click.preview',
|
Chris@17
|
61 'a:not([href^="#"], .node-preview-container a)',
|
Chris@17
|
62 clickPreviewModal,
|
Chris@17
|
63 );
|
Chris@0
|
64 }
|
Chris@0
|
65 },
|
Chris@0
|
66 detach(context, settings, trigger) {
|
Chris@0
|
67 if (trigger === 'unload') {
|
Chris@17
|
68 const $preview = $(context)
|
Chris@17
|
69 .find('.content')
|
Chris@17
|
70 .removeOnce('node-preview');
|
Chris@0
|
71 if ($preview.length) {
|
Chris@0
|
72 $preview.off('click.preview');
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75 },
|
Chris@0
|
76 };
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Switch view mode.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @type {Drupal~behavior}
|
Chris@0
|
82 *
|
Chris@0
|
83 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
84 * Attaches automatic submit on `formUpdated.preview` events.
|
Chris@0
|
85 */
|
Chris@0
|
86 Drupal.behaviors.nodePreviewSwitchViewMode = {
|
Chris@0
|
87 attach(context) {
|
Chris@17
|
88 const $autosubmit = $(context)
|
Chris@17
|
89 .find('[data-drupal-autosubmit]')
|
Chris@17
|
90 .once('autosubmit');
|
Chris@0
|
91 if ($autosubmit.length) {
|
Chris@17
|
92 $autosubmit.on('formUpdated.preview', function() {
|
Chris@0
|
93 $(this.form).trigger('submit');
|
Chris@0
|
94 });
|
Chris@0
|
95 }
|
Chris@0
|
96 },
|
Chris@0
|
97 };
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Theme function for node preview modal.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @return {string}
|
Chris@0
|
103 * Markup for the node preview modal.
|
Chris@0
|
104 */
|
Chris@17
|
105 Drupal.theme.nodePreviewModal = function() {
|
Chris@17
|
106 return `<p>${Drupal.t(
|
Chris@17
|
107 'Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?',
|
Chris@17
|
108 )}</p><small class="description">${Drupal.t(
|
Chris@17
|
109 'CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.',
|
Chris@17
|
110 )}</small>`;
|
Chris@0
|
111 };
|
Chris@17
|
112 })(jQuery, Drupal);
|