(function ($, document, window, FileReader, escape) { var currentItem; var currentFile; var browsableImagePageSize = 10; var browsableImageStartIndex = 0; var browsableImageType = 'Primary'; function updateTabs(page, item) { var query = MetadataEditor.getEditQueryString(item); $('#btnEditPeople', page).attr('href', 'edititempeople.html?' + query); $('#btnEditMetadata', page).attr('href', 'edititemmetadata.html?' + query); } function getBaseRemoteOptions() { var options = {}; if (currentItem.Type == "Artist") { options.artist = currentItem.Name; } else if (currentItem.Type == "Artist") { options.artist = currentItem.Name; } else if (currentItem.Type == "Person") { options.person = currentItem.Name; } else if (currentItem.Type == "Genre") { options.genre = currentItem.Name; } else if (currentItem.Type == "GameGenre") { options.gameGenre = currentItem.Name; } else if (currentItem.Type == "MusicGenre") { options.musicGenre = currentItem.Name; } else if (currentItem.Type == "Studio") { options.studio = currentItem.Name; } else { options.itemId = currentItem.Id; } return options; } function reloadBrowsableImages(page) { Dashboard.showLoadingMsg(); var options = getBaseRemoteOptions(); options.type = browsableImageType; options.startIndex = browsableImageStartIndex; options.limit = browsableImagePageSize; var provider = $('#selectImageProvider', page).val(); if (provider) { options.ProviderName = provider; } ApiClient.getAvailableRemoteImages(options).done(function (result) { renderRemoteImages(page, currentItem, result, browsableImageType, options.startIndex, options.limit); $('#selectBrowsableImageType', page).val(browsableImageType).selectmenu('refresh'); var providersHtml = result.Providers.map(function (p) { return ''; }); $('#selectImageProvider', page).html('' + providersHtml).val(provider).selectmenu('refresh'); Dashboard.hideLoadingMsg(); }); } function renderRemoteImages(page, item, imagesResult, imageType, startIndex, limit) { $('.availableImagesPaging', page).html(getPagingHtml(startIndex, limit, imagesResult.TotalRecordCount)).trigger('create'); var html = ''; for (var i = 0, length = imagesResult.Images.length; i < length; i++) { html += getRemoteImageHtml(imagesResult.Images[i], imageType); } $('.availableImagesList', page).html(html).trigger('create'); $('.selectPage', page).on('change', function () { browsableImageStartIndex = (parseInt(this.value) - 1) * browsableImagePageSize; reloadBrowsableImages(page); }); $('.btnNextPage', page).on('click', function () { browsableImageStartIndex += browsableImagePageSize; reloadBrowsableImages(page); }); $('.btnPreviousPage', page).on('click', function () { browsableImageStartIndex -= browsableImagePageSize; reloadBrowsableImages(page); }); $('.btnDownloadRemoteImage', page).on('click', function () { downloadRemoteImage(page, this.getAttribute('data-imageurl'), this.getAttribute('data-imagetype'), this.getAttribute('data-imageprovider')); }); } function downloadRemoteImage(page, url, type, provider) { var options = getBaseRemoteOptions(); options.Type = type; options.ImageUrl = url; options.ProviderName = provider; Dashboard.showLoadingMsg(); ApiClient.downloadRemoteImage(options).done(function () { $('#popupDownload', page).popup("close"); reload(page); }); } function getRemoteImageHtml(image, imageType) { var html = ''; html += '
'; var cssClass = "remoteImage"; if (imageType == "Backdrop" || imageType == "Art" || imageType == "Thumb" || imageType == "Logo") { cssClass += " remoteBackdropImage"; } else if (imageType == "Banner") { cssClass += " remoteBannerImage"; } else if (imageType == "Disc") { cssClass += " remoteDiscImage"; } else { cssClass += currentItem.Type == "Episode" ? " remoteBackdropImage" : " remotePosterImage"; } html += ''; html += ''; html += '
'; html += image.ProviderName; html += '
'; if (image.Width || image.Height) { html += '
'; html += image.Width + ' x ' + image.Height; if (image.Language) { html += ' • ' + image.Language; } html += '
'; } if (image.CommunityRating != null) { html += '
'; if (image.RatingType == "Likes") { html += image.CommunityRating + (image.CommunityRating == 1 ? " like" : " likes"); } else { if (image.CommunityRating) { html += image.CommunityRating.toFixed(1); if (image.VoteCount) { html += ' • ' + image.VoteCount + ' votes'; } } else { html += "Unrated"; } } html += '
'; } html += '
'; html += '
'; return html; } function getPagingHtml(startIndex, limit, totalRecordCount) { var html = ''; var pageCount = Math.ceil(totalRecordCount / limit); var pageNumber = (startIndex / limit) + 1; var dropdownHtml = ''; var recordsEnd = Math.min(startIndex + limit, totalRecordCount); // 20 is the minimum page size var showControls = totalRecordCount > limit; html += '
'; html += ''; var startAtDisplay = totalRecordCount ? startIndex + 1 : 0; html += startAtDisplay + '-' + recordsEnd + ' of ' + totalRecordCount; if (showControls) { html += ', page ' + dropdownHtml + ' of ' + pageCount; } html += ''; if (showControls) { html += ''; html += ''; } html += '
'; return html; } function reload(page) { Dashboard.showLoadingMsg(); MetadataEditor.getItemPromise().done(function (item) { currentItem = item; LibraryBrowser.renderName(item, $('.itemName', page), true); updateTabs(page, item); if (item.Type == "Person" || item.Type == "Studio" || item.Type == "MusicGenre" || item.Type == "Genre" || item.Type == "Artist" || item.Type == "GameGenre") { $('#btnEditPeople', page).hide(); } else { $('#btnEditPeople', page).show(); } ApiClient.getItemImageInfos(currentItem.Id, currentItem.Type, currentItem.Name).done(function (imageInfos) { renderStandardImages(page, item, imageInfos); renderBackdrops(page, item, imageInfos); renderScreenshots(page, item, imageInfos); Dashboard.hideLoadingMsg(); }); }); } function renderImages(page, item, images, elem) { var html = ''; for (var i = 0, length = images.length; i < length; i++) { var image = images[i]; html += '
'; html += '
'; html += '
'; html += '

' + image.ImageType + '

'; html += '

' + image.Width + ' * ' + image.Height + '

'; html += '

' + (parseInt(image.Size / 1024)) + ' KB

'; html += '

'; if (image.ImageType == "Backdrop" || image.ImageType == "Screenshot") { if (i > 0) { html += ''; } else { html += ''; } if (i < length - 1) { html += ''; } else { html += ''; } } html += ''; html += ''; html += '

'; html += '
'; html += '
'; } elem.html(html).trigger('create'); } function renderStandardImages(page, item, imageInfos) { var images = imageInfos.filter(function (i) { return i.ImageType != "Screenshot" && i.ImageType != "Backdrop" && i.ImageType != "Chapter"; }); if (images.length) { $('#imagesContainer', page).show(); renderImages(page, item, images, $('#images', page)); } else { $('#imagesContainer', page).hide(); } } function renderBackdrops(page, item, imageInfos) { var images = imageInfos.filter(function (i) { return i.ImageType == "Backdrop"; }).sort(function (a, b) { return a.ImageIndex - b.ImageIndex; }); if (images.length) { $('#backdropsContainer', page).show(); renderImages(page, item, images, $('#backdrops', page)); } else { $('#backdropsContainer', page).hide(); } } function renderScreenshots(page, item, imageInfos) { var images = imageInfos.filter(function (i) { return i.ImageType == "Screenshot"; }).sort(function (a, b) { return a.ImageIndex - b.ImageIndex; }); if (images.length) { $('#screenshotsContainer', page).show(); renderImages(page, item, images, $('#screenshots', page)); } else { $('#screenshotsContainer', page).hide(); } } function onFileReaderError(evt) { Dashboard.hideLoadingMsg(); switch (evt.target.error.code) { case evt.target.error.NOT_FOUND_ERR: Dashboard.showError('File Not Found!'); break; case evt.target.error.NOT_READABLE_ERR: Dashboard.showError('File is not readable'); break; case evt.target.error.ABORT_ERR: break; // noop default: Dashboard.showError('An error occurred reading this file.'); }; } function setFiles(page, files) { var file = files[0]; if (!file || !file.type.match('image.*')) { $('#imageOutput', page).html(''); $('#fldUpload', page).hide(); currentFile = null; return; } currentFile = file; var reader = new FileReader(); reader.onerror = onFileReaderError; reader.onloadstart = function () { $('#fldUpload', page).hide(); }; reader.onabort = function () { Dashboard.hideLoadingMsg(); Dashboard.showError('File read cancelled'); }; // Closure to capture the file information. reader.onload = (function (theFile) { return function (e) { // Render thumbnail. var html = [''].join(''); $('#imageOutput', page).html(html); $('#fldUpload', page).show(); }; })(file); // Read in the image file as a data URL. reader.readAsDataURL(file); } function processImageChangeResult(page) { reload(page); } function editItemImages() { var self = this; self.onSubmit = function () { var file = currentFile; if (!file) { return false; } if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") { return false; } Dashboard.showLoadingMsg(); var page = $.mobile.activePage; var imageType = $('#selectImageType', page).val(); ApiClient.uploadItemImage(currentItem.Id, currentItem.Type, currentItem.Name, imageType, file).done(function () { $('#uploadImage', page).val('').trigger('change'); $('#popupUpload', page).popup("close"); processImageChangeResult(page); }); return false; }; self.deleteImage = function (type, index) { var page = $.mobile.activePage; Dashboard.confirm("Are you sure you wish to delete this image?", "Delete " + type + " Image", function (result) { if (result) { ApiClient.deleteItemImage(currentItem.Id, currentItem.Type, currentItem.Name, type, index).done(function () { processImageChangeResult(page); }); } }); }; self.moveImage = function (type, index, newIndex) { var page = $.mobile.activePage; ApiClient.updateItemImageIndex(currentItem.Id, currentItem.Type, currentItem.Name, type, index, newIndex).done(function () { processImageChangeResult(page); }); }; self.showDownloadMenu = function (type) { browsableImageStartIndex = 0; browsableImageType = type; $('.lnkBrowseImages').trigger('click'); }; } window.EditItemImagesPage = new editItemImages(); $(document).on('pageinit', "#editItemImagesPage", function () { var page = this; $('.libraryTree', page).on('itemclicked', function (event, data) { if (data.id != currentItem.Id) { MetadataEditor.currentItemId = data.id; MetadataEditor.currentItemName = data.itemName; MetadataEditor.currentItemType = data.itemType; //Dashboard.navigate('edititemmetadata.html?id=' + data.id); $.mobile.urlHistory.ignoreNextHashChange = true; window.location.hash = 'editItemImagesPage?id=' + data.id; reload(page); } }); $('.lnkBrowseImages', page).on('click', function () { reloadBrowsableImages(page); }); $('#selectBrowsableImageType', page).on('change', function () { browsableImageType = this.value; browsableImageStartIndex = 0; reloadBrowsableImages(page); }); $('#selectImageProvider', page).on('change', function () { browsableImageStartIndex = 0; reloadBrowsableImages(page); }); }).on('pageshow', "#editItemImagesPage", function () { var page = this; reload(page); $('#uploadImage', page).on("change", function () { setFiles(page, this.files); }); $("#imageDropZone", page).on('dragover', function (e) { e.preventDefault(); e.originalEvent.dataTransfer.dropEffect = 'Copy'; return false; }).on('drop', function (e) { e.preventDefault(); setFiles(page, e.originalEvent.dataTransfer.files); return false; }); }).on('pagehide', "#editItemImagesPage", function () { var page = this; currentItem = null; $('#uploadImage', page).off("change"); $("#imageDropZone", page).off('dragover').off('drop'); }); })(jQuery, document, window, window.FileReader, escape);