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

237 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-03-22 10:46:57 -07:00
define(['paperdialoghelper', 'jQuery', '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) {
2016-03-06 11:09:20 -07:00
require(['alert'], function (alert) {
alert({
text: Globalize.translate('PleaseAddAtLeastOneFolder'),
type: 'error'
});
2015-10-25 22:29:32 -07:00
});
return false;
}
2015-10-15 11:48:43 -07:00
var form = this;
2016-03-22 10:46:57 -07:00
var dlg = $(form).parents('dialog')[0];
2015-10-15 11:48:43 -07:00
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
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(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;
}
2016-03-22 10:46:57 -07:00
var dlg = $(this).parents('dialog')[0];
2015-10-15 19:06:44 -07:00
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() {
2016-03-06 11:09:20 -07:00
var page = $(this).parents('.popupEditor')[0];
2015-10-25 22:29:32 -07:00
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();
});
2016-03-06 11:09:20 -07:00
var page = $(this).parents('.popupEditor')[0];
2015-10-25 22:29:32 -07:00
renderPaths(page);
2015-10-15 11:48:43 -07:00
}
function onDialogClosed() {
Dashboard.hideLoadingMsg();
currentDeferred.resolveWith(null, [hasChanges]);
}
function editor() {
var self = this;
self.show = function (options) {
2016-02-17 19:55:15 -07:00
var deferred = jQuery.Deferred();
2015-10-15 11:48:43 -07:00
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',
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
2016-03-06 11:09:20 -07:00
modal: false,
removeOnClose: true
2015-12-14 08:43:03 -07:00
});
2015-10-15 11:48:43 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('popupEditor');
2016-03-06 11:09:20 -07:00
dlg.innerHTML = Globalize.translateDocument(template);
2015-12-14 08:43:03 -07:00
document.body.appendChild(dlg);
2015-10-15 11:48:43 -07:00
2016-03-06 11:09:20 -07:00
initEditor(dlg, options.collectionTypeOptions);
2015-10-15 11:48:43 -07:00
2016-03-22 10:46:57 -07:00
dlg.addEventListener('close', 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
2016-03-06 11:09:20 -07:00
dlg.querySelector('.btnCancel').addEventListener('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 = [];
2016-03-06 11:09:20 -07:00
renderPaths(dlg);
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-10-15 11:48:43 -07:00
return deferred.promise();
};
}
return editor;
});