| 1 |
|
| 2 |
(function( $ ) { |
| 3 |
'use strict'; |
| 4 |
|
| 5 |
$('.cf7sg-dynamic-list-tag-manager').change(':input', function(e){ |
| 6 |
let $target = $(e.target), |
| 7 |
$form = $target.closest('.cf7sg-dynamic-list-tag-manager'), |
| 8 |
tagname = $form.data('tag'), |
| 9 |
$tab = $('input[name="sections"]:checked', $form), |
| 10 |
$is_cat = $('input[name="is_hierarchical"]', $form), |
| 11 |
$taxonomy = $('input[name="taxonomy_slug"]', $form), |
| 12 |
$plural = $('input[name="plural_name"]', $form), |
| 13 |
$single = $('input[name="singular_name"]', $form), |
| 14 |
$name = $('input[name="name"]', $form), |
| 15 |
$hasNesting = $('#enable-branches'), |
| 16 |
source = 'taxonomy', |
| 17 |
selectType = $('.list-style:checked', $form).val(); |
| 18 |
|
| 19 |
switch(true){ |
| 20 |
case $target.is('select.post-list'): |
| 21 |
$('div.post-taxonomies.cf7sg-dynamic-tag',$form).hide(); |
| 22 |
let $tax = $('.post-taxonomies.cf7sg-dynamic-tag.'+e.target.value, $form).show(); |
| 23 |
//enable select2 if not yet initialised. |
| 24 |
$('select.select2', $tax).not('.select2-hidden-accessible').select2({ |
| 25 |
placeholder: "Filter by term", |
| 26 |
multiple:true, |
| 27 |
dropdownParent:$tax, |
| 28 |
allowClear:true, |
| 29 |
forceAbove:true, |
| 30 |
templateSelection: function (state) { |
| 31 |
if (!state.id) { |
| 32 |
return state.text; |
| 33 |
} |
| 34 |
let label = state.element.closest('optgroup'); |
| 35 |
if(label) label = label.getAttribute('label'); |
| 36 |
else label=''; |
| 37 |
return $('<span>'+label+':'+state.text+'</span>'); |
| 38 |
} |
| 39 |
}); |
| 40 |
break; |
| 41 |
case $target.is('select.taxonomy-list'): |
| 42 |
let $option = $target.find('option:selected'); |
| 43 |
$taxonomy.val($target.val()); |
| 44 |
$plural.val($option.text()); |
| 45 |
$single.val($option.data('name')); |
| 46 |
//by default disable name fields. |
| 47 |
$plural.prop('disabled',true); |
| 48 |
$single.prop('disabled',true); |
| 49 |
$taxonomy.prop('disabled',true); |
| 50 |
$is_cat.parent().hide(); |
| 51 |
if($option.is('.cf7sg-new-taxonomy')){ |
| 52 |
$plural.prop('disabled',false); |
| 53 |
$single.prop('disabled',false); |
| 54 |
$taxonomy.prop('disabled',false); |
| 55 |
$is_cat.parent().show(); |
| 56 |
}else if($option.is('.cf7sg-taxonomy')){ |
| 57 |
$plural.prop('disabled',false); |
| 58 |
$single.prop('disabled',false); |
| 59 |
} |
| 60 |
/** @since 4.11 allow nesting of lists */ |
| 61 |
if($option.is('.hierarchical') && $hasNesting) $hasNesting.show(); |
| 62 |
else if($hasNesting){ |
| 63 |
$('input',$hasNesting).prop('checked',false); |
| 64 |
$hasNesting.hide(); |
| 65 |
} |
| 66 |
break; |
| 67 |
case $target.is('.list-style'): //---------list-type |
| 68 |
$target.closest('td.cf7sg-dl-styles').find('span.cf7sg-se-option').hide().find(':input').prop('checked', false).val(''); //rest all/ |
| 69 |
break; |
| 70 |
case $target.is('.source-tab'): //source selection. |
| 71 |
break; |
| 72 |
} |
| 73 |
/* which source ? */ |
| 74 |
if($tab.is('.taxonomy-tab')){ |
| 75 |
source = 'taxonomy'; |
| 76 |
}else if($tab.is('.post-tab')){ |
| 77 |
source = 'post'; |
| 78 |
}else if($tab.is('.custom-tab')){ |
| 79 |
source = 'filter'; |
| 80 |
} |
| 81 |
//set filters. |
| 82 |
$('p.filter-hook a', $form).each(function(){ |
| 83 |
let $a=$(this); |
| 84 |
if( !$a.is('.init') ){ |
| 85 |
$a.attr('data-cf72post', $('#fieldhelperdiv li.'+this.classList+' a').data('cf72post') ); |
| 86 |
$a.addClass('init').addClass('helper'); |
| 87 |
} |
| 88 |
new Clipboard($a[0], { |
| 89 |
text: function(t) { |
| 90 |
let $f = $(t); |
| 91 |
let text = $f.data('cf72post'); |
| 92 |
//get post slug |
| 93 |
let key = $('#post_name').val(); |
| 94 |
text = text.replace(/\{\$form_key\}/gi, key); |
| 95 |
text = text.replace(/\{\$field_type\}/gi, tagname); |
| 96 |
text = text.replace(/\{\$field_name\}/gi, $name.val()); |
| 97 |
text = text.replace(/\{\$field_name_slug\}/gi, $name.val().replace(/\-/g,'_')); |
| 98 |
text = text.replace(/\[dqt\]/gi, '"'); |
| 99 |
return text; |
| 100 |
} |
| 101 |
}) |
| 102 |
}); |
| 103 |
//update tag field. |
| 104 |
let $req = $('input[name="required"]', $form), |
| 105 |
id = $('input[name="id"]', $form).val(), |
| 106 |
classes = $('input[name="class"]', $form).val(), |
| 107 |
postlinks='',postimgs='', |
| 108 |
values='', dataAttr = '',multiple = '', |
| 109 |
$post = $('select.post-list', $form), |
| 110 |
$tag = $form.siblings('.cf7sg-dynamic-tag-submit').find('input.tag.code'); |
| 111 |
|
| 112 |
//other attributes. |
| 113 |
$('tr.others :input:visible:checked', $form).each(function(){ |
| 114 |
multiple += ' '+this.value; |
| 115 |
}) |
| 116 |
if(id.length > 0) id =' id:'+id; |
| 117 |
|
| 118 |
if(classes.length > 0){ |
| 119 |
classes = classes.split(',').reduce((r,v)=>r+'class:'+v+' ', ''); |
| 120 |
} |
| 121 |
classes = selectType.split(' ').reduce((r,v)=>r+'class:'+v+' ', classes); |
| 122 |
|
| 123 |
switch(source){ |
| 124 |
case 'taxonomy': |
| 125 |
if($taxonomy.val().length > 0){ |
| 126 |
values = '"slug:'+ $taxonomy.val(); |
| 127 |
if($hasNesting && $('input',$hasNesting).is(':checked')) values +=':tree'; |
| 128 |
values +='"'; |
| 129 |
} |
| 130 |
break; |
| 131 |
case 'post': |
| 132 |
if($post.val().length > 0){ |
| 133 |
let $tax = $('.post-taxonomies.'+$post.val()+' > select.select2', $form); |
| 134 |
/** @since 4.0 */ |
| 135 |
if($('.include-links input',$form).is(':checked')) postlinks = ' permalinks'; |
| 136 |
if($('.include-images input',$form).is(':checked')) postimgs = ' thumbnails'; |
| 137 |
values = ' "source:post:'+$post.val()+'"'; |
| 138 |
if(null != $tax.val()){ |
| 139 |
let term=''; |
| 140 |
for(term of $tax.val()){ |
| 141 |
values += ' "'+term+'"'; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
break; |
| 146 |
case 'filter': |
| 147 |
values = '"source:filter"'; |
| 148 |
break; |
| 149 |
} |
| 150 |
//get any data-attribute hidden fields. |
| 151 |
$('input.data-attribute', $form).each(function(){ |
| 152 |
if(''!=this.value.trim()) dataAttr += ' "data-'+this.value+'"'; |
| 153 |
}); |
| 154 |
|
| 155 |
//update tag. |
| 156 |
|
| 157 |
|
| 158 |
if($req.is(':checked')) tagname = tagname+'*'; |
| 159 |
$tag.val('[' + tagname +' '+ $name.val() + multiple + postimgs + postlinks + id +' '+ classes + values + dataAttr +']'); |
| 160 |
}); |
| 161 |
|
| 162 |
//udpate the new category if created |
| 163 |
$('.cf7sg-dynamic-tag-submit .button').on('click', function(){ |
| 164 |
let $form = $(this).closest('form'), |
| 165 |
tag = $form.data('id'), |
| 166 |
$is_cat = $('input[name="is_hierarchical"]', $form), |
| 167 |
$taxonomy = $('input[name="taxonomy_slug"]', $form), |
| 168 |
$plural = $('input[name="plural_name"]', $form), |
| 169 |
$single = $('input[name="singular_name"]', $form), |
| 170 |
$select = $('select.taxonomy-list', $form), |
| 171 |
$option = $select.find('option:selected'); |
| 172 |
|
| 173 |
if($option.is('.cf7sg-new-taxonomy') ){ |
| 174 |
$option.after('<option data-name="'+$single.val()+'" value="'+$taxonomy.val()+'">'+$plural.val()+'</option>'); |
| 175 |
//$option.next().prop('selected', true); |
| 176 |
}else if($option.is('.cf7sg-taxonomy')){ |
| 177 |
$option.data('name', $single.val()); |
| 178 |
$option.text( $plural.val()); |
| 179 |
}else{ |
| 180 |
return true; //for other dynamic options just continue. |
| 181 |
} |
| 182 |
//store this new value in the hidden field |
| 183 |
let values = $('input#cf72post-dynamic-select').val(); |
| 184 |
if(0 == values.length){ |
| 185 |
values = []; |
| 186 |
}else{ |
| 187 |
values = JSON.parse(values); |
| 188 |
} |
| 189 |
values[values.length] = { |
| 190 |
"slug":$taxonomy.val(), |
| 191 |
"singular":$single.val(), |
| 192 |
"plural":$plural.val(), |
| 193 |
"hierarchical":$is_cat.is(':checked') |
| 194 |
}; |
| 195 |
$('input#cf72post-dynamic-select').val(JSON.stringify(values)); |
| 196 |
//rest the selection |
| 197 |
$plural.prop('disabled',true).val(''); |
| 198 |
$single.prop('disabled',true).val('');; |
| 199 |
$taxonomy.prop('disabled',true).val(''); |
| 200 |
$is_cat.prop('checked', false).parent().hide(); |
| 201 |
$select.val('');//reset the dropdown. |
| 202 |
}); |
| 203 |
|
| 204 |
})( jQuery ); |
| 205 |
|