').html(cf7grid.preHTML+'\\s*(\\[.*\\s*\\].*\\s*){0,}\\s*'+cf7grid.postHTML);
var required = cf7grid.requiredHTML.replace('*', '\\*');
// console.log('r:'+required);
$pattern.find('label').html('((\\s*.*)('+required+'){1}|(\\s*.*))');
$pattern.find('.info-tip').text('(.*\\s*)');
//console.log('p:'+$pattern.html());
var templateRegex = new RegExp($pattern.html(), 'ig');
var seekTemplate = false;
var cssTemplate = 'div.field';
var $template = $('
').append(cf7grid.preHTML+cf7grid.postHTML);
$template = $template.children();
if(1==$template.length && $template.find('label').length>0){
seekTemplate = true;
cssTemplate = $template.prop('nodeName');
if($template.prop('class').length>0){
cssTemplate += '.'+$template.attr('class').split(' ').join('.');
}
}
var wpcf7Value = '';
$(document).ready( function(){
//change the form id to mimic cf7 plugin custom admin page.
/** @since 2.11.0 full screen button*/
var $editor = $('#cf7sg-editor'),
bodyWidth = $('#wpbody').width();
$editor.css('background-color',$('body').css('background-color'));
$('#full-screen-cf7').on('click', function(){
$editor.toggleClass('full-screen');
$(this).toggleClass('full-screen');
if($editor.is('.full-screen')) $editor.width(bodyWidth);
else $editor.width('auto');
// $oldParent = $editor.parent();
// $editor.detach();
// $nextParent.prepend($editor);
// $nextParent = $oldParent;
});
$wpcf7Editor = $('textarea#wpcf7-form-hidden');
$grid = $('#grid-form');
$rowControl = $('#top-grid-controls');
/*
Build grid from existing form------------------------- BUILD UI FORM
*/
function buildGridForm(){
var formhtml = $wpcf7Editor.text();
if(0===formhtml.length){
formhtml = '
';
}
var $form = $('
').append( formhtml );
var isGrid = true; //return value.
$grid.html(''); //empty the grid.
if(0===$form.children('.container').length){
isGrid = false;
}
//remove the external forms
$('.cf7sg-external-form .cf7sg-external-form-content', $form).remove();
//replace columns content with textareas
/*--------------------------------------------------- convert columns */
$('div.columns', $form).each(function(){
var $this = $(this);
var $area = $($('#grid-col').html());
if($this.children().is('.container')){
$('textarea.grid-input', $area).remove();
$('div.cf7-field-inner', $area).remove();
}else{
if(cf7grid.ui) $('textarea.grid-input', $area).html($this.html().trim());
else $('textarea.grid-input', $area).val($this.html().trim());
$this.children().remove();
$this.text('');
}
$this.prepend($area);
});
$('div.row', $form).each(function(){
$(this).append( $('#grid-row .row-controls').clone() );
});
/*--------------------------------------------------- convert collapsible sections */
$('div.container.cf7sg-collapsible', $form).each(function(){
var $this = $(this);
var id = $this.attr('id');
if(typeof id == 'undefined'){
id = randString(6);
$this.attr('id', id); //assign a random id
}
var text = $this.children('.cf7sg-collapsible-title span.cf7sg-title').text();
if(0==text.length){ //pre v1.8 title?.
text = $this.children('.cf7sg-collapsible-title').text();
}
var $toggle = $('.toggle', $this.children('.cf7sg-collapsible-title'));
if($toggle.length>0){
$toggle = $toggle.clone();
}
$this.children('.cf7sg-collapsible-title').remove();
$this.prepend( $('#grid-collapsible').html());
$('input', $this.children('.cf7sg-collapsible-title')).not('[type="checkbox"]').val(text);
if($toggle.length>0){
$this.children('.cf7sg-collapsible-title').append($toggle);
$('input[type="checkbox"]', $this.children('.cf7sg-collapsible-title') ).prop('checked', true);
}
var $ctrl = $this.children('.row').children('.row-controls').find('.collapsible-row-label');
$('input', $ctrl).prop('checked', true);
//toggle disable the sibling input
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;});
});
/*--------------------------------------------------- convert tables */
$('div.container.cf7-sg-table', $form).each(function(){
var $this = $(this);
var id = $this.attr('id');
if(typeof id == 'undefined'){
id = 'cf7-sg-table-'+(new Date).getTime();
$this.attr('id', id);
}
var $ctrl = $this.find('.row.cf7-sg-table > .row-controls' ).first().find('.table-row-label');
$('input', $ctrl).prop('checked', true);
//set button label
var text = $this.data('button');
if(typeof text == 'undefined'){
text = 'Add Row';
$this.attr('data-button',text);
}
$ctrl.next('.table-row-button').children('input').val(text);
//toggle disable the sibling input
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;});
//toggle footer row
var $footer = $this.next();
if($footer.is('.cf7-sg-table-footer')){
$ctrl = $footer.children('.row').first().find('.row-controls .footer-row-label');
$('input.footer-row', $ctrl).prop('checked', true);
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;});
}
});
//tabs
/*--------------------------------------------------- convert tabs */
$('ul.cf7-sg-tabs-list li', $form).each(function(){
var $this = $(this);
var text = $this.children('a').text();
$this.append($('#grid-tabs ul li label').clone());
$('label input', $this).val(text);
//setup checkbox
var $ctrl = $this.parent().siblings('.cf7-sg-tabs-panel');
$ctrl = $ctrl.children('.row').find('.row-controls' ).first().find('.tabs-row-label');
$('input.tabs-row', $ctrl).prop('checked', true);
$('input', $ctrl.siblings('.unique-mod')).prop('disabled', function(i,v){return !v;});
});
//reinsert the external forms
$('.cf7sg-external-form', $form).each(function(){
var $extform = $(this);
$extform.append($( $('#grid-cf7-forms .cf7sg-external-form').html() ) );
var id = $extform.data('form');
if($('#grid-cf7-forms .form-select option[value="'+id+'"]' ).length > 0 ){
//add controls
//$extform.append($('#grid-cf7-forms .form-controls').clone());
$('.form-controls .form-select', $extform).val(id);
//check for form update.
var data = {
'action' : 'get_cf7_content',
'id': $('#post_ID').val(),
'nonce' : $('#_wpcf7nonce').val(),
'cf7_key' : id,
'update' :true
};
$.post(ajaxurl, data, function(response) {
if(response.length > 0){
$('.cf7sg-external-form-content', $extform).attr('id','cf7sg-form-'+id).append( response );
}//TODO if error insert a msg.
});
}
});
//add the form to the grid
if($form.children('.container').length >0 || $form.children('.cf7sg-external-form').length>0){
$grid.append($form.children());
}else{ //this is not a cf7sg form.
$grid.html($form.html());
}
//set the value of each textarea as inner text
$('textarea', $grid).each(function(){
var $this = $(this);
$this.html($this.val());
});
/*--------------------------------------------------- if ui mode, then convert to gui template */
var $textareaSelected='';
if(cf7grid.ui){
$('div.columns', $grid).each(function(){
var $this = $(this);
if($this.children().is('.container')) return true;
$this.html2gui();
});
}else{
//set the first textarea as our default tag consumer
$('textarea#wpcf7-form').attr('id','');
$textareaSelected = $('textarea', $grid).first();
$textareaSelected.attr('id', 'wpcf7-form');
}
//change this to whichever is live
$('textarea', $grid).on('focus', function(){
var $this = $(this);
if($textareaSelected.length>0 && $textareaSelected.is('#wpcf7-form')){
$textareaSelected.attr('id','');
$textareaSelected.html($textareaSelected.val()); //set its inner html
}
if($this.is('.grid-input')){
$('textarea#wpcf7-form').attr('id','');
$textareaSelected = $this.attr('id','wpcf7-form');
}
});
return isGrid;
} //end buildGridForm()
//offset/size change using event delegation
/*---------------------------------------------------------------------------- ui menus */
$grid.on('change', function(event){
var $target = $(event.target);
if($target.is('.column-setting')){ //----------- column size/offset settings
var validation = ['dummy'];
if( $target.is('.column-offset') ){
validation = offsets;
}else if( $target.is('.column-size') ){
validation = columnsizes;
}else{
return false;
}
var $column = $target.closest('.columns');
var classList = $column.attr('class').split(/\s+/);
var idx;
for(idx=0; idx -1){
$column.removeClass(classList[idx]);
}
}
$column.addClass($target.val());
//filter the options
$target.closest('.grid-controls').filterColumnControls();
}else if($target.is('.form-select')){ //-------------- external form selection
var $container = $target.closest('.cf7sg-external-form');
$container.attr('data-form', $target.val());
var data = {
'action' : 'get_cf7_content',
'id': $('#post_ID').val(),
'nonce' : $('#_wpcf7nonce').val(),
'cf7_key' : $target.val()
};
$.post(ajaxurl, data, function(response) {
$('.cf7sg-external-form-content', $container).attr('id','cf7sg-form-'+$target.val()).html(response);
});
}
/*
Collapsible / tabs rows input changes
*/
if($target.is('.cf7sg-collapsible-title label input[type="text"]')){ //------- Collapsible title
$target.siblings('input[type="hidden"]').val($target.val());
}else if( $target.is('.cf7sg-collapsible-title label input[type="checkbox"]')){ //------- Collapsible title toggled
var $title = $target.closest('.cf7sg-collapsible-title');
if($target.is(':checked')){
$title.append($('#grid-collapsible-with-toggle').html());
$title.closest('.container.cf7sg-collapsible').addClass('with-toggle').attr('data-group','');
}else{
$('.toggle', $title).remove();
$title.closest('.container.cf7sg-collapsible').removeClass('with-toggle').removeAttr( 'data-group');
}
}else if($target.is('ul.cf7-sg-tabs-list li label input[type="text"]')){ //------- Tabs title
$target.parent().siblings('a').text($target.val());
}else if($target.is('label.table-row-button input')){
$target.closest('.container.cf7-sg-table').attr('data-button',$target.val());
}
if(cf7grid.ui){
if($target.is('.cf7-field-inner textarea')){
var label = $target.scanCF7Tag();
$target.siblings('p.content').html(label);//.show();
$target.parent().siblings('textarea.grid-input').updateGridForm();
}else if($target.is('.cf7-field-inner input')){
$target.siblings('p.content').html($target.val());
$target.parent().siblings('textarea.grid-input').updateGridForm();
}
}
});
//grid click event delegation
$grid.on('click', function(event){
var $target = $(event.target);
switch(true){
case $target.is('input[type="text"]:visible'):
case $target.is('textarea:visible'): //click on a field.
case $target.is('select:visible'):
return true;
break;
}
//close any open row/column controls
closeAllControls();
/*
Row controls
----------------------------------------------------------ROW CONTRLS
*/
var $parentRow;
if($target.is('.dashicons-trash.form-control')){ //--------TRASH included form
$target.closest('.cf7sg-external-form').remove();
}else if($target.is('.dashicons-plus.form-control') ){ //---ADD external form
$target.closest('.cf7sg-external-form').insertNewRow();
}else if($target.is('.dashicons-trash.row-control')){ //--------TRASH
var $parentContainer = $target.closest('.container');
var $parent = $parentContainer.parent();
$parentContainer.remove();
if( $parent.is('.columns') ) { //verify is this is the last row being deleted
if( 0 == $parent.children('.container').length ){
//add a text area to the column
$parent.children('.grid-column').append('');
}
}
}else if($target.is('.dashicons-plus.row-control')){ //-----------ADD Row
$target.closest('.container').insertNewRow();
}else if($target.is('.dashicons-edit.row-control')){ //-----------Show controls
//hide any other controls that might be open
//taken care by closeAllControls
// $('.grid-controls', $grid).hide();
// $('.dashicons-no-alt', $grid).hide();
// $('.dashicons-edit', $grid).show();
//now show this control
$target.siblings('.grid-controls').show();
$target.hide();
$target.siblings('.dashicons-no-alt').show();
/*
TODO: use $('.grid-controls')filterColumnControls() to make sure columns sizes/offsets are correct.
possibly introduce a boolean to check if filter has been run already on this row
*/
}else if( $target.is('.dashicons-no-alt.row-control') ) { //----------------hide controls
//take care by closeAllControls
// $target.siblings('.grid-controls').hide();
// $target.hide();
// $target.siblings('.dashicons-edit').show();
}else if($target.is('input.collapsible-row')){ //-------------checkbox collapsible row
var $container = $target.closest('.container');
if($target.is(':checked')){
$container.addClass('cf7sg-collapsible');
var id = $container.attr('id');
if(typeof id == 'undefined'){
id = randString(6);
$container.attr('id', id); //assign a random id
}
$container.prepend($('#grid-collapsible').html());
}else{
$container.removeClass('cf7sg-collapsible');
$container.children('.cf7sg-collapsible-title').remove();
}
//toggle disable the sibling input
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;});
}else if($target.is('input.table-row')){ //-------------checkbox table row
if($target.is(':checked')){
var id = 'cf7-sg-table-'+(new Date).getTime();
$target.closest('.row').addClass('cf7-sg-table');
$target.closest('.container').addClass('cf7-sg-table').attr('id',id);
}else{
$target.closest('.row').removeClass('cf7-sg-table');
$target.closest('.container').removeClass('cf7-sg-table').removeAttr('id');
}
//toggle disable the sibling input
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;});
}else if($target.is('input.tabs-row')){ //-------------checkbox tabbed row
var $panel = $target.closest('.container');
if($target.is(':checked')){
var id = 'cf7-sg-tab-'+(new Date).getTime();
$panel.addClass('cf7-sg-tabs-panel').attr('id',id);
$panel.before($('#grid-tabs').html());
$panel.closest('.columns').addClass('cf7-sg-tabs');
$('li a', $panel.siblings('ul.cf7-sg-tabs-list')).attr('href','#'+id);
}else{
$panel.removeClass('cf7-sg-tabs-panel');
$panel.closest('.columns').removeClass('cf7-sg-tabs');
$panel.siblings('ul.cf7-sg-tabs-list').remove();
}
//toggle disable the sibling input
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;});
}else if($target.is('input.footer-row')){ //-------------checkbox footer row
var $table = $target.closest('.container').prev();
if($table.is('.container.cf7-sg-table')){
if($target.is(':checked')){
$target.closest('.container').addClass('cf7-sg-table-footer');
}else{
$target.closest('.container').removeClass('cf7-sg-table-footer');
}
//toggle disable the sibling input
$target.parent().siblings('label.unique-mod').children('input').prop('disabled', function(i,v){return !v;});
}
}
/*
Column controls, show/hide contols, add column, and refresh select dropdowns for colum size on addition
*/
var $parentColumn ;
if( $target.is('.columns')){
$parentColumn = $target;
}else{
$parentColumn = $target.closest('.columns');
}
//let's close any column controls : taken care by closeAllControls
// if(0===$target.closest('.grid-controls').length ){
// $('.grid-column .grid-controls', $grid).hide();
// $('.grid-column .column-control.dashicons-no-alt', $grid).hide();
// $('.grid-column .column-control.dashicons-edit', $grid).show();
// //close any ui fields if any
// if(cf7grid.ui && !$target.is('.cf7-field-inner *')){
// $('.grid-column .cf7-field-inner span.dashicons').trigger('click');
// }
// }
//verify which target was clicked
if( $target.is('.dashicons-edit.column-control') ){ //------------------show controls
//now show this control
$target.siblings('.grid-controls').show().filterColumnControls();
$target.hide();
$target.siblings('.dashicons-no-alt').show();
}else if( $target.is('.php-icon.column-control') ) { //--------show hooks
let $helper =$('