Expected behavior:
In your image table view, click on any image. This will load and display the carousel, with selected images displayed at their original size. A caption describing the image can be displayed above or below the image. To close the carousel, click the 'close' button on the top right.
Okay lets get going! BACK UP YOUR IMAGE TABLE FILE FIRST, OBVIOUSLY.
Instructions:
1. Add a table to your AppGini. I called mine 'carousel'. Fields: id (int), included (checkbox, tinyint), image (varchar 255), caption (varchar 255), location (varchar 10), with options: ;;top;;bottom, and default bottom.
of course, you can use an existing table (example: images) and fields, just adjust accordingly.
2. In your hooks folder, create a new file called carousel-tv.js (or use the name of your file, like images-tv.js) and paste the following code into it:
Code: Select all
$j(() => {
const table = $j('.table_view');
if(!table.length) return;
table.find('td.carousel-image a').removeAttr('data-lightbox');
const controls = $j('.action-links, .selected_records_menu, .pagination-section, .filter-breadcrumbs');
const pageTitle = $j('.page-header, h1').first();
let slides = [];
const textFromCell = (row, className) => {
return $j.trim(row.find(`td.${className}`).text());
};
const isIncluded = (row) => {
const includeCell = row.find('td.carousel-include');
if(!includeCell.length) return true;
return includeCell.find('.glyphicon-check').length > 0 || $j.trim(includeCell.text()) === '1';
};
const collectSlides = () => {
slides = [];
table.find('tbody tr').each(function() {
const row = $j(this);
const imageLink = row.find('td.carousel-image a').first();
if(!imageLink.length || !isIncluded(row)) return;
const imageUrl = imageLink.attr('href');
if(!imageUrl) return;
slides.push({
image: imageUrl,
caption: textFromCell(row, 'carousel-caption'),
location: (textFromCell(row, 'carousel-display') || textFromCell(row, 'carousel-location') || 'below').toLowerCase()
});
});
};
const buildCarousel = (startIndex) => {
const indicators = [];
const items = [];
slides.forEach((slide, index) => {
const active = (index === startIndex ? 'active' : '');
const captionAtTop = ['top', 'above', 'caption above'].indexOf(slide.location) !== -1;
const caption = `
<div class="custom-carousel-caption" style="background: rgba(0,0,0,0.72); border-radius: 6px; padding: 12px; margin: 12px auto; max-width: 90%;">
<p style="margin: 0; color: #fff; font-weight: bold;"></p>
</div>
`;
const image = `<img src="${slide.image}" class="center-block" style="max-width: 100%; max-height: 70vh; width: auto; height: auto; object-fit: contain; background: #222;" alt="">`;
indicators.push(`<li data-target="#customImageCarousel" data-slide-to="${index}" class="${active}"></li>`);
items.push(`
<div class="item ${active}">
${captionAtTop ? caption : ''}
${image}
${captionAtTop ? '' : caption}
</div>
`);
});
const carousel = $j(`
<div id="customCarouselWrap" style="max-width: 900px; margin: 20px auto;">
<div class="clearfix" style="margin-bottom: 10px;">
<button type="button" id="closeCustomCarousel" class="btn btn-default pull-right">
<i class="glyphicon glyphicon-remove"></i> Close carousel
</button>
</div>
<div id="customImageCarousel" class="carousel slide" data-ride="carousel" data-interval="false">
<ol class="carousel-indicators">${indicators.join('')}</ol>
<div class="carousel-inner" style="border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.2);">${items.join('')}</div>
<a class="left carousel-control" href="#customImageCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#customImageCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
`);
carousel.find('.item').each(function(index) {
$j(this).find('.custom-carousel-caption p').text(slides[index].caption);
});
return carousel;
};
const showTable = () => {
$j('#customCarouselWrap').remove();
table.show();
controls.show();
pageTitle.show();
};
const showCarousel = (startIndex) => {
collectSlides();
if(!slides.length) return;
$j('#customCarouselWrap').remove();
table.hide();
controls.hide();
pageTitle.hide();
const carousel = buildCarousel(startIndex);
table.before(carousel);
carousel.find('#customImageCarousel').carousel(startIndex);
};
table.on('click', 'td.carousel-image a, td.carousel-image img', function(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
collectSlides();
const clickedHref = $j(this).closest('a').attr('href') || $j(this).closest('td.carousel-image').find('a').attr('href');
const startIndex = Math.max(0, slides.findIndex((slide) => slide.image === clickedHref));
showCarousel(startIndex);
});
$j(document).on('click', '#closeCustomCarousel', function() {
showTable();
});
});