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