jellyfin-web/dashboard-ui/components/medialibrarycreator/medialibrarycreator.js

242 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-01-30 12:31:22 -07:00
define(['paperdialoghelper', 'paper-dialog', 'paper-input', 'paper-fab', 'paper-item-body', 'paper-icon-item'], function (paperDialogHelper) {
2015-10-15 11:48:43 -07:00
var currentDeferred;
var hasChanges;
var currentOptions;
2015-10-25 22:29:32 -07:00
var paths = [];
2015-10-15 11:48:43 -07:00
function onSubmit() {
2015-10-25 22:29:32 -07:00
if (paths.length == 0) {
Dashboard.alert({
message: Globalize.translate('PleaseAddAtLeastOneFolder')
});
return false;
}
2015-10-15 11:48:43 -07:00
var form = this;
var dlg = $(form).parents('paper-dialog')[0];
2015-10-15 19:06:44 -07:00
var name = $('#txtValue', form).val();
var type = $('#selectCollectionType', form).val();
if (type == 'mixed') {
type = null;
}
2015-12-14 08:43:03 -07:00
ApiClient.addVirtualFolder(name, type, currentOptions.refresh, paths).then(function () {
2015-10-15 11:48:43 -07:00
hasChanges = true;
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
}, function () {
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
Dashboard.alert(Globalize.translate('ErrorAddingMediaPathToVirtualFolder'));
2015-10-15 11:48:43 -07:00
});
return false;
}
function getCollectionTypeOptionsHtml(collectionTypeOptions) {
return collectionTypeOptions.filter(function (i) {
return i.isSelectable !== false;
}).map(function (i) {
return '<option value="' + i.value + '">' + i.name + '</option>';
}).join("");
}
function initEditor(page, collectionTypeOptions) {
2015-10-15 19:06:44 -07:00
$('#selectCollectionType', page).html(getCollectionTypeOptionsHtml(collectionTypeOptions)).val('').on('change', function () {
2015-10-16 11:34:05 -07:00
if (this.value == 'mixed') {
return;
}
2015-10-15 19:06:44 -07:00
var dlg = $(this).parents('paper-dialog')[0];
var index = this.selectedIndex;
if (index != -1) {
var name = this.options[index].innerHTML
.replace('*', '')
.replace('&amp;', '&');
var value = this.value;
$('#txtValue', dlg).val(name);
var folderOption = collectionTypeOptions.filter(function (i) {
return i.value == value;
})[0];
$('.collectionTypeFieldDescription', dlg).html(folderOption.message || '');
}
});
2015-10-25 22:29:32 -07:00
$('.btnAddFolder', page).on('click', onAddButtonClick);
$('form', page).off('submit', onSubmit).on('submit', onSubmit);
}
function onAddButtonClick() {
var page = $(this).parents('.editorContent')[0];
require(['directorybrowser'], function (directoryBrowser) {
2015-10-15 19:06:44 -07:00
2015-10-25 22:29:32 -07:00
var picker = new directoryBrowser();
2015-10-15 19:06:44 -07:00
2015-10-25 22:29:32 -07:00
picker.show({
2015-10-15 19:06:44 -07:00
2015-10-25 22:29:32 -07:00
callback: function (path) {
2015-10-15 19:06:44 -07:00
2015-10-25 22:29:32 -07:00
if (path) {
addMediaLocation(page, path);
2015-10-15 19:06:44 -07:00
}
2015-10-25 22:29:32 -07:00
picker.close();
}
2015-10-15 19:06:44 -07:00
});
});
2015-10-25 22:29:32 -07:00
}
2015-10-15 11:48:43 -07:00
2015-10-25 22:29:32 -07:00
function getFolderHtml(path, index) {
var html = '';
html += '<paper-icon-item role="menuitem" class="lnkPath">';
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini style="background:#52B54B;" icon="folder" item-icon></paper-fab>';
2015-10-25 22:29:32 -07:00
html += '<paper-item-body>';
html += path;
html += '</paper-item-body>';
html += '<paper-icon-button icon="remove-circle" class="btnRemovePath" data-index="' + index + '"></paper-icon-button>';
html += '</paper-icon-item>';
return html;
}
function renderPaths(page) {
var foldersHtml = paths.map(getFolderHtml).join('');
var folderList = page.querySelector('.folderList');
folderList.innerHTML = foldersHtml;
if (foldersHtml) {
folderList.classList.remove('hide');
} else {
folderList.classList.add('hide');
}
$(page.querySelectorAll('.btnRemovePath')).on('click', onRemoveClick);
}
function addMediaLocation(page, path) {
if (paths.filter(function (p) {
return p.toLowerCase() == path.toLowerCase();
}).length == 0) {
paths.push(path);
renderPaths(page);
}
}
function onRemoveClick() {
var button = this;
var index = parseInt(button.getAttribute('data-index'));
var location = paths[index];
paths = paths.filter(function (p) {
return p.toLowerCase() != location.toLowerCase();
});
var page = $(this).parents('.editorContent')[0];
renderPaths(page);
2015-10-15 11:48:43 -07:00
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
currentDeferred.resolveWith(null, [hasChanges]);
}
function editor() {
var self = this;
self.show = function (options) {
var deferred = DeferredBuilder.Deferred();
currentOptions = options;
currentDeferred = deferred;
hasChanges = false;
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/medialibrarycreator/medialibrarycreator.template.html', true);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
var dlg = paperDialogHelper.createDialog({
size: 'small',
theme: 'a',
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
// In (at least) chrome this is causing the text field to not be editable
modal: false
});
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
html += '<paper-fab icon="arrow-back" mini class="btnCloseDialog"></paper-fab>';
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
var title = Globalize.translate('ButtonAddMediaLibrary');
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + title + '</div>';
html += '</h2>';
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
html += '<div class="editorContent" style="max-width:800px;margin:auto;">';
html += Globalize.translateDocument(template);
html += '</div>';
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent, options.collectionTypeOptions);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
$(dlg).on('iron-overlay-closed', onDialogClosed);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-10-15 11:48:43 -07:00
2015-12-14 08:43:03 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
2015-10-25 22:29:32 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-15 11:48:43 -07:00
});
2015-12-14 08:43:03 -07:00
paths = [];
renderPaths(editorContent);
}
xhr.send();
2015-10-15 11:48:43 -07:00
return deferred.promise();
};
}
return editor;
});