(function($){ if(typeof acf === 'undefined') return; /* * Dropdown Init */ var Popup = acf.models.TooltipConfirm.extend({ render: function(){ // set HTML this.html(this.get('text')); // add class this.$el.addClass('acf-fc-popup'); } }); acf.addAction('new_field/type=flexible_content', function(flexible){ /* * Existing Layouts */ var $layouts = flexible.$layouts(); if($layouts.length){ $layouts.each(function(){ var $layout = $(this); var $controls = $layout.find('> .acf-fc-layout-controls'); // Remove potential duplicated buttons $controls.find('> .acfe-flexible-icon').remove(); // Add Button: Copy $controls.prepend(''); // Add Button: Clone $controls.prepend(''); }); } /* * Available Layouts */ var $clones = flexible.$clones(); if($clones.length){ $clones.each(function(){ var $layout = $(this); var $controls = $layout.find('> .acf-fc-layout-controls'); // Remove potential duplicated buttons $controls.find('> .acfe-flexible-icon').remove(); // Add Button: Copy $controls.prepend(''); // Add Button: Clone $controls.prepend(''); }); } /* * Layout: Clone */ flexible.acfe_flexible_control_single_clone = function(e, $el){ // Current layout var $layout_original = $el.closest('.layout'); var $layout = $el.closest('.layout').clone(); // Clean Layout acfe_flexible_control_clean_layout($layout); // Clone flexible.acfe_duplicate_layout({ layout: $layout, before: $layout_original }); } flexible.addEvents({'click [data-acfe-flexible-control-clone]': 'acfe_flexible_control_single_clone'}); /* * Layout: Copy */ flexible.acfe_flexible_control_single_copy = function(e, $el){ // Current layout var $layout = $el.closest('.layout').clone(); // Fix input value $layout.find('input').each(function(){ $(this).attr('value', this.value); }); // Fix teaxtarea value $layout.find('textarea').each(function(){ $(this).html(this.value); }); // Fix radio/checkbox value $layout.find('input:radio,input:checkbox').each(function() { if(this.checked) $(this).attr('checked', 'checked'); else $(this).attr('checked', false); }); // Fix Select value $layout.find('option').each(function(){ if(this.selected) $(this).attr('selected', 'selected'); else $(this).attr('selected', false); }); // Clean layout acfe_flexible_control_clean_layout($layout); // Get layout data var layout_data = JSON.stringify($layout[0].outerHTML); // Append temp input $el.append('').find('input').attr('value', layout_data).select(); // Command: Copy if(document.execCommand('copy')){ alert('Layout has been transferred to your clipboard'); } // Command: Copy not available. Display prompt else{ prompt('Copy the following layout data to your clipboard', layout_data); } // Remove the temp input $el.find('input').remove(); } flexible.addEvents({'click [data-acfe-flexible-control-copy]': 'acfe_flexible_control_single_copy'}); /* * Dropdown: Paste */ function acfe_flexible_control_button_paste(flexible){ var paste = prompt('Paste layouts data in the folowing field'); // No input if(paste == null || paste == '') return; try{ // Paste HTML var $html = $(JSON.parse(paste)); // Parsed layouts var $html_layouts = $html.closest('[data-layout]'); if(!$html_layouts.length) return alert('Invalid data'); // init var validated_layouts = []; // Each first level layouts $html_layouts.each(function(){ var $this = $(this); // Validate layout against available layouts var get_clone_layout = flexible.$clone($this.attr('data-layout')); // Layout is invalid if(!get_clone_layout.length) return; // Add validated layout validated_layouts.push($this); }); // Nothing to add if(!validated_layouts.length) return alert('No corresponding layouts found'); // Add layouts $.each(validated_layouts, function(){ flexible.acfe_duplicate_layout({ layout: $(this), before: false }); }); }catch(e){ alert('Invalid data'); } } /* * Dropdown: Copy All layouts */ function acfe_flexible_control_button_copy(flexible){ // Get layouts var $layouts = flexible.$layoutsWrap().clone(); // Fix attribute value different to DOM $layouts.find('input').each(function(){ $(this).attr('value', this.value); }); $layouts.find('textarea').each(function(){ $(this).html(this.value); }); $layouts.find('input:radio,input:checkbox').each(function() { if(this.checked) $(this).attr('checked', 'checked'); else $(this).attr('checked', false); }); $layouts.find('option').each(function(){ if(this.selected) $(this).attr('selected', 'selected'); else $(this).attr('selected', false); }); // Clean layout acfe_flexible_control_clean_layout($layouts); // Get layouts data var layouts_data = JSON.stringify($layouts.html()); var input = ''; flexible.$el.append(input); var $copy_layout_button = flexible.$el.find('.acfe_button_copy_input'); // Command: Copy select the input $copy_layout_button.attr('value', layouts_data).select(); // Command: Copy if(document.execCommand('copy')){ alert('Layouts have been transferred to your clipboard'); } // Command: Copy not available. Display prompt else{ prompt('Copy the following layouts data to your clipboard', layouts_data); } $copy_layout_button.remove(); } /* * Dropdown HTML */ var dropdown_grey = '' + ' ' + '' + '