[3705] | 1 | /*global $ */ |
---|
| 2 | 'use strict'; |
---|
[3709] | 3 | |
---|
[2738] | 4 | $(function() { |
---|
[3704] | 5 | $('#media-insert-cancel').click(function() { |
---|
| 6 | window.close(); |
---|
| 7 | }); |
---|
[2751] | 8 | |
---|
[3704] | 9 | $('#media-insert-ok').click(function() { |
---|
[3880] | 10 | const insert_form = $('#media-insert-form').get(0); |
---|
[3704] | 11 | if (insert_form === undefined) { |
---|
| 12 | return; |
---|
| 13 | } |
---|
[2738] | 14 | |
---|
[3880] | 15 | const editor_name = window.opener.$.getEditorName(); |
---|
| 16 | const editor = window.opener.CKEDITOR.instances[editor_name]; |
---|
| 17 | const type = insert_form.elements.type.value; |
---|
| 18 | const media_align_grid = { |
---|
| 19 | left: 'float: left; margin: 0 1em 1em 0;', |
---|
| 20 | right: 'float: right; margin: 0 0 1em 1em;', |
---|
| 21 | center: 'margin: 0 auto; display: table;' |
---|
| 22 | }; |
---|
[2738] | 23 | |
---|
[3704] | 24 | if (type == 'image') { |
---|
| 25 | if (editor.mode == 'wysiwyg') { |
---|
[3037] | 26 | |
---|
[3880] | 27 | const align = $('input[name="alignment"]:checked', insert_form).val(); |
---|
| 28 | let media_legend = $('input[name="legend"]:checked', insert_form).val(); |
---|
| 29 | const img_description = $('input[name="description"]', insert_form).val(); |
---|
| 30 | let style = ''; |
---|
| 31 | let template = ''; |
---|
| 32 | let template_figure = [ |
---|
[3704] | 33 | '', |
---|
| 34 | '' |
---|
| 35 | ]; |
---|
[3880] | 36 | let template_link = [ |
---|
[3704] | 37 | '', |
---|
| 38 | '' |
---|
| 39 | ]; |
---|
[3880] | 40 | let template_image = ''; |
---|
[2738] | 41 | |
---|
[3704] | 42 | if (media_legend != '' && media_legend != 'title' && media_legend != 'none') { |
---|
| 43 | media_legend = 'legend'; |
---|
| 44 | } |
---|
[3041] | 45 | |
---|
[3704] | 46 | // Build template |
---|
| 47 | if (align != '' && align != 'none') { |
---|
| 48 | // Set alignment |
---|
| 49 | style = ' style="{figureStyle}"'; |
---|
| 50 | } |
---|
| 51 | if (media_legend == 'legend') { |
---|
| 52 | // With a legend |
---|
| 53 | template_figure[0] = '<figure' + style + '>'; |
---|
| 54 | style = ''; // Do not use style further |
---|
| 55 | if (img_description != '') { |
---|
| 56 | template_figure[1] = '<figcaption>{figCaption}</figcaption>'; |
---|
| 57 | } |
---|
| 58 | template_figure[1] = template_figure[1] + '</figure>'; |
---|
| 59 | } |
---|
[3880] | 60 | template_image = `<img class="media" src="{imgSrc}" alt="{imgAlt}"${style}/>`; |
---|
[3704] | 61 | if ($('input[name="insertion"]:checked', insert_form).val() == 'link') { |
---|
| 62 | // With a link to original |
---|
| 63 | template_link[0] = '<a class="media-link" href="{aHref}">'; |
---|
| 64 | template_link[1] = '</a>'; |
---|
| 65 | } |
---|
| 66 | template = template_figure[0] + template_link[0] + template_image + template_link[1] + template_figure[1]; |
---|
[2738] | 67 | |
---|
[3880] | 68 | let block = new window.opener.CKEDITOR.template(template); |
---|
| 69 | let params = {}; |
---|
[2751] | 70 | |
---|
[3704] | 71 | // Set parameters for template |
---|
| 72 | if (media_legend != '' && media_legend != 'none') { |
---|
| 73 | params.imgAlt = window.opener.CKEDITOR.tools.htmlEncodeAttr( |
---|
| 74 | window.opener.$.stripBaseURL($('input[name="title"]', insert_form).val())); |
---|
| 75 | } else { |
---|
| 76 | params.imgAlt = ''; |
---|
| 77 | } |
---|
| 78 | params.imgSrc = window.opener.$.stripBaseURL($('input[name="src"]:checked', insert_form).val()); |
---|
| 79 | if (align != '' && align != 'none') { |
---|
| 80 | params.figureStyle = media_align_grid[align]; |
---|
| 81 | } |
---|
| 82 | params.figCaption = window.opener.CKEDITOR.tools.htmlEncodeAttr(img_description); |
---|
| 83 | if ($('input[name="insertion"]:checked', insert_form).val() == 'link') { |
---|
| 84 | params.aHref = window.opener.$.stripBaseURL($('input[name="url"]', insert_form).val()); |
---|
| 85 | } |
---|
[3037] | 86 | |
---|
[3704] | 87 | // Insert element |
---|
[3880] | 88 | const figure = window.opener.CKEDITOR.dom.element.createFromHtml( |
---|
[3704] | 89 | block.output(params), editor.document |
---|
| 90 | ); |
---|
| 91 | editor.insertElement(figure); |
---|
| 92 | } |
---|
[3880] | 93 | |
---|
[3704] | 94 | } else if (type == 'mp3') { |
---|
| 95 | // Audio media |
---|
[3880] | 96 | let player_audio = $('#public_player').val(); |
---|
| 97 | const align_audio = $('input[name="alignment"]:checked', insert_form).val(); |
---|
[3051] | 98 | |
---|
[3705] | 99 | if (align_audio != undefined && align_audio != 'none') { |
---|
[3880] | 100 | player_audio = `<div style="${media_align_grid[align_audio]}">${player_audio}</div>`; |
---|
[3704] | 101 | } |
---|
[3705] | 102 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_audio)); |
---|
[3880] | 103 | |
---|
[3704] | 104 | } else if (type == 'flv') { |
---|
| 105 | // Video media |
---|
[3880] | 106 | const oplayer = $(`<div>${$('#public_player').val()}</div>`); |
---|
| 107 | let flashvars = $('[name=FlashVars]', oplayer).val(); |
---|
[2754] | 108 | |
---|
[3880] | 109 | const align_video = $('input[name="alignment"]:checked', insert_form).val(); |
---|
[3725] | 110 | |
---|
[3880] | 111 | const title = insert_form.elements.title.value; |
---|
[3704] | 112 | if (title) { |
---|
[3880] | 113 | flashvars = `title=${encodeURI(title)}&${flashvars}`; |
---|
[3704] | 114 | } |
---|
[3725] | 115 | |
---|
[3880] | 116 | const vw = $('#video_w').val(); |
---|
| 117 | const vh = $('#video_h').val(); |
---|
[3725] | 118 | |
---|
| 119 | if (vw > 0) { |
---|
| 120 | $('video', oplayer).attr('width', vw); |
---|
| 121 | $('object', oplayer).attr('width', vw); |
---|
| 122 | flashvars = flashvars.replace(/(width=\d*)/, 'width=' + vw); |
---|
| 123 | } else { |
---|
| 124 | $('video', oplayer).removeAttr('width'); |
---|
| 125 | $('object', oplayer).removeAttr('width'); |
---|
| 126 | flashvars = flashvars.replace(/(width=\d*)/, ''); |
---|
| 127 | } |
---|
| 128 | if (vh > 0) { |
---|
| 129 | $('video', oplayer).attr('height', vh); |
---|
| 130 | $('object', oplayer).attr('height', vh); |
---|
| 131 | flashvars = flashvars.replace(/(height=\d*)/, 'height=' + vh); |
---|
| 132 | } else { |
---|
| 133 | $('video', oplayer).removeAttr('height'); |
---|
| 134 | $('object', oplayer).removeAttr('height'); |
---|
| 135 | flashvars = flashvars.replace(/(height=\d*)/, ''); |
---|
| 136 | } |
---|
[3704] | 137 | |
---|
[3705] | 138 | $('[name=FlashVars]', oplayer).val(flashvars); |
---|
[3880] | 139 | let player_video = oplayer.html(); |
---|
[3704] | 140 | |
---|
[3705] | 141 | if (align_video != undefined && align_video != 'none') { |
---|
[3880] | 142 | player_video = `<div style="${media_align_grid[align_video]}">${player_video}</div>`; |
---|
[3704] | 143 | } |
---|
[3705] | 144 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_video)); |
---|
[3880] | 145 | |
---|
[3704] | 146 | } else { |
---|
| 147 | // Unknown media type |
---|
[3880] | 148 | const url = window.opener.$.stripBaseURL($('input[name="url"]', insert_form).val()); |
---|
| 149 | const text = window.opener.CKEDITOR.tools.htmlEncodeAttr(insert_form.elements.title.value); |
---|
| 150 | const element = window.opener.CKEDITOR.dom.element.createFromHtml(`<a href="${url}">${text}</a>`); |
---|
[3704] | 151 | |
---|
| 152 | editor.insertElement(element); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | window.close(); |
---|
| 156 | }); |
---|
[2738] | 157 | }); |
---|