| 1 |
/* global jQuery, beyondwords */ |
| 2 |
|
| 3 |
'use strict'; |
| 4 |
|
| 5 |
jQuery( document ).ready( function ( $ ) { |
| 6 |
$( document ).on( 'click', 'input[name="post_category[]"]', function () { |
| 7 |
const postType = beyondwords.postType; |
| 8 |
const preselect = |
| 9 |
typeof beyondwords.preselect === 'object' && |
| 10 |
beyondwords.preselect !== null |
| 11 |
? beyondwords.preselect |
| 12 |
: {}; |
| 13 |
|
| 14 |
if ( ! ( postType in preselect ) ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
// Exit if Generate audio is not being handled by taxonomies |
| 19 |
if ( ! ( 'category' in preselect[ postType ] ) ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
// Exit if category is not an array |
| 24 |
if ( ! Array.isArray( preselect[ postType ].category ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
const $checkbox = $( 'input#beyondwords_generate_audio' ); |
| 29 |
|
| 30 |
if ( ! $checkbox ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// Get ALL currently-checked categories |
| 35 |
const checkedCategories = $( 'input[name="post_category[]"]:checked' ) |
| 36 |
.map( function () { |
| 37 |
return this.value; |
| 38 |
} ) |
| 39 |
.get(); |
| 40 |
|
| 41 |
const intersections = checkedCategories.filter( |
| 42 |
( e ) => preselect[ postType ].category.indexOf( e ) !== -1 |
| 43 |
); |
| 44 |
|
| 45 |
if ( intersections.length ) { |
| 46 |
$checkbox.prop( 'checked', true ); |
| 47 |
} else { |
| 48 |
$checkbox.prop( 'checked', false ); |
| 49 |
} |
| 50 |
} ); |
| 51 |
} ); |
| 52 |
|