| 1 | /*global $ */ |
|---|
| 2 | 'use strict'; |
|---|
| 3 | |
|---|
| 4 | $(function() { |
|---|
| 5 | $('#media-insert-cancel').click(function() { |
|---|
| 6 | window.close(); |
|---|
| 7 | }); |
|---|
| 8 | |
|---|
| 9 | $('#media-insert-ok').click(function() { |
|---|
| 10 | const insert_form = $('#media-insert-form').get(0); |
|---|
| 11 | if (insert_form === undefined) { |
|---|
| 12 | return; |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 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 | }; |
|---|
| 23 | |
|---|
| 24 | if (type == 'image') { |
|---|
| 25 | if (editor.mode == 'wysiwyg') { |
|---|
| 26 | |
|---|
| 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 = [ |
|---|
| 33 | '', |
|---|
| 34 | '' |
|---|
| 35 | ]; |
|---|
| 36 | let template_link = [ |
|---|
| 37 | '', |
|---|
| 38 | '' |
|---|
| 39 | ]; |
|---|
| 40 | let template_image = ''; |
|---|
| 41 | |
|---|
| 42 | if (media_legend != '' && media_legend != 'title' && media_legend != 'none') { |
|---|
| 43 | media_legend = 'legend'; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 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 | } |
|---|
| 60 | template_image = `<img class="media" src="{imgSrc}" alt="{imgAlt}"${style}/>`; |
|---|
| 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]; |
|---|
| 67 | |
|---|
| 68 | let block = new window.opener.CKEDITOR.template(template); |
|---|
| 69 | let params = {}; |
|---|
| 70 | |
|---|
| 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 | } |
|---|
| 86 | |
|---|
| 87 | // Insert element |
|---|
| 88 | const figure = window.opener.CKEDITOR.dom.element.createFromHtml( |
|---|
| 89 | block.output(params), editor.document |
|---|
| 90 | ); |
|---|
| 91 | editor.insertElement(figure); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | } else if (type == 'mp3') { |
|---|
| 95 | // Audio media |
|---|
| 96 | let player_audio = $('#public_player').val(); |
|---|
| 97 | const align_audio = $('input[name="alignment"]:checked', insert_form).val(); |
|---|
| 98 | |
|---|
| 99 | if (align_audio != undefined && align_audio != 'none') { |
|---|
| 100 | player_audio = `<div style="${media_align_grid[align_audio]}">${player_audio}</div>`; |
|---|
| 101 | } |
|---|
| 102 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_audio)); |
|---|
| 103 | |
|---|
| 104 | } else if (type == 'flv') { |
|---|
| 105 | // Video media |
|---|
| 106 | const oplayer = $(`<div>${$('#public_player').val()}</div>`); |
|---|
| 107 | let flashvars = $('[name=FlashVars]', oplayer).val(); |
|---|
| 108 | |
|---|
| 109 | const align_video = $('input[name="alignment"]:checked', insert_form).val(); |
|---|
| 110 | |
|---|
| 111 | const title = insert_form.elements.title.value; |
|---|
| 112 | if (title) { |
|---|
| 113 | flashvars = `title=${encodeURI(title)}&${flashvars}`; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | const vw = $('#video_w').val(); |
|---|
| 117 | const vh = $('#video_h').val(); |
|---|
| 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 | } |
|---|
| 137 | |
|---|
| 138 | $('[name=FlashVars]', oplayer).val(flashvars); |
|---|
| 139 | let player_video = oplayer.html(); |
|---|
| 140 | |
|---|
| 141 | if (align_video != undefined && align_video != 'none') { |
|---|
| 142 | player_video = `<div style="${media_align_grid[align_video]}">${player_video}</div>`; |
|---|
| 143 | } |
|---|
| 144 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_video)); |
|---|
| 145 | |
|---|
| 146 | } else { |
|---|
| 147 | // Unknown media type |
|---|
| 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>`); |
|---|
| 151 | |
|---|
| 152 | editor.insertElement(element); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | window.close(); |
|---|
| 156 | }); |
|---|
| 157 | }); |
|---|