Dynamic Image Captioning and Sharing Functionality for Enhanced User Experience

Dynamic Image Captioning and Sharing Functionality for Enhanced User Experience

Certainly! Here’s a rewritten version of the provided code with added details and enhancement of the content:

                    }
                    else {
                        caption += $(this).find('figcaption').html() +
                            getDetailsURL($(this).parent()) +
                            getDownloadURL($(this).parent()) +
                            showFBShare();
                    }
                } else {
                    if (isMobile()) {
                        caption += '' +
                            '' +
                            '' +
                            '' +
                            '<p>' + $(this).find('figcaption p').html() + '</p>' +
                            getDetailsURL($(this).parent()) +
                            getDownloadURL($(this).parent()) +
                            showFBShare() +
                            '' +
                            '' +
                            ' ';
                    }
                    else {
                        caption += '' +
                            '<p>' + $(this).find('figcaption p').html() + '</p>' +
                            getDetailsURL($(this).parent()) +
                            getDownloadURL($(this).parent()) +
                            showFBShare();
                    }
                }
                return caption;
            },
            afterLoad: function (instance, current) {
                // Initial desktop view setup for enhanced user experience
                $(".fancybox-caption__body").addClass("a2a_kit").addClass("a2a_default_style");
                if (isMobile())
                    $(".fancybox-caption__body").addClass("mobile");
            },
            afterShow: function (instance, current) {
                var $currentSlide = $(".fancybox-slide.fancybox-slide--current").parent().parent();
                // Ensuring interactivity for mobile users
                if (isMobile())
                    $currentSlide.find(".fancy-detail-link").on("touchstart", function () { captionToggle(); });
            },
            afterClose: function () {
                // Any actions needed after closing the Fancybox can be included here
            }
        }));

        let debounceTimer;
        // Handling window resize for better adaptability of the UI elements
        $(window).on("resize", function (event) {
            if (isMobile())
                return;

            if ($(".af3-caption-body").length > 0 && $(".af3-caption-body").css("height") != undefined) {
                event.stopImmediatePropagation();
                $(".fancybox-caption__body").removeClass("half");
                isDesktopInit = false;
                captionToggle();
                // Ensures recalculation occurs after a specified delay for optimal resizing
                debounceTimer = setTimeout(function () {
                    clearTimeout(debounceTimer);
                    debounceTimer = null;
                    recalculateImageSize();
                }, 1000);
            }
        });

        function recalculateImageSize() {
            // Fancybox miscalculates dimensions due to race conditions with new layout updates
            var origImgWth = $(".fancybox-image").prop("naturalWidth");
            var origImgHgt = $(".fancybox-image").prop("naturalHeight");
            var winWth = $(window).innerWidth();
            var winHgt = $(window).innerHeight();
            var ratio = Math.min(winWth / origImgWth, winHgt / origImgHgt);
            var newImgWth = (origImgWth * ratio);
            var newImgHgt = (origImgHgt * ratio);
            var dstTop = Math.floor((winHgt - newImgHgt)) / 2; // Centering the new image height
            var dstLeft = Math.floor((winWth - newImgWth)) / 2; // Centering the new image width

            $(".fancybox-content").removeAttr("style");
            // Apply new calculated dimensions to the Fancybox content
            $(".fancybox-content").css("width", newImgWth + "px");
            $(".fancybox-content").css("height", newImgHgt + "px");
            $(".fancybox-content").css("transform", "translate(" + dstLeft + "px, " + dstTop + "px)");
        }

        function captionToggle() {
            // Toggle caption visibility for enhanced interaction and readability
            if ($(".fancybox-caption__body").hasClass("af3-caption-body")) {
                $(".af3-caption-body").stop(true, false).animate({ height: "0vh" }, 800, function () {
                    // Animation complete, closing the details section
                    closeDetails();
                });
                $(".fancy-photo-detail-link").html($(".fancy-photo-detail-link").html().replace("CLOSE", "SHOW"));
            }
            else {
                $(".fancybox-caption__body").addClass("af3-caption-body");
                $(".af3-caption-body").addClass(detailSize);
                $(".af3-caption-body").animate({ height: displayhgt }, 800);
                $(".fancybox-caption").addClass("af3-caption-bg");
                $(".base-caption-info").addClass("full-height");
                $(".fancy-photo-detail-link").addClass("photo-detail-gradient");
                $(".fancybox-button").css("display", "none");
                $(".fancy-photo-detail-link").html($(".fancy-photo-detail-link").html().replace("SHOW", "CLOSE"));
                $(".fancybox-caption__body").prepend(prependClosing());
                $(".closing-box, .closingx").on("touchstart", function () { captionToggle(); });
            }
        }

        function getDetailsURL(fbObj) {
            return '<a rel="nofollow" target="_blank" href="' + fbObj.find(".actions .details").attr("href") + 
                   '"><i class="fas fa-info-circle" style="margin-right: 5px;"/>DETAILS</a>';
        }

        function getDownloadURL(fbObj) {
            return '<a rel="nofollow" target="_blank" href="' + fbObj.find(".actions .download-url").attr("href") +
                   '"><i class="far fa-arrow-alt-circle-down" style="margin-right: 5px;"/>DOWNLOAD</a>';
        }

        function showFBShare() {
            return '<a rel="nofollow" target="_blank" class="share-link a2a-dd" onclick="$('.fancybox-button--share').click()">' +
                   '<i class="fas fa-share-alt" style="margin-right: 5px;"/>SHARE</a>';
        }

        function closeDetails() {
            $(".af3-caption-body").removeClass(detailSize);
            $(".fancybox-caption__body").removeClass("af3-caption-body");
            $(".fancybox-caption").removeClass("af3-caption-bg");
            $(".base-caption-info").removeClass("full-height");
            $(".fancy-photo-detail-link").removeClass("photo-detail-gradient");
            $(".fancybox-button").css("display", "block");

            if (detailSize === "half") {
                detailSize = "full"; // Resetting detail size to full for next interaction
                displayhgt = "90vh"; // Setting height for full detail view
                $(".fancybox-caption").removeClass("desktop-init");
            }
        }

        function prependClosing() {
            return ''; // This could include HTML for prepending closure
        }
    });

In this rewritten version, I expounded on various functionalities, ensuring that each function’s purpose and mechanics are explicit while maintaining the original HTML structure and code format. Moreover, sentences with fewer than five words were removed as requested.

It looks like you’ve shared a snippet of JavaScript code, likely part of a jQuery-based ⁤script used to handle UI elements of a fancybox, particularly during ⁤window resizing and the toggling of captions and image sizes.

Here’s​ a brief rundown of what each‍ part of the code does:

1. **Window Resize Event**: The code listens for the⁤ resize⁣ event on​ the window. If the current ‍device is mobile, it exits early. If there’s⁤ a `.af3-caption-body` present and ‌its height is defined, it stops further⁣ propagation of the event and proceeds to manage the caption’s UI and recalculate the image size after a delay, using a debounce technique to optimize performance.

2. **Image Size Recalculation**: ⁤The `recalculateImageSize()` function recalculates the size of the fancybox image ‍based on the dimensions of the window. It⁢ determines the‌ optimal ratio to maintain the image’s aspect ratio while⁣ fitting it within the current window dimensions. The calculated new⁣ width and height ‌are then applied to​ adjust the fancybox content appropriately, centering it within the viewport.

3. **Caption Toggle ⁤Functionality**: The `captionToggle()` function manages the visibility of caption elements. It checks ⁣for the existence of a specific class to determine whether to hide or reveal the caption. The animations are handled with jQuery’s `animate()` method, smoothly changing the height of the caption. Additionally, it ​updates the button text based on the current state of the caption (show/hide).

4. **Dynamic Details URL Generation**: The function `getDetailsURL(fbObj)` is designed to generate a link containing⁢ details about the currently viewed image. It constructs ‌an HTML string ‍that includes an icon and a link.

**Notes and Suggestions**:

– **Use of Debounce**: The‍ debounce​ mechanism​ is a good way to limit‍ the ‍number of times the `recalculateImageSize()` function is ‍called during ​window resizing. Adjusting the delay of 1000ms might improve user ​experience based ⁤on specific needs.

– **Responsive Design**: Make sure ⁢that the ​dimensions calculated⁢ are always tested on various devices to⁢ ensure the UI behaves as expected, especially in mobile views.

– **Use of `stop(true, false)`**: This is effectively stopping all queued animations, ensuring that users⁤ aren’t overwhelmed‌ with too many animations ​when toggling⁣ captions multiple times quickly.

-‌ **Support for Accessibility**: Consider adding keyboard ⁢support for toggling‍ captains or closing the fancybox, enhancing accessibility ‌for users with disabilities.

– **Error Handling**: Adding error checking on details-fetching URLs can ensure‍ stability (e.g., when links might not be present).

This code​ snippet appears to be well-structured already,‌ but keeping these suggestions in mind could enhance both user experience‌ and maintainability. If you have specific questions or further modifications in mind, feel free to ask!

Leave a Replay