/*global $ */
'use strict';
$(function() {
$('#media-insert-cancel').click(function() {
window.close();
});
$('#media-insert-ok').click(function() {
const insert_form = $('#media-insert-form').get(0);
if (insert_form === undefined) {
return;
}
const editor_name = window.opener.$.getEditorName();
const editor = window.opener.CKEDITOR.instances[editor_name];
const type = insert_form.elements.type.value;
const media_align_grid = {
left: 'float: left; margin: 0 1em 1em 0;',
right: 'float: right; margin: 0 0 1em 1em;',
center: 'margin: 0 auto; display: table;'
};
if (type == 'image') {
if (editor.mode == 'wysiwyg') {
const align = $('input[name="alignment"]:checked', insert_form).val();
let media_legend = $('input[name="legend"]:checked', insert_form).val();
const img_description = $('input[name="description"]', insert_form).val();
let style = '';
let template = '';
let template_figure = [
'',
''
];
let template_link = [
'',
''
];
let template_image = '';
if (media_legend != '' && media_legend != 'title' && media_legend != 'none') {
media_legend = 'legend';
}
// Build template
if (align != '' && align != 'none') {
// Set alignment
style = ' style="{figureStyle}"';
}
if (media_legend == 'legend') {
// With a legend
template_figure[0] = '`;
if ($('input[name="insertion"]:checked', insert_form).val() == 'link') {
// With a link to original
template_link[0] = '';
template_link[1] = '';
}
template = template_figure[0] + template_link[0] + template_image + template_link[1] + template_figure[1];
let block = new window.opener.CKEDITOR.template(template);
let params = {};
// Set parameters for template
if (media_legend != '' && media_legend != 'none') {
params.imgAlt = window.opener.CKEDITOR.tools.htmlEncodeAttr(
window.opener.$.stripBaseURL($('input[name="title"]', insert_form).val()));
} else {
params.imgAlt = '';
}
params.imgSrc = window.opener.$.stripBaseURL($('input[name="src"]:checked', insert_form).val());
if (align != '' && align != 'none') {
params.figureStyle = media_align_grid[align];
}
params.figCaption = window.opener.CKEDITOR.tools.htmlEncodeAttr(img_description);
if ($('input[name="insertion"]:checked', insert_form).val() == 'link') {
params.aHref = window.opener.$.stripBaseURL($('input[name="url"]', insert_form).val());
}
// Insert element
const figure = window.opener.CKEDITOR.dom.element.createFromHtml(
block.output(params), editor.document
);
editor.insertElement(figure);
}
} else if (type == 'mp3') {
// Audio media
let player_audio = $('#public_player').val();
const align_audio = $('input[name="alignment"]:checked', insert_form).val();
if (align_audio != undefined && align_audio != 'none') {
player_audio = `