| 1 |
jQuery(document).ready(function () { |
| 2 |
|
| 3 |
// Init the groups menu |
| 4 |
jQuery('#mainwp-groups-menu').find('a.item').on('click', function () { |
| 5 |
jQuery(this).addClass('active green'); |
| 6 |
jQuery(this).siblings().removeClass('active green'); |
| 7 |
jQuery(this).find('.label').addClass('green'); |
| 8 |
jQuery(this).siblings().find('.label').removeClass('green'); |
| 9 |
jQuery('#mainwp-delete-group-button').removeClass('disabled'); |
| 10 |
jQuery('#mainwp-rename-group-button').removeClass('disabled'); |
| 11 |
jQuery('#mainwp-save-sites-groups-selection-button').removeClass('disabled'); |
| 12 |
show_group_items(this); |
| 13 |
}); |
| 14 |
|
| 15 |
// Trigger the create a new group modal |
| 16 |
jQuery(document).on('click', '#mainwp-new-sites-group-button', function () { |
| 17 |
jQuery('#mainwp-create-group-modal').modal({ |
| 18 |
onHide: function () { |
| 19 |
window.location.href = location.href; |
| 20 |
}, |
| 21 |
onShow: function () { |
| 22 |
jQuery('#mainwp-create-group-modal').find('input#mainwp-group-name').val(''); |
| 23 |
jQuery('#mainwp-create-group-modal').find('input#mainwp-new-tag-color').val(''); |
| 24 |
} |
| 25 |
}).modal('show'); |
| 26 |
}); |
| 27 |
|
| 28 |
// Trigger the edit group modal |
| 29 |
jQuery(document).on('click', '#mainwp-rename-group-button', function () { |
| 30 |
jQuery('#mainwp-rename-group-modal').modal({ |
| 31 |
onHide: function () { |
| 32 |
window.location.href = location.href; |
| 33 |
}, |
| 34 |
onShow: function () { |
| 35 |
var groupName = jQuery('#mainwp-groups-menu').find('.active').find('#mainwp-hidden-group-name').val(); |
| 36 |
var groupColor = jQuery('#mainwp-groups-menu').find('.active').find('#mainwp-hidden-group-color').val(); |
| 37 |
jQuery('#mainwp-rename-group-modal').find('input#mainwp-group-name').val(groupName); |
| 38 |
jQuery('#mainwp-rename-group-modal').find('input#mainwp-new-tag-color').val(groupColor); |
| 39 |
var $renameColor = jQuery('#mainwp-rename-group-modal .mainwp-tag-color-picker').wpColorPicker({ |
| 40 |
hide: true, |
| 41 |
clear: false, |
| 42 |
palettes: ['#18a4e0', '#0253b3', '#7fb100', '#446200', '#ad0000', '#ffd300', '#2d3b44', '#6435c9', '#e03997', '#00b5ad'], |
| 43 |
}); |
| 44 |
$renameColor.wpColorPicker('color', groupColor); |
| 45 |
} |
| 46 |
}).modal('show'); |
| 47 |
}); |
| 48 |
|
| 49 |
// Create a new group |
| 50 |
jQuery(document).on('click', '#mainwp-save-new-group-button', function () { |
| 51 |
var newName = jQuery('#mainwp-create-group-modal').find('input#mainwp-group-name').val(); |
| 52 |
var newColor = jQuery('#mainwp-create-group-modal').find('input#mainwp-new-tag-color').val(); |
| 53 |
|
| 54 |
var data = mainwp_secure_data({ |
| 55 |
action: 'mainwp_group_add', |
| 56 |
newName: newName, |
| 57 |
newColor: newColor |
| 58 |
}); |
| 59 |
jQuery.post(ajaxurl, data, function (response) { |
| 60 |
try { |
| 61 |
resp = JSON.parse(response); |
| 62 |
|
| 63 |
if (resp.error != undefined) |
| 64 |
return; |
| 65 |
} catch (err) { |
| 66 |
// to fix js error. |
| 67 |
} |
| 68 |
jQuery('#mainwp-create-group-modal').modal({ |
| 69 |
onHide: function () { |
| 70 |
window.location.reload(); |
| 71 |
} |
| 72 |
}).modal('hide'); |
| 73 |
}); |
| 74 |
return false; |
| 75 |
}); |
| 76 |
|
| 77 |
// Delete a group |
| 78 |
jQuery(document).on('click', '#mainwp-delete-group-button', function () { |
| 79 |
var gruopItem = jQuery('#mainwp-groups-menu').find('.active'); |
| 80 |
mainwp_confirm('Are you sure you want to delete this group?', function () { |
| 81 |
var groupID = gruopItem.attr('id'); |
| 82 |
var data = mainwp_secure_data({ |
| 83 |
action: 'mainwp_group_delete', |
| 84 |
groupId: groupID |
| 85 |
}); |
| 86 |
jQuery.post(ajaxurl, data, function (gruopItem) { |
| 87 |
return function (response) { |
| 88 |
response = response.trim(); |
| 89 |
if (response == 'OK') { |
| 90 |
gruopItem.fadeOut(300); |
| 91 |
} |
| 92 |
} |
| 93 |
}(gruopItem)); |
| 94 |
}); |
| 95 |
return false; |
| 96 |
}); |
| 97 |
|
| 98 |
// Update group name |
| 99 |
jQuery(document).on('click', '#mainwp-update-new-group-button', function () { |
| 100 |
var groupID = jQuery('#mainwp-groups-menu').find('.active').attr('id'); |
| 101 |
var newName = jQuery('#mainwp-rename-group-modal').find('input#mainwp-group-name').val(); |
| 102 |
var newColor = jQuery('#mainwp-rename-group-modal').find('input#mainwp-new-tag-color').val(); |
| 103 |
var data = mainwp_secure_data({ |
| 104 |
action: 'mainwp_group_rename', |
| 105 |
groupId: groupID, |
| 106 |
newName: newName, |
| 107 |
newColor: newColor |
| 108 |
}); |
| 109 |
jQuery.post(ajaxurl, data, function () { |
| 110 |
return function (response) { |
| 111 |
if (response.error) { |
| 112 |
return; |
| 113 |
} |
| 114 |
jQuery('#mainwp-create-group-modal').modal({ |
| 115 |
onHide: function () { |
| 116 |
window.location.reload(); |
| 117 |
return false; |
| 118 |
} |
| 119 |
}).modal('hide'); |
| 120 |
} |
| 121 |
}(), 'json'); |
| 122 |
return false; |
| 123 |
}); |
| 124 |
|
| 125 |
// Select all sites |
| 126 |
jQuery('#mainwp-manage-groups-sites-table th input[type="checkbox"]').on('change', function () { |
| 127 |
var checkboxes = jQuery('#mainwp-manage-groups-sites-table').find(':checkbox'); |
| 128 |
if (jQuery(this).prop('checked')) { |
| 129 |
checkboxes.prop('checked', true); |
| 130 |
checkboxes.parents('tr').addClass('active'); |
| 131 |
} else { |
| 132 |
checkboxes.prop('checked', false); |
| 133 |
checkboxes.parents('tr').removeClass('active'); |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
// Set class 'active' to selected sites table row |
| 138 |
jQuery('.mainwp-site-checkbox').on('change', function () { |
| 139 |
if (jQuery(this).prop('checked')) { |
| 140 |
jQuery(this).parents('tr').addClass('active'); |
| 141 |
} else { |
| 142 |
jQuery(this).parents('tr').removeClass('active'); |
| 143 |
} |
| 144 |
}); |
| 145 |
|
| 146 |
// Save selected sites for a group |
| 147 |
jQuery(document).on('click', '#mainwp-save-sites-groups-selection-button', function () { |
| 148 |
var groupID = jQuery('#mainwp-groups-menu').find('.active').attr('id'); |
| 149 |
var sites = jQuery('#mainwp-manage-groups-sites-table').find('input.mainwp-site-checkbox:checked'); |
| 150 |
var sitesIDs = []; |
| 151 |
|
| 152 |
for (var i = 0; i < sites.length; i++) { |
| 153 |
sitesIDs.push(jQuery(sites[i]).val()); |
| 154 |
} |
| 155 |
|
| 156 |
if (groupID == undefined) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
var data = mainwp_secure_data({ |
| 161 |
action: 'mainwp_group_updategroup', |
| 162 |
groupId: groupID, |
| 163 |
websiteIds: sitesIDs |
| 164 |
}); |
| 165 |
|
| 166 |
jQuery(this).addClass('disabled'); |
| 167 |
|
| 168 |
jQuery.post(ajaxurl, data, function () { |
| 169 |
jQuery(this).removeClass('disabled'); |
| 170 |
jQuery('#mainwp-message-zone').stop(true, true); |
| 171 |
jQuery('#mainwp-message-zone').show(); |
| 172 |
jQuery('#mainwp-message-zone').fadeOut(3000); |
| 173 |
return; |
| 174 |
}, 'json'); |
| 175 |
|
| 176 |
}); |
| 177 |
|
| 178 |
// Load group sites |
| 179 |
show_group_items = function (group) { |
| 180 |
var groupID = jQuery(group).attr('id'); |
| 181 |
var data = mainwp_secure_data({ |
| 182 |
action: 'mainwp_group_getsites', |
| 183 |
groupId: groupID |
| 184 |
}); |
| 185 |
jQuery('.dimmer').addClass('active'); |
| 186 |
jQuery.post(ajaxurl, data, function (response) { |
| 187 |
jQuery('.dimmer').removeClass('active'); |
| 188 |
response = response.trim(); |
| 189 |
if (response == 'ERROR') { |
| 190 |
return; |
| 191 |
} |
| 192 |
jQuery('input.mainwp-site-checkbox').prop('checked', false); |
| 193 |
jQuery('input.mainwp-site-checkbox').closest('tr').removeClass('active'); |
| 194 |
var sites = JSON.parse(response); |
| 195 |
for (var i = 0; i < sites.length; i++) { |
| 196 |
jQuery('input[value="' + sites[i] + '"].mainwp-site-checkbox').prop('checked', true); |
| 197 |
jQuery('input[value="' + sites[i] + '"].mainwp-site-checkbox').closest('tr').addClass('active'); |
| 198 |
} |
| 199 |
}); |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
}); |
| 204 |
|