|
7 | 7 | iframe, |
8 | 8 | win, |
9 | 9 | throbber, |
| 10 | + selected_class = '', |
10 | 11 | editor = ed; |
11 | 12 |
|
12 | 13 | function showDialog() { |
| 14 | + var classList = getClassList(); |
| 15 | + var body = [ |
| 16 | + {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')} |
| 19 | + ]; |
| 20 | + |
| 21 | + if (classList.length > 0) { |
| 22 | + selected_class = classList[0].value; |
| 23 | + body = body.concat([ |
| 24 | + { |
| 25 | + type: 'listbox', |
| 26 | + name: 'class', |
| 27 | + label: ed.translate('Class'), |
| 28 | + values: classList, |
| 29 | + onSelect: function(e) { |
| 30 | + selected_class = this.value(); |
| 31 | + } |
| 32 | + } |
| 33 | + ]); |
| 34 | + } |
| 35 | + |
| 36 | + body = body.concat([ |
| 37 | + {type: 'container', classes: 'error', html: "<p style='color: #b94a48;'> </p>"}, |
| 38 | + |
| 39 | + // Trick TinyMCE to add a empty div that "preloads" the throbber image |
| 40 | + {type: 'container', classes: 'throbber'} |
| 41 | + ]); |
| 42 | + |
13 | 43 | win = editor.windowManager.open({ |
14 | 44 | title: ed.translate('Insert an image from your computer'), |
15 | 45 | width: 520 + parseInt(editor.getLang('uploadimage.delta_width', 0), 10), |
16 | 46 | height: 180 + parseInt(editor.getLang('uploadimage.delta_height', 0), 10), |
17 | | - body: [ |
18 | | - {type: 'iframe', url: 'javascript:void(0)'}, |
19 | | - {type: 'textbox', name: 'file', label: ed.translate('Choose an image'), subtype: 'file'}, |
20 | | - {type: 'textbox', name: 'alt', label: ed.translate('Image description')}, |
21 | | - {type: 'container', classes: 'error', html: "<p style='color: #b94a48;'> </p>"}, |
22 | | - |
23 | | - // Trick TinyMCE to add a empty div that "preloads" the throbber image |
24 | | - {type: 'container', classes: 'throbber'}, |
25 | | - ], |
| 47 | + body: body, |
26 | 48 | buttons: [ |
27 | 49 | { |
28 | 50 | text: ed.translate('Insert'), |
|
38 | 60 | plugin_url: url |
39 | 61 | }); |
40 | 62 |
|
41 | | - // TinyMCE likes pointless submit handlers |
42 | | - win.off('submit'); |
43 | | - win.on('submit', insertImage); |
44 | | - |
45 | 63 | /* WHY DO YOU HATE <form>, TINYMCE!? */ |
46 | 64 | iframe = win.find("iframe")[0]; |
47 | 65 | form = createElement('form', { |
|
59 | 77 | // Create some needed hidden inputs |
60 | 78 | form.appendChild(createElement('input', {type: "hidden", name: "utf8", value: "✓"})); |
61 | 79 | form.appendChild(createElement('input', {type: 'hidden', name: 'authenticity_token', value: getMetaContents('csrf-token')})); |
62 | | - form.appendChild(createElement('input', {type: 'hidden', name: 'hint', value: ed.getParam("uploadimage_hint", "")})); |
| 80 | + form.appendChild(createElement('input', {type: 'hidden', name: hintName(), value: hintValue()})); |
63 | 81 |
|
64 | 82 | var el = win.getEl(); |
65 | 83 | var body = document.getElementById(el.id + "-body"); |
|
77 | 95 |
|
78 | 96 | if(ctrl.tagName.toLowerCase() == 'input' && ctrl.type != "hidden") { |
79 | 97 | if(ctrl.type == "file") { |
80 | | - ctrl.name = "file"; |
| 98 | + ctrl.name = inputName('file'); |
81 | 99 |
|
82 | 100 | // Hack styles |
83 | 101 | tinymce.DOM.setStyles(ctrl, { |
|
86 | 104 | 'webkitBoxShadow': 'none', |
87 | 105 | }); |
88 | 106 | } else { |
89 | | - ctrl.name = "alt"; |
| 107 | + ctrl.name = inputName('alt'); |
90 | 108 | } |
91 | 109 | } |
92 | 110 | } |
93 | 111 |
|
94 | 112 | body.appendChild(form); |
95 | 113 | } |
96 | 114 |
|
| 115 | + function hintName() { |
| 116 | + return inputName(ed.getParam('uploadimage_hint_key', 'hint')); |
| 117 | + } |
| 118 | + |
| 119 | + function hintValue() { |
| 120 | + return ed.getParam('uploadimage_hint', ''); |
| 121 | + } |
| 122 | + |
| 123 | + function inputName(name) { |
| 124 | + if (ed.getParam('uploadimage_model', false)) { |
| 125 | + return ed.getParam('uploadimage_model') + '[' + name + ']'; |
| 126 | + } else { |
| 127 | + return name; |
| 128 | + } |
| 129 | + } |
| 130 | + |
97 | 131 | function insertImage() { |
98 | | - if(getInputValue("file") == "") { |
| 132 | + if(getInputValue(inputName('file')) == "") { |
99 | 133 | return handleError('You must choose a file'); |
100 | 134 | } |
101 | 135 |
|
|
128 | 162 | var target = iframe.getEl(); |
129 | 163 | if(target.document || target.contentDocument) { |
130 | 164 | var doc = target.contentDocument || target.contentWindow.document; |
131 | | - if(String(doc.contentType).includes("html")) { |
| 165 | + if(String(doc.contentType).indexOf("html") > -1) { |
132 | 166 | handleResponse(doc.getElementsByTagName("body")[0].innerHTML); |
133 | 167 | } else { |
134 | 168 | handleResponse(doc.getElementsByTagName("pre")[0].innerHTML); |
|
180 | 214 | } |
181 | 215 |
|
182 | 216 | function buildHTML(json) { |
| 217 | + var image = json[ed.getParam('uploadimage_model', 'image')]; |
183 | 218 | var default_class = ed.getParam("uploadimage_default_img_class", ""); |
184 | 219 | var figure = ed.getParam("uploadimage_figure", false); |
185 | | - var alt_text = getInputValue("alt"); |
| 220 | + var alt_text = getInputValue(inputName('alt')); |
186 | 221 |
|
187 | | - var imgstr = "<img src='" + json["image"]["url"] + "'"; |
| 222 | + var imgstr = "<img src='" + image["url"] + "'"; |
188 | 223 |
|
189 | 224 | if(default_class != "") |
190 | | - imgstr += " class='" + default_class + "'"; |
| 225 | + imgstr += " class='" + default_class + ' ' + selected_class + "'"; |
191 | 226 |
|
192 | | - if(json["image"]["height"]) |
193 | | - imgstr += " height='" + json["image"]["height"] + "'"; |
194 | | - if(json["image"]["width"]) |
195 | | - imgstr += " width='" + json["image"]["width"] + "'"; |
| 227 | + if(image["height"]) |
| 228 | + imgstr += " height='" + image["height"] + "'"; |
| 229 | + if(image["width"]) |
| 230 | + imgstr += " width='" + image["width"] + "'"; |
196 | 231 |
|
197 | 232 | imgstr += " alt='" + alt_text + "'/>"; |
198 | 233 |
|
|
237 | 272 | return null; |
238 | 273 | } |
239 | 274 |
|
240 | | - // Add a button that opens a window |
241 | | - editor.addButton('uploadimage', { |
242 | | - tooltip: ed.translate('Insert an image from your computer'), |
243 | | - icon : 'image', |
244 | | - onclick: showDialog |
245 | | - }); |
246 | | - |
247 | | - // Adds a menu item to the tools menu |
248 | | - editor.addMenuItem('uploadimage', { |
249 | | - text: ed.translate('Insert an image from your computer'), |
250 | | - icon : 'image', |
251 | | - context: 'insert', |
252 | | - onclick: showDialog |
253 | | - }); |
| 275 | + function getClassList() { |
| 276 | + var config = ed.getParam('image_class_list', []); |
| 277 | + var values = []; |
| 278 | + for (var i = 0; i < config.length; i++) { |
| 279 | + values[i] = { |
| 280 | + text: config[i]['title'], |
| 281 | + value: config[i]['value'] |
| 282 | + }; |
| 283 | + } |
| 284 | + return values; |
| 285 | + }; |
| 286 | + |
| 287 | + if (editor.getParam('uploadimage', true)) { |
| 288 | + // Add a button that opens a window |
| 289 | + editor.addButton('uploadimage', { |
| 290 | + tooltip: ed.translate('Insert an image from your computer'), |
| 291 | + icon : 'image', |
| 292 | + onclick: showDialog |
| 293 | + }); |
| 294 | + |
| 295 | + // Adds a menu item to the tools menu |
| 296 | + editor.addMenuItem('uploadimage', { |
| 297 | + text: ed.translate('Insert an image from your computer'), |
| 298 | + icon : 'image', |
| 299 | + context: 'insert', |
| 300 | + onclick: showDialog |
| 301 | + }); |
| 302 | + } |
254 | 303 | } |
255 | 304 | }); |
256 | 305 |
|
|
0 commit comments