| 1 |
|
| 2 |
(function( $ ) { |
| 3 |
'use strict'; |
| 4 |
let $form = $('#dynamic-select-tag-generator'), |
| 5 |
$tag = $('input.tag' , $form.closest('.control-box').siblings('.insert-box')), |
| 6 |
$button = $tag.siblings('.submitbox').find('input.insert-tag'), |
| 7 |
$select = $('select.taxonomy-list', $form), |
| 8 |
$plural = $('input[name="plural_name"]', $form), |
| 9 |
$single = $('input[name="singular_name"]', $form), |
| 10 |
$taxonomy = $('input[name="taxonomy_slug"]', $form), |
| 11 |
$is_cat = $('input[name="is_hierarchical"]', $form), |
| 12 |
$req = $('input[name="required"]', $form), |
| 13 |
$name = $('input[name="name"]', $form), |
| 14 |
$id = $('input[name="id"]', $form), |
| 15 |
$cl = $('input[name="class"]', $form), |
| 16 |
$post = $(' select.post-list', $form), |
| 17 |
selectType = 'select', |
| 18 |
multiple=''; |
| 19 |
|
| 20 |
$('select.post-list').on('change', function(){ |
| 21 |
$('div.post-taxonomies').hide(); |
| 22 |
//$('div.post-taxonomies').off('change'); |
| 23 |
let type = $(this).val(); |
| 24 |
$('div#'+type).show(); |
| 25 |
$('select.select2', $('div#'+type)).not('.select2-hidden-accessible').select2(); |
| 26 |
}); |
| 27 |
|
| 28 |
$form.on('change',':input', function(event){ |
| 29 |
let $target = $(event.target), |
| 30 |
$tab = $('input[name="sections"]:checked'), |
| 31 |
source = 'taxonomy'; |
| 32 |
|
| 33 |
$('#select-multiple').prop('disabled',false); |
| 34 |
|
| 35 |
if($target.is('select.taxonomy-list')){ |
| 36 |
let $option = $target.find('option:selected'); |
| 37 |
$taxonomy.val($target.val()); |
| 38 |
$plural.val($option.text()); |
| 39 |
$single.val($option.data('name')); |
| 40 |
//by default disable name fields. |
| 41 |
$plural.prop('disabled',true); |
| 42 |
$single.prop('disabled',true); |
| 43 |
$taxonomy.prop('disabled',true); |
| 44 |
$is_cat.parent().hide(); |
| 45 |
if($option.is('.cf7sg-new-taxonomy')){ |
| 46 |
$plural.prop('disabled',false); |
| 47 |
$single.prop('disabled',false); |
| 48 |
$taxonomy.prop('disabled',false); |
| 49 |
$is_cat.parent().show(); |
| 50 |
}else if($option.is('.cf7sg-taxonomy')){ |
| 51 |
$plural.prop('disabled',false); |
| 52 |
$single.prop('disabled',false); |
| 53 |
} |
| 54 |
}else if($target.is('input.select-type')){ //---------select-type |
| 55 |
selectType = $('input.select-type:checked').val(); |
| 56 |
if('select2' == selectType){ |
| 57 |
$('input#select2-tags', $form).prop('disabled', false); |
| 58 |
}else{ |
| 59 |
$('input#select2-tags', $form).prop('checked', false); |
| 60 |
$('input#select2-tags', $form).prop('disabled', true); |
| 61 |
} |
| 62 |
if( $target.is('#nice-select:checked') ){ |
| 63 |
//no multiple in nice-select. |
| 64 |
$('#select-multiple').prop('checked',false); |
| 65 |
$('#select-multiple').prop('disabled',true); |
| 66 |
} |
| 67 |
} |
| 68 |
/* which source ? */ |
| 69 |
if($tab.is('#taxonomy-tab')){ |
| 70 |
source = 'taxonomy'; |
| 71 |
}else if($tab.is('#post-tab')){ |
| 72 |
source = 'post'; |
| 73 |
}else if($tab.is('#custom-tab')){ |
| 74 |
source = 'filter'; |
| 75 |
$('a.helper.init', $tab.parent()).each(function(){ |
| 76 |
new Clipboard($(this)[0], { |
| 77 |
text: function(trigger) { |
| 78 |
return $(trigger).data('cf72post');; |
| 79 |
} |
| 80 |
}); |
| 81 |
$(this).removeClass('init'); |
| 82 |
}); |
| 83 |
} |
| 84 |
updateCF7Tag(source); |
| 85 |
}); |
| 86 |
//udpate the new category if created |
| 87 |
$button.on('click', function(){ |
| 88 |
let $option = $select.find('option:selected'); |
| 89 |
if($option.is('.cf7sg-new-taxonomy') ){ |
| 90 |
$option.after('<option data-name="'+$single.val()+'" value="'+$taxonomy.val()+'">'+$plural.val()+'</option>'); |
| 91 |
//$option.next().prop('selected', true); |
| 92 |
}else if($option.is('.cf7sg-taxonomy')){ |
| 93 |
$option.data('name', $single.val()); |
| 94 |
$option.text( $plural.val()); |
| 95 |
}else{ |
| 96 |
return true; //for other dynamic options just continue. |
| 97 |
} |
| 98 |
//store this new value in the hidden field |
| 99 |
let values = $('input#cf72post-dynamic-select').val(); |
| 100 |
if(0 == values.length){ |
| 101 |
values = []; |
| 102 |
}else{ |
| 103 |
values = JSON.parse(values); |
| 104 |
} |
| 105 |
values[values.length] = { |
| 106 |
"slug":$taxonomy.val(), |
| 107 |
"singular":$single.val(), |
| 108 |
"plural":$plural.val(), |
| 109 |
"hierarchical":$is_cat.is(':checked') |
| 110 |
}; |
| 111 |
$('input#cf72post-dynamic-select').val(JSON.stringify(values)); |
| 112 |
//rest the selection |
| 113 |
$plural.prop('disabled',true).val(''); |
| 114 |
$single.prop('disabled',true).val('');; |
| 115 |
$taxonomy.prop('disabled',true).val(''); |
| 116 |
$is_cat.prop('checked', false).parent().hide(); |
| 117 |
$select.val('');//reset the dropdown. |
| 118 |
}); |
| 119 |
|
| 120 |
|
| 121 |
function updateCF7Tag(source='taxonomy') { |
| 122 |
let id=$id.val(); |
| 123 |
if(id.length > 0) id =' id:'+id; |
| 124 |
|
| 125 |
let classes = $cl.val(); |
| 126 |
if(classes.length > 0){ |
| 127 |
let classArr = classes.split(','), idx; |
| 128 |
classes=''; |
| 129 |
for(idx=0; idx<classArr.length; idx++){ |
| 130 |
classes += " class:" + classArr[idx].trim() + " "; |
| 131 |
} |
| 132 |
} |
| 133 |
let values = '' |
| 134 |
switch(source){ |
| 135 |
case 'taxonomy': |
| 136 |
if($taxonomy.val().length > 0){ |
| 137 |
values = ' "slug:'+ $taxonomy.val()+'"'; |
| 138 |
} |
| 139 |
break; |
| 140 |
case 'post': |
| 141 |
if($post.val().length > 0){ |
| 142 |
let $tax = $('div#'+$post.val()+' > select.select2'); |
| 143 |
//.val(); |
| 144 |
values = ' "source:post:'+$post.val()+'"'; |
| 145 |
if(null != $tax.val()){ |
| 146 |
let term=''; |
| 147 |
for(term of $tax.val()){ |
| 148 |
values += ' "'+term+'"'; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
break; |
| 153 |
case 'filter': |
| 154 |
values = ' "source:filter"'; |
| 155 |
break; |
| 156 |
} |
| 157 |
//select type |
| 158 |
switch(selectType){ |
| 159 |
case 'nice': |
| 160 |
classes+= ' class:nice-select'; |
| 161 |
break; |
| 162 |
case 'select2': |
| 163 |
classes += ' class:select2'; |
| 164 |
if($('input#select2-tags').is(':checked')){ |
| 165 |
classes += ' class:tags'; |
| 166 |
} |
| 167 |
break; |
| 168 |
case 'standard': |
| 169 |
default: |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
//update tag. |
| 174 |
let type = 'dynamic_select '; |
| 175 |
if($('#select-multiple').is(':checked')) multiple=' multiple'; |
| 176 |
if($req.is(':checked')) type = 'dynamic_select* '; |
| 177 |
$tag.val('[' + type + $name.val() + multiple + id + classes + values +']'); |
| 178 |
} |
| 179 |
})( jQuery ); |
| 180 |
|