define(['jQuery'], function ($) {
var currentType;
function loadTabs(page, tabs) {
var html = '';
for (var i = 0, length = tabs.length; i < length; i++) {
var tab = tabs[i];
var isChecked = i == 0 ? ' selected="selected"' : '';
html += '';
}
$('#selectItemType', page).html(html).trigger('change');
Dashboard.hideLoadingMsg();
}
function loadType(page, type) {
Dashboard.showLoadingMsg();
currentType = type;
var promise1 = ApiClient.getServerConfiguration();
var promise2 = ApiClient.getJSON(ApiClient.getUrl("System/Configuration/MetadataPlugins"));
Promise.all([promise1, promise2]).then(function (responses) {
var config = responses[0];
var metadataPlugins = responses[1];
config = config.MetadataOptions.filter(function (c) {
return c.ItemType == type;
})[0];
if (config) {
renderType(page, type, config, metadataPlugins);
Dashboard.hideLoadingMsg();
} else {
ApiClient.getJSON(ApiClient.getUrl("System/Configuration/MetadataOptions/Default")).then(function (defaultConfig) {
config = defaultConfig;
renderType(page, type, config, metadataPlugins);
Dashboard.hideLoadingMsg();
});
}
});
}
function setVisibilityOfBackdrops(elem, visible) {
if (visible) {
elem.show();
$('input', elem).attr('required', 'required');
} else {
elem.hide();
$('input', elem).attr('required', '').removeAttr('required');
}
}
function renderType(page, type, config, metadataPlugins) {
var metadataInfo = metadataPlugins.filter(function (f) {
return type == f.ItemType;
})[0];
setVisibilityOfBackdrops($('.backdropFields', page), metadataInfo.SupportedImageTypes.indexOf('Backdrop') != -1);
setVisibilityOfBackdrops($('.screenshotFields', page), metadataInfo.SupportedImageTypes.indexOf('Screenshot') != -1);
$('.imageType', page).each(function () {
var imageType = this.getAttribute('data-imagetype');
if (metadataInfo.SupportedImageTypes.indexOf(imageType) == -1) {
this.classList.add('hide');
} else {
this.classList.remove('hide');
}
if (getImageConfig(config, imageType).Limit) {
this.checked = true;
} else {
this.checked = false;
}
});
var backdropConfig = getImageConfig(config, 'Backdrop');
$('#txtMaxBackdrops', page).val(backdropConfig.Limit);
$('#txtMinBackdropDownloadWidth', page).val(backdropConfig.MinWidth);
var screenshotConfig = getImageConfig(config, 'Screenshot');
$('#txtMaxScreenshots', page).val(screenshotConfig.Limit);
$('#txtMinScreenshotDownloadWidth', page).val(screenshotConfig.MinWidth);
renderMetadataLocals(page, type, config, metadataInfo);
renderMetadataFetchers(page, type, config, metadataInfo);
renderMetadataSavers(page, type, config, metadataInfo);
renderImageFetchers(page, type, config, metadataInfo);
}
function getImageConfig(config, type) {
return config.ImageOptions.filter(function (i) {
return i.Type == type;
})[0] || {
Type: type,
MinWidth: type == 'Backdrop' ? 1280 : 0,
Limit: type == 'Backdrop' ? 3 : 1
};
}
function renderImageFetchers(page, type, config, metadataInfo) {
var plugins = metadataInfo.Plugins.filter(function (p) {
return p.Type == 'ImageFetcher';
});
var html = '';
if (!plugins.length) {
$('.imageFetchers', page).html(html).hide();
return;
}
var i, length, plugin, id;
html += '
' + Globalize.translate('LabelImageFetchers') + '
';
html += '
';
for (i = 0, length = plugins.length; i < length; i++) {
plugin = plugins[i];
var isChecked = config.DisabledImageFetchers.indexOf(plugin.Name) == -1 ? ' checked="checked"' : '';
html += '';
html += '';
html += '';
html += '
';
html += plugin.Name;
html += '
';
html += '';
html += '';
html += '';
html += '';
}
html += '
';
page.querySelector('.metadataSavers').innerHTML = html;
}
function renderMetadataFetchers(page, type, config, metadataInfo) {
var plugins = metadataInfo.Plugins.filter(function (p) {
return p.Type == 'MetadataFetcher';
});
var html = '';
if (!plugins.length) {
$('.metadataFetchers', page).html(html).hide();
return;
}
var i, length, plugin;
html += '
';
for (i = 0, length = plugins.length; i < length; i++) {
plugin = plugins[i];
var isChecked = config.DisabledMetadataFetchers.indexOf(plugin.Name) == -1 ? ' checked="checked"' : '';
html += '';
html += '';
html += '';
html += '
';
html += plugin.Name;
html += '
';
html += '';
html += '';
html += '';
html += '';
}
html += '