diff --git a/js/editor.assets.js b/js/editor.assets.js new file mode 100644 index 00000000..00703155 --- /dev/null +++ b/js/editor.assets.js @@ -0,0 +1,301 @@ +// Image library and colour scale editors for the map editor; loaded after editor.js. + +/** + * Send an image library request, adding the map name, configuration revision + * and CSRF token. + * + * @param {string} action Editor action; image_data is sent as GET, others as POST. + * @param {Object} extra Additional request fields. + * @param {function(Object)} callback Success handler. + * @param {string} errorId Id of the element that shows errors. + * @returns {void} + */ +function wmAssetRequest(action, extra, callback, errorId) { + var values = {action: action, mapname: $('#mapname').val()}; + if (action !== 'image_data' && typeof WMConfigRevision !== 'undefined') values.config_revision = WMConfigRevision; + if (typeof csrfMagicToken !== 'undefined') values.__csrf_magic = csrfMagicToken; + $.ajax({url: editor_url, type: action === 'image_data' ? 'GET' : 'POST', dataType: 'json', data: $.extend(values, extra), + success: callback, error: function(xhr) { $('#' + errorId).text(xhr.responseJSON && xhr.responseJSON.error || wmEditorText.imageText3); }}); +} + +/** + * Render the image library and refresh the icon and background selects from it. + * + * @param {Array} images Library entries returned by the editor. + * @returns {void} + */ +function wmAssetImages(images) { + $('#wm-images-objects, #wm-images-backgrounds').empty(); + images.forEach(function(item) { + var card = $('
').appendTo('#wm-images-' + item.kind); + var src = wmEditorImageSrc(item.url); + if (src) { + var image = document.createElement('img'); + image.src = src; + image.alt = item.name; + image.loading = 'lazy'; + card.append(image); + } + $('').text(item.name).appendTo(card); + if (item.width && item.height) $('').text(item.width + ' × ' + item.height + ' px').appendTo(card); + $('').text(item.users && item.users.length ? wmEditorText.imageUsed.replace('%s', item.users.join(', ')) : wmEditorText.imageUnused).appendTo(card); + if (item.deletable !== false) $('