PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.0.1
Smart Grid-Layout Design for Contact Form 7 v4.0.1
4.18.0 4.17.0 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10 4.11 4.12 4.13 4.14 All 119 releases
cf7-grid-layout / admin / js / cf7sg-dynamic-tag.js

cf7sg-dynamic-tag.js in Smart Grid-Layout Design for Contact Form 7 4.0.1, at admin/js/cf7sg-dynamic-tag.js

190 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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', multiple='';
18
19 $('select.post-list').on('change', function(){
20 $('div.post-taxonomies').hide();
21 //$('div.post-taxonomies').off('change');
22 let type = $(this).val();
23 $('div#'+type).show();
24 $('select.select2', $('div#'+type)).not('.select2-hidden-accessible').select2();
25 });
26
27 $form.on('change',':input', function(event){
28 let $target = $(event.target),
29 $tab = $('input[name="sections"]:checked'),
30 source = 'taxonomy';
31
32 $('#select-multiple').prop('disabled',false);
33
34 if($target.is('select.taxonomy-list')){
35 let $option = $target.find('option:selected');
36 $taxonomy.val($target.val());
37 $plural.val($option.text());
38 $single.val($option.data('name'));
39 //by default disable name fields.
40 $plural.prop('disabled',true);
41 $single.prop('disabled',true);
42 $taxonomy.prop('disabled',true);
43 $is_cat.parent().hide();
44 if($option.is('.cf7sg-new-taxonomy')){
45 $plural.prop('disabled',false);
46 $single.prop('disabled',false);
47 $taxonomy.prop('disabled',false);
48 $is_cat.parent().show();
49 }else if($option.is('.cf7sg-taxonomy')){
50 $plural.prop('disabled',false);
51 $single.prop('disabled',false);
52 }
53 }else if($target.is('input.select-type')){ //---------select-type
54 selectType = $('input.select-type:checked').val();
55 if('select2' == selectType){
56 $('input#select2-tags', $form).prop('disabled', false);
57 }else{
58 $('input#select2-tags', $form).prop('checked', false);
59 $('input#select2-tags', $form).prop('disabled', true);
60 }
61 if( $target.is('#nice-select:checked') ){
62 //no multiple in nice-select.
63 $('#select-multiple').prop('checked',false);
64 $('#select-multiple').prop('disabled',true);
65 }
66 }
67 /* which source ? */
68 if($tab.is('#taxonomy-tab')){
69 source = 'taxonomy';
70 }else if($tab.is('#post-tab')){
71 source = 'post';
72 }else if($tab.is('#custom-tab')){
73 source = 'filter';
74 let $a = $('p.position-relative a', $tab.parent());
75 if( !$a.is('.init') ){
76 $a.attr('data-cf72post', $('#fieldhelperdiv li.cf7sg_filter_source a').data('cf72post') );
77 $a.addClass('init').addClass('helper');
78 }
79 new Clipboard($a[0], {
80 text: function(t) {
81 let $f = $(t);
82 let text = $f.data('cf72post');
83 //get post slug
84 let key = $('#post_name').val();
85 text = text.replace(/\{\$form_key\}/gi, key);
86 text = text.replace(/\{\$field_name\}/gi, $name.val());
87 text = text.replace(/\{\$field_name_slug\}/gi, $name.val().replace(/\-/g,'_'));
88 text = text.replace(/\[dqt\]/gi, '"');
89 return text;
90 }
91 })
92 }
93 updateCF7Tag(source);
94 });
95 //udpate the new category if created
96 $button.on('click', function(){
97 let $option = $select.find('option:selected');
98 if($option.is('.cf7sg-new-taxonomy') ){
99 $option.after('<option data-name="'+$single.val()+'" value="'+$taxonomy.val()+'">'+$plural.val()+'</option>');
100 //$option.next().prop('selected', true);
101 }else if($option.is('.cf7sg-taxonomy')){
102 $option.data('name', $single.val());
103 $option.text( $plural.val());
104 }else{
105 return true; //for other dynamic options just continue.
106 }
107 //store this new value in the hidden field
108 let values = $('input#cf72post-dynamic-select').val();
109 if(0 == values.length){
110 values = [];
111 }else{
112 values = JSON.parse(values);
113 }
114 values[values.length] = {
115 "slug":$taxonomy.val(),
116 "singular":$single.val(),
117 "plural":$plural.val(),
118 "hierarchical":$is_cat.is(':checked')
119 };
120 $('input#cf72post-dynamic-select').val(JSON.stringify(values));
121 //rest the selection
122 $plural.prop('disabled',true).val('');
123 $single.prop('disabled',true).val('');;
124 $taxonomy.prop('disabled',true).val('');
125 $is_cat.prop('checked', false).parent().hide();
126 $select.val('');//reset the dropdown.
127 });
128
129
130 function updateCF7Tag(source='taxonomy') {
131 let id=$id.val();
132 if(id.length > 0) id =' id:'+id;
133
134 let classes = $cl.val(), postlinks='';
135 if(classes.length > 0){
136 let classArr = classes.split(','), idx;
137 classes='';
138 for(idx=0; idx<classArr.length; idx++){
139 classes += " class:" + classArr[idx].trim() + " ";
140 }
141 }
142 let values = ''
143 switch(source){
144 case 'taxonomy':
145 if($taxonomy.val().length > 0){
146 values = ' "slug:'+ $taxonomy.val()+'"';
147 }
148 break;
149 case 'post':
150 if($post.val().length > 0){
151 let $tax = $('div#'+$post.val()+' > select.select2');
152 /** @since 4.0 */
153 if($('#include-post-links').is(':checked')) postlinks = ' permalinks';
154 values = ' "source:post:'+$post.val()+'"';
155 if(null != $tax.val()){
156 let term='';
157 for(term of $tax.val()){
158 values += ' "'+term+'"';
159 }
160 }
161 }
162 break;
163 case 'filter':
164 values = ' "source:filter"';
165 break;
166 }
167 //select type
168 switch(selectType){
169 case 'nice':
170 classes+= ' class:nice-select';
171 break;
172 case 'select2':
173 classes += ' class:select2';
174 if($('input#select2-tags').is(':checked')){
175 classes += ' class:tags';
176 }
177 break;
178 case 'standard':
179 default:
180 break;
181 }
182
183 //update tag.
184 let type = 'dynamic_select ';
185 if($('#select-multiple').is(':checked')) multiple=' multiple';
186 if($req.is(':checked')) type = 'dynamic_select* ';
187 $tag.val('[' + type + $name.val() + multiple + postlinks + id + classes + values +']');
188 }
189 })( jQuery );
190