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 | let title = insert_form.elements.title.value; |
---|
98 | if (title) { |
---|
99 | player_audio = `<figure><figcaption>${title}</figcaption>${player_audio}</figure>`; |
---|
100 | } |
---|
101 | |
---|
102 | const align_audio = $('input[name="alignment"]:checked', insert_form).val(); |
---|
103 | |
---|
104 | if (align_audio != undefined && align_audio != 'none') { |
---|
105 | player_audio = `<div style="${media_align_grid[align_audio]}">${player_audio}</div>`; |
---|
106 | } |
---|
107 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_audio)); |
---|
108 | |
---|
109 | } else if (type == 'flv') { |
---|
110 | // Video media |
---|
111 | const oplayer = $(`<div>${$('#public_player').val()}</div>`); |
---|
112 | let flashvars = $('[name=FlashVars]', oplayer).val(); |
---|
113 | |
---|
114 | const align_video = $('input[name="alignment"]:checked', insert_form).val(); |
---|
115 | |
---|
116 | const title = insert_form.elements.title.value; |
---|
117 | if (title) { |
---|
118 | flashvars = `title=${encodeURI(title)}&${flashvars}`; |
---|
119 | } |
---|
120 | |
---|
121 | const vw = $('#video_w').val(); |
---|
122 | const vh = $('#video_h').val(); |
---|
123 | |
---|
124 | if (vw > 0) { |
---|
125 | $('video', oplayer).attr('width', vw); |
---|
126 | $('object', oplayer).attr('width', vw); |
---|
127 | flashvars = flashvars.replace(/(width=\d*)/, 'width=' + vw); |
---|
128 | } else { |
---|
129 | $('video', oplayer).removeAttr('width'); |
---|
130 | $('object', oplayer).removeAttr('width'); |
---|
131 | flashvars = flashvars.replace(/(width=\d*)/, ''); |
---|
132 | } |
---|
133 | if (vh > 0) { |
---|
134 | $('video', oplayer).attr('height', vh); |
---|
135 | $('object', oplayer).attr('height', vh); |
---|
136 | flashvars = flashvars.replace(/(height=\d*)/, 'height=' + vh); |
---|
137 | } else { |
---|
138 | $('video', oplayer).removeAttr('height'); |
---|
139 | $('object', oplayer).removeAttr('height'); |
---|
140 | flashvars = flashvars.replace(/(height=\d*)/, ''); |
---|
141 | } |
---|
142 | |
---|
143 | $('[name=FlashVars]', oplayer).val(flashvars); |
---|
144 | let player_video = oplayer.html(); |
---|
145 | |
---|
146 | if (align_video != undefined && align_video != 'none') { |
---|
147 | player_video = `<div style="${media_align_grid[align_video]}">${player_video}</div>`; |
---|
148 | } |
---|
149 | editor.insertElement(window.opener.CKEDITOR.dom.element.createFromHtml(player_video)); |
---|
150 | |
---|
151 | } else { |
---|
152 | // Unknown media type |
---|
153 | const url = window.opener.$.stripBaseURL($('input[name="url"]', insert_form).val()); |
---|
154 | const text = window.opener.CKEDITOR.tools.htmlEncodeAttr(insert_form.elements.title.value); |
---|
155 | const element = window.opener.CKEDITOR.dom.element.createFromHtml(`<a href="${url}">${text}</a>`); |
---|
156 | |
---|
157 | editor.insertElement(element); |
---|
158 | } |
---|
159 | |
---|
160 | window.close(); |
---|
161 | }); |
---|
162 | }); |
---|