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

188 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-06-27 10:28:36 -07:00
define(['libraryBrowser', 'focusManager', 'emby-input', 'paper-icon-button-light', 'material-icons'], function (libraryBrowser, focusManager) {
2015-09-23 19:31:40 -07:00
function loadSuggestions(page) {
var options = {
2015-09-24 10:08:10 -07:00
SortBy: "IsFavoriteOrLiked,Random",
2015-09-23 19:31:40 -07:00
IncludeItemTypes: "Movie,Series,MusicArtist",
Limit: 20,
Recursive: true,
ImageTypeLimit: 0,
EnableImages: false
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(Dashboard.getCurrentUserId(), options).then(function (result) {
2015-09-23 19:31:40 -07:00
var html = result.Items.map(function (i) {
var href = LibraryBrowser.getHref(i);
2015-09-24 10:08:10 -07:00
var itemHtml = '<div><a style="display:inline-block;padding:.55em 1em;" href="' + href + '">';
2015-09-23 19:31:40 -07:00
itemHtml += i.Name;
2015-09-24 10:08:10 -07:00
itemHtml += '</a></div>';
2015-09-23 19:31:40 -07:00
return itemHtml;
}).join('');
page.querySelector('.searchSuggestions').innerHTML = html;
});
}
2016-06-27 10:28:36 -07:00
return function (view, params) {
2015-09-23 19:31:40 -07:00
2016-06-27 10:28:36 -07:00
var textSuggestions = view.querySelector('.textSuggestions');
var searchResults = view.querySelector('.searchResults');
var searchHintTimeout;
2015-09-23 19:31:40 -07:00
2016-06-27 10:28:36 -07:00
function clearSearchHintTimeout() {
2015-09-23 19:31:40 -07:00
2016-06-27 10:28:36 -07:00
if (searchHintTimeout) {
2015-09-23 19:31:40 -07:00
2016-06-27 10:28:36 -07:00
clearTimeout(searchHintTimeout);
searchHintTimeout = null;
}
}
function showTextSuggestions() {
if (AppInfo.enableAppLayouts) {
textSuggestions.classList.remove('hide');
}
}
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('Series')];
}
else if (hint.Type == "BoxSet") {
return [Globalize.translate('LabelCollection')];
}
else if (hint.ChannelName) {
return [hint.ChannelName];
}
return [hint.Type];
}
function renderSearchResultsInOverlay(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,
shape: "auto",
lazy: true,
overlayText: false,
showTitle: true,
centerImage: true,
centerText: true,
textLines: getAdditionalTextLines,
overlayPlayButton: true
});
var itemsContainer = searchResults;
itemsContainer.innerHTML = html;
searchResults.classList.remove('hide');
textSuggestions.classList.add('hide');
ImageLoader.lazyChildren(itemsContainer);
}
function requestSearchHintsForOverlay(searchTerm) {
var currentTimeout = searchHintTimeout;
Dashboard.showLoadingMsg();
ApiClient.getSearchHints({
userId: Dashboard.getCurrentUserId(),
searchTerm: (searchTerm || '').trim(),
limit: 30
}).then(function (result) {
if (currentTimeout == searchHintTimeout) {
renderSearchResultsInOverlay(result.SearchHints);
}
Dashboard.hideLoadingMsg();
}, function () {
Dashboard.hideLoadingMsg();
});
}
function onSearchChange(val) {
if (!val) {
clearSearchHintTimeout();
searchResults.classList.add('hide');
searchResults.innerHTML = '';
showTextSuggestions();
return;
}
clearSearchHintTimeout();
searchHintTimeout = setTimeout(function () {
requestSearchHintsForOverlay(val);
}, 300);
}
if (AppInfo.enableAppLayouts) {
showTextSuggestions();
loadSuggestions(view);
}
view.addEventListener('viewshow', function () {
focusManager.focus(view.querySelector('.txtSearch'));
});
view.querySelector('.txtSearch').addEventListener('input', function () {
onSearchChange(this.value);
});
};
});