With PrizmDoc, how can I hide a predefined search if there are no results?

Question

With PrizmDoc, how can I hide a predefined search if there are no results?

Answer

The predefined search option does not support that functionality, but you can instead perform a server-side search, and then activate the search panel if there are results to show:

var viewer;
var viewingSessionId = <%= viewingSessionId %>;

var fixedSearchTerm = "the";
var pasUrl = "/pas";

var viewerReady = false;
var searchReady = false;
var searchDisplayed = false;

function displaySearchIfNeeded() {
    // The search is only displayed once the viewer is ready, and once our preliminary server-side search comes back positive.
    if (viewerReady && searchReady && !searchDisplayed) {
        searchDisplayed = true;

        $("[data-pcc-search=\"input\"]").val(fixedSearchTerm);
        $("[data-pcc-search=\"submit\"]").click();
    }
}

function sendSearchPost() {
    $.ajax({
        "method": "POST",
        "url": pasUrl + "/v2/viewingSessions/" + viewingSessionId + "/searchTasks",
        "data": JSON.stringify({
            "input": {
                "searchTerms": [
                    {
                        "type": "simple",
                        "pattern": fixedSearchTerm,
                        "caseSensitive": false,
                        "termId": "0"
                    }
                ]
            }
        }),
        "contentType": "application/json",
        "success": function(response) {
            $.ajax({
                "url": pasUrl + "/v2/searchTasks/" + response["processId"] + "/results?limit=1",
                "success": function(response) {
                    if (response.results.length !== 0) {
                        searchReady = true;

                        displaySearchIfNeeded();
                    }
                },
            });
        },
        "error": function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.status === 480) {
                setTimeout(sendSearchPost, 2000);
            }
        }
    });
};

setTimeout(sendSearchPost, 500);

$(document).ready(function() {
    // Since we are no longer restricted to a predefined search, we can load the viewer ASAP.
    viewer = $("#viewer").pccViewer({
        "documentID": viewingSessionId,
        "imageHandlerUrl": "/pas",
        "language": viewerCustomizations.languages["en-US"],
        "template": viewerCustomizations.template,
        "icons": viewerCustomizations.icons
    });

    viewer.viewerControl.on("ViewerReady", function(event) {
        viewerReady = true;

        displaySearchIfNeeded();
    });
});