| 1 |
|
| 2 |
/** |
| 3 |
Javascript to handle grid editor |
| 4 |
Event 'cf7sg-form-change' fired on #contact-form-editor element when codemirror changes occur |
| 5 |
*/ |
| 6 |
(function( $ ) { |
| 7 |
'use strict'; |
| 8 |
const offsets = ['offset-one','offset-two', 'offset-three', 'offset-four', 'offset-five', 'offset-six', 'offset-seven', 'offset-eight', 'offset-nine', 'offset-ten', 'offset-eleven'], |
| 9 |
columnsizes = ['one', 'two', 'one-fourth', 'one-third', 'five', 'one-half', 'seven', 'two-thirds', 'nine', 'ten', 'eleven', 'full']; |
| 10 |
let $wpcf7Editor,$grid,$rowControl = null; |
| 11 |
//graphics UI template pattern |
| 12 |
const $pattern = $('<div>').html(cf7grid.preHTML+'\\s*(\\[.*\\s*\\].*\\s*){0,}\\s*'+cf7grid.postHTML), |
| 13 |
required = cf7grid.requiredHTML.replace('*', '\\*'); |
| 14 |
// console.log('r:'+required); |
| 15 |
$pattern.find('label').html('((\\s*.*)('+required+'){1}|(\\s*.*))'); |
| 16 |
$pattern.find('.info-tip').text('(.*\\s*)'); |
| 17 |
//console.log('p:'+$pattern.html()); |
| 18 |
const templateRegex = new RegExp($pattern.html().replace('><','>\\s?<'), 'ig'); |
| 19 |
let seekTemplate = false, cssTemplate = 'div.field', |
| 20 |
$template = $('<div id="cf7sg-dummy">').append(cf7grid.preHTML+cf7grid.postHTML); |
| 21 |
$template = $template.children(); |
| 22 |
if(1==$template.length && $template.find('label').length>0){ |
| 23 |
seekTemplate = true; |
| 24 |
cssTemplate = $template.prop('nodeName'); |
| 25 |
if($template.prop('class').length>0){ |
| 26 |
cssTemplate += '.'+$template.attr('class').split(' ').join('.'); |
| 27 |
} |
| 28 |
} |
| 29 |
let wpcf7Value = ''; |
| 30 |
|
| 31 |
$(document).ready( function(){ |
| 32 |
//change the form id to mimic cf7 plugin custom admin page. |
| 33 |
/** @since 2.11.0 full screen button*/ |
| 34 |
let $editor = $('#cf7sg-editor'), bodyWidth = $('#wpbody').width(); |
| 35 |
$editor.css('background-color',$('body').css('background-color')); |
| 36 |
$('#full-screen-cf7').on('click', function(){ |
| 37 |
$editor.toggleClass('full-screen'); |
| 38 |
$(this).toggleClass('full-screen'); |
| 39 |
if($editor.is('.full-screen')) $editor.width(bodyWidth); |
| 40 |
else $editor.width('auto'); |
| 41 |
$('#form-editor-tabs .ui-tabs-panel', $editor).trigger('cf7sg-screen-resize'); |
| 42 |
}); |
| 43 |
$wpcf7Editor = $('textarea#wpcf7-form-hidden'); |
| 44 |
$grid = $('#grid-form'); |
| 45 |
$rowControl = $('#top-grid-controls'); |
| 46 |
|
| 47 |
/* |
| 48 |
Build grid from existing form------------------------- BUILD UI FORM |
| 49 |
*/ |
| 50 |
function buildGridForm(){ |
| 51 |
let formhtml = $wpcf7Editor.text(); |
| 52 |
if(0===formhtml.length){ |
| 53 |
formhtml = '<div class="container"><div class="row"><div class="columns full"></div></div></div>'; |
| 54 |
} |
| 55 |
const $form = $('<div>').append( formhtml ); |
| 56 |
let isGrid = true; //return value. |
| 57 |
$grid.html(''); //empty the grid. |
| 58 |
if(0===$form.children('.container').length){ |
| 59 |
isGrid = false; |
| 60 |
} |
| 61 |
//remove the external forms |
| 62 |
$('.cf7sg-external-form .cf7sg-external-form-content', $form).remove(); |
| 63 |
//replace columns content with textareas |
| 64 |
/*--------------------------------------------------- convert columns */ |
| 65 |
$('div.columns', $form).each(function(){ |
| 66 |
const $this = $(this), $area = $($('#grid-col').html()); |
| 67 |
if($this.children().is('.container')){ |
| 68 |
$('textarea.grid-input', $area).remove(); |
| 69 |
$('div.cf7-field-inner', $area).remove(); |
| 70 |
}else{ |
| 71 |
if(cf7grid.ui) $('textarea.grid-input', $area).html($this.html().trim()); |
| 72 |
else $('textarea.grid-input', $area).val($this.html().trim()); |
| 73 |
|
| 74 |
$this.children().remove(); |
| 75 |
$this.text(''); |
| 76 |
} |
| 77 |
$this.prepend($area); |
| 78 |
}); |
| 79 |
$('div.row', $form).each(function(){ |
| 80 |
$(this).append( $('#grid-row .row-controls').clone() ); |
| 81 |
}); |
| 82 |
/*--------------------------------------------------- convert collapsible sections */ |
| 83 |
$('div.container.cf7sg-collapsible', $form).each(function(){ |
| 84 |
const $this = $(this); |
| 85 |
let id = $this.attr('id'); |
| 86 |
if(typeof id == 'undefined'){ |
| 87 |
id = randString(6); |
| 88 |
$this.attr('id', id); //assign a random id |
| 89 |
} |
| 90 |
let text = $this.children('.cf7sg-collapsible-title span.cf7sg-title').text(); |
| 91 |
if(0==text.length){ //pre v1.8 title?. |
| 92 |
text = $this.children('.cf7sg-collapsible-title').text(); |
| 93 |
} |
| 94 |
let $toggle = $('.toggle', $this.children('.cf7sg-collapsible-title')); |
| 95 |
if($toggle.length>0){ |
| 96 |
$toggle = $toggle.clone(); |
| 97 |
} |
| 98 |
|
| 99 |
$this.children('.cf7sg-collapsible-title').remove(); |
| 100 |
$this.prepend( $('#grid-collapsible').html()); |
| 101 |
$('input', $this.children('.cf7sg-collapsible-title')).not('[type="checkbox"]').val(text); |
| 102 |
if($toggle.length>0){ |
| 103 |
$this.children('.cf7sg-collapsible-title').append($toggle); |
| 104 |
$('input[type="checkbox"]', $this.children('.cf7sg-collapsible-title') ).prop('checked', true); |
| 105 |
} |
| 106 |
const $ctrl = $this.children('.row').children('.row-controls').find('.collapsible-row-label'); |
| 107 |
$('input', $ctrl).prop('checked', true); |
| 108 |
//toggle disable the sibling input |
| 109 |
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;}); |
| 110 |
}); |
| 111 |
/*--------------------------------------------------- convert tables */ |
| 112 |
$('div.container.cf7-sg-table', $form).each(function(){ |
| 113 |
const $this = $(this); |
| 114 |
let id = $this.attr('id'); |
| 115 |
if(typeof id == 'undefined'){ |
| 116 |
id = 'cf7-sg-table-'+(new Date).getTime(); |
| 117 |
$this.attr('id', id); |
| 118 |
} |
| 119 |
let $ctrl = $this.find('.row.cf7-sg-table > .row-controls' ).first().find('.table-row-label'); |
| 120 |
$('input', $ctrl).prop('checked', true); |
| 121 |
//set button label |
| 122 |
let text = $this.data('button'); |
| 123 |
if(typeof text == 'undefined'){ |
| 124 |
text = 'Add Row'; |
| 125 |
$this.attr('data-button',text); |
| 126 |
} |
| 127 |
$ctrl.next('.table-row-button').children('input').val(text); |
| 128 |
//toggle disable the sibling input |
| 129 |
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;}); |
| 130 |
//toggle footer row |
| 131 |
const $footer = $this.next(); |
| 132 |
if($footer.is('.cf7-sg-table-footer')){ |
| 133 |
$ctrl = $footer.children('.row').first().find('.row-controls .footer-row-label'); |
| 134 |
$('input.footer-row', $ctrl).prop('checked', true); |
| 135 |
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;}); |
| 136 |
} |
| 137 |
}); |
| 138 |
//tabs |
| 139 |
/*--------------------------------------------------- convert tabs */ |
| 140 |
$('ul.cf7-sg-tabs-list li', $form).each(function(){ |
| 141 |
const $this = $(this); |
| 142 |
let text = $this.children('a').text(); |
| 143 |
$this.append($('#grid-tabs ul li label').clone()); |
| 144 |
$('label input', $this).val(text); |
| 145 |
//setup checkbox |
| 146 |
let $ctrl = $this.parent().siblings('.cf7-sg-tabs-panel'); |
| 147 |
$ctrl = $ctrl.children('.row').find('.row-controls' ).first().find('.tabs-row-label'); |
| 148 |
$('input.tabs-row', $ctrl).prop('checked', true); |
| 149 |
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;}); |
| 150 |
}); |
| 151 |
//reinsert the external forms |
| 152 |
$('.cf7sg-external-form', $form).each(function(){ |
| 153 |
const $extform = $(this); |
| 154 |
$extform.append($( $('#grid-cf7-forms .cf7sg-external-form').html() ) ); |
| 155 |
let id = $extform.data('form'); |
| 156 |
if($('#grid-cf7-forms .form-select option[value="'+id+'"]' ).length > 0 ){ |
| 157 |
//add controls |
| 158 |
//$extform.append($('#grid-cf7-forms .form-controls').clone()); |
| 159 |
$('.form-controls .form-select', $extform).val(id); |
| 160 |
//check for form update. |
| 161 |
const data = { |
| 162 |
'action' : 'get_cf7_content', |
| 163 |
'id': $('#post_ID').val(), |
| 164 |
'nonce' : $('#_wpcf7nonce').val(), |
| 165 |
'cf7_key' : id, |
| 166 |
'update' :true |
| 167 |
}; |
| 168 |
$.post(ajaxurl, data, function(response) { |
| 169 |
if(response.length > 0){ |
| 170 |
$('.cf7sg-external-form-content', $extform).attr('id','cf7sg-form-'+id).append( response ); |
| 171 |
}//TODO if error insert a msg. |
| 172 |
}); |
| 173 |
|
| 174 |
} |
| 175 |
}); |
| 176 |
/** @since 3.4 enable groupings of collapsible rows */ |
| 177 |
$('.cf7sg-accordion-rows.columns, .cf7sg-slider-section.columns', $form).each(function(){ |
| 178 |
let $col = $(this), |
| 179 |
$control = $col.children('.grid-column').addClass('enable-grouping'); //enable checkboxes. |
| 180 |
if($col.is('.cf7sg-accordion-rows')){ |
| 181 |
$('.accordion-rows:input', $control).prop('checked', true); |
| 182 |
}else{ //is .cf7sg-slider-section |
| 183 |
$('.slider-rows:input', $control).prop('checked', true); |
| 184 |
} |
| 185 |
//remove toggle checkbox. |
| 186 |
$('input[type="checkbox"]', $col.children('.cf7sg-collapsible').children('.cf7sg-collapsible-title') ).hide().next('span').hide(); |
| 187 |
}); |
| 188 |
//check if any columns have more than 2 collapsible sections. |
| 189 |
$('.row .cf7sg-collapsible:first-child', $form).each(function(){ |
| 190 |
$(this).siblings('.cf7sg-collapsible').closest('.columns').children('.grid-column').addClass('enable-grouping'); |
| 191 |
}); |
| 192 |
//add the form to the grid |
| 193 |
if($form.children('.container').length >0 || $form.children('.cf7sg-external-form').length>0){ |
| 194 |
$grid.append($form.children()); |
| 195 |
}else{ //this is not a cf7sg form. |
| 196 |
$grid.html($form.html()); |
| 197 |
} |
| 198 |
//set the value of each textarea as inner text |
| 199 |
$('textarea', $grid).each(function(){ |
| 200 |
const $this = $(this); |
| 201 |
$this.html($this.val()); |
| 202 |
}); |
| 203 |
/*--------------------------------------------------- if ui mode, then convert to gui template */ |
| 204 |
let $textareaSelected=''; |
| 205 |
if(cf7grid.ui){ |
| 206 |
$('div.columns', $grid).each(function(){ |
| 207 |
const $this = $(this); |
| 208 |
if($this.children().is('.container')) return true; |
| 209 |
$this.html2gui(); |
| 210 |
}); |
| 211 |
}else{ |
| 212 |
//set the first textarea as our default tag consumer |
| 213 |
$('textarea#wpcf7-form').attr('id',''); |
| 214 |
$textareaSelected = $('textarea', $grid).first(); |
| 215 |
$textareaSelected.attr('id', 'wpcf7-form'); |
| 216 |
} |
| 217 |
//change this to whichever is live |
| 218 |
$('textarea', $grid).on('focus', function(){ |
| 219 |
const $this = $(this); |
| 220 |
if($textareaSelected.length>0 && $textareaSelected.is('#wpcf7-form')){ |
| 221 |
$textareaSelected.attr('id',''); |
| 222 |
$textareaSelected.html($textareaSelected.val()); //set its inner html |
| 223 |
} |
| 224 |
if($this.is('.grid-input')){ |
| 225 |
$('textarea#wpcf7-form').attr('id',''); |
| 226 |
$textareaSelected = $this.attr('id','wpcf7-form'); |
| 227 |
} |
| 228 |
}); |
| 229 |
return isGrid; |
| 230 |
} //end buildGridForm() |
| 231 |
/** @since 3.4.0 enalbe accordion class for containers having multiple collapsible rows */ |
| 232 |
$grid.on('cf7sg-update', function(e, update){ |
| 233 |
let $container = $(e.target); |
| 234 |
if(undefined !== update.add){ |
| 235 |
switch(update.add){ |
| 236 |
case 'collapsible-row': |
| 237 |
let $pc = $container.closest('.columns'); |
| 238 |
if( $pc.children('.cf7sg-collapsible').length>1 ){ |
| 239 |
//enable column accordion control. |
| 240 |
$pc.children('.grid-column').addClass('enable-grouping'); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
if(undefined !== update.remove){ |
| 245 |
switch(update.remove){ |
| 246 |
case 'collapsible-row': |
| 247 |
let $pc = $container.closest('.columns'); |
| 248 |
if( $pc.children('.cf7sg-collapsible').length<2 ){ |
| 249 |
//disable column accordion control. |
| 250 |
$pc.children('.grid-column').removeClass('enable-grouping'); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
}); |
| 255 |
//offset/size change using event delegation |
| 256 |
/*---------------------------------------------------------------------------- ui menus */ |
| 257 |
$grid.on('change', function(event){ |
| 258 |
const $target = $(event.target); |
| 259 |
if($target.is('.column-setting')){ //----------- column size/offset settings |
| 260 |
let validation = ['dummy']; |
| 261 |
if( $target.is('.column-offset') ){ |
| 262 |
validation = offsets; |
| 263 |
}else if( $target.is('.column-size') ){ |
| 264 |
validation = columnsizes; |
| 265 |
}else{ |
| 266 |
return false; |
| 267 |
} |
| 268 |
const $column = $target.closest('.columns'), classList = $column.attr('class').split(/\s+/); |
| 269 |
for(let idx=0; idx<classList.length; idx++){ |
| 270 |
if($.inArray(classList[idx], validation) > -1){ |
| 271 |
$column.removeClass(classList[idx]); |
| 272 |
} |
| 273 |
} |
| 274 |
$column.addClass($target.val()); |
| 275 |
//filter the options |
| 276 |
$target.closest('.grid-controls').filterColumnControls(); |
| 277 |
}else if($target.is('.form-select')){ //-------------- external form selection |
| 278 |
const $container = $target.closest('.cf7sg-external-form'); |
| 279 |
$container.attr('data-form', $target.val()); |
| 280 |
const data = { |
| 281 |
'action' : 'get_cf7_content', |
| 282 |
'id': $('#post_ID').val(), |
| 283 |
'nonce' : $('#_wpcf7nonce').val(), |
| 284 |
'cf7_key' : $target.val() |
| 285 |
}; |
| 286 |
$.post(ajaxurl, data, function(response) { |
| 287 |
$('.cf7sg-external-form-content', $container).attr('id','cf7sg-form-'+$target.val()).html(response); |
| 288 |
}); |
| 289 |
} |
| 290 |
/* |
| 291 |
Collapsible / tabs rows input changes |
| 292 |
*/ |
| 293 |
if($target.is('.cf7sg-collapsible-title label input[type="text"]')){ //------- Collapsible title |
| 294 |
$target.siblings('input[type="hidden"]').val($target.val()); |
| 295 |
}else if( $target.is('.cf7sg-collapsible-title label input[type="checkbox"]')){ //------- Collapsible title toggled |
| 296 |
const $title = $target.closest('.cf7sg-collapsible-title'); |
| 297 |
if($target.is(':checked')){ |
| 298 |
$title.append($('#grid-collapsible-with-toggle').html()); |
| 299 |
$title.closest('.container.cf7sg-collapsible').addClass('with-toggle').attr('data-group','').attr('data-open','false'); |
| 300 |
}else{ |
| 301 |
$('.toggle', $title).remove(); |
| 302 |
$title.closest('.container.cf7sg-collapsible').removeClass('with-toggle').removeAttr( 'data-group').removeAttr( 'data-open'); |
| 303 |
} |
| 304 |
}else if($target.is('ul.cf7-sg-tabs-list li label input[type="text"]')){ //------- Tabs title |
| 305 |
$target.parent().siblings('a').text($target.val()); |
| 306 |
}else if($target.is('label.table-row-button input')){ |
| 307 |
$target.closest('.container.cf7-sg-table').attr('data-button',$target.val()); |
| 308 |
} |
| 309 |
if(cf7grid.ui){ |
| 310 |
if($target.is('.cf7-field-inner textarea')){ |
| 311 |
let label = $target.scanCF7Tag(); |
| 312 |
$target.siblings('p.content').html(label);//.show(); |
| 313 |
$target.parent().siblings('textarea.grid-input').updateGridForm(); |
| 314 |
}else if($target.is('.cf7-field-inner input')){ |
| 315 |
$target.siblings('p.content').html($target.val()); |
| 316 |
$target.parent().siblings('textarea.grid-input').updateGridForm(); |
| 317 |
} |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
//grid click event delegation |
| 322 |
$grid.on('click', function(event){ |
| 323 |
const $target = $(event.target); |
| 324 |
switch(true){ |
| 325 |
case $target.is('input[type="text"]:visible'): |
| 326 |
case $target.is('textarea:visible'): //click on a field. |
| 327 |
case $target.is('select:visible'): |
| 328 |
return true; |
| 329 |
break; |
| 330 |
} |
| 331 |
//close any open row/column controls |
| 332 |
closeAllControls(); |
| 333 |
|
| 334 |
/* |
| 335 |
Row controls |
| 336 |
----------------------------------------------------------ROW CONTRLS |
| 337 |
*/ |
| 338 |
let $parentRow,$parentContainer, $parentColumn; |
| 339 |
if($target.is('.dashicons-trash.form-control')){ //--------TRASH included form |
| 340 |
$target.closest('.cf7sg-external-form').remove(); |
| 341 |
}else if($target.is('.dashicons-plus.form-control') ){ //---ADD external form |
| 342 |
$target.closest('.cf7sg-external-form').insertNewRow(); |
| 343 |
}else if($target.is('.dashicons-trash.row-control')){ //--------TRASH |
| 344 |
$parentContainer = $target.closest('.container'); |
| 345 |
let $parent = $parentContainer.parent(); |
| 346 |
$parentContainer.remove(); |
| 347 |
if( $parent.is('.columns') ) { //verify is this is the last row being deleted |
| 348 |
if( 0 == $parent.children('.container').length ){ |
| 349 |
//add a text area to the column |
| 350 |
$parent.children('.grid-column').append('<textarea class="grid-input"></textarea>'); |
| 351 |
} |
| 352 |
} |
| 353 |
}else if($target.is('.dashicons-plus.row-control')){ //-----------ADD Row |
| 354 |
$target.closest('.container').insertNewRow(); |
| 355 |
}else if($target.is('.dashicons-edit.row-control')){ //-----------Show controls |
| 356 |
//hide any other controls that might be open |
| 357 |
//taken care by closeAllControls |
| 358 |
|
| 359 |
$target.siblings('.grid-controls').show(); |
| 360 |
$target.hide(); |
| 361 |
$target.siblings('.dashicons-no-alt').show(); |
| 362 |
/* |
| 363 |
TODO: use $('.grid-controls')filterColumnControls() to make sure columns sizes/offsets are correct. |
| 364 |
possibly introduce a boolean to check if filter has been run already on this row |
| 365 |
*/ |
| 366 |
}else if( $target.is('.dashicons-no-alt.row-control') ) { //----------------hide controls |
| 367 |
//take care by closeAllControls |
| 368 |
}else if($target.is('input.collapsible-row')){ //-------------checkbox collapsible row |
| 369 |
const $container = $target.closest('.container'); |
| 370 |
if($target.is(':checked')){ |
| 371 |
$container.addClass('cf7sg-collapsible'); |
| 372 |
let id = $container.attr('id'); |
| 373 |
if(typeof id == 'undefined'){ |
| 374 |
id = randString(6); |
| 375 |
$container.attr('id', id); //assign a random id |
| 376 |
} |
| 377 |
$container.prepend($('#grid-collapsible').html()).fireGridUpdate('add','collapsible-row'); |
| 378 |
}else{ |
| 379 |
$container.removeClass('cf7sg-collapsible'); |
| 380 |
$container.children('.cf7sg-collapsible-title').remove(); |
| 381 |
$container.fireGridUpdate('remove','collapsible-row'); |
| 382 |
} |
| 383 |
//toggle disable the sibling input |
| 384 |
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;}); |
| 385 |
}else if($target.is('input.table-row')){ //-------------checkbox table row |
| 386 |
if($target.is(':checked')){ |
| 387 |
let id = 'cf7-sg-table-'+(new Date).getTime(); |
| 388 |
$target.closest('.row').addClass('cf7-sg-table'); |
| 389 |
$target.closest('.container').addClass('cf7-sg-table').attr('id',id).fireGridUpdate('add','table-row'); |
| 390 |
}else{ |
| 391 |
$target.closest('.row').removeClass('cf7-sg-table'); |
| 392 |
$target.closest('.container').removeClass('cf7-sg-table').removeAttr('id').removeAttr('data-button').fireGridUpdate('remove','table-row'); |
| 393 |
} |
| 394 |
//toggle disable the sibling input |
| 395 |
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;}); |
| 396 |
}else if($target.is('input.tabs-row')){ //-------------checkbox tabbed row |
| 397 |
const $panel = $target.closest('.container'); |
| 398 |
if($target.is(':checked')){ |
| 399 |
let id = 'cf7-sg-tab-'+(new Date).getTime(); |
| 400 |
$panel.addClass('cf7-sg-tabs-panel').attr('id',id); |
| 401 |
$panel.before($('#grid-tabs').html()); |
| 402 |
$panel.closest('.columns').addClass('cf7-sg-tabs'); |
| 403 |
$('li a', $panel.siblings('ul.cf7-sg-tabs-list')).attr('href','#'+id); |
| 404 |
$panel.fireGridUpdate('add','tabbed-row'); |
| 405 |
}else{ |
| 406 |
$panel.removeClass('cf7-sg-tabs-panel'); |
| 407 |
$panel.closest('.columns').removeClass('cf7-sg-tabs'); |
| 408 |
$panel.siblings('ul.cf7-sg-tabs-list').remove(); |
| 409 |
$panel.fireGridUpdate('remove','tabbed-row'); |
| 410 |
} |
| 411 |
//toggle disable the sibling input |
| 412 |
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;}); |
| 413 |
}else if($target.is('input.footer-row')){ //-------------checkbox footer row |
| 414 |
const $table = $target.closest('.container').prev(); |
| 415 |
if($table.is('.container.cf7-sg-table')){ |
| 416 |
if($target.is(':checked')){ |
| 417 |
$target.closest('.container').addClass('cf7-sg-table-footer').fireGridUpdate('add','footer-row'); |
| 418 |
}else{ |
| 419 |
$target.closest('.container').removeClass('cf7-sg-table-footer').fireGridUpdate('remove','footer-row'); |
| 420 |
} |
| 421 |
//toggle disable the sibling input |
| 422 |
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;}); |
| 423 |
} |
| 424 |
}else if( $target.is('.make-grid.row-control') ){ /** @since 3.4.0--wrap row into column ->make grid*/ |
| 425 |
//close the grid-control box |
| 426 |
$target.parent().hide(); |
| 427 |
$target.parent().siblings('.dashicons-no-alt').hide(); |
| 428 |
$target.parent().siblings('.dashicons-edit').show(); |
| 429 |
//convert to grid if multiple columns |
| 430 |
$parentRow = $target.closest('.row'); |
| 431 |
if($parentRow && $parentRow.children('.columns').length>1){ |
| 432 |
//is row table/tab |
| 433 |
$parentContainer = $parentRow.closest('.container'); |
| 434 |
const $content = $parentRow.children('.columns'); |
| 435 |
$content.wrapAll('<div class="columns full"></div>'); |
| 436 |
$parentColumn = $content.parent('.columns'); |
| 437 |
$parentColumn.prepend( $( $('#grid-col').html() ) ); |
| 438 |
//remove single cell UI content |
| 439 |
$parentColumn.children('.grid-column').children('textarea.grid-input').remove(); |
| 440 |
$parentColumn.children('.grid-column').children('div.cf7-field-inner').remove(); |
| 441 |
//finally insert a new row with the content moved to the new row. |
| 442 |
$parentColumn.insertNewRow($content.remove()); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
/* |
| 448 |
Column controls, show/hide contols, add column, and refresh select dropdowns for colum size on addition |
| 449 |
*/ |
| 450 |
|
| 451 |
if( $target.is('.columns')){ |
| 452 |
$parentColumn = $target; |
| 453 |
}else{ |
| 454 |
$parentColumn = $target.closest('.columns'); |
| 455 |
} |
| 456 |
//verify which target was clicked |
| 457 |
if( $target.is('.dashicons-edit.column-control') ){ //------------------show controls |
| 458 |
//now show this control |
| 459 |
$target.siblings('.grid-controls').show().filterColumnControls(); |
| 460 |
$target.hide(); |
| 461 |
$target.siblings('.dashicons-no-alt').show(); |
| 462 |
}else if( $target.is('.php-icon.column-control') ) { //--------show hooks |
| 463 |
let $helper =$('<div class="helper-popup">').html( $('#grid-helper').html()), |
| 464 |
$copy = $('.copy-helper', $helper), |
| 465 |
field = $target.data('field'), |
| 466 |
tag = $target.data('tag'), |
| 467 |
search = $target.data('search'), |
| 468 |
$hooks = $(search, '#fieldhelperdiv').clone(); |
| 469 |
$target.after($helper) |
| 470 |
$('.cf7sg-helper-list', $helper).append($hooks); |
| 471 |
$('a.helper', $helper).each(function(){ |
| 472 |
new Clipboard($(this)[0], { |
| 473 |
text: function(trigger) { |
| 474 |
let $target = $(trigger); |
| 475 |
let text = $target.data('cf72post'); |
| 476 |
//get post slug |
| 477 |
let key = $('#post_name').val(); |
| 478 |
text = text.replace(/\{\$form_key\}/gi, key); |
| 479 |
text = text.replace(/\{\$field_name\}/gi, field); |
| 480 |
text = text.replace(/\{\$field_name_slug\}/gi, field.replace(/\-/g,'_')); |
| 481 |
text = text.replace(/\{\$field_type\}/gi, tag); |
| 482 |
text = text.replace(/\[dqt\]/gi, '"'); |
| 483 |
return text; |
| 484 |
} |
| 485 |
}); |
| 486 |
$(this).append($copy.clone()); |
| 487 |
}); |
| 488 |
$helper.click('a.helper, .dashicons-no-alt', function(e){ |
| 489 |
$(this).remove(); |
| 490 |
}); |
| 491 |
}else if( $target.is('.js-icon.column-control') ) { //--------show js hooks |
| 492 |
let $helper =$('<div class="helper-popup">').html( $('#grid-js-helper').html()), |
| 493 |
$copy = $('.copy-helper', $helper), |
| 494 |
field = $target.data('field'), |
| 495 |
tag = $target.data('tag'), |
| 496 |
search = $target.data('search'), |
| 497 |
$hooks = $(search, '#fieldhelperjs').clone(); |
| 498 |
$target.after($helper); |
| 499 |
$('.cf7sg-helper-list', $helper).append($hooks); |
| 500 |
$('a.helper', $helper).each(function(){ |
| 501 |
new Clipboard($(this)[0], { |
| 502 |
text: function(trigger) { |
| 503 |
let $target = $(trigger); |
| 504 |
let text = $target.data('cf72post'); |
| 505 |
//get post slug |
| 506 |
let key = $('#post_name').val(); |
| 507 |
text = text.replace(/\{\$form_key\}/gi, key); |
| 508 |
text = text.replace(/\{\$field_name\}/gi, field); |
| 509 |
text = text.replace(/\{\$field_name_slug\}/gi, field.replace(/\-/g,'_')); |
| 510 |
text = text.replace(/\{\$field_type\}/gi, tag); |
| 511 |
text = text.replace(/\[dqt\]/gi, '"'); |
| 512 |
return text; |
| 513 |
} |
| 514 |
}); |
| 515 |
$(this).append($copy.clone()); |
| 516 |
}); |
| 517 |
$helper.click('a.helper, .dashicons-no-alt', function(e){ |
| 518 |
$(this).remove(); |
| 519 |
}); |
| 520 |
}else if($target.is('.icon-code.column-control') ){ |
| 521 |
const $focus = $target.closest('.columns'); |
| 522 |
//toggle cf7sgfocus class on inner field to focus on. |
| 523 |
if($focus.is('.cf7sgfocus')){ |
| 524 |
$focus.removeClass('cf7sgfocus'); |
| 525 |
}else{ |
| 526 |
$('.cf7sgfocus', $grid).removeClass('cf7sgfocus'); |
| 527 |
$focus.addClass('cf7sgfocus'); |
| 528 |
} |
| 529 |
//move to text editor. |
| 530 |
$('#form-editor-tabs').tabs('option',{ active:1}); |
| 531 |
$('body').addClass('disable-scroll'); |
| 532 |
}else if($target.is('.dashicons-trash.column-control') ){ //-------------------delete column |
| 533 |
$parentColumn.closest('.row').fireGridUpdate('remove','column'); |
| 534 |
$parentColumn.remove(); |
| 535 |
//refilter |
| 536 |
//$target.siblings('.grid-controls').filterColumnControls(); |
| 537 |
}else if( $target.is('.make-grid.column-control') ){ //--------------------add row/convert column to grid |
| 538 |
|
| 539 |
//close the grid-control box |
| 540 |
$target.parent().hide(); |
| 541 |
$target.parent().siblings('.dashicons-no-alt').hide(); |
| 542 |
$target.parent().siblings('.dashicons-edit').show(); |
| 543 |
//convert to grid |
| 544 |
$parentColumn = $target.closest('.columns'); |
| 545 |
if($parentColumn.length > 0){ |
| 546 |
//column already has a row? |
| 547 |
let text = ''; |
| 548 |
if(0 == $parentColumn.children('.container').length){ |
| 549 |
//keep the textarea and remove from the column |
| 550 |
if(cf7grid.ui){ |
| 551 |
text = $('textarea.grid-input', $parentColumn).remove().text(); |
| 552 |
$('div.cf7-field-inner', $parentColumn).remove(); |
| 553 |
}else{ |
| 554 |
text = $('textarea.grid-input', $parentColumn).remove().val(); |
| 555 |
} |
| 556 |
}else{ |
| 557 |
text = $parentColumn.children('.container').remove(); |
| 558 |
} |
| 559 |
$parentColumn.insertNewRow(text); |
| 560 |
}else{ //add to the main container |
| 561 |
$grid.insertNewRow(); |
| 562 |
} |
| 563 |
}else if($target.is('.external-form')){ //---------------- insert cf7 form |
| 564 |
//close the grid-control box |
| 565 |
$target.parent().hide(); |
| 566 |
$target.parent().siblings('.dashicons-no-alt').hide(); |
| 567 |
$target.parent().siblings('.dashicons-edit').show(); |
| 568 |
//replace container with form selector |
| 569 |
$target.closest('.container').after($('#grid-cf7-forms').html()); |
| 570 |
$target.closest('.container').remove(); |
| 571 |
}else if( $target.is('.dashicons-plus.column-control') ){ //--------------------add column |
| 572 |
$parentColumn = $target.closest('.columns'); |
| 573 |
$parentRow = $parentColumn.closest('.row'); |
| 574 |
let classList, idx , columns, row, newSize=0, createColumn = true, total = 0; |
| 575 |
let sizes = [], $newColumn = $('<div class="columns"></div>'); |
| 576 |
//is the row filled up? |
| 577 |
//first check if the current column fills the entire row |
| 578 |
if( $parentColumn.is('.full') ){ |
| 579 |
total = 12; |
| 580 |
columns = 1; |
| 581 |
sizes[0] = columnsizes.length - 1; //ie 11, the last value |
| 582 |
}else{ |
| 583 |
row = $parentRow.getRowSize(); |
| 584 |
total = row.length; |
| 585 |
sizes = row.cols; |
| 586 |
columns = sizes.length; |
| 587 |
} |
| 588 |
if(12 == total) { |
| 589 |
newSize = Math.floor( total/(columns + 1) ) - 1 ; |
| 590 |
if(newSize < 0 ){ //max columns reached |
| 591 |
/*TODO: display an error message and return */ |
| 592 |
return false; |
| 593 |
} |
| 594 |
createColumn = false; |
| 595 |
let newClass = columnsizes[newSize]; |
| 596 |
$parentRow.children('.columns').each(function(index){ |
| 597 |
//make sure the column is not size 1 |
| 598 |
if( sizes[index] > 0 ){ |
| 599 |
$(this).changeColumnSize(columnsizes[sizes[index]], newClass ); |
| 600 |
createColumn = true; |
| 601 |
} |
| 602 |
}); |
| 603 |
if(!createColumn){ //not enough space to create extra column |
| 604 |
/*TODO: display an error message and return */ |
| 605 |
return false; |
| 606 |
} |
| 607 |
}else if(total < 12){ //just add the new column as size 1 |
| 608 |
if( (12 - total) < total){ |
| 609 |
newSize = 12 - total - 1; |
| 610 |
}else{ |
| 611 |
newSize = total - 1; |
| 612 |
} |
| 613 |
}else{ //should never reach here |
| 614 |
return false; |
| 615 |
} |
| 616 |
//add the new column |
| 617 |
$newColumn.append( $( $('#grid-col').html() ) ); |
| 618 |
if(cf7grid.ui){ |
| 619 |
$('textarea.grid-input' ,$newColumn).hide(); |
| 620 |
}else{ |
| 621 |
$('div.cf7-field-inner', $newColumn).hide(); |
| 622 |
} |
| 623 |
$newColumn.changeColumnSize('',columnsizes[newSize]); |
| 624 |
$parentColumn.after($newColumn); |
| 625 |
}else if( $target.is('.accordion-rows.column-control') ){ /** @since 3.4.0 enable accordion */ |
| 626 |
if($target.is(':checked')){ |
| 627 |
$parentColumn.addClass('cf7sg-accordion-rows').removeClass('cf7sg-slider-section').removeAttr("data-next data-prev data-submit"); |
| 628 |
$target.parent('label').siblings('.grouping-option').children(':input').prop('checked', false); |
| 629 |
$target.fireGridUpdate('add','accordion-rows'); |
| 630 |
//hide toggle checkbox. |
| 631 |
$('input[type="checkbox"]', $parentColumn.children('.cf7sg-collapsible').children('.cf7sg-collapsible-title') ).hide().next('span').hide(); |
| 632 |
}else{ |
| 633 |
$target.closest('.columns').removeClass('cf7sg-accordion-rows'); |
| 634 |
$target.fireGridUpdate('remove','accordion-rows'); |
| 635 |
//show toggle checkbox. |
| 636 |
$('input[type="checkbox"]', $parentColumn.children('.cf7sg-collapsible').children('.cf7sg-collapsible-title') ).show().next('span').show(); |
| 637 |
} |
| 638 |
}else if( $target.is('.slider-rows.column-control') ) { /** @since 3.4.0 enable slider */ |
| 639 |
if($target.is(':checked')){ |
| 640 |
let attrs = { "data-next":"", "data-prev":"", "data-submit":""}; |
| 641 |
$target.closest('.columns').addClass('cf7sg-slider-section').attr(attrs).removeClass('cf7sg-accordion-rows'); |
| 642 |
$target.parent('label').siblings('.grouping-option').children(':input').prop('checked', false); |
| 643 |
$target.fireGridUpdate('add','slider-section'); |
| 644 |
//hide toggle checkbox. |
| 645 |
$('input[type="checkbox"]', $parentColumn.children('.cf7sg-collapsible').children('.cf7sg-collapsible-title') ).hide().next('span').hide(); |
| 646 |
}else{ |
| 647 |
$target.closest('.columns').removeClass('cf7sg-slider-section').removeAttr("data-next data-prev data-submit"); |
| 648 |
$target.fireGridUpdate('remove','slider-section'); |
| 649 |
//show toggle checkbox. |
| 650 |
$('input[type="checkbox"]', $parentColumn.children('.cf7sg-collapsible').children('.cf7sg-collapsible-title') ).show().next('span').show(); |
| 651 |
} |
| 652 |
} |
| 653 |
/* |
| 654 |
Column UI fields |
| 655 |
*/ |
| 656 |
if(cf7grid.ui){ |
| 657 |
//close any open ui fields |
| 658 |
closeAlluiFields(); |
| 659 |
if($target.is('.cf7-field-inner p.content')){ |
| 660 |
$target.parent().showUIfield(); |
| 661 |
}else if($target.is('.cf7-field-inner span.dashicons')){ |
| 662 |
//field will be closed by closeAlluiFields |
| 663 |
}else if('none'!==$('#wpcf7-form').css('display') && !$target.is('#wpcf7-form')){ |
| 664 |
changeTextarea(); |
| 665 |
} |
| 666 |
} |
| 667 |
}); |
| 668 |
|
| 669 |
// capture tab and move to the next field. |
| 670 |
if(cf7grid.ui){ |
| 671 |
$grid.keydown('div.cf7-field-inner', function(e){ |
| 672 |
if(9 !== (e.which || e.keyCode) ) return; //check for tab. |
| 673 |
let $target = $(e.target); |
| 674 |
if($target.is('div.cf7-field-inner :input')){ |
| 675 |
$target.closeUIfield(); |
| 676 |
$target = $target.parent(); //switch to parent. |
| 677 |
let found = false, stopSearch = false, $next = $grid.find('div.cf7-field-inner').filter(function(i,el){ |
| 678 |
if(stopSearch) return false; |
| 679 |
if(found) return (stopSearch = true) ; |
| 680 |
else if($(el).is($target) ) found = true; |
| 681 |
}); |
| 682 |
if($next){ |
| 683 |
$next.showUIfield(); |
| 684 |
e.preventDefault(); //stop tab focus on this element. |
| 685 |
} |
| 686 |
} |
| 687 |
}); |
| 688 |
|
| 689 |
} |
| 690 |
//general inputs into the textareas will trigger form change event |
| 691 |
$grid.on('input selectionchange propertychange', 'textarea', function(event){ |
| 692 |
const $target = $(event.target); |
| 693 |
if($target.is('.cf7-field-type textarea')){ |
| 694 |
return false; //form change trigger will happen after this |
| 695 |
}else if($target.is('textarea')){ |
| 696 |
$target.html($target.val()+'\n'); //ensure changes are capture in the codemirror editor |
| 697 |
$('#contact-form-editor').trigger('cf7sg-form-change'); |
| 698 |
} |
| 699 |
}); |
| 700 |
//before grid editor is closed, update the form with the last textarea |
| 701 |
//event 'cf7grid-tab-finalise' is fired in cf7-grid-codemirror.js file |
| 702 |
$grid.on('cf7grid-form-finalise', function(){ |
| 703 |
if(cf7grid.ui){ |
| 704 |
$('#wpcf7-form').parent().siblings('textarea.grid-input').on('change', function(){ |
| 705 |
$grid.trigger('cf7grid-form-ready'); //codemirror initialisation |
| 706 |
}); |
| 707 |
$('textarea.grid-input#wpcf7-form').on('change', function(){ //special case for custom code in cf7ui mode |
| 708 |
$grid.trigger('cf7grid-form-ready'); //codemirror initialisation |
| 709 |
}); |
| 710 |
changeTextarea(true); |
| 711 |
}else{ |
| 712 |
const $txta = $('textarea#wpcf7-form'); |
| 713 |
$txta.html($txta.val()+'\n'); |
| 714 |
$grid.trigger('cf7grid-form-ready'); //codemirror initialisation |
| 715 |
} |
| 716 |
}); |
| 717 |
|
| 718 |
//initial construction of grid form |
| 719 |
buildGridForm(); |
| 720 |
$grid.on('build-grid', function(){ |
| 721 |
if( !buildGridForm() ){ |
| 722 |
$('#form-editor-tabs').tabs('option',{ active:1, disabled:true}); |
| 723 |
}else{ |
| 724 |
const $focus = $('.cf7sgfocus', $grid); |
| 725 |
if($focus.length>0){ |
| 726 |
const scrollPos = $focus.offset().top - $(window).height()/2 + $focus.height()/2; |
| 727 |
//console.log(scrollPos); |
| 728 |
$(window).scrollTop(scrollPos); |
| 729 |
$focus.removeClass('cf7sgfocus'); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
}); |
| 734 |
//make columns sortable |
| 735 |
sortableRows(); |
| 736 |
//make rows sortable |
| 737 |
$('.columns, #grid-form').sortable({ |
| 738 |
//placeholder: "ui-state-highlight", |
| 739 |
handle:'.row-controls > .dashicons-move', |
| 740 |
axis: 'y', |
| 741 |
//containment:'parent', |
| 742 |
items: '> .container, > .cf7sg-external-form', |
| 743 |
helper:'clone' |
| 744 |
}); |
| 745 |
//grid is ready |
| 746 |
$wpcf7Editor.trigger('grid-ready'); |
| 747 |
}); //end document ready |
| 748 |
|
| 749 |
//random id function. |
| 750 |
function randString(n){ |
| 751 |
if(!n){ |
| 752 |
n = 5; |
| 753 |
} |
| 754 |
let text = ''; |
| 755 |
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; |
| 756 |
for(let i=0; i < n; i++){ |
| 757 |
text += possible.charAt(Math.floor(Math.random() * possible.length)); |
| 758 |
} |
| 759 |
return text; |
| 760 |
} |
| 761 |
//function to close any ui fields |
| 762 |
function closeAlluiFields(){ |
| 763 |
$('.cf7-field-inner :input:visible').each(function(){ |
| 764 |
$(this).closeUIfield(); |
| 765 |
}); |
| 766 |
} |
| 767 |
//close controls row/column |
| 768 |
function closeAllControls(){ |
| 769 |
$('.grid-controls:visible', $grid).each(function(){ |
| 770 |
const $this = $(this); |
| 771 |
$this.hide(); |
| 772 |
$this.siblings('.dashicons-no-alt').hide(); |
| 773 |
$this.siblings('.dashicons-edit').show(); |
| 774 |
}); |
| 775 |
//close helper popups. |
| 776 |
$('.column-control+.helper-popup').remove(); |
| 777 |
} |
| 778 |
//trigger a change on the textarea#wpcf7-form field if its value has changed |
| 779 |
function changeTextarea(finalise = false){ |
| 780 |
if(cf7grid.ui){ |
| 781 |
const $lastTA = $('#wpcf7-form'); |
| 782 |
if($lastTA.length >0 && wpcf7Value !== $lastTA.val()){ |
| 783 |
wpcf7Value = ''; |
| 784 |
$lastTA.trigger('change'); |
| 785 |
}else if(finalise){ |
| 786 |
$grid.trigger('cf7grid-form-ready'); //codemirror initialisation |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
function sortableRows( $newRow='' ){ |
| 791 |
if($newRow.lenght>0){ |
| 792 |
$('.row', $grid).not($newRow).sortable('destroy'); |
| 793 |
} |
| 794 |
$('.row', $grid).sortable({ |
| 795 |
//placeholder: "ui-state-highlight", |
| 796 |
//containment:'.row', |
| 797 |
handle:'.grid-column > .dashicons-move', |
| 798 |
connectWith: '.row', |
| 799 |
helper:'clone', |
| 800 |
items: '> .columns', |
| 801 |
receive: function( event, ui ) { |
| 802 |
//ui.item validate column size relative to new row columns. |
| 803 |
//ui.sender original row. cnacel if column does not fit in new row. |
| 804 |
//$targetRow has more than 12 columns then cancel. |
| 805 |
const $targetRow = $(event.target);//receiving row |
| 806 |
let row = $targetRow.getRowSize(); |
| 807 |
|
| 808 |
if( row.length > 12){ |
| 809 |
ui.sender.sortable('cancel'); |
| 810 |
const $warning = $('<span class="cf7sg-warning">Not enough space in this row to add column</span>'); |
| 811 |
$targetRow.after($warning); |
| 812 |
$warning.delay(2000).fadeOut('slow', function(){ |
| 813 |
$(this).remove(); |
| 814 |
}); |
| 815 |
}else{ |
| 816 |
//make sure the row controls is at the end. |
| 817 |
const control = $targetRow.children('.row-controls').remove(); |
| 818 |
$targetRow.append(control); |
| 819 |
} |
| 820 |
} |
| 821 |
}); |
| 822 |
} |
| 823 |
/* some function definitions...*/ |
| 824 |
$.fn.closeUIfield = function(){ |
| 825 |
let obj = this; |
| 826 |
if(!Array.isArray(this)) obj=[this]; |
| 827 |
$.each(obj,function(idx, item){ |
| 828 |
const $this = $(item); |
| 829 |
if(!$this.is('.cf7-field-inner :input:visible')){ |
| 830 |
return $this; |
| 831 |
} |
| 832 |
if($this.parent().is('.cf7-field-type')) changeTextarea(); |
| 833 |
$this.hide().attr('id', ''); |
| 834 |
$this.siblings('.dashicons-no-alt').hide(); |
| 835 |
$this.siblings('.content').show(); |
| 836 |
}); |
| 837 |
return true; |
| 838 |
} |
| 839 |
$.fn.showUIfield = function(){ |
| 840 |
const $this = $(this); |
| 841 |
if(!$this.is('.cf7-field-inner')){ |
| 842 |
return $this; |
| 843 |
} |
| 844 |
$this.find('p.content').hide(); |
| 845 |
$this.find('span.dashicons').show(); |
| 846 |
const $input = $(':input', $this).show(); |
| 847 |
$input.focus(); |
| 848 |
if($input.is('textarea')){ |
| 849 |
$('textarea#wpcf7-form').attr('id',''); |
| 850 |
$input.attr('id', 'wpcf7-form'); |
| 851 |
wpcf7Value = $input.val(); |
| 852 |
}else{ |
| 853 |
changeTextarea(); |
| 854 |
} |
| 855 |
return $this; |
| 856 |
} |
| 857 |
$.fn.html2gui = function(html){ |
| 858 |
const $this = $(this); |
| 859 |
if(typeof html === 'undefined') html =''; |
| 860 |
if(html.length === 0){ |
| 861 |
//get the fields from the textarea |
| 862 |
html = $('textarea.grid-input', $this).text(); |
| 863 |
if(html.length === 0){ |
| 864 |
$('textarea.grid-input', $this).hide(); |
| 865 |
return $this; |
| 866 |
} |
| 867 |
} |
| 868 |
let singleField = true; |
| 869 |
let search = $('<div>').append(html); |
| 870 |
|
| 871 |
if(seekTemplate && 1==search.children(cssTemplate).length){ |
| 872 |
const lines = html.split(/\r\n|\r|\n/g); |
| 873 |
search = ''; |
| 874 |
for(let i=0; i<lines.length; i++){ |
| 875 |
search += lines[i].trim(); |
| 876 |
} |
| 877 |
const match = templateRegex.exec(search); |
| 878 |
if(null !== match){ |
| 879 |
//populate the fields |
| 880 |
let $field = $('div.cf7-field-label', $this); |
| 881 |
$('input', $field).val(match[1]); |
| 882 |
$('p.content', $field).html(match[1]); |
| 883 |
$field = $('div.cf7-field-type', $this); |
| 884 |
const tag = $('textarea', $field).val(match[5]).scanCF7Tag(); |
| 885 |
$('p.content', $field).html(tag); |
| 886 |
$field = $('div.cf7-field-tip', $this); |
| 887 |
$('input', $field).val(match[6]); |
| 888 |
$('p.content', $field).html(match[6]); |
| 889 |
//hide the textarea |
| 890 |
$('textarea.grid-input', $this).hide(); |
| 891 |
//reset global regex |
| 892 |
templateRegex.lastIndex = 0; |
| 893 |
}else{ //this html does not match our templates |
| 894 |
$('div.cf7-field-inner', $this).remove(); |
| 895 |
} |
| 896 |
}else{//this html does not match our templates |
| 897 |
$('div.cf7-field-inner', $this).remove(); |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
$.fn.scanCF7Tag = function(){ |
| 902 |
let $this = $(this); |
| 903 |
if(!$this.is('textarea')){ |
| 904 |
return ''; |
| 905 |
} |
| 906 |
let $parent = $this.parent(), //.cf7-field-type. |
| 907 |
$helper = $parent.siblings('.php-icon'), |
| 908 |
$jshelper = $parent.siblings('.js-icon'); |
| 909 |
$helper.each(function(index){ |
| 910 |
if(index>0){ |
| 911 |
$(this).remove(); |
| 912 |
return; |
| 913 |
} |
| 914 |
$(this).removeAttr('data-field').removeAttr('data-tag').removeAttr('data-search'); |
| 915 |
}); |
| 916 |
//reset helper. |
| 917 |
$helper = $parent.siblings('.php-icon'); |
| 918 |
//handle jshleper |
| 919 |
$jshelper.each(function(index){ |
| 920 |
if(index>0){ |
| 921 |
$(this).remove(); |
| 922 |
return; |
| 923 |
} |
| 924 |
$(this).removeAttr('data-field').removeAttr('data-tag').removeAttr('data-search'); |
| 925 |
}); |
| 926 |
//reset jshelper. |
| 927 |
$jshelper = $parent.siblings('.js-icon'); |
| 928 |
|
| 929 |
let cf7TagRegexp = /\[(.[^\s]*)\s*(.[^\s\]]*)[\s\[]*(.[^\[]*\"source:([^\s]*)\"[\s^\[]*|[.^\[]*(?!\"source:)[^\[]*)\]/img, |
| 930 |
search = $this.val(), |
| 931 |
match = cf7TagRegexp.exec(search), |
| 932 |
label='', |
| 933 |
isRequired = false, |
| 934 |
type = [], |
| 935 |
fields = [], |
| 936 |
hooks = [],jshooks=[], |
| 937 |
tag='', |
| 938 |
isSubmit = false, hasHidden = false, |
| 939 |
count =0, counth = 0, |
| 940 |
field = '', |
| 941 |
stopSearch = false; |
| 942 |
|
| 943 |
while (match != null && !stopSearch) { |
| 944 |
count++; |
| 945 |
label+='['+match[1]+' '+match[2]+']'; |
| 946 |
tag = match[1].replace('*',''); |
| 947 |
field = match[2]; |
| 948 |
let helpers = ['cf7sg-tag-all'], jsHelpers = ['cf7sg-tag-all']; |
| 949 |
helpers[helpers.length] = 'cf7sg-tag-'+tag; |
| 950 |
jsHelpers[jsHelpers.length] = 'cf7sg-tag-'+tag; |
| 951 |
switch(tag){ |
| 952 |
case 'submit': |
| 953 |
case 'save': |
| 954 |
tag +='-button'; |
| 955 |
isSubmit = true; |
| 956 |
break; |
| 957 |
case 'textarea': |
| 958 |
if( match[0].search(/\s[0-9]{0,3}x[0-9]{1,3}\s?/ig) <0){ |
| 959 |
let cf7sc = match[0].replace(']',' x5]'); |
| 960 |
cf7sc = search.replace(match[0], cf7sc); |
| 961 |
$this.val(cf7sc); |
| 962 |
} |
| 963 |
break; |
| 964 |
case 'acceptance': //special case with closing tag. |
| 965 |
stopSearch = true; |
| 966 |
break; |
| 967 |
case 'recaptch': |
| 968 |
case 'recaptcha': //special case with closing tag. |
| 969 |
label='[recaptcha]'; |
| 970 |
stopSearch = true; |
| 971 |
break; |
| 972 |
case 'dynamic_select': |
| 973 |
let source =''; |
| 974 |
switch(true){ |
| 975 |
case ( match.length > 4 && 'undefined' !== typeof match[4] ) : //match[4] exists. |
| 976 |
source = match[4].split(':'); |
| 977 |
source = source[0]; |
| 978 |
helpers[helpers.length] = 'cf7sg-tag-dynamic_select-'+source; |
| 979 |
case match.length > 3: //lets deal with match[3] |
| 980 |
if(0=== source.length){ |
| 981 |
source="filter"; |
| 982 |
switch(true){ |
| 983 |
case match[3].indexOf("source:post")>0: |
| 984 |
source="post"; |
| 985 |
break; |
| 986 |
case match[3].indexOf("slug:")>0: |
| 987 |
source="taxonomy"; |
| 988 |
break; |
| 989 |
} |
| 990 |
} |
| 991 |
helpers[helpers.length] = 'cf7sg-tag-dynamic_select-'+source; |
| 992 |
|
| 993 |
if(match[3].indexOf('class:tags')>-1){ |
| 994 |
helpers[helpers.length] = 'cf7sg-tag-dynamic_select-tags'; |
| 995 |
if(source.length>0){ |
| 996 |
helpers[helpers.length] = 'cf7sg-tag-dynamic_select-'+source+'-tags'; |
| 997 |
} |
| 998 |
} |
| 999 |
break; |
| 1000 |
} |
| 1001 |
break; |
| 1002 |
case 'hidden': /** @since 3.2.1 fix hidden field class */ |
| 1003 |
tag+='-input'; |
| 1004 |
counth++; |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
/** @since 3.3.0 add extension classes */ |
| 1008 |
if(undefined !== cf7sgCustomHelperModule[tag]){ |
| 1009 |
let ch = cf7sgCustomHelperModule[tag](search); |
| 1010 |
if(Array.isArray(ch.php) && ch.php.length>0) helpers = helpers.concat(ch.php); |
| 1011 |
if(Array.isArray(ch.js) && ch.js.length>0) jsHelpers = jsHelpers.concat(ch.js); |
| 1012 |
} |
| 1013 |
type[type.length] = tag; |
| 1014 |
fields[fields.length] = field; |
| 1015 |
hooks[hooks.length] = helpers; |
| 1016 |
jshooks[jshooks.length] = jsHelpers; |
| 1017 |
if('*' === match[1][match[1].length -1]){ |
| 1018 |
isRequired = true; |
| 1019 |
} |
| 1020 |
if(!stopSearch) match = cf7TagRegexp.exec(search); //get the next match. |
| 1021 |
} |
| 1022 |
let classes = $('#grid-col div.cf7-field-type').attr('class'); |
| 1023 |
classes += " "+ type.join(' '); |
| 1024 |
field = fields.join(' '); |
| 1025 |
// $parent.removeClass('required'); |
| 1026 |
if(isRequired) classes += ' required';//$parent.addClass('required'). |
| 1027 |
const $parentColumn = $parent.closest('.columns'); |
| 1028 |
if($parentColumn.is('[class*="cf7-tags-"]')){ |
| 1029 |
$parentColumn.removeClass(function (index, className) { |
| 1030 |
return (className.match (/(^|\s)cf7-tags-\S+/g) || []).join(' '); |
| 1031 |
}); |
| 1032 |
} |
| 1033 |
if( (count-counth) >1 ){ /** @since 3.2.1 don't count hidden fields */ |
| 1034 |
classes += ' cf7-tags-'+count; |
| 1035 |
$parentColumn.addClass('cf7-tags-'+count); |
| 1036 |
} |
| 1037 |
$parent.attr('class',classes); |
| 1038 |
/**@since 2.0.0 |
| 1039 |
* setup fields for tag specific filters/actions. |
| 1040 |
*/ |
| 1041 |
//for each tag get corresponding set of filters. |
| 1042 |
let helperUsed = false, jsHelperUsed=false,len = type.length, jlen=0; |
| 1043 |
search = ''; |
| 1044 |
for (let i = 0; i < len; i++) { |
| 1045 |
for (let j=0, jlen = hooks[i].length; j<jlen; j++){ |
| 1046 |
search += 'li.'+hooks[i][j]+','; |
| 1047 |
} |
| 1048 |
search = search.slice(0,-1); //remove last ',' |
| 1049 |
if($( search ,$('#fieldhelperdiv')).length>0){ |
| 1050 |
//this tag has some filters. |
| 1051 |
if(helperUsed){ |
| 1052 |
const $clone = $helper.clone(); |
| 1053 |
$helper.after($clone); |
| 1054 |
$helper = $clone; |
| 1055 |
} |
| 1056 |
helperUsed = true; |
| 1057 |
$helper.attr('data-field', fields[i]); |
| 1058 |
$helper.attr('data-tag', type[i]); |
| 1059 |
$helper.attr('data-search', search); |
| 1060 |
$helper.show(); |
| 1061 |
} |
| 1062 |
//js helpers. |
| 1063 |
search='', jlen = jshooks[i].length; |
| 1064 |
for (let j=0; j<jlen; j++){ |
| 1065 |
search += 'li.'+jshooks[i][j]+','; |
| 1066 |
} |
| 1067 |
search = search.slice(0,-1); //remove last ',' |
| 1068 |
if($( search ,$('#fieldhelperjs')).length>0){ |
| 1069 |
//this tag has some filters. |
| 1070 |
if(jsHelperUsed){ |
| 1071 |
const $clone = $jshelper.clone(); |
| 1072 |
$jshelper.after($clone); |
| 1073 |
$jshelper = $clone; |
| 1074 |
} |
| 1075 |
jsHelperUsed = true; |
| 1076 |
$jshelper.attr('data-field', fields[i]); |
| 1077 |
$jshelper.attr('data-tag', type[i]); |
| 1078 |
$jshelper.attr('data-search', search); |
| 1079 |
$jshelper.show(); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
if(isSubmit){ |
| 1083 |
$parent.parent().addClass('submit-field'); |
| 1084 |
} |
| 1085 |
|
| 1086 |
return label; |
| 1087 |
} |
| 1088 |
$.fn.updateGridForm = function(){ |
| 1089 |
const $this = $(this); |
| 1090 |
if(!$this.is('textarea.grid-input')){ |
| 1091 |
return $this; |
| 1092 |
} |
| 1093 |
//label |
| 1094 |
const $label = $this.siblings('div.cf7-field-label').find(':input'); |
| 1095 |
let label = $label.val(); |
| 1096 |
//field |
| 1097 |
const field = $this.siblings('div.cf7-field-type').find('textarea.field-entry').val(); |
| 1098 |
let idx = 0; |
| 1099 |
if(cf7grid.requiredHTML.length>0) idx=label.indexOf(cf7grid.requiredHTML) |
| 1100 |
if($this.siblings('div.cf7-field-type').is('.required')){ |
| 1101 |
/** @since 2.10.4 fix for custom manual labels, allow replacement with empty span*/ |
| 1102 |
if(idx<0) idx = label.search(/<span>[\w\W]*<\/span>/g); |
| 1103 |
if(idx<0){ |
| 1104 |
label += cf7grid.requiredHTML; |
| 1105 |
$label.val(label);//input field. |
| 1106 |
$label.siblings('p.content').html(label); |
| 1107 |
} |
| 1108 |
}else{ |
| 1109 |
if(idx>=0){ |
| 1110 |
label = label.replace(cf7grid.requiredHTML, ''); |
| 1111 |
$label.val(label);//input field. |
| 1112 |
$label.siblings('p.content').html(label); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
//tip |
| 1116 |
const tip = $this.siblings('div.cf7-field-tip').find(':input').val(), |
| 1117 |
$cell = $('<div>').append( cf7grid.preHTML + field + cf7grid.postHTML ); |
| 1118 |
$('label', $cell).html(label); |
| 1119 |
$('.info-tip', $cell).html(tip); |
| 1120 |
//update grid input and trigger change to udpate form |
| 1121 |
if(cf7grid.ui) $this.html($cell.html()+'\n').trigger('change'); |
| 1122 |
else $this.val($cell.html()).trigger('change'); |
| 1123 |
return $this; |
| 1124 |
}; |
| 1125 |
|
| 1126 |
$.fn.toggleSiblingUIFields = function(){ |
| 1127 |
const $this = $(this); |
| 1128 |
if(!$this.is('div.cf7-field-inner')){ |
| 1129 |
return $this; |
| 1130 |
} |
| 1131 |
$this.siblings('div.cf7-field-inner').each(function(){ |
| 1132 |
const $this = $(this); |
| 1133 |
$('p.content', $this).show(); |
| 1134 |
$(':input', $this).hide().attr('id',''); |
| 1135 |
$('span.dashicons', $this).hide(); |
| 1136 |
}); |
| 1137 |
} |
| 1138 |
$.fn.getRowSize = function(){ |
| 1139 |
let size, off, idx, foundSize, classList,total = 0; |
| 1140 |
const sizes = [0]; |
| 1141 |
$(this).children('.columns').each(function(index){ |
| 1142 |
classList = $(this).attr('class').split(/\s+/); |
| 1143 |
foundSize = false; |
| 1144 |
for(idx=0;idx<classList.length; idx++){ |
| 1145 |
size = $.inArray(classList[idx], columnsizes); |
| 1146 |
off = $.inArray(classList[idx], offsets); |
| 1147 |
if(size > -1){ |
| 1148 |
foundSize = true; |
| 1149 |
sizes[index] = size; |
| 1150 |
total += size + 1; |
| 1151 |
} |
| 1152 |
if(off > -1) total+= off+1; |
| 1153 |
} |
| 1154 |
if(!foundSize){ //by default a colum which is not set set is treated as 1 |
| 1155 |
sizes[index] = 0; |
| 1156 |
total += 1; |
| 1157 |
} |
| 1158 |
}); |
| 1159 |
return {'length':total, 'cols':sizes}; |
| 1160 |
} |
| 1161 |
$.fn.getColumnTotalSize = function(){ |
| 1162 |
const $this = $(this); |
| 1163 |
if(! $this.is('.columns')){ |
| 1164 |
return 0; |
| 1165 |
} |
| 1166 |
let off, foundSize, size = 0,total = 0, classList = $this.attr('class').split(/\s+/); |
| 1167 |
const $sizes = $this.find('.grid-column select.column-size'), $offsets = $this.find('.grid-column select.column-offset'); |
| 1168 |
$offsets.val(''); |
| 1169 |
$sizes.val('one'); |
| 1170 |
foundSize = false; |
| 1171 |
for(let idx=0;idx<classList.length; idx++){ |
| 1172 |
if(!foundSize){ |
| 1173 |
size = $.inArray(classList[idx], columnsizes); |
| 1174 |
if(size > -1){ |
| 1175 |
foundSize = true; |
| 1176 |
total += size + 1; |
| 1177 |
//reset select |
| 1178 |
$sizes.val(classList[idx]); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
off = $.inArray(classList[idx], offsets); |
| 1182 |
if(off > -1){ |
| 1183 |
total += off+1; |
| 1184 |
$offsets.val(classList[idx]); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
if(!foundSize){ //by default a colum which is not set set is treated as 1 |
| 1188 |
size = 0; //by default a colum which is not set set is treated as 1 |
| 1189 |
total += 1; |
| 1190 |
} |
| 1191 |
return {'length':total, 'size':size}; |
| 1192 |
} |
| 1193 |
//add new rows |
| 1194 |
$.fn.insertNewRow = function(areaCode){ |
| 1195 |
const $this = $(this); |
| 1196 |
if(typeof areaCode === 'undefined') areaCode =''; |
| 1197 |
const $newRow = $( $('#grid-row').html() ); |
| 1198 |
//append the column controls and textarea |
| 1199 |
$('.columns', $newRow).append( $($('#grid-col').html()) ); |
| 1200 |
|
| 1201 |
switch(true){ |
| 1202 |
case $this.is('.columns'): |
| 1203 |
case $this.is($grid): |
| 1204 |
case $this.is('.row'): |
| 1205 |
$this.append($newRow); |
| 1206 |
break; |
| 1207 |
// $this.prepend($newRow); |
| 1208 |
// break; |
| 1209 |
case $this.is('.container'): |
| 1210 |
case $this.is('.cf7sg-external-form'): |
| 1211 |
$this.after($newRow); |
| 1212 |
break; |
| 1213 |
default: //unknown element, maybe an error. |
| 1214 |
return $this; |
| 1215 |
} |
| 1216 |
|
| 1217 |
//is areaCode text or jQuery object? |
| 1218 |
if(areaCode instanceof jQuery){ |
| 1219 |
$('.columns', $newRow).remove(); |
| 1220 |
$('.row', $newRow).prepend(areaCode); |
| 1221 |
}else{ |
| 1222 |
//add the code to the textarea |
| 1223 |
if(cf7grid.ui){ |
| 1224 |
$('textarea.grid-input',$newRow).html(areaCode).hide();//.trigger('change'); |
| 1225 |
$newRow.html2gui(areaCode); |
| 1226 |
}else{ |
| 1227 |
$('textarea.grid-input',$newRow).val(areaCode);//.trigger('change'); |
| 1228 |
$('div.cf7-field-inner', $newRow).hide(); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
//make new row's columns sortable. |
| 1232 |
sortableRows($newRow); |
| 1233 |
$newRow.fireGridUpdate('add','row'); |
| 1234 |
return $this; |
| 1235 |
} |
| 1236 |
//refresh controls select |
| 1237 |
$.fn.changeColumnSize = function(oldSize, newSize){ |
| 1238 |
const $this = $(this); |
| 1239 |
if(oldSize.length > 0) $this.removeClass(oldSize); |
| 1240 |
$this.addClass(newSize); |
| 1241 |
$('.column-size option[value="'+newSize+'"]', $this ).prop('selected', true); |
| 1242 |
} |
| 1243 |
//$target.closest('.grid-controls').filterColumnControls(); |
| 1244 |
$.fn.filterColumnControls = function(){ |
| 1245 |
const $this = $(this); |
| 1246 |
if(!$this.is('.grid-controls')){ |
| 1247 |
return $this; |
| 1248 |
} |
| 1249 |
//enable all options |
| 1250 |
$('.column-size option', $this ).prop('disabled', false); |
| 1251 |
$('.column-offset option', $this ).prop('disabled', false); |
| 1252 |
const $parentRow = $this.closest('.row'), $parentColumn = $this.closest('.columns'); |
| 1253 |
const row = $parentRow.getRowSize(), col = $parentColumn.getColumnTotalSize(); |
| 1254 |
let idx, start, free = 0; |
| 1255 |
if(row.length < 12) free = (12 - row.length); |
| 1256 |
for(idx = start = col.size+1; idx < columnsizes.length; idx++){ |
| 1257 |
if( idx > (free + start - 1) ){ |
| 1258 |
$('.column-size option[value="'+columnsizes[idx]+'"]', $this ).prop('disabled', true); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
for(idx = start = col.length - col.size - 1 ;idx< offsets.length; idx++){ |
| 1262 |
if( idx > (free + start - 1) ){ |
| 1263 |
$('.column-offset option[value="'+offsets[idx]+'"]', $this ).prop('disabled', true); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
return $this; |
| 1267 |
} |
| 1268 |
/** @since 3.4.0 fire grid ui update events.*/ |
| 1269 |
$.fn.fireGridUpdate = function(action, element){ |
| 1270 |
let e={ |
| 1271 |
bubbles: true, |
| 1272 |
cancelable: true, |
| 1273 |
}; |
| 1274 |
e[action]=element; |
| 1275 |
$(this).trigger('cf7sg-update',e); |
| 1276 |
} |
| 1277 |
|
| 1278 |
})( jQuery ); |
| 1279 |
|
| 1280 |
/** @since 3.3.0 custom helpers */ |
| 1281 |
var cf7sgCustomHelperModule = (function (cch) { |
| 1282 |
return cch; |
| 1283 |
}(cf7sgCustomHelperModule || {})); |
| 1284 |
|