(function ($, document, window, clearTimeout, setTimeout) { var searchHintTimeout; function clearSearchHintTimeout() { if (searchHintTimeout) { clearTimeout(searchHintTimeout); searchHintTimeout = null; } } function createSearchHintsElement(page) { $(document.body).off("mousedown.hidesearchhints").on("mousedown.hidesearchhints", function (e) { var elem = $(e.target); if (!elem.is('#searchHints,#txtSearch,#btnSearch') && !elem.parents('#searchHints,#txtSearch,#btnSearch').length) { hideFlyout(page); } }); return $('#searchHints', page).show(); } function hideFlyout(page) { $('#searchHints', page).hide(); $(document.body).off("mousedown.hidesearchhints"); } function showFlyout(page) { var hints = $('#searchHints:visible'); if (!hints.length) { createSearchHintsElement(page); } } function updateFlyout(page, searchTerm) { if (!searchTerm) { clearSearchHintTimeout(); hideFlyout(page); return; } clearSearchHintTimeout(); searchHintTimeout = setTimeout(function () { requestSearchHints(page, searchTerm); }, 100); } function requestSearchHints(page, searchTerm) { var currentTimeout = searchHintTimeout; $.getJSON(ApiClient.getUrl("Search/Hints", { userId: Dashboard.getCurrentUserId(), searchTerm: searchTerm, limit: 10 })).done(function (result) { if (currentTimeout != searchHintTimeout) { return; } if (!result.TotalRecordCount) { hideFlyout(page); return; } renderSearchHintResult(page, result.SearchHints); }); } function preg_quote(str, delimiter) { // http://kevin.vanzonneveld.net // + original by: booeyOH // + improved by: Ates Goral (http://magnetiq.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Onno Marsman // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: preg_quote("$40"); // * returns 1: '\$40' // * example 2: preg_quote("*RRRING* Hello?"); // * returns 2: '\*RRRING\* Hello\?' // * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:"); // * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:' return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&'); } function getHintDisplayName(data, term) { var regexp = new RegExp("(" + preg_quote(term) + ")", 'gi'); return data.replace(regexp, "$1"); } function getSearchHintHtml(hint) { var html = ''; html += ''; var imgUrl; if (hint.PrimaryImageTag) { hint.ImageTags = { Primary: hint.PrimaryImageTag }; imgUrl = LibraryBrowser.getImageUrl(hint, "Primary", 0, { maxwidth: 150, maxheight: 150 }); } else if (hint.MediaType == "Game") { imgUrl = "css/images/items/searchhints/game.png"; } else if (hint.Type == "Episode" || hint.Type == "Season" || hint.Type == "Series") { imgUrl = "css/images/items/searchhints/tv.png"; } else if (hint.Type == "Audio" || hint.Type == "MusicAlbum" || hint.Type == "Artist") { imgUrl = "css/images/items/searchhints/music.png"; } else if (hint.Type == "Person") { imgUrl = "css/images/items/searchhints/person.png"; } else { imgUrl = "css/images/items/searchhints/film.png"; } html += ''; html += '
'; html += '
' + getHintDisplayName(hint.Name, hint.MatchedTerm) + '
'; if (hint.Type == "Audio") { html += '
' + [hint.AlbumArtist, hint.Album].join(" - ") + '
'; } else if (hint.Type == "MusicAlbum") { html += '
' + hint.AlbumArtist + '
'; } else { html += '
' + (hint.DisplayMediaType || hint.Type) + '
'; } var text; if (hint.EpisodeCount) { text = hint.EpisodeCount == 1 ? "1 Episode" : hint.EpisodeCount + " Episodes"; html += '
' + text + '
'; } else if (hint.SongCount) { text = hint.SongCount == 1 ? "1 Song" : hint.SongCount + " Songs"; html += '
' + text + '
'; } else if (hint.RunTimeTicks) { html += '
' + DashboardPage.getDisplayText(hint.RunTimeTicks) + '
'; } html += '
'; html += '
'; return html; } function renderSearchHintResult(page, hints) { var html = ''; for (var i = 0, length = hints.length; i < length; i++) { html += getSearchHintHtml(hints[i]); } $('#searchHints', page).html(html); } function search() { var self = this; self.getSearchHtml = function () { var html = '
'; html += ''; html += ''; html += '
'; html += ''; html += '
'; return html; }; self.onSearchRendered = function (parentElem) { $('#searchForm', parentElem).on("submit", function () { Dashboard.alert('Coming soon.'); return false; }); $('#txtSearch', parentElem).on("keyup", function (e) { var value = this.value; showFlyout(parentElem); updateFlyout(parentElem, value); }).on("focus", function () { var value = this.value; if (value) { showFlyout(parentElem, value); } }); }; } window.Search = new search(); $(document).on('pagehide', ".libraryPage", function () { hideFlyout(this); $('#txtSearch', this).val(''); }); })(jQuery, document, window, clearTimeout, setTimeout);