Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Preview behaviors.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
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@0
|
26 if (event.button === 0 && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
|
Chris@0
|
27 event.preventDefault();
|
Chris@0
|
28 const $previewDialog = $(`<div>${Drupal.theme('nodePreviewModal')}</div>`).appendTo('body');
|
Chris@0
|
29 Drupal.dialog($previewDialog, {
|
Chris@0
|
30 title: Drupal.t('Leave preview?'),
|
Chris@0
|
31 buttons: [
|
Chris@0
|
32 {
|
Chris@0
|
33 text: Drupal.t('Cancel'),
|
Chris@0
|
34 click() {
|
Chris@0
|
35 $(this).dialog('close');
|
Chris@0
|
36 },
|
Chris@0
|
37 }, {
|
Chris@0
|
38 text: Drupal.t('Leave preview'),
|
Chris@0
|
39 click() {
|
Chris@0
|
40 window.top.location.href = event.target.href;
|
Chris@0
|
41 },
|
Chris@0
|
42 },
|
Chris@0
|
43 ],
|
Chris@0
|
44 }).showModal();
|
Chris@0
|
45 }
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 const $preview = $(context).find('.content').once('node-preview');
|
Chris@0
|
49 if ($(context).find('.node-preview-container').length) {
|
Chris@0
|
50 $preview.on('click.preview', 'a:not([href^=#], #edit-backlink, #toolbar-administration a)', clickPreviewModal);
|
Chris@0
|
51 }
|
Chris@0
|
52 },
|
Chris@0
|
53 detach(context, settings, trigger) {
|
Chris@0
|
54 if (trigger === 'unload') {
|
Chris@0
|
55 const $preview = $(context).find('.content').removeOnce('node-preview');
|
Chris@0
|
56 if ($preview.length) {
|
Chris@0
|
57 $preview.off('click.preview');
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60 },
|
Chris@0
|
61 };
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Switch view mode.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @type {Drupal~behavior}
|
Chris@0
|
67 *
|
Chris@0
|
68 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
69 * Attaches automatic submit on `formUpdated.preview` events.
|
Chris@0
|
70 */
|
Chris@0
|
71 Drupal.behaviors.nodePreviewSwitchViewMode = {
|
Chris@0
|
72 attach(context) {
|
Chris@0
|
73 const $autosubmit = $(context).find('[data-drupal-autosubmit]').once('autosubmit');
|
Chris@0
|
74 if ($autosubmit.length) {
|
Chris@0
|
75 $autosubmit.on('formUpdated.preview', function () {
|
Chris@0
|
76 $(this.form).trigger('submit');
|
Chris@0
|
77 });
|
Chris@0
|
78 }
|
Chris@0
|
79 },
|
Chris@0
|
80 };
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Theme function for node preview modal.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return {string}
|
Chris@0
|
86 * Markup for the node preview modal.
|
Chris@0
|
87 */
|
Chris@0
|
88 Drupal.theme.nodePreviewModal = function () {
|
Chris@0
|
89 return `<p>${
|
Chris@0
|
90 Drupal.t('Leaving the preview will cause unsaved changes to be lost. Are you sure you want to leave the preview?')
|
Chris@0
|
91 }</p><small class="description">${
|
Chris@0
|
92 Drupal.t('CTRL+Left click will prevent this dialog from showing and proceed to the clicked link.')}</small>`;
|
Chris@0
|
93 };
|
Chris@0
|
94 }(jQuery, Drupal));
|