ACC SHELL
// start the popup specefic scripts
// safe to use $
jQuery(document).ready(function($) {
var coffees = {
iconsSelect: function() {
$('.coffee-form-icons').each(function() {
var icons= $(this).find('.icon-select');
var input = '#'+$(this).attr('id').replace('-select', '');
icons.click(function() {
if($(this).hasClass('current')){
return;
} else {
$(input).attr("value", jQuery(this).attr('data-value'));
icons.removeClass('current');
$(this).addClass('current');
coffees.loadVals();
}
});
});
},
loadVals: function()
{
var shortcode = $('#_coffee_shortcode').text(),
uShortcode = shortcode;
// fill in the gaps eg {{param}}
$('.coffee-input').each(function() {
var input = $(this),
id = input.attr('id'),
id = id.replace('coffee_', ''), // gets rid of the coffee_ prefix
re = new RegExp("{{"+id+"}}","g");
uShortcode = uShortcode.replace(re, input.val());
});
// adds the filled-in shortcode as hidden input
$('#_coffee_ushortcode').remove();
$('#coffee-sc-form-table').prepend('<div id="_coffee_ushortcode" class="hidden">' + uShortcode + '</div>');
// updates preview
coffees.updatePreview();
},
cLoadVals: function()
{
var shortcode = $('#_coffee_cshortcode').text(),
pShortcode = '';
shortcodes = '';
// fill in the gaps eg {{param}}
$('.child-clone-row').each(function() {
var row = $(this),
rShortcode = shortcode;
$('.coffee-cinput', this).each(function() {
var input = $(this),
id = input.attr('id'),
id = id.replace('coffee_', '') // gets rid of the coffee_ prefix
re = new RegExp("{{"+id+"}}","g");
rShortcode = rShortcode.replace(re, input.val());
});
shortcodes = shortcodes + rShortcode + "\n";
});
// adds the filled-in shortcode as hidden input
$('#_coffee_cshortcodes').remove();
$('.child-clone-rows').prepend('<div id="_coffee_cshortcodes" class="hidden">' + shortcodes + '</div>');
// add to parent shortcode
this.loadVals();
pShortcode = $('#_coffee_ushortcode').text().replace('{{child_shortcode}}', shortcodes);
// add updated parent shortcode
$('#_coffee_ushortcode').remove();
$('#coffee-sc-form-table').prepend('<div id="_coffee_ushortcode" class="hidden">' + pShortcode + '</div>');
// updates preview
coffees.updatePreview();
},
children: function()
{
// assign the cloning plugin
$('.child-clone-rows').appendo({
subSelect: '> div.child-clone-row:last-child',
allowDelete: false,
focusFirst: false
});
// remove button
$('.child-clone-row-remove').live('click', function() {
var btn = $(this),
row = btn.parent();
if( $('.child-clone-row').size() > 1 )
{
row.remove();
}
else
{
alert('You need a minimum of one row');
}
return false;
});
// assign jUI sortable
$( ".child-clone-rows" ).sortable({
placeholder: "sortable-placeholder",
items: '.child-clone-row'
});
},
updatePreview: function()
{
if( $('#coffee-sc-preview').size() > 0 )
{
var shortcode = $('#_coffee_ushortcode').html(),
iframe = $('#coffee-sc-preview'),
iframeSrc = iframe.attr('src'),
iframeSrc = iframeSrc.split('preview.php'),
iframeSrc = iframeSrc[0] + 'preview.php';
// updates the src value
iframe.attr( 'src', iframeSrc + '?sc=' + base64_encode( shortcode ) );
// update the height
$('#coffee-sc-preview').height( $('#coffee-popup').outerHeight()-42 );
}
},
resizeTB: function()
{
var ajaxCont = $('#TB_ajaxContent'),
tbWindow = $('#TB_window'),
coffeePopup = $('#coffee-popup'),
no_preview = ($('#_coffee_preview').text() == 'false') ? true : false;
if( no_preview )
{
ajaxCont.css({
paddingTop: 0,
paddingLeft: 0,
height: (tbWindow.outerHeight()-47),
overflow: 'scroll', // IMPORTANT
width: 560
});
tbWindow.css({
width: ajaxCont.outerWidth()-15,
marginLeft: -(ajaxCont.outerWidth()/2)
});
$('#coffee-popup').addClass('no_preview');
}
else
{
ajaxCont.css({
padding: 0,
// height: (tbWindow.outerHeight()-47),
height: coffeePopup.outerHeight()-15,
overflow: 'hidden' // IMPORTANT
});
tbWindow.css({
width: ajaxCont.outerWidth(),
height: (ajaxCont.outerHeight() + 30),
marginLeft: -(ajaxCont.outerWidth()/2),
marginTop: -((ajaxCont.outerHeight() + 47)/2),
top: '50%'
});
}
},
load: function()
{
var coffees = this,
popup = $('#coffee-popup'),
form = $('#coffee-sc-form', popup),
shortcode = $('#_coffee_shortcode', form).text(),
popupType = $('#_coffee_popup', form).text(),
uShortcode = '';
// resize TB
coffees.resizeTB();
$(window).resize(function() { coffees.resizeTB() });
// initialise
coffees.loadVals();
coffees.children();
coffees.cLoadVals();
coffees.iconsSelect();
// update on children value change
$('.coffee-cinput', form).live('change', function() {
coffees.cLoadVals();
});
// update on value change
$('.coffee-input', form).change(function() {
coffees.loadVals();
});
// when insert is clicked
$('.coffee-insert', form).click(function() {
if(window.tinyMCE)
{
window.tinyMCE.execInstanceCommand('content', 'mceInsertContent', false, $('#_coffee_ushortcode', form).html());
tb_remove();
}
});
}
}
// run
$('#coffee-popup').livequery( function() { coffees.load(); } );
});
ACC SHELL 2018