jellyfin-web/dashboard-ui/scripts/search.js

265 lines
7.0 KiB
JavaScript
Raw Normal View History

2013-04-27 06:05:33 -07:00
(function ($, document, window, clearTimeout, setTimeout) {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
var searchHintTimeout;
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
function clearSearchHintTimeout() {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
if (searchHintTimeout) {
clearTimeout(searchHintTimeout);
searchHintTimeout = null;
}
}
function createSearchHintsElement(page) {
$(document.body).off("mousedown.hidesearchhints").on("mousedown.hidesearchhints", function (e) {
2013-04-26 13:53:54 -07:00
var elem = $(e.target);
if (!elem.is('#searchHints,#txtSearch,#btnSearch') && !elem.parents('#searchHints,#txtSearch,#btnSearch').length) {
2013-04-27 06:05:33 -07:00
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);
}
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
function requestSearchHints(page, searchTerm) {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
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;
2013-04-26 13:53:54 -07:00
}
2013-04-27 06:05:33 -07:00
renderSearchHintResult(page, result.SearchHints);
2013-04-26 13:53:54 -07:00
});
2013-04-27 06:05:33 -07:00
}
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
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'), '\\$&');
}
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
function getHintDisplayName(data, term) {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
var regexp = new RegExp("(" + preg_quote(term) + ")", 'gi');
return data.replace(regexp, "<b>$1</b>");
2013-04-26 13:53:54 -07:00
}
2013-04-27 06:05:33 -07:00
function getSearchHintHtml(hint) {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
var html = '';
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
html += '<a class="searchHint" href="' + LibraryBrowser.getHref(hint) + '">';
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";
2013-04-26 13:53:54 -07:00
}
2013-04-27 06:05:33 -07:00
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 += '<img class="searchHintImage" src="' + imgUrl + '" />';
html += '<div class="searchHintContent"><div class="searchHintContentInner">';
html += '<div class="searchHintName">' + getHintDisplayName(hint.Name, hint.MatchedTerm) + '</div>';
if (hint.Type == "Audio") {
html += '<div class="searchHintSecondaryText">' + [hint.AlbumArtist, hint.Album].join(" - ") + '</div>';
}
else if (hint.Type == "MusicAlbum") {
html += '<div class="searchHintSecondaryText">' + hint.AlbumArtist + '</div>';
}
else {
html += '<div class="searchHintSecondaryText">' + (hint.DisplayMediaType || hint.Type) + '</div>';
}
var text;
if (hint.EpisodeCount) {
text = hint.EpisodeCount == 1 ? "1 Episode" : hint.EpisodeCount + " Episodes";
html += '<div class="searchHintSecondaryText">' + text + '</div>';
}
else if (hint.SongCount) {
text = hint.SongCount == 1 ? "1 Song" : hint.SongCount + " Songs";
html += '<div class="searchHintSecondaryText">' + text + '</div>';
}
else if (hint.RunTimeTicks) {
html += '<div class="searchHintSecondaryText">' + DashboardPage.getDisplayText(hint.RunTimeTicks) + '</div>';
}
html += '</div></div>';
html += '</a>';
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);
2013-04-26 13:53:54 -07:00
}
function search() {
var self = this;
self.getSearchHtml = function () {
var html = '<div class="headerSearch"><form id="searchForm" name="searchForm">';
html += '<input id="txtSearch" class="txtSearch" type="search" required="required" />';
html += '<button id="btnSearch" class="btnSearch" type="submit">';
html += '<img src="css/images/searchbutton.png" />';
html += '</button>';
2013-04-27 06:05:33 -07:00
html += '</form>';
html += '<div id="searchHints" class="searchHints" style="display:none;"><div class="searchHintsContent"></div></div>';
html += '</div>';
2013-04-26 13:53:54 -07:00
return html;
};
self.onSearchRendered = function (parentElem) {
$('#searchForm', parentElem).on("submit", function () {
Dashboard.alert('Coming soon.');
return false;
});
2013-04-27 06:05:33 -07:00
$('#txtSearch', parentElem).on("keyup", function (e) {
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
var value = this.value;
showFlyout(parentElem);
updateFlyout(parentElem, value);
2013-04-26 13:53:54 -07:00
}).on("focus", function () {
var value = this.value;
2013-04-27 06:05:33 -07:00
2013-04-26 13:53:54 -07:00
if (value) {
2013-04-27 06:05:33 -07:00
showFlyout(parentElem, value);
2013-04-26 13:53:54 -07:00
}
});
};
}
window.Search = new search();
2013-04-27 07:00:22 -07:00
$(document).on('pagehide', ".page", function () {
hideFlyout(this);
$('#txtSearch', this).val('');
});
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
})(jQuery, document, window, clearTimeout, setTimeout);