|
3 | 3 |
|
4 | 4 | tinymce.create('tinymce.plugins.UploadImage', {
|
5 | 5 | UploadImage: function(ed, url) {
|
| 6 | + var PLUGIN_CLASS = 'mce-plugins-uploadimage'; |
6 | 7 | var form,
|
7 | 8 | iframe,
|
8 | 9 | win,
|
|
11 | 12 | editor = ed;
|
12 | 13 |
|
13 | 14 | function showDialog() {
|
| 15 | + var node = ed.selection.getNode(); |
| 16 | + |
| 17 | + if (node && node.tagName === 'IMG') { |
| 18 | + showUpdateDialog(); |
| 19 | + } else { |
| 20 | + showInsertDialog(); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + function showInsertDialog() { |
14 | 25 | var classList = getClassList();
|
15 | 26 | var body = [
|
16 | 27 | { type: 'iframe', url: 'javascript:void(0)' },
|
17 |
| - { type: 'textbox', name: 'file', label: ed.translate('Choose an image'), subtype: 'file' }, |
18 |
| - { type: 'textbox', name: 'alt', label: ed.translate('Image description') } |
| 28 | + { |
| 29 | + type: 'textbox', |
| 30 | + name: 'file', |
| 31 | + label: ed.translate('Choose an image'), |
| 32 | + subtype: 'file' |
| 33 | + }, |
| 34 | + { |
| 35 | + type: 'textbox', |
| 36 | + name: 'alt', |
| 37 | + label: ed.translate('Image description') |
| 38 | + } |
19 | 39 | ];
|
20 | 40 |
|
21 | 41 | if (classList.length > 0) {
|
|
34 | 54 | }
|
35 | 55 |
|
36 | 56 | body = body.concat([
|
37 |
| - { type: 'container', classes: 'error', html: "<p style='color: #b94a48;'> </p>" }, |
| 57 | + { |
| 58 | + type: 'container', |
| 59 | + classes: 'error', |
| 60 | + html: "<p style='color: #b94a48;'> </p>" |
| 61 | + }, |
38 | 62 |
|
39 | 63 | // Trick TinyMCE to add a empty div that "preloads" the throbber image
|
40 | 64 | { type: 'container', classes: 'throbber' }
|
|
80 | 104 | // Create some needed hidden inputs
|
81 | 105 | form.appendChild(createElement('input', { type: 'hidden', name: 'utf8', value: '✓' }));
|
82 | 106 | form.appendChild(
|
83 |
| - createElement('input', { type: 'hidden', name: 'authenticity_token', value: getMetaContents('csrf-token') }) |
| 107 | + createElement('input', { |
| 108 | + type: 'hidden', |
| 109 | + name: 'authenticity_token', |
| 110 | + value: getMetaContents('csrf-token') |
| 111 | + }) |
| 112 | + ); |
| 113 | + form.appendChild( |
| 114 | + createElement('input', { |
| 115 | + type: 'hidden', |
| 116 | + name: hintName(), |
| 117 | + value: hintValue() |
| 118 | + }) |
84 | 119 | );
|
85 |
| - form.appendChild(createElement('input', { type: 'hidden', name: hintName(), value: hintValue() })); |
86 | 120 |
|
87 | 121 | var el = win.getEl();
|
88 | 122 | var body = document.getElementById(el.id + '-body');
|
|
117 | 151 | body.appendChild(form);
|
118 | 152 | }
|
119 | 153 |
|
| 154 | + function showUpdateDialog() { |
| 155 | + var node = ed.selection.getNode(); |
| 156 | + var classList = getClassList(); |
| 157 | + var body = [ |
| 158 | + { |
| 159 | + type: 'textbox', |
| 160 | + name: 'alt', |
| 161 | + value: node.getAttribute('alt'), |
| 162 | + label: ed.translate('Image description') |
| 163 | + } |
| 164 | + ]; |
| 165 | + |
| 166 | + if (classList.length > 0) { |
| 167 | + for (var i = 0; i < classList.length; i++) { |
| 168 | + if (node.className.indexOf(classList[i].value) > 0) { |
| 169 | + selectedClass = classList[i]; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + selectedClass = selectedClass || classList[0].value; |
| 174 | + body = body.concat([ |
| 175 | + { |
| 176 | + type: 'listbox', |
| 177 | + name: 'class', |
| 178 | + label: ed.translate('Class'), |
| 179 | + value: selectedClass.value, |
| 180 | + values: classList, |
| 181 | + onSelect: function(e) { |
| 182 | + selectedClass = this.value(); |
| 183 | + } |
| 184 | + } |
| 185 | + ]); |
| 186 | + } |
| 187 | + |
| 188 | + win = editor.windowManager.open( |
| 189 | + { |
| 190 | + title: ed.translate('Update image'), |
| 191 | + width: 520 + parseInt(editor.getLang('uploadimage.delta_width', 0), 10), |
| 192 | + height: 180 + parseInt(editor.getLang('uploadimage.delta_height', 0), 10), |
| 193 | + body: body, |
| 194 | + buttons: [ |
| 195 | + { |
| 196 | + text: ed.translate('Update'), |
| 197 | + onclick: updateImage, |
| 198 | + subtype: 'primary' |
| 199 | + }, |
| 200 | + { |
| 201 | + text: ed.translate('Cancel'), |
| 202 | + onclick: ed.windowManager.close |
| 203 | + } |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + plugin_url: url |
| 208 | + } |
| 209 | + ); |
| 210 | + } |
| 211 | + |
120 | 212 | function hintName() {
|
121 | 213 | return inputName(ed.getParam('uploadimage_hint_key', 'hint'));
|
122 | 214 | }
|
|
133 | 225 | }
|
134 | 226 | }
|
135 | 227 |
|
| 228 | + function windowData() { |
| 229 | + if (ed.windowManager.windows[0]) { |
| 230 | + return ed.windowManager.windows[0].toJSON(); |
| 231 | + } else { |
| 232 | + return {}; |
| 233 | + } |
| 234 | + } |
| 235 | + |
136 | 236 | function insertImage() {
|
137 | 237 | if (getInputValue(inputName('file')) === '') {
|
138 | 238 | return handleError('You must choose a file');
|
|
159 | 259 | form.submit();
|
160 | 260 | }
|
161 | 261 |
|
| 262 | + function updateImage(e) { |
| 263 | + var node = ed.selection.getNode(); |
| 264 | + var data = windowData(); |
| 265 | + |
| 266 | + node.setAttribute('class', defaultClass() + ' ' + data.class); |
| 267 | + node.setAttribute('alt', data.alt); |
| 268 | + win.close(); |
| 269 | + } |
| 270 | + |
162 | 271 | function uploadDone() {
|
163 | 272 | if (throbber) {
|
164 | 273 | throbber.hide();
|
|
222 | 331 |
|
223 | 332 | function buildHTML(json) {
|
224 | 333 | var image = json[ed.getParam('uploadimage_model', 'image')];
|
225 |
| - var defaultClass = ed.getParam('uploadimage_default_img_class', ''); |
226 | 334 | var figure = ed.getParam('uploadimage_figure', false);
|
227 | 335 | var altText = getInputValue(inputName('alt'));
|
228 | 336 |
|
229 | 337 | var imgstr = "<img src='" + image['url'] + "'";
|
230 | 338 |
|
231 |
| - if (defaultClass !== '') { |
232 |
| - imgstr += " class='" + defaultClass + ' ' + selectedClass + "'"; |
| 339 | + if (defaultClass() !== '') { |
| 340 | + imgstr += " class='" + defaultClass() + ' ' + selectedClass + "'"; |
233 | 341 | }
|
234 | 342 |
|
235 | 343 | if (image['height']) {
|
|
288 | 396 | return null;
|
289 | 397 | }
|
290 | 398 |
|
| 399 | + function defaultClass() { |
| 400 | + return ed.getParam('uploadimage_default_img_class', '') + ' ' + PLUGIN_CLASS; |
| 401 | + } |
| 402 | + |
291 | 403 | function getClassList() {
|
292 | 404 | var config = ed.getParam('image_class_list', []);
|
293 | 405 | var values = [];
|
|
303 | 415 | if (editor.getParam('uploadimage', true)) {
|
304 | 416 | // Add a button that opens a window
|
305 | 417 | editor.addButton('uploadimage', {
|
306 |
| - tooltip: ed.translate('Insert an image from your computer'), |
| 418 | + tooltip: ed.translate('Insert/update an image'), |
307 | 419 | icon: 'image',
|
308 |
| - onclick: showDialog |
| 420 | + onclick: showDialog, |
| 421 | + stateSelector: '.' + PLUGIN_CLASS |
309 | 422 | });
|
310 | 423 |
|
311 | 424 | // Adds a menu item to the tools menu
|
312 | 425 | editor.addMenuItem('uploadimage', {
|
313 |
| - text: ed.translate('Insert an image from your computer'), |
| 426 | + text: ed.translate('Insert/update an image'), |
314 | 427 | icon: 'image',
|
315 | 428 | context: 'insert',
|
316 | 429 | onclick: showDialog
|
|
0 commit comments