Skip to content

Instantly share code, notes, and snippets.

@karlhinze
Created October 22, 2018 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlhinze/4d8df618327c5bbaf1f466deb12dcb32 to your computer and use it in GitHub Desktop.
Save karlhinze/4d8df618327c5bbaf1f466deb12dcb32 to your computer and use it in GitHub Desktop.
// JS functionality for a redundant checkbox above the group suggest selector box, providing a checkbox for users to select to a specific chosen group (like the homepage or main communications team).
(function($, LW) {
// Make adjustments below to match the group id and group name
// to the calendar group you want to use for this feature.
var group_id = '8';
var group_name = 'Homepage';
if (LW.page === 'events_edit' || LW.page === 'events_sub_edit' || LW.page === 'news_edit') { // define pages to target with this transformation
if (livewhale.group_title != group_name) { // if we're not in the target group
// suggest to main calendar checkbox
var $share_checkbox = $('#main_group_share input[type="checkbox"]');
var $suggest = $('.group_suggest').bind('multisuggestchange', function(e) {
var selected = $suggest.multisuggest('getSelected'),
main_exists = (_.findIndex(selected, { id: group_id }) > -1);
if (main_exists && !$share_checkbox.prop('checked')) {
$share_checkbox.prop('checked', true);
}
if (!main_exists && $share_checkbox.prop('checked')) {
$share_checkbox.prop('checked', false);
}
});
$share_checkbox.click(function() {
var checked = $(this).prop('checked'),
selected, main_exists;
if ($(this).prop('checked')) {
selected = $suggest.multisuggest('getSelected');
main_exists = (_.findIndex(selected, { id: group_id }) > -1);
if (!main_exists) {
$suggest.multisuggest('addItem', { id: group_id, title: group_name });
}
} else {
$suggest.multisuggest('removeItem', group_id);
}
});
} else { // we are in the target group, hide suggestion
$('#main_group_share').hide();
}
}
}(livewhale.jQuery, livewhale));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment