PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / assets / js / mainwp-groups.js

mainwp-groups.js in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.2, at assets/js/mainwp-groups.js

360 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(function () {
2
3 // Auto-select newly created group from URL parameter
4 let urlParams = new URLSearchParams(globalThis.location.search);
5 let newGroupId = urlParams.get('new-tag');
6
7 jQuery(document).ready(function () {
8 if (newGroupId) {
9 // Find the menu item with the new group ID
10 let newGroupItem = jQuery('#mainwp-groups-menu').find('a.item#' + newGroupId);
11
12 if (newGroupItem.length > 0) {
13 // Trigger click on the new group item to select it
14 newGroupItem.trigger('click');
15 // Show success message
16 feedback('mainwp-message-zone', 'Tag created successfully.', 'ui green message');
17
18 // Auto-hide message after 5 seconds
19 setTimeout(() => {
20 jQuery('#mainwp-message-zone').fadeOut();
21 }, 5000);
22
23 // Remove the URL parameter without reloading the page
24 let cleanUrl = globalThis.location.pathname + globalThis.location.search.replace(/[?&]new-tag=[^&]+/, '').replace(/^&/, '?');
25 if (cleanUrl.endsWith('?')) {
26 cleanUrl = cleanUrl.slice(0, -1);
27 }
28 globalThis.history.replaceState({}, document.title, cleanUrl);
29 }
30 }
31 })
32
33 // Init the groups menu
34 jQuery('#mainwp-groups-menu').find('a.item').on('click', function () {
35 jQuery(this).addClass('active green');
36 jQuery(this).siblings().removeClass('active green');
37 jQuery(this).find('.label').addClass('green');
38 jQuery(this).siblings().find('.label').removeClass('green');
39 jQuery('#mainwp-delete-group-button').removeClass('disabled');
40 jQuery('#mainwp-rename-group-button').removeClass('disabled');
41 jQuery('#mainwp-save-sites-groups-selection-button').removeClass('disabled');
42 jQuery('#mainwp-message-zone').fadeOut();
43 show_group_items(this);
44 apply_tag_fade_effect(this);
45
46 });
47
48
49 // Trigger the create a new group modal
50 jQuery(document).on('click', '#mainwp-new-sites-group-button', function () {
51 jQuery('#mainwp-create-group-modal').modal({
52 onHide: function () {
53 mainwp_forceReload();
54 },
55 onShow: function () {
56 jQuery('#mainwp-create-group-modal').find('input[name="mainwp-group-name"]').val('');
57 jQuery('#mainwp-create-group-modal').find('input#mainwp-new-tag-color').val('');
58 // Disable button initially.
59 jQuery('#mainwp-save-new-group-button').addClass('disabled');
60 }
61 }).modal('show');
62 });
63
64 // Enable/disable create button based on input validation
65 jQuery(document).on('input', '#mainwp-create-group-modal input[name="mainwp-group-name"], #mainwp-rename-group-modal input[name="mainwp-group-name"]', function () {
66 let groupName = jQuery(this).val().trim();
67 if (groupName.length > 0) {
68 jQuery('#mainwp-save-new-group-button').removeClass('disabled');
69 jQuery('#mainwp-update-new-group-button').removeClass('disabled');
70 } else {
71 jQuery('#mainwp-save-new-group-button').addClass('disabled');
72 jQuery('#mainwp-update-new-group-button').addClass('disabled');
73 }
74 });
75
76 // Enable update button when color changes in rename modal
77 jQuery(document).on('input change', '#mainwp-rename-group-modal input#mainwp-new-tag-color', function () {
78 let groupName = jQuery('#mainwp-rename-group-modal').find('input[name="mainwp-group-name"]').val().trim();
79 if (groupName.length > 0) {
80 jQuery('#mainwp-update-new-group-button').removeClass('disabled');
81 }
82 });
83
84 // Trigger the edit group modal
85 jQuery(document).on('click', '#mainwp-rename-group-button', function () {
86 jQuery('#mainwp-rename-group-modal').modal({
87 onHide: function () {
88 mainwp_forceReload();
89 },
90 onShow: function () {
91 let groupName = jQuery('#mainwp-groups-menu').find('.active').find('#mainwp-hidden-group-name').val();
92 let groupColor = jQuery('#mainwp-groups-menu').find('.active').find('#mainwp-hidden-group-color').val();
93 jQuery('#mainwp-rename-group-modal').find('input[name="mainwp-group-name"]').val(groupName);
94 jQuery('#mainwp-rename-group-modal').find('input#mainwp-new-tag-color').val(groupColor);
95 // Disable button initially.
96 jQuery('#mainwp-update-new-group-button').addClass('disabled');
97 }
98 }).modal('show');
99 });
100
101 // Create a new group
102 jQuery(document).on('click', '#mainwp-save-new-group-button', function (e) {
103
104 let $button = jQuery(this);
105 let $modal = jQuery('#mainwp-create-group-modal');
106 let newName = $modal.find('input[name="mainwp-group-name"]').val().trim();
107 let newColor = $modal.find('input#mainwp-new-tag-color').val();
108
109 // Validate input
110 if (newName.length === 0) {
111 return false;
112 }
113
114 // Check if button is already disabled (loading)
115 if ($button.hasClass('disabled') || $button.data('loading')) {
116 return false;
117 }
118
119 let selected_sites = [];
120 jQuery("input[name='sites']:checked").each(function () {
121 selected_sites.push(jQuery(this).val());
122 });
123
124 // Disable button and show loading state.
125 let originalText = $button.html();
126 $button.data('loading', true);
127 $button.addClass('disabled');
128 $button.html('<i class="spinner loading icon"></i> Creating...');
129 $modal.find('input').prop('disabled', true);
130
131 let data = mainwp_secure_data({
132 //action: 'mainwp_group_add',
133 action: 'mainwp_group_sites_add',
134 selected_sites: selected_sites,
135 newName: newName,
136 newColor: newColor
137 });
138 jQuery.post(ajaxurl, data, function (response) {
139 try {
140 let resp = JSON.parse(response);
141
142 if (resp.error != undefined) {
143 // Remove loading state on error
144 $button.data("loading", false);
145 $button.removeClass("disabled");
146 $button.html(originalText);
147 $modal.find("input").prop("disabled", false);
148 return;
149 }
150
151 globalThis.location.href = "admin.php?page=ManageGroups&new-tag=" + resp.success;
152 } catch {
153 // to fix js error.
154 // Remove loading state on error
155 $button.data("loading", false);
156 $button.removeClass("disabled");
157 $button.html(originalText);
158 $modal.find("input").prop("disabled", false);
159 }
160 });
161 e.preventDefault();
162 e.stopPropagation();
163 return true;
164 });
165
166 // Delete a Tag
167 jQuery(document).on('click', '#mainwp-delete-group-button', function () {
168 let gruopItem = jQuery('#mainwp-groups-menu').find('.active');
169
170 let _deltag_callback = function () {
171 let groupID = gruopItem.attr('id');
172 let data = mainwp_secure_data({
173 action: 'mainwp_group_delete',
174 groupId: groupID
175 });
176 jQuery.post(ajaxurl, data, function (response) {
177 response = response.trim();
178 if (response == 'OK') {
179 mainwp_forceReload();
180 }
181 });
182 };
183
184 mainwp_confirm('Are you sure you want to delete this tag?', _deltag_callback);
185 return false;
186 });
187
188 // Update group name
189 jQuery(document).on('click', '#mainwp-update-new-group-button', function () {
190 let $button = jQuery(this);
191 let $modal = jQuery('#mainwp-rename-group-modal');
192
193 let groupID = jQuery('#mainwp-groups-menu').find('.active').attr('id');
194 let newName = $modal.find('input[name="mainwp-group-name"]').val();
195 let newColor = $modal.find('input#mainwp-new-tag-color').val();
196 let data = mainwp_secure_data({
197 action: 'mainwp_group_rename',
198 groupId: groupID,
199 newName: newName,
200 newColor: newColor
201 });
202
203 // Disable button and show loading state.
204 let originalText = $button.html();
205 $button.data('loading', true);
206 $button.addClass('disabled');
207 $button.html('<i class="spinner loading icon"></i> Updating...');
208 $modal.find('input').prop('disabled', true);
209
210
211 jQuery.post(ajaxurl, data, function (response) {
212 if (response.error) {
213 // Remove loading state on error
214 $button.data("loading", false);
215 $button.removeClass("disabled");
216 $button.html(originalText);
217 $modal.find("input").prop("disabled", false);
218 return;
219 }
220 jQuery('#mainwp-create-group-modal').modal({
221 onHide: function () {
222 mainwp_forceReload();
223 return false;
224 }
225 }).modal('hide');
226 }, 'json');
227
228 return false;
229 });
230
231 // Select all sites
232 jQuery('#mainwp-manage-groups-sites-table th input[type="checkbox"]').on('change', function () {
233 let checkboxes = jQuery('#mainwp-manage-groups-sites-table').find(':checkbox');
234 if (jQuery(this).prop('checked')) {
235 checkboxes.prop('checked', true);
236 checkboxes.parents('tr').addClass('selected active');
237 } else {
238 checkboxes.prop('checked', false);
239 checkboxes.parents('tr').removeClass('selected active');
240 }
241 });
242
243 // Set class 'active' to selected sites table row
244 jQuery('.mainwp-site-checkbox').on('change', function () {
245 if (jQuery(this).prop('checked')) {
246 jQuery(this).parents('tr').addClass('selected active');
247 } else {
248 jQuery(this).parents('tr').removeClass('selected active');
249 }
250 });
251
252 // Keep update selected tag site ids.
253 jQuery(document).on('change', '#mainwp-manage-groups-sites-table .mainwp-site-checkbox', function () {
254 let tag_siteids = jQuery('#mainwp-save-sites-groups-selection-button').attr('selected-tag-siteids');
255 if (tag_siteids !== undefined) {
256 tag_siteids = '' == tag_siteids ? [] : JSON.parse(tag_siteids);
257
258 const valSiteId = jQuery(this).val();
259 if (jQuery(this).is(':checked')) {
260 if (!tag_siteids.includes(valSiteId)) {
261 tag_siteids.push(valSiteId);
262 }
263 } else {
264 const index = tag_siteids.indexOf(valSiteId);
265 if (index !== -1) {
266 tag_siteids.splice(index, 1);
267 }
268 }
269
270 jQuery('#mainwp-save-sites-groups-selection-button').attr('selected-tag-siteids', tag_siteids.length ? JSON.stringify(tag_siteids) : '')
271 }
272 });
273
274
275 // Save selected sites for a group
276 jQuery(document).on('click', '#mainwp-save-sites-groups-selection-button', function (e) {
277
278 e.preventDefault();
279
280 let groupID = jQuery('#mainwp-groups-menu').find('.active').attr('id');
281 let tag_siteids = jQuery('#mainwp-save-sites-groups-selection-button').attr('selected-tag-siteids');
282
283 if (groupID == undefined) {
284 return;
285 }
286 let data = mainwp_secure_data({
287 action: 'mainwp_group_updategroup',
288 groupId: groupID,
289 websiteIds: tag_siteids == '' ? [] : JSON.parse(tag_siteids),
290 });
291
292 jQuery(this).addClass('disabled');
293
294 jQuery.post(ajaxurl, data, function (response) {
295 jQuery('#mainwp-save-sites-groups-selection-button').removeClass('disabled');
296 if (response?.result === true) {
297 feedback('mainwp-message-zone', 'Selection saved successfully.', 'ui green message');
298 setTimeout(() => {
299 jQuery('#mainwp-message-zone').fadeOut();
300 }, 5000);
301 } else {
302 feedback('mainwp-message-zone', 'Undefined error occurred. Please try again.', 'ui green message');
303 }
304 }, 'json');
305 });
306
307 // Load group sites
308 let show_group_items = function (group) {
309 let groupID = jQuery(group).attr('id');
310 let data = mainwp_secure_data({
311 action: 'mainwp_group_getsites',
312 groupId: groupID
313 });
314 jQuery('.dimmer').addClass('active');
315 let dtApi = jQuery('#mainwp-manage-groups-sites-table').dataTable().api();
316 let searchValue = dtApi.search();
317 dtApi.search('').draw();
318 jQuery.post(ajaxurl, data, function (response) {
319 jQuery('.dimmer').removeClass('active');
320 response = response.trim();
321 if (response == 'ERROR') {
322 dtApi.search(searchValue).draw();
323 return;
324 }
325 mainwp_datatable_fix_to_update_selected_rows_status(dtApi, 'deselected'); // clear saved state.
326 jQuery('input.mainwp-site-checkbox').prop('checked', false);
327 jQuery('input.mainwp-site-checkbox').closest('tr').removeClass('selected active');
328 let sites = JSON.parse(response);
329 for (let id of sites) {
330 jQuery('input[value="' + id + '"].mainwp-site-checkbox').prop('checked', true);
331 jQuery('input[value="' + id + '"].mainwp-site-checkbox').closest('tr').addClass('selected active');
332 }
333 jQuery('#mainwp-save-sites-groups-selection-button').attr('selected-tag-siteids', sites?.length ? response : '');
334 mainwp_datatable_fix_to_update_selected_rows_status(dtApi, 'selected active'); // clear saved state.
335 dtApi.search(searchValue).draw();
336 });
337
338 return false;
339 }
340
341 // Apply fade effect to tags in the sites table
342 let apply_tag_fade_effect = function (menuItem) {
343 // Get the selected tag ID from the menu item
344 let selectedTagId = jQuery(menuItem).attr('id');
345
346 if (!selectedTagId) {
347 // If no tag is selected, reset all tags to full opacity
348 jQuery('#mainwp-manage-groups-sites-table span.ui.tag.label').css('opacity', '1');
349 return;
350 }
351
352 // Fade all tags to 10% opacity
353 jQuery('#mainwp-manage-groups-sites-table span.ui.tag.label').css('opacity', '0.1');
354
355 // Restore full opacity for the selected tag
356 jQuery('#mainwp-manage-groups-sites-table span.ui.tag.label[tag_id="' + selectedTagId + '"]').css('opacity', '1');
357 }
358
359 });
360