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 / dashboard.js

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

182 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Internal dependencies.
3 */
4 import Tooltip from '@10up/component-tooltip';
5
6 /**
7 * WordPress dependencies.
8 */
9 import { __ } from '@wordpress/i18n';
10
11 /**
12 * Window dependencies.
13 */
14 const {
15 ajaxurl,
16 epDash: { skipUrl, syncUrl },
17 } = window;
18
19 /**
20 * Determine whether a Feature's new settings will require a reindex.
21 *
22 * @param {FormData} data Form data.
23 * @returns {boolean} Whether a reindex will need to occur when saved.
24 */
25 const willChangeTriggerReindex = (data) => {
26 return (
27 data.get('requires_reindex') === '1' &&
28 data.get('was_active') === '0' &&
29 data.get('settings[active]') === '1'
30 );
31 };
32
33 /**
34 * Handle Feature settings being submitted.
35 *
36 * @param {Event} event Submit event.
37 */
38 const onSubmit = async (event) => {
39 event.preventDefault();
40
41 const form = event.target;
42 const data = new FormData(form);
43 const requiresConfirmation = willChangeTriggerReindex(data);
44
45 if (requiresConfirmation) {
46 /* eslint-disable no-alert */
47 const isConfirmed = window.confirm(
48 __(
49 'Enabling this feature will begin re-indexing your content. Do you wish to proceed?',
50 'elasticpress',
51 ),
52 );
53
54 if (!isConfirmed) {
55 return;
56 }
57 }
58
59 const feature = form.closest('.ep-feature');
60
61 feature.classList.add('saving');
62 form.submit.disabled = true;
63
64 const request = await fetch(ajaxurl, { method: 'POST', body: data });
65 const response = await request.json();
66
67 feature.classList.toggle('feature-active', response.data.active);
68
69 if (response.data.reindex) {
70 window.location = syncUrl;
71 } else {
72 feature.classList.remove('saving');
73 form.submit.disabled = false;
74 form.was_active.value = response.data.active ? '1' : '0';
75 }
76 };
77
78 /**
79 * Handle a Feature being set to be turned on or off.
80 *
81 * @param {Event} event Change event.
82 */
83 const onToggle = (event) => {
84 const { form } = event.target;
85 const data = new FormData(form);
86
87 const notice = form.querySelector('.requirements-status-notice--reindex');
88 const requiresConfirmation = willChangeTriggerReindex(data);
89
90 if (notice) {
91 notice.style.display = requiresConfirmation ? 'block' : null;
92 }
93 };
94
95 /**
96 * Handle click events within a Feature.
97 *
98 * @param {Event} event Click event.
99 */
100 const onClick = (event) => {
101 const { target } = event;
102
103 /**
104 * Handle toggling settings.
105 */
106 if (target.classList.contains('settings-button')) {
107 const feature = target.closest('.ep-feature');
108
109 feature.classList.toggle('show-settings');
110 target.setAttribute('aria-expanded', feature.classList.contains('show-settings'));
111 }
112
113 /**
114 * Handle toggling description.
115 */
116 if (target.classList.contains('learn-more') || target.classList.contains('collapse')) {
117 target.closest('.ep-feature').classList.toggle('show-full');
118 }
119 };
120
121 /**
122 * Handle setup form submission.
123 *
124 * Asks for confirmation if the user doesn't select any features to activate.
125 * If the user wants to continue then skip installation.
126 *
127 * @param {Event} event Submit event.
128 * @returns {void}
129 */
130 const onSubmitSetup = (event) => {
131 const features = new FormData(event.target).getAll('features[]');
132
133 /**
134 * If any features are selected continue as normal...
135 */
136 if (features.length > 0) {
137 return;
138 }
139
140 /**
141 * ...otherwise stop submission and ask for confirmation.
142 */
143 event.preventDefault();
144
145 const confirm = window.confirm(
146 __(
147 'It looks like you’re trying to use ElasticPress’s advanced features only. If you’d like to activate basic search, please select Cancel and activate the Post Search Feature. Otherwise, please click Ok to configure advanced features.',
148 'elasticpress',
149 ),
150 );
151
152 /**
153 * If the user wants to proceed, skip installation.
154 */
155 if (confirm) {
156 window.location = skipUrl;
157 }
158 };
159
160 /**
161 * Bind events.
162 */
163 const featuresEl = document.querySelector('.ep-features');
164
165 if (featuresEl) {
166 featuresEl.addEventListener('change', onToggle);
167 featuresEl.addEventListener('submit', onSubmit);
168 featuresEl.addEventListener('click', onClick);
169 }
170
171 const submitEl = document.querySelector('button.setup-button');
172
173 if (submitEl) {
174 submitEl.form.addEventListener('submit', onSubmitSetup);
175 }
176
177 /**
178 * Tooltips.
179 */
180 // eslint-disable-next-line no-new
181 new Tooltip('.a11y-tip');
182