// // Alfresco OpenSearch library // Gavin Cornwell 09-01-2007 // // NOTE: This script relies on common.js so therefore needs to be loaded // prior to this one on the containing HTML page. var _OS_NS_PREFIX = "opensearch"; var _OS_NS_URI = "http://a9.com/-/spec/opensearch/1.1/"; var _searchTermFieldId = null; var _pageSizeFieldId = null; var _resultsDivId = "os-results"; var _optionsDivId = "os-options"; var _resultSetPanelId = "-osresults-panel"; var _resultSetListId = "-osresults-list"; var _resultSetPositionId = "-osresults-position"; var _resultSetPagingId = "-osresults-paging"; var _engineEnabledId = "-engine-enabled"; var _engines = []; var _enginesById = []; /** * Define an object to hold the definition of an OpenSearch engine */ function OpenSearchEngine(id, label, url) { this.id = id; this.label = label; this.url = url; } /** * Sets the field id of the search term input control */ function setSearchTermFieldId(id) { _searchTermFieldId = id; } /** * Sets the field id of the page size input control */ function setPageSizeFieldId(id) { _pageSizeFieldId = id; } /** * Registers an OpenSearch engine to be called when performing queries */ function registerOpenSearchEngine(id, label, url) { var se = new OpenSearchEngine(id, label, url); _engines[_engines.length] = se; _enginesById[id] = se; } /** * Handles the key press event, if ENTER is pressed execute the query */ function handleKeyPress(e) { var keycode; // get the keycode if (window.event) { keycode = window.event.keyCode; } else if (e) { keycode = e.which; } // if ENTER was pressed execute the query if (keycode == 13) { executeQuery(); return false; } return true; } /** * Toggles the visibility of the options panel */ function toggleOptions(icon) { var currentState = icon.className; var optionsDiv = document.getElementById(_optionsDivId); if (currentState == "collapsed") { icon.src = getContextPath() + "/images/icons/expanded.gif"; icon.className = "expanded"; // show the div holding the options if (optionsDiv != null) { optionsDiv.style.display = "block"; } } else { icon.src = getContextPath() + "/images/icons/collapsed.gif"; icon.className = "collapsed"; // hide the div holding the options if (optionsDiv != null) { optionsDiv.style.display = "none"; } } } /** * Executes the query against all the registered and selected opensearch engines */ function executeQuery() { // gather the required parameters var term = document.getElementById(_searchTermFieldId).value; var count = document.getElementById(_pageSizeFieldId).value; // default the count if its invalid if (count.length == 0 || isNaN(count)) { count = 5; } // issue the queries if there is enough search criteria if (term != null && term.length > 1) { // issue the search request for each enabled engine for (var e = 0; e < _engines.length; e++) { // get the checkbox for the current engine var ose = _engines[e]; var engCheckbox = document.getElementById(ose.id + _engineEnabledId); if (engCheckbox != null && engCheckbox.checked) { issueSearchRequest(ose, term, count); } } } } /** * Issues an Ajax request for the given OpenSearchEngine * using the given search term and page size. */ function issueSearchRequest(ose, term, pageSize) { // generate the search url var searchUrl = generateSearchUrl(ose.url, term, pageSize); // issue the request if (searchUrl != null) { YAHOO.util.Connect.asyncRequest("GET", searchUrl, { success: processSearchResults, failure: handleSearchError, argument: [ose.id] }, null); } else { handleErrorYahoo("Failed to generate url for search engine '" + ose.label + "'.\n\nThis is probably caused by missing required parameters, check the template url for the search engine."); } } /** * Generates a concrete url for the given template url and parameters. * * All parameters (inside { and }) have to be replaced. We only need to populate * the 'searchTerms' and 'count' parameters, all optional ones will use the * empty string. If there is a mandatory parameter present (other than searchTerms * and count) null will be returned. */ function generateSearchUrl(templateUrl, term, count) { var searchUrl = null; // define regex pattern to look for params var pattern = /\{+\w*\}+|\{+\w*\?\}+|\{+\w*:\w*\}+|\{+\w*:\w*\?\}+/g; var params = templateUrl.match(pattern); if (params != null && params.length > 0) { searchUrl = templateUrl; // go through the parameters and replace the searchTerms and count // parameters with the given values and then replace all optional // parameters with an empty string. for (var p = 0; p < params.length; p++) { var param = params[p]; if (param == "{searchTerms}") { searchUrl = searchUrl.replace(param, term); } else if (param == "{count}" || param == "{count?}") { searchUrl = searchUrl.replace(param, count); } else if (param.indexOf("?") != -1) { // replace the optional parameter with "" searchUrl = searchUrl.replace(param, ""); } else { // an unknown manadatory parameter return searchUrl = null; break; } } } return searchUrl; } /** * Processes the XML search results */ function processSearchResults(ajaxResponse) { try { // render the results from the Ajax response var engineId = ajaxResponse.argument[0]; var feed = ajaxResponse.responseXML.documentElement; // if the name of the feed element is "rss", get the channel child element if (feed.tagName == "rss") { feed = getElementByTagName(feed, "channel"); } var resultsDiv = renderSearchResults(engineId, feed); // create the div to hold the results and add the results var resultsPanel = document.getElementById(_resultsDivId); if (resultsPanel != null) { // first remove any existing results while (resultsPanel.firstChild) { resultsPanel.removeChild(resultsPanel.firstChild); }; // add the new results resultsPanel.appendChild(resultsDiv); } else { alert("Failed to final results panel, unable to render search results!"); return; } } catch (e) { handleError(e); } } /** * Renders the results for the given feed element. */ function renderSearchResults(engineId, feed) { // look up the label from the osengine registry var engineLabel = _enginesById[engineId].label; // create the div to hold the results and the header bar var sb = []; sb[sb.length] = "
"; sb[sb.length] = engineLabel; sb[sb.length] = " | "; sb[sb.length] = generatePostionHTML(feed); sb[sb.length] = " |
";
if (icon != null)
{
sb[sb.length] = " | ";
if (title != null)
{
if (link != null)
{
sb[sb.length] = "";
}
sb[sb.length] = title;
if (link != null)
{
sb[sb.length] = "";
}
}
sb[sb.length] = " ";
if (summary != null)
{
sb[sb.length] = summary;
}
sb[sb.length] = " |