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

278 lines
6.7 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;
}
}
2015-01-24 23:34:50 -07:00
function getAdditionalTextLines(hint) {
if (hint.Type == "Audio") {
return [[hint.AlbumArtist, hint.Album].join(" - ")];
}
else if (hint.Type == "MusicAlbum") {
return [hint.AlbumArtist];
}
else if (hint.Type == "MusicArtist") {
return [Globalize.translate('LabelArtist')];
}
else if (hint.Type == "Movie") {
return [Globalize.translate('LabelMovie')];
}
else if (hint.Type == "MusicVideo") {
return [Globalize.translate('LabelMusicVideo')];
}
else if (hint.Type == "Episode") {
return [Globalize.translate('LabelEpisode')];
}
else if (hint.Type == "Series") {
return [Globalize.translate('LabelSeries')];
}
2015-01-25 12:13:04 -07:00
else if (hint.Type == "BoxSet") {
return [Globalize.translate('LabelCollection')];
}
2015-01-24 23:34:50 -07:00
return [hint.Type];
}
2013-12-27 10:12:23 -07:00
function search() {
var self = this;
2015-05-25 10:32:22 -07:00
self.showSearchPanel = function () {
2013-12-27 10:12:23 -07:00
2015-06-08 14:32:20 -07:00
showSearchMenu();
2015-05-13 22:42:55 -07:00
$('.headerSearchInput').focus();
2013-04-26 13:53:54 -07:00
};
}
window.Search = new search();
2013-04-28 10:21:56 -07:00
2015-01-21 12:21:19 -07:00
function renderSearchResultsInOverlay(elem, hints) {
// Massage the objects to look like regular items
hints = hints.map(function (i) {
i.Id = i.ItemId;
i.ImageTags = {};
i.UserData = {};
if (i.PrimaryImageTag) {
i.ImageTags.Primary = i.PrimaryImageTag;
}
return i;
});
var html = LibraryBrowser.getPosterViewHtml({
items: hints,
2015-06-01 22:46:06 -07:00
shape: "auto",
2015-01-22 23:15:15 -07:00
lazy: true,
2015-01-21 12:21:19 -07:00
overlayText: false,
2015-01-24 12:03:55 -07:00
showTitle: true,
2015-01-24 23:34:50 -07:00
coverImage: true,
2015-01-26 23:50:40 -07:00
centerImage: true,
2015-06-04 13:27:46 -07:00
textLines: getAdditionalTextLines
2015-01-21 12:21:19 -07:00
});
2015-06-28 08:43:49 -07:00
var itemsContainer = elem.querySelector('.itemsContainer');
itemsContainer.innerHTML = html;
2015-06-29 13:38:15 -07:00
ImageLoader.lazyChildren(itemsContainer);
2015-01-21 12:21:19 -07:00
}
function requestSearchHintsForOverlay(elem, searchTerm) {
var currentTimeout = searchHintTimeout;
2015-06-17 18:41:22 -07:00
Dashboard.showLoadingMsg();
2015-01-21 12:21:19 -07:00
ApiClient.getSearchHints({ userId: Dashboard.getCurrentUserId(), searchTerm: searchTerm, limit: 30 }).done(function (result) {
2015-06-17 18:41:22 -07:00
if (currentTimeout == searchHintTimeout) {
renderSearchResultsInOverlay(elem, result.SearchHints);
2015-01-21 12:21:19 -07:00
}
2015-06-17 18:41:22 -07:00
Dashboard.hideLoadingMsg();
}).fail(function () {
Dashboard.hideLoadingMsg();
2015-01-21 12:21:19 -07:00
});
}
function updateSearchOverlay(elem, searchTerm) {
if (!searchTerm) {
$('.itemsContainer', elem).empty();
clearSearchHintTimeout();
return;
}
clearSearchHintTimeout();
searchHintTimeout = setTimeout(function () {
requestSearchHintsForOverlay(elem, searchTerm);
}, 100);
}
function getSearchOverlay(createIfNeeded) {
2015-01-20 23:26:16 -07:00
2015-06-29 13:38:15 -07:00
var elem = document.querySelector('.searchResultsOverlay');
2015-01-20 23:26:16 -07:00
2015-06-29 13:38:15 -07:00
if (createIfNeeded && !elem) {
2015-01-22 23:15:15 -07:00
2015-06-07 18:23:56 -07:00
var html = '<div class="searchResultsOverlay ui-page-theme-b smoothScrollY">';
2015-01-22 23:15:15 -07:00
html += '<div class="searchResultsContainer"><div class="itemsContainer"></div></div></div>';
2015-06-29 13:38:15 -07:00
elem = $(html).appendTo(document.body).hide()[0];
$(elem).createCardMenus();
2015-01-20 23:26:16 -07:00
}
return elem;
}
function onHeaderSearchChange(val) {
2015-06-29 13:38:15 -07:00
var elem;
2015-01-20 23:26:16 -07:00
if (val) {
2015-06-29 13:38:15 -07:00
elem = getSearchOverlay(true);
$(elem).show();
elem.style.opacity = '1';
2015-06-28 07:45:21 -07:00
document.body.classList.add('bodyWithPopupOpen');
2015-01-20 23:26:16 -07:00
2015-06-29 13:38:15 -07:00
updateSearchOverlay(elem, val);
2015-01-20 23:26:16 -07:00
} else {
2015-06-29 13:38:15 -07:00
elem = getSearchOverlay(false);
if (elem) {
require(["jquery", "velocity"], function ($, Velocity) {
2015-01-20 23:26:16 -07:00
2015-06-29 13:38:15 -07:00
$(elem).velocity("fadeOut");
document.body.classList.remove('bodyWithPopupOpen');
});
updateSearchOverlay(elem, '');
}
2015-01-20 23:26:16 -07:00
}
}
2015-01-21 12:21:19 -07:00
function bindSearchEvents() {
2013-04-27 07:00:22 -07:00
2015-01-21 12:21:19 -07:00
$('.headerSearchInput').on("keyup", function (e) {
2013-04-27 07:00:22 -07:00
2015-01-21 12:21:19 -07:00
// Down key
if (e.keyCode == 40) {
2015-01-20 23:26:16 -07:00
2015-01-22 09:41:34 -07:00
//var first = $('.card', panel)[0];
2015-01-21 12:21:19 -07:00
//if (first) {
// first.focus();
//}
2015-01-20 23:26:16 -07:00
2015-01-21 12:21:19 -07:00
return false;
} else {
2015-01-20 23:26:16 -07:00
onHeaderSearchChange(this.value);
}
}).on("search", function (e) {
if (!this.value) {
onHeaderSearchChange('');
}
});
2015-01-23 21:50:45 -07:00
2015-01-24 12:03:55 -07:00
$('.btnCloseSearch').on('click', closeSearchOverlay);
2015-02-14 12:36:40 -07:00
$('.viewMenuSearchForm').on('submit', function () {
return false;
});
2015-01-24 12:03:55 -07:00
}
function closeSearchOverlay() {
$('.headerSearchInput').val('');
onHeaderSearchChange('');
2015-06-08 14:32:20 -07:00
hideSearchMenu();
}
function showSearchMenu() {
require(["jquery", "velocity"], function ($, Velocity) {
$('.btnCloseSearch').hide();
var elem = $('.viewMenuSearch')
.css({ left: '100%' })
.removeClass('hide')[0];
Velocity.animate(elem, { "left": "0px" },
{
complete: function () {
2015-06-16 12:17:12 -07:00
$('.headerSearchInput').focus();
2015-06-08 14:32:20 -07:00
$('.btnCloseSearch').show();
}
});
});
}
function hideSearchMenu() {
2015-06-29 11:45:42 -07:00
var viewMenuSearch = document.querySelector('.viewMenuSearch');
if (!viewMenuSearch) {
return;
}
if (!viewMenuSearch.classList.contains('hide')) {
2015-06-26 11:32:21 -07:00
require(["jquery", "velocity"], function ($, Velocity) {
$('.btnCloseSearch').hide();
2015-06-29 11:45:42 -07:00
viewMenuSearch.style.left = '0';
2015-06-26 11:32:21 -07:00
2015-06-29 11:45:42 -07:00
Velocity.animate(viewMenuSearch, { "left": "100%" },
2015-06-26 11:32:21 -07:00
{
complete: function () {
$('.viewMenuSearch').visible(false);
}
});
2015-06-08 14:32:20 -07:00
});
2015-06-26 11:32:21 -07:00
}
2015-01-21 12:21:19 -07:00
}
2015-06-20 17:49:42 -07:00
$(document).on('pagebeforehide', ".libraryPage", function () {
2015-01-21 12:21:19 -07:00
$('#txtSearch', this).val('');
$('#searchHints', this).empty();
2015-01-24 12:03:55 -07:00
}).on('pagecontainerbeforehide', closeSearchOverlay);
2015-01-21 12:21:19 -07:00
$(document).on('headercreated', function () {
2015-01-20 23:26:16 -07:00
2015-01-21 12:21:19 -07:00
bindSearchEvents();
2015-01-20 23:26:16 -07:00
});
2013-04-26 13:53:54 -07:00
2013-04-27 06:05:33 -07:00
})(jQuery, document, window, clearTimeout, setTimeout);