0
|
1 /**
|
|
2 * Roundcube editor js library
|
|
3 *
|
|
4 * This file is part of the Roundcube Webmail client
|
|
5 *
|
|
6 * @licstart The following is the entire license notice for the
|
|
7 * JavaScript code in this file.
|
|
8 *
|
|
9 * Copyright (c) 2006-2014, The Roundcube Dev Team
|
|
10 *
|
|
11 * The JavaScript code in this page is free software: you can
|
|
12 * redistribute it and/or modify it under the terms of the GNU
|
|
13 * General Public License (GNU GPL) as published by the Free Software
|
|
14 * Foundation, either version 3 of the License, or (at your option)
|
|
15 * any later version. The code is distributed WITHOUT ANY WARRANTY;
|
|
16 * without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
17 * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
|
|
18 *
|
|
19 * As additional permission under GNU GPL version 3 section 7, you
|
|
20 * may distribute non-source (e.g., minimized or compacted) forms of
|
|
21 * that code without the copy of the GNU GPL normally required by
|
|
22 * section 4, provided you include this license notice and a URL
|
|
23 * through which recipients can access the Corresponding Source.
|
|
24 *
|
|
25 * @licend The above is the entire license notice
|
|
26 * for the JavaScript code in this file.
|
|
27 *
|
|
28 * @author Eric Stadtherr <estadtherr@gmail.com>
|
|
29 * @author Aleksander Machniak <alec@alec.pl>
|
|
30 */
|
|
31
|
|
32 /**
|
|
33 * Roundcube Text Editor Widget class
|
|
34 * @constructor
|
|
35 */
|
|
36 function rcube_text_editor(config, id)
|
|
37 {
|
|
38 var ref = this,
|
|
39 abs_url = location.href.replace(/[?#].*$/, '').replace(/\/$/, ''),
|
|
40 conf = {
|
|
41 selector: '#' + ($('#' + id).is('.mce_editor') ? id : 'fake-editor-id'),
|
|
42 cache_suffix: 's=4050800',
|
|
43 theme: 'modern',
|
|
44 language: config.lang,
|
|
45 content_css: rcmail.assets_path('program/resources/tinymce/content.css'),
|
|
46 menubar: false,
|
|
47 statusbar: false,
|
|
48 toolbar_items_size: 'small',
|
|
49 extended_valid_elements: 'font[face|size|color|style],span[id|class|align|style]',
|
|
50 relative_urls: false,
|
|
51 remove_script_host: false,
|
|
52 convert_urls: false, // #1486944
|
|
53 image_description: false,
|
|
54 paste_webkit_style: "color font-size font-family",
|
|
55 paste_data_images: true,
|
|
56 browser_spellcheck: true
|
|
57 };
|
|
58
|
|
59 // register spellchecker for plain text editor
|
|
60 this.spellcheck_observer = function() {};
|
|
61 if (config.spellchecker) {
|
|
62 this.spellchecker = config.spellchecker;
|
|
63 if (config.spellcheck_observer) {
|
|
64 this.spellchecker.spelling_state_observer = this.spellcheck_observer = config.spellcheck_observer;
|
|
65 }
|
|
66 }
|
|
67
|
|
68 // secure spellchecker requests with Roundcube token
|
|
69 // Note: must be registered only once (#1490311)
|
|
70 if (!tinymce.registered_request_token) {
|
|
71 tinymce.registered_request_token = true;
|
|
72 tinymce.util.XHR.on('beforeSend', function(e) {
|
|
73 e.xhr.setRequestHeader('X-Roundcube-Request', rcmail.env.request_token);
|
|
74 });
|
|
75 }
|
|
76
|
|
77 // minimal editor
|
|
78 if (config.mode == 'identity') {
|
|
79 $.extend(conf, {
|
|
80 plugins: 'autolink charmap code colorpicker hr image link paste tabfocus textcolor',
|
|
81 toolbar: 'bold italic underline alignleft aligncenter alignright alignjustify'
|
|
82 + ' | outdent indent charmap hr link unlink image code forecolor'
|
|
83 + ' | fontselect fontsizeselect',
|
|
84 file_browser_callback: function(name, url, type, win) { ref.file_browser_callback(name, url, type); },
|
|
85 file_browser_callback_types: 'image'
|
|
86 });
|
|
87 }
|
|
88 // full-featured editor
|
|
89 else {
|
|
90 $.extend(conf, {
|
|
91 plugins: 'autolink charmap code colorpicker directionality link lists image media nonbreaking'
|
|
92 + ' paste table tabfocus textcolor searchreplace spellchecker',
|
|
93 toolbar: 'bold italic underline | alignleft aligncenter alignright alignjustify'
|
|
94 + ' | bullist numlist outdent indent ltr rtl blockquote | forecolor backcolor | fontselect fontsizeselect'
|
|
95 + ' | link unlink table | $extra charmap image media | code searchreplace undo redo',
|
|
96 spellchecker_rpc_url: abs_url + '/?_task=utils&_action=spell_html&_remote=1',
|
|
97 spellchecker_language: rcmail.env.spell_lang,
|
|
98 accessibility_focus: false,
|
|
99 file_browser_callback: function(name, url, type, win) { ref.file_browser_callback(name, url, type); },
|
|
100 // @todo: support more than image (types: file, image, media)
|
|
101 file_browser_callback_types: 'image media'
|
|
102 });
|
|
103 }
|
|
104
|
|
105 // add TinyMCE plugins/buttons from Roundcube plugin
|
|
106 $.each(config.extra_plugins || [], function() {
|
|
107 if (conf.plugins.indexOf(this) < 0)
|
|
108 conf.plugins = conf.plugins + ' ' + this;
|
|
109 });
|
|
110 $.each(config.extra_buttons || [], function() {
|
|
111 if (conf.toolbar.indexOf(this) < 0)
|
|
112 conf.toolbar = conf.toolbar.replace('$extra', '$extra ' + this);
|
|
113 });
|
|
114
|
|
115 // disable TinyMCE plugins/buttons from Roundcube plugin
|
|
116 $.each(config.disabled_plugins || [], function() {
|
|
117 conf.plugins = conf.plugins.replace(this, '');
|
|
118 });
|
|
119 $.each(config.disabled_buttons || [], function() {
|
|
120 conf.toolbar = conf.toolbar.replace(this, '');
|
|
121 });
|
|
122
|
|
123 conf.toolbar = conf.toolbar.replace('$extra', '').replace(/\|\s+\|/g, '|');
|
|
124
|
|
125 // support external configuration settings e.g. from skin
|
|
126 if (window.rcmail_editor_settings)
|
|
127 $.extend(conf, window.rcmail_editor_settings);
|
|
128
|
|
129 conf.setup = function(ed) {
|
|
130 ed.on('init', function(ed) { ref.init_callback(ed); });
|
|
131 // add handler for spellcheck button state update
|
|
132 ed.on('SpellcheckStart SpellcheckEnd', function(args) {
|
|
133 ref.spellcheck_active = args.type == 'spellcheckstart';
|
|
134 ref.spellcheck_observer();
|
|
135 });
|
|
136 ed.on('keypress', function() {
|
|
137 rcmail.compose_type_activity++;
|
|
138 });
|
|
139 };
|
|
140
|
|
141 // textarea identifier
|
|
142 this.id = id;
|
|
143 // reference to active editor (if in HTML mode)
|
|
144 this.editor = null;
|
|
145
|
|
146 tinymce.init(conf);
|
|
147
|
|
148 // react to real individual tinyMCE editor init
|
|
149 this.init_callback = function(event)
|
|
150 {
|
|
151 this.editor = event.target;
|
|
152
|
|
153 if (rcmail.env.action != 'compose') {
|
|
154 return;
|
|
155 }
|
|
156
|
|
157 var area = $('#' + this.id),
|
|
158 height = $('div.mce-toolbar-grp:first', area.parent()).height();
|
|
159
|
|
160 // the editor might be still not fully loaded, making the editing area
|
|
161 // inaccessible, wait and try again (#1490310)
|
|
162 if (height > 200 || height > area.height()) {
|
|
163 return setTimeout(function () { ref.init_callback(event); }, 300);
|
|
164 }
|
|
165
|
|
166 var css = {},
|
|
167 elem = rcube_find_object('_from'),
|
|
168 fe = rcmail.env.compose_focus_elem;
|
|
169
|
|
170 if (rcmail.env.default_font)
|
|
171 css['font-family'] = rcmail.env.default_font;
|
|
172
|
|
173 if (rcmail.env.default_font_size)
|
|
174 css['font-size'] = rcmail.env.default_font_size;
|
|
175
|
|
176 if (css['font-family'] || css['font-size'])
|
|
177 $(this.editor.getBody()).css(css);
|
|
178
|
|
179 if (elem && elem.type == 'select-one') {
|
|
180 // insert signature (only for the first time)
|
|
181 if (!rcmail.env.identities_initialized)
|
|
182 rcmail.change_identity(elem);
|
|
183
|
|
184 // Focus previously focused element
|
|
185 if (fe && fe.id != this.id && fe.nodeName != 'BODY') {
|
|
186 window.focus(); // for WebKit (#1486674)
|
|
187 fe.focus();
|
|
188 rcmail.env.compose_focus_elem = null;
|
|
189 }
|
|
190 }
|
|
191
|
|
192 // set tabIndex and set focus to element that was focused before
|
|
193 ref.tabindex(fe && fe.id == ref.id);
|
|
194 // Trigger resize (needed for proper editor resizing in some browsers)
|
|
195 $(window).resize();
|
|
196 };
|
|
197
|
|
198 // set tabIndex on tinymce editor
|
|
199 this.tabindex = function(focus)
|
|
200 {
|
|
201 if (rcmail.env.task == 'mail' && this.editor) {
|
|
202 var textarea = this.editor.getElement(),
|
|
203 body = this.editor.getBody(),
|
|
204 node = this.editor.getContentAreaContainer().childNodes[0];
|
|
205
|
|
206 if (textarea && node)
|
|
207 node.tabIndex = textarea.tabIndex;
|
|
208
|
|
209 // find :prev and :next elements to get focus when tabbing away
|
|
210 if (textarea.tabIndex > 0) {
|
|
211 var x = null,
|
|
212 tabfocus_elements = [':prev',':next'],
|
|
213 el = tinymce.DOM.select('*[tabindex='+textarea.tabIndex+']:not(iframe)');
|
|
214
|
|
215 tinymce.each(el, function(e, i) { if (e.id == ref.id) { x = i; return false; } });
|
|
216 if (x !== null) {
|
|
217 if (el[x-1] && el[x-1].id) {
|
|
218 tabfocus_elements[0] = el[x-1].id;
|
|
219 }
|
|
220 if (el[x+1] && el[x+1].id) {
|
|
221 tabfocus_elements[1] = el[x+1].id;
|
|
222 }
|
|
223 this.editor.settings.tabfocus_elements = tabfocus_elements.join(',');
|
|
224 }
|
|
225 }
|
|
226
|
|
227 // ContentEditable reset fixes invisible cursor issue in Firefox < 25
|
|
228 if (bw.mz && bw.vendver < 25)
|
|
229 $(body).prop('contenteditable', false).prop('contenteditable', true);
|
|
230
|
|
231 if (focus)
|
|
232 body.focus();
|
|
233 }
|
|
234 };
|
|
235
|
|
236 // switch html/plain mode
|
|
237 this.toggle = function(ishtml, noconvert)
|
|
238 {
|
|
239 var curr, content, result,
|
|
240 // these non-printable chars are not removed on text2html and html2text
|
|
241 // we can use them as temp signature replacement
|
|
242 sig_mark = "\u0002\u0003",
|
|
243 input = $('#' + this.id),
|
|
244 signature = rcmail.env.identity ? rcmail.env.signatures[rcmail.env.identity] : null,
|
|
245 is_sig = signature && signature.text && signature.text.length > 1;
|
|
246
|
|
247 // apply spellcheck changes if spell checker is active
|
|
248 this.spellcheck_stop();
|
|
249
|
|
250 if (ishtml) {
|
|
251 content = input.val();
|
|
252
|
|
253 // replace current text signature with temp mark
|
|
254 if (is_sig) {
|
|
255 content = content.replace(/\r\n/, "\n");
|
|
256 content = content.replace(signature.text.replace(/\r\n/, "\n"), sig_mark);
|
|
257 }
|
|
258
|
|
259 var init_editor = function(data) {
|
|
260 // replace signature mark with html version of the signature
|
|
261 if (is_sig)
|
|
262 data = data.replace(sig_mark, '<div id="_rc_sig">' + signature.html + '</div>');
|
|
263
|
|
264 input.val(data);
|
|
265 tinymce.execCommand('mceAddEditor', false, ref.id);
|
|
266
|
|
267 if (ref.editor) {
|
|
268 var body = $(ref.editor.getBody());
|
|
269 // #1486593
|
|
270 ref.tabindex(true);
|
|
271 // put cursor on start of the compose body
|
|
272 ref.editor.selection.setCursorLocation(body.children().first().get(0));
|
|
273 }
|
|
274 };
|
|
275
|
|
276 // convert to html
|
|
277 if (!noconvert) {
|
|
278 result = rcmail.plain2html(content, init_editor);
|
|
279 }
|
|
280 else {
|
|
281 init_editor(content);
|
|
282 result = true;
|
|
283 }
|
|
284 }
|
|
285 else if (this.editor) {
|
|
286 if (is_sig) {
|
|
287 // get current version of signature, we'll need it in
|
|
288 // case of html2text conversion abort
|
|
289 if (curr = this.editor.dom.get('_rc_sig'))
|
|
290 curr = curr.innerHTML;
|
|
291
|
|
292 // replace current signature with some non-printable characters
|
|
293 // we use non-printable characters, because this replacement
|
|
294 // is visible to the user
|
|
295 // doing this after getContent() would be hard
|
|
296 this.editor.dom.setHTML('_rc_sig', sig_mark);
|
|
297 }
|
|
298
|
|
299 // get html content
|
|
300 content = this.editor.getContent();
|
|
301
|
|
302 var init_plaintext = function(data) {
|
|
303 tinymce.execCommand('mceRemoveEditor', false, ref.id);
|
|
304 ref.editor = null;
|
|
305
|
|
306 // replace signture mark with text version of the signature
|
|
307 if (is_sig)
|
|
308 data = data.replace(sig_mark, "\n" + signature.text);
|
|
309
|
|
310 input.val(data).focus();
|
|
311 rcmail.set_caret_pos(input.get(0), 0);
|
|
312 };
|
|
313
|
|
314 // convert html to text
|
|
315 if (!noconvert) {
|
|
316 result = rcmail.html2plain(content, init_plaintext);
|
|
317 }
|
|
318 else {
|
|
319 init_plaintext(input.val());
|
|
320 result = true;
|
|
321 }
|
|
322
|
|
323 // bring back current signature
|
|
324 if (!result && curr)
|
|
325 this.editor.dom.setHTML('_rc_sig', curr);
|
|
326 }
|
|
327
|
|
328 return result;
|
|
329 };
|
|
330
|
|
331 // start spellchecker
|
|
332 this.spellcheck_start = function()
|
|
333 {
|
|
334 if (this.editor) {
|
|
335 tinymce.execCommand('mceSpellCheck', true);
|
|
336 this.spellcheck_observer();
|
|
337 }
|
|
338 else if (this.spellchecker && this.spellchecker.spellCheck) {
|
|
339 this.spellchecker.spellCheck();
|
|
340 }
|
|
341 };
|
|
342
|
|
343 // stop spellchecker
|
|
344 this.spellcheck_stop = function()
|
|
345 {
|
|
346 var ed = this.editor;
|
|
347
|
|
348 if (ed) {
|
|
349 if (ed.plugins && ed.plugins.spellchecker && this.spellcheck_active) {
|
|
350 ed.execCommand('mceSpellCheck', false);
|
|
351 this.spellcheck_observer();
|
|
352 }
|
|
353 }
|
|
354 else if (ed = this.spellchecker) {
|
|
355 if (ed.state && ed.state != 'ready' && ed.state != 'no_error_found')
|
|
356 $(ed.spell_span).trigger('click');
|
|
357 }
|
|
358 };
|
|
359
|
|
360 // spellchecker state
|
|
361 this.spellcheck_state = function()
|
|
362 {
|
|
363 var ed;
|
|
364
|
|
365 if (this.editor)
|
|
366 return this.spellcheck_active;
|
|
367 else if ((ed = this.spellchecker) && ed.state)
|
|
368 return ed.state != 'ready' && ed.state != 'no_error_found';
|
|
369 };
|
|
370
|
|
371 // resume spellchecking, highlight provided mispellings without a new ajax request
|
|
372 this.spellcheck_resume = function(data)
|
|
373 {
|
|
374 var ed = this.editor;
|
|
375
|
|
376 if (ed) {
|
|
377 ed.plugins.spellchecker.markErrors(data);
|
|
378 }
|
|
379 else if (ed = this.spellchecker) {
|
|
380 ed.prepare(false, true);
|
|
381 ed.processData(data);
|
|
382 }
|
|
383 };
|
|
384
|
|
385 // get selected (spellcheker) language
|
|
386 this.get_language = function()
|
|
387 {
|
|
388 if (this.editor) {
|
|
389 return this.editor.settings.spellchecker_language || rcmail.env.spell_lang;
|
|
390 }
|
|
391 else if (this.spellchecker) {
|
|
392 return GOOGIE_CUR_LANG;
|
|
393 }
|
|
394 };
|
|
395
|
|
396 // set language for spellchecking
|
|
397 this.set_language = function(lang)
|
|
398 {
|
|
399 var ed = this.editor;
|
|
400
|
|
401 if (ed) {
|
|
402 ed.settings.spellchecker_language = lang;
|
|
403 }
|
|
404 if (ed = this.spellchecker) {
|
|
405 ed.setCurrentLanguage(lang);
|
|
406 }
|
|
407 };
|
|
408
|
|
409 // replace selection with text snippet
|
|
410 // input can be a string or object with 'text' and 'html' properties
|
|
411 this.replace = function(input)
|
|
412 {
|
|
413 var format, ed = this.editor;
|
|
414
|
|
415 if (!input)
|
|
416 return false;
|
|
417
|
|
418 // insert into tinymce editor
|
|
419 if (ed) {
|
|
420 ed.getWin().focus(); // correct focus in IE & Chrome
|
|
421
|
|
422 if ($.type(input) == 'object' && ('html' in input)) {
|
|
423 input = input.html;
|
|
424 format = 'html';
|
|
425 }
|
|
426 else {
|
|
427 if ($.type(input) == 'object')
|
|
428 input = input.text || '';
|
|
429
|
|
430 input = rcmail.quote_html(input).replace(/\r?\n/g, '<br/>');
|
|
431 format = 'text';
|
|
432 }
|
|
433
|
|
434 ed.selection.setContent(input, {format: format});
|
|
435 }
|
|
436 // replace selection in compose textarea
|
|
437 else if (ed = rcube_find_object(this.id)) {
|
|
438 var selection = $(ed).is(':focus') ? rcmail.get_input_selection(ed) : {start: 0, end: 0},
|
|
439 value = ed.value;
|
|
440 pre = value.substring(0, selection.start),
|
|
441 end = value.substring(selection.end, value.length);
|
|
442
|
|
443 if ($.type(input) == 'object')
|
|
444 input = input.text || '';
|
|
445
|
|
446 // insert response text
|
|
447 ed.value = pre + input + end;
|
|
448
|
|
449 // set caret after inserted text
|
|
450 rcmail.set_caret_pos(ed, selection.start + input.length);
|
|
451 ed.focus();
|
|
452 }
|
|
453 };
|
|
454
|
|
455 // get selected text (if no selection returns all text) from the editor
|
|
456 this.get_content = function(args)
|
|
457 {
|
|
458 var sigstart, ed = this.editor, text = '', strip = false,
|
|
459 defaults = {refresh: true, selection: false, nosig: false, format: 'html'};
|
|
460
|
|
461 args = $.extend(defaults, args);
|
|
462
|
|
463 // apply spellcheck changes if spell checker is active
|
|
464 if (args.refresh) {
|
|
465 this.spellcheck_stop();
|
|
466 }
|
|
467
|
|
468 // get selected text from tinymce editor
|
|
469 if (ed) {
|
|
470 if (args.selection)
|
|
471 text = ed.selection.getContent({format: args.format});
|
|
472
|
|
473 if (!text) {
|
|
474 text = ed.getContent({format: args.format});
|
|
475 // @todo: strip signature in html mode
|
|
476 strip = args.format == 'text';
|
|
477 }
|
|
478 }
|
|
479 // get selected text from compose textarea
|
|
480 else if (ed = rcube_find_object(this.id)) {
|
|
481 if (args.selection && $(ed).is(':focus')) {
|
|
482 text = rcmail.get_input_selection(ed).text;
|
|
483 }
|
|
484
|
|
485 if (!text) {
|
|
486 text = ed.value;
|
|
487 strip = true;
|
|
488 }
|
|
489 }
|
|
490
|
|
491 // strip off signature
|
|
492 // @todo: make this optional
|
|
493 if (strip && args.nosig) {
|
|
494 sigstart = text.indexOf('-- \n');
|
|
495 if (sigstart > 0) {
|
|
496 text = text.substring(0, sigstart);
|
|
497 }
|
|
498 }
|
|
499
|
|
500 return text;
|
|
501 };
|
|
502
|
|
503 // change user signature text
|
|
504 this.change_signature = function(id, show_sig)
|
|
505 {
|
|
506 var position_element, cursor_pos, p = -1,
|
|
507 input_message = $('#' + this.id),
|
|
508 message = input_message.val(),
|
|
509 sig = rcmail.env.identity;
|
|
510
|
|
511 if (!this.editor) { // plain text mode
|
|
512 // remove the 'old' signature
|
|
513 if (show_sig && sig && rcmail.env.signatures && rcmail.env.signatures[sig]) {
|
|
514 sig = rcmail.env.signatures[sig].text;
|
|
515 sig = sig.replace(/\r\n/g, '\n');
|
|
516
|
|
517 p = rcmail.env.top_posting ? message.indexOf(sig) : message.lastIndexOf(sig);
|
|
518 if (p >= 0)
|
|
519 message = message.substring(0, p) + message.substring(p+sig.length, message.length);
|
|
520 }
|
|
521
|
|
522 // add the new signature string
|
|
523 if (show_sig && rcmail.env.signatures && rcmail.env.signatures[id]) {
|
|
524 sig = rcmail.env.signatures[id].text;
|
|
525 sig = sig.replace(/\r\n/g, '\n');
|
|
526
|
|
527 // in place of removed signature
|
|
528 if (p >= 0) {
|
|
529 message = message.substring(0, p) + sig + message.substring(p, message.length);
|
|
530 cursor_pos = p - 1;
|
|
531 }
|
|
532 // empty message or new-message mode
|
|
533 else if (!message || !rcmail.env.compose_mode) {
|
|
534 cursor_pos = message.length;
|
|
535 message += '\n\n' + sig;
|
|
536 }
|
|
537 else if (rcmail.env.top_posting && !rcmail.env.sig_below) {
|
|
538 // at cursor position
|
|
539 if (pos = rcmail.get_caret_pos(input_message.get(0))) {
|
|
540 message = message.substring(0, pos) + '\n' + sig + '\n\n' + message.substring(pos, message.length);
|
|
541 cursor_pos = pos;
|
|
542 }
|
|
543 // on top
|
|
544 else {
|
|
545 message = '\n\n' + sig + '\n\n' + message.replace(/^[\r\n]+/, '');
|
|
546 cursor_pos = 0;
|
|
547 }
|
|
548 }
|
|
549 else {
|
|
550 message = message.replace(/[\r\n]+$/, '');
|
|
551 cursor_pos = !rcmail.env.top_posting && message.length ? message.length + 1 : 0;
|
|
552 message += '\n\n' + sig;
|
|
553 }
|
|
554 }
|
|
555 else {
|
|
556 cursor_pos = rcmail.env.top_posting ? 0 : message.length;
|
|
557 }
|
|
558
|
|
559 input_message.val(message);
|
|
560
|
|
561 // move cursor before the signature
|
|
562 rcmail.set_caret_pos(input_message.get(0), cursor_pos);
|
|
563 }
|
|
564 else if (show_sig && rcmail.env.signatures) { // html
|
|
565 var sigElem = this.editor.dom.get('_rc_sig');
|
|
566
|
|
567 // Append the signature as a div within the body
|
|
568 if (!sigElem) {
|
|
569 var body = this.editor.getBody();
|
|
570
|
|
571 sigElem = $('<div id="_rc_sig"></div>').get(0);
|
|
572
|
|
573 // insert at start or at cursor position in top-posting mode
|
|
574 // (but not if the content is empty and not in new-message mode)
|
|
575 if (rcmail.env.top_posting && !rcmail.env.sig_below
|
|
576 && rcmail.env.compose_mode && (body.childNodes.length > 1 || $(body).text())
|
|
577 ) {
|
|
578 this.editor.getWin().focus(); // correct focus in IE & Chrome
|
|
579
|
|
580 var node = this.editor.selection.getNode();
|
|
581
|
|
582 $(sigElem).insertBefore(node.nodeName == 'BODY' ? body.firstChild : node.nextSibling);
|
|
583 $('<p>').append($('<br>')).insertBefore(sigElem);
|
|
584 }
|
|
585 else {
|
|
586 body.appendChild(sigElem);
|
|
587 position_element = rcmail.env.top_posting && rcmail.env.compose_mode ? body.firstChild : $(sigElem).prev();
|
|
588 }
|
|
589 }
|
|
590
|
|
591 sigElem.innerHTML = rcmail.env.signatures[id] ? rcmail.env.signatures[id].html : '';
|
|
592 }
|
|
593 else if (!rcmail.env.top_posting) {
|
|
594 position_element = $(this.editor.getBody()).children().last();
|
|
595 }
|
|
596
|
|
597 // put cursor before signature and scroll the window
|
|
598 if (this.editor && position_element && position_element.length) {
|
|
599 this.editor.selection.setCursorLocation(position_element.get(0));
|
|
600 this.editor.getWin().scroll(0, position_element.offset().top);
|
|
601 }
|
|
602 };
|
|
603
|
|
604 // trigger content save
|
|
605 this.save = function()
|
|
606 {
|
|
607 if (this.editor) {
|
|
608 this.editor.save();
|
|
609 }
|
|
610 };
|
|
611
|
|
612 // focus the editing area
|
|
613 this.focus = function()
|
|
614 {
|
|
615 (this.editor || rcube_find_object(this.id)).focus();
|
|
616 };
|
|
617
|
|
618 // image selector
|
|
619 this.file_browser_callback = function(field_name, url, type)
|
|
620 {
|
|
621 var i, elem, cancel, dialog, fn, list = [];
|
|
622
|
|
623 // open image selector dialog
|
|
624 dialog = this.editor.windowManager.open({
|
|
625 title: rcmail.get_label('select' + type),
|
|
626 width: 500,
|
|
627 height: 300,
|
|
628 html: '<div id="image-selector-list"><ul></ul></div>'
|
|
629 + '<div id="image-selector-form"><div id="image-upload-button" class="mce-widget mce-btn" role="button" tabindex="0"></div></div>',
|
|
630 buttons: [{text: 'Cancel', onclick: function() { ref.file_browser_close(); }}]
|
|
631 });
|
|
632
|
|
633 rcmail.env.file_browser_field = field_name;
|
|
634 rcmail.env.file_browser_type = type;
|
|
635
|
|
636 // fill images list with available images
|
|
637 for (i in rcmail.env.attachments) {
|
|
638 if (elem = ref.file_browser_entry(i, rcmail.env.attachments[i])) {
|
|
639 list.push(elem);
|
|
640 }
|
|
641 }
|
|
642
|
|
643 if (list.length) {
|
|
644 $('#image-selector-list > ul').append(list).find('li:first').focus();
|
|
645 }
|
|
646
|
|
647 // add hint about max file size (in dialog footer)
|
|
648 $('div.mce-abs-end', dialog.getEl()).append($('<div class="hint">')
|
|
649 .text($('div.hint', rcmail.gui_objects.uploadform).text()));
|
|
650
|
|
651 // init upload button
|
|
652 elem = $('#image-upload-button').append($('<span>').text(rcmail.get_label('add' + type)));
|
|
653 cancel = elem.parents('.mce-panel').find('button:last').parent();
|
|
654
|
|
655 // we need custom Tab key handlers, until we find out why
|
|
656 // tabindex do not work here as expected
|
|
657 elem.keydown(function(e) {
|
|
658 if (e.which == 9) {
|
|
659 // on Tab + Shift focus first file
|
|
660 if (rcube_event.get_modifier(e) == SHIFT_KEY)
|
|
661 $('#image-selector-list li:last').focus();
|
|
662 // on Tab focus Cancel button
|
|
663 else
|
|
664 cancel.focus();
|
|
665
|
|
666 return false;
|
|
667 }
|
|
668 });
|
|
669 cancel.keydown(function(e) {
|
|
670 if (e.which == 9) {
|
|
671 // on Tab + Shift focus upload button
|
|
672 if (rcube_event.get_modifier(e) == SHIFT_KEY)
|
|
673 elem.focus();
|
|
674 else
|
|
675 $('#image-selector-list li:first').focus();
|
|
676
|
|
677 return false;
|
|
678 }
|
|
679 });
|
|
680
|
|
681 // enable (smart) upload button
|
|
682 this.hack_file_input(elem, rcmail.gui_objects.uploadform);
|
|
683
|
|
684 // enable drag-n-drop area
|
|
685 if ((window.XMLHttpRequest && XMLHttpRequest.prototype && XMLHttpRequest.prototype.sendAsBinary) || window.FormData) {
|
|
686 if (!rcmail.env.filedrop) {
|
|
687 rcmail.env.filedrop = {};
|
|
688 }
|
|
689 if (rcmail.gui_objects.filedrop) {
|
|
690 rcmail.env.old_file_drop = rcmail.gui_objects.filedrop;
|
|
691 }
|
|
692
|
|
693 rcmail.gui_objects.filedrop = $('#image-selector-form');
|
|
694 rcmail.gui_objects.filedrop.addClass('droptarget')
|
|
695 .on('dragover dragleave', function(e) {
|
|
696 e.preventDefault();
|
|
697 e.stopPropagation();
|
|
698 $(this)[(e.type == 'dragover' ? 'addClass' : 'removeClass')]('hover');
|
|
699 })
|
|
700 .get(0).addEventListener('drop', function(e) { return rcmail.file_dropped(e); }, false);
|
|
701 }
|
|
702
|
|
703 // register handler for successful file upload
|
|
704 if (!rcmail.env.file_dialog_event) {
|
|
705 rcmail.env.file_dialog_event = true;
|
|
706 rcmail.addEventListener('fileuploaded', function(attr) {
|
|
707 var elem;
|
|
708 if (elem = ref.file_browser_entry(attr.name, attr.attachment)) {
|
|
709 $('#image-selector-list > ul').prepend(elem);
|
|
710 elem.focus();
|
|
711 }
|
|
712 });
|
|
713 }
|
|
714
|
|
715 // @todo: upload progress indicator
|
|
716 };
|
|
717
|
|
718 // close file browser window
|
|
719 this.file_browser_close = function(url)
|
|
720 {
|
|
721 var input = $('#' + rcmail.env.file_browser_field);
|
|
722
|
|
723 if (url)
|
|
724 input.val(url);
|
|
725
|
|
726 this.editor.windowManager.close();
|
|
727
|
|
728 input.focus();
|
|
729
|
|
730 if (rcmail.env.old_file_drop)
|
|
731 rcmail.gui_objects.filedrop = rcmail.env.old_file_drop;
|
|
732 };
|
|
733
|
|
734 // creates file browser entry
|
|
735 this.file_browser_entry = function(file_id, file)
|
|
736 {
|
|
737 if (!file.complete || !file.mimetype) {
|
|
738 return;
|
|
739 }
|
|
740
|
|
741 if (rcmail.file_upload_id) {
|
|
742 rcmail.set_busy(false, null, rcmail.file_upload_id);
|
|
743 }
|
|
744
|
|
745 var rx, img_src;
|
|
746
|
|
747 switch (rcmail.env.file_browser_type) {
|
|
748 case 'image':
|
|
749 rx = /^image\//i;
|
|
750 break;
|
|
751
|
|
752 case 'media':
|
|
753 rx = /^video\//i;
|
|
754 img_src = 'program/resources/tinymce/video.png';
|
|
755 break;
|
|
756
|
|
757 default:
|
|
758 return;
|
|
759 }
|
|
760
|
|
761 if (rx.test(file.mimetype)) {
|
|
762 var path = rcmail.env.comm_path + '&_from=' + rcmail.env.action,
|
|
763 action = rcmail.env.compose_id ? '&_id=' + rcmail.env.compose_id + '&_action=display-attachment' : '&_action=upload-display',
|
|
764 href = path + action + '&_file=' + file_id,
|
|
765 img = $('<img>').attr({title: file.name, src: img_src ? img_src : href + '&_thumbnail=1'});
|
|
766
|
|
767 return $('<li>').attr({tabindex: 0})
|
|
768 .data('url', href)
|
|
769 .append($('<span class="img">').append(img))
|
|
770 .append($('<span class="name">').text(file.name))
|
|
771 .click(function() { ref.file_browser_close($(this).data('url')); })
|
|
772 .keydown(function(e) {
|
|
773 if (e.which == 13) {
|
|
774 ref.file_browser_close($(this).data('url'));
|
|
775 }
|
|
776 // we need custom Tab key handlers, until we find out why
|
|
777 // tabindex do not work here as expected
|
|
778 else if (e.which == 9) {
|
|
779 if (rcube_event.get_modifier(e) == SHIFT_KEY) {
|
|
780 if (!$(this).prev().focus().length)
|
|
781 $('#image-upload-button').parents('.mce-panel').find('button:last').parent().focus();
|
|
782 }
|
|
783 else {
|
|
784 if (!$(this).next().focus().length)
|
|
785 $('#image-upload-button').focus();
|
|
786 }
|
|
787
|
|
788 return false;
|
|
789 }
|
|
790 });
|
|
791 }
|
|
792 };
|
|
793
|
|
794 // create smart files upload button
|
|
795 this.hack_file_input = function(elem, clone_form)
|
|
796 {
|
|
797 var offset, link = $(elem),
|
|
798 file = $('<input>').attr('name', '_file[]'),
|
|
799 form = $('<form>').attr({method: 'post', enctype: 'multipart/form-data'});
|
|
800
|
|
801 // clone existing upload form
|
|
802 if (clone_form) {
|
|
803 file.attr('name', $('input[type="file"]', clone_form).attr('name'));
|
|
804 form.attr('action', $(clone_form).attr('action'))
|
|
805 .append($('<input>').attr({type: 'hidden', name: '_token', value: rcmail.env.request_token}));
|
|
806 }
|
|
807
|
|
808 function move_file_input(e) {
|
|
809 if (!offset) offset = link.offset();
|
|
810 file.css({top: (e.pageY - offset.top - 10) + 'px', left: (e.pageX - offset.left - 10) + 'px'});
|
|
811 }
|
|
812
|
|
813 file.attr({type: 'file', multiple: 'multiple', size: 5, title: '', tabindex: -1})
|
|
814 .change(function() { rcmail.upload_file(form, 'upload'); })
|
|
815 .click(function() { setTimeout(function() { link.mouseleave(); }, 20); })
|
|
816 // opacity:0 does the trick, display/visibility doesn't work
|
|
817 .css({opacity: 0, cursor: 'pointer', position: 'relative', outline: 'none'})
|
|
818 .appendTo(form);
|
|
819
|
|
820 // In FF and IE we need to move the browser file-input's button under the cursor
|
|
821 // Thanks to the size attribute above we know the length of the input field
|
|
822 if (navigator.userAgent.match(/Firefox|MSIE/))
|
|
823 file.css({marginLeft: '-80px'});
|
|
824
|
|
825 // Note: now, I observe problem with cursor style on FF < 4 only
|
|
826 link.css({overflow: 'hidden', cursor: 'pointer'})
|
|
827 .mouseenter(function() { this.__active = true; })
|
|
828 // place button under the cursor
|
|
829 .mousemove(function(e) {
|
|
830 if (this.__active)
|
|
831 move_file_input(e);
|
|
832 // move the input away if button is disabled
|
|
833 else
|
|
834 $(this).mouseleave();
|
|
835 })
|
|
836 .mouseleave(function() {
|
|
837 file.css({top: '-10000px', left: '-10000px'});
|
|
838 this.__active = false;
|
|
839 })
|
|
840 .click(function(e) {
|
|
841 // forward click if mouse-enter event was missed
|
|
842 if (!this.__active) {
|
|
843 this.__active = true;
|
|
844 move_file_input(e);
|
|
845 file.trigger(e);
|
|
846 }
|
|
847 })
|
|
848 .keydown(function(e) {
|
|
849 if (e.which == 13) file.trigger('click');
|
|
850 })
|
|
851 .mouseleave()
|
|
852 .append(form);
|
|
853 };
|
|
854 }
|