Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

tltype_srch_generate_js_for_background_and_count()

Description

Generate client side logic for doing expensive computation async for retrieving the tile background and total results count.

Parameters

ColumnTypeDefaultDescription
$tile array Tile information {@see pages/ajax/dash_tile.php}
$tile_id string HTML ID for the container div
$tile_width int Tile width {@see pages/ajax/dash_tile.php}
$tile_height int Tile height {@see pages/ajax/dash_tile.php}
$promoted_image int ID of the promoted resource (for background)

Location

include/dash_functions.php lines 2034 to 2165

Definition

 
function tltype_srch_generate_js_for_background_and_count(array $tilestring $tile_idint $tile_widthint $tile_heightint $promoted_image)
{
    global 
$lang$baseurl_short;
    
// Prevent function from running for the wrong tile type and style
    
parse_str(parse_url($tile['url'] ?? ''PHP_URL_QUERY), $tile_meta);
    if (
        !(
        isset(
$tile_meta['tltype'], $tile_meta['tlstyle'])
        && 
$tile_meta['tltype'] === 'srch'
        
&& in_array($tile_meta['tlstyle'], $GLOBALS['tile_styles']['srch'])
        )
    ) {
        return;
    }

    
$tile_style $tile_meta['tlstyle'];

    
?>
    <!-- Resource counter -->
    <p class="tile_corner_box DisplayNone">
    </p>

    <script>
        jQuery(document).ready(function() {
            const TILE_STYLE = ' echo escape($tile_style); ?>';
            const SHOW_RESOURCE_COUNT =  echo $tile['resource_count'] ? 'true' 'false'?>;

            let data = {
                'link': ' echo escape($tile["link"]); ?>',
                'promimg': ' echo (int)$promoted_image?>',
            };

            api('get_dash_search_data', data, function(response) {
                const TILE_ID = ' echo escape($tile_id); ?>';
                const TILE_WIDTH =  echo $tile_width?>;
                const TILE_HEIGHT =  echo $tile_height?>;
                var preview_resources;
                var placeholder = response.images.pop();

                if (TILE_STYLE === 'thmbs') {
                    let promoted_image =  echo (int)$promoted_image?>;
                    let promoted_image_resource = response.images.filter(resource => resource.ref == promoted_image && typeof resource.url !== 'undefined');
                    console.debug('promoted_image_resource = %o', promoted_image_resource);

                    // Filter response
                    preview_resources = promoted_image > 0 && promoted_image_resource[0] !== undefined ? [promoted_image_resource[0]]
                    : promoted_image === 0 && response.images[0] !== undefined ? [response.images[0]]
                    : [];
                    // Fit (adjust) the 'pre' size to the tile size
                    preview_resources = preview_resources.map(function(resource) {
                        if (resource['thumb_width'] * 0.7 >= resource['thumb_height']) {
                            let ratio = resource['thumb_height'] / TILE_HEIGHT;
                            if (ratio == 0) { ratio = 1; } // attempt fit if 'thumb_height' is 0
                            let width = resource['thumb_width'] / ratio;
                            var size = width < TILE_WIDTH ? ' width="100%"' : ' height="100%"';
                        } else {
                            let ratio = resource['thumb_width'] / TILE_WIDTH;
                            if (ratio == 0) { ratio = 1; } // attempt fit if 'thumb_width' is 0

                            let height = resource['thumb_height'] / ratio;
                            var size = height < TILE_HEIGHT ? ' height="100%"' : ' width="100%"';
                        }

                        return '<img alt="' + resource.title + '" src="' + resource.url + '"' + size + ' class="thmbs-tile-img">';
                    });

                    // Tile background - resource(s) preview
                    console.debug('preview_resources = %o', preview_resources);
                    let tile_div = jQuery('div#' + TILE_ID);
                    if (preview_resources.length > 0) {
                        tile_div.prepend(preview_resources[0]);
                    } else {
                        tile_div.prepend(
                            jQuery('<div>').addClass('tile-placeholder')
                                .prepend(
                                    jQuery('<div>').addClass('thumbs-tile-image')
                                )
                        )
                    }
                } else if (TILE_STYLE === 'multi') {
                    preview_resources = response.images
                        .map(function(resource, index, resources_list) {
                            return '<img alt="' + resource.title + '" src="' + resource.url + '">';
                        })
                    let use_placeholder = false;
                    while(preview_resources.length < 3) {
                        if (preview_resources.length == 0) {
                            use_placeholder = true;
                        }
                        preview_resources.push(jQuery('<div>'));
                    }
                    console.debug('preview_resources = %o', preview_resources);
                    let tile_div = jQuery('div#' + TILE_ID);
                    for (let i = 0; i<= 2; i++) {
                        if (i == 0) {
                            if (use_placeholder == false) {
                                tile_div.find('.tile-multi').prepend(preview_resources[i]);
                            } else { 
                                tile_div.find('.tile-multi').prepend(
                                    jQuery('<div>').addClass('tile-placeholder')
                                        .prepend(
                                            jQuery('<div>').addClass('thumbs-tile-image')
                                        )
                                );
                            }
                        } else {
                            tile_div.find('.tile-sub-multi').append(preview_resources[i]);
                        }
                    }
                }

                // Resource count
                let tile_corner_box = jQuery('div#' + TILE_ID + ' p.tile_corner_box');

                if (SHOW_RESOURCE_COUNT) {
                    let count_string = response.count + ' ' + (response.count > 1 ? ' echo escape($lang['items']); ?>': ' echo escape($lang['item']); ?>');
                    tile_corner_box.html(count_string);
                    tile_corner_box.removeClass('DisplayNone');
                } else if(response.count == 0) {
                    jQuery('div#' + TILE_ID + ' p.no_resources').removeClass('DisplayNone');
                }
            },
             echo generate_csrf_js_object('get_dash_search_data'); ?>,
            {
                onStart: function() { jQuery('# echo escape($tile_id); ?>').addClass('is-loading'); },
                onEnd:   function() { jQuery('# echo escape($tile_id); ?>').removeClass('is-loading'); }
            }
            );
        });
    </script>
    
}

This article was last updated 2nd June 2026 17:35 Europe/London time based on the source file dated 1st June 2026 17:10 Europe/London time.