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