PluginProbe
ElasticPress / 4.2.0
ElasticPress v4.2.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / assets / js / notice.js

notice.js in ElasticPress 4.2.0, at assets/js/notice.js

65 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * WordPress dependencies.
3 */
4 import apiFetch from '@wordpress/api-fetch';
5 import domReady from '@wordpress/dom-ready';
6
7 /**
8 * Window dependencies.
9 */
10 const { epAdmin, ajaxurl } = window;
11
12 /**
13 * Initialize.
14 *
15 * @returns {void}
16 */
17 const init = () => {
18 const notices = document.querySelectorAll('.notice[data-ep-notice]');
19
20 /**
21 * Handle clicking in an ElasticPress notice.
22 *
23 * If the click target is the dismiss button send an AJAX request to remember
24 * the dismissal.
25 *
26 * @param {Event} event Click event.
27 * @returns {void}
28 */
29 const onClick = (event) => {
30 /**
31 * Only proceed if we're clicking dismiss.
32 */
33 if (!event.target.classList.contains('notice-dismiss')) {
34 return;
35 }
36
37 /**
38 * Handler is admin-ajax.php, so the body needs to be form data.
39 */
40 const formData = new FormData();
41
42 formData.append('action', 'ep_notice_dismiss');
43 formData.append('notice', event.currentTarget.dataset.epNotice);
44 formData.append('nonce', epAdmin.nonce);
45
46 apiFetch({
47 method: 'POST',
48 url: ajaxurl,
49 body: formData,
50 });
51 };
52
53 /**
54 * Bind click events to notices.
55 */
56 for (const notice of notices) {
57 notice.addEventListener('click', onClick);
58 }
59 };
60
61 /**
62 * Initialize.
63 */
64 domReady(init);
65