(function () {
function changeCollectionType(page, virtualFolder) {
Dashboard.alert({
message: Globalize.translate('HeaderChangeFolderTypeHelp'),
title: Globalize.translate('HeaderChangeFolderType')
});
}
function addVirtualFolder(page) {
require(['medialibrarycreator'], function (medialibrarycreator) {
new medialibrarycreator().show({
collectionTypeOptions: getCollectionTypeOptions(),
refresh: shouldRefreshLibraryAfterChanges(page)
}).then(function (hasChanges) {
if (hasChanges) {
reloadLibrary(page);
}
});
});
}
function editVirtualFolder(page, virtualFolder) {
require(['medialibraryeditor'], function (medialibraryeditor) {
new medialibraryeditor().show({
refresh: shouldRefreshLibraryAfterChanges(page),
library: virtualFolder
}).then(function (hasChanges) {
if (hasChanges) {
reloadLibrary(page);
}
});
});
}
function deleteVirtualFolder(page, virtualFolder) {
var msg = Globalize.translate('MessageAreYouSureYouWishToRemoveMediaFolder');
if (virtualFolder.Locations.length) {
msg += "
" + Globalize.translate("MessageTheFollowingLocationWillBeRemovedFromLibrary") + "
";
msg += virtualFolder.Locations.join("
");
}
require(['confirm'], function (confirm) {
confirm(msg, Globalize.translate('HeaderRemoveMediaFolder')).then(function () {
var refreshAfterChange = shouldRefreshLibraryAfterChanges(page);
ApiClient.removeVirtualFolder(virtualFolder.Name, refreshAfterChange).then(function () {
reloadLibrary(page);
});
});
});
}
function renameVirtualFolder(page, virtualFolder) {
require(['prompt'], function (prompt) {
prompt({
label: Globalize.translate('LabelNewName')
}).then(function (newName) {
if (newName && newName != virtualFolder.Name) {
var refreshAfterChange = shouldRefreshLibraryAfterChanges(page);
ApiClient.renameVirtualFolder(virtualFolder.Name, newName, refreshAfterChange).then(function () {
reloadLibrary(page);
});
}
});
});
}
function showCardMenu(page, elem, virtualFolders) {
var card = $(elem).parents('.card')[0];
var index = parseInt(card.getAttribute('data-index'));
var virtualFolder = virtualFolders[index];
var menuItems = [];
menuItems.push({
name: Globalize.translate('ButtonChangeContentType'),
id: 'changetype',
ironIcon: 'videocam'
});
menuItems.push({
name: Globalize.translate('ButtonManageFolders'),
id: 'edit',
ironIcon: 'folder-open'
});
menuItems.push({
name: Globalize.translate('ButtonRemove'),
id: 'delete',
ironIcon: 'remove'
});
menuItems.push({
name: Globalize.translate('ButtonRename'),
id: 'rename',
ironIcon: 'mode-edit'
});
require(['actionsheet'], function (actionsheet) {
actionsheet.show({
items: menuItems,
positionTo: elem,
callback: function (resultId) {
switch (resultId) {
case 'changetype':
changeCollectionType(page, virtualFolder);
break;
case 'edit':
editVirtualFolder(page, virtualFolder);
break;
case 'rename':
renameVirtualFolder(page, virtualFolder);
break;
case 'delete':
deleteVirtualFolder(page, virtualFolder);
break;
default:
break;
}
}
});
});
}
function reloadLibrary(page) {
Dashboard.showLoadingMsg();
ApiClient.getVirtualFolders().then(function (result) {
reloadVirtualFolders(page, result);
});
}
function shouldRefreshLibraryAfterChanges(page) {
return $(page).is('#mediaLibraryPage');
}
function reloadVirtualFolders(page, virtualFolders) {
var html = '';
virtualFolders.push({
Name: Globalize.translate('ButtonAddMediaLibrary'),
icon: 'add-circle',
Locations: [],
showType: false,
showLocations: false,
showMenu: false,
showNameWithIcon: true
});
for (var i = 0, length = virtualFolders.length; i < length; i++) {
var virtualFolder = virtualFolders[i];
html += getVirtualFolderHtml(page, virtualFolder, i);
}
var divVirtualFolders = page.querySelector('#divVirtualFolders');
divVirtualFolders.innerHTML = html;
$('.btnCardMenu', divVirtualFolders).on('click', function () {
showCardMenu(page, this, virtualFolders);
});
$('.addLibrary', divVirtualFolders).on('click', function () {
addVirtualFolder(page);
});
$('.editLibrary', divVirtualFolders).on('click', function () {
var card = $(this).parents('.card')[0];
var index = parseInt(card.getAttribute('data-index'));
var virtualFolder = virtualFolders[index];
if (!virtualFolder.ItemId) {
return;
}
require(['components/imageeditor/imageeditor'], function (ImageEditor) {
ImageEditor.show(virtualFolder.ItemId, {
theme: 'a'
}).then(function (hasChanged) {
if (hasChanged) {
reloadLibrary(page);
}
});
});
});
Dashboard.hideLoadingMsg();
}
function getCollectionTypeOptions() {
return [
{ name: "", value: "" },
{ name: Globalize.translate('FolderTypeMovies'), value: "movies" },
{ name: Globalize.translate('FolderTypeMusic'), value: "music" },
{ name: Globalize.translate('FolderTypeTvShows'), value: "tvshows" },
{ name: Globalize.translate('FolderTypeBooks'), value: "books", message: Globalize.translate('MessageBookPluginRequired') },
{ name: Globalize.translate('FolderTypeGames'), value: "games", message: Globalize.translate('MessageGamePluginRequired') },
{ name: Globalize.translate('FolderTypeHomeVideos'), value: "homevideos" },
{ name: Globalize.translate('FolderTypeMusicVideos'), value: "musicvideos" },
{ name: Globalize.translate('FolderTypePhotos'), value: "photos" },
{ name: Globalize.translate('FolderTypeUnset'), value: "mixed", message: Globalize.translate('MessageUnsetContentHelp') }
];
}
function getIcon(type) {
switch (type) {
case "movies":
return "local-movies";
case "music":
return "library-music";
case "photos":
return "photo";
case "livetv":
return "live-tv";
case "tvshows":
return "live-tv";
case "games":
return "folder";
case "trailers":
return "local-movies";
case "homevideos":
return "video-library";
case "musicvideos":
return "video-library";
case "books":
return "folder";
case "channels":
return "folder";
case "playlists":
return "folder";
default:
return "folder";
}
}
function getVirtualFolderHtml(page, virtualFolder, index) {
var html = '';
var style = "";
if (page.classList.contains('wizardPage')) {
style += "min-width:33.3%;";
}
html += '