Thomas
2013-10-09 d016dcc6f6a3daf8c19e2ececd3c676cd274381a
commit | author | age
a0109c 1 /*
S 2  +-----------------------------------------------------------------------+
e019f2 3  | Roundcube editor js library                                           |
a0109c 4  |                                                                       |
e019f2 5  | This file is part of the Roundcube web development suite              |
7fe381 6  | Copyright (C) 2006-2012, The Roundcube Dev Team                       |
T 7  |                                                                       |
8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
a0109c 11  |                                                                       |
S 12  +-----------------------------------------------------------------------+
13  | Author: Eric Stadtherr <estadtherr@gmail.com>                         |
14  +-----------------------------------------------------------------------+
15 */
16
b8ae50 17 // Initialize HTML editor
66df08 18 function rcmail_editor_init(config)
4ca10b 19 {
2d889e 20   var ret, conf = {
e0a5ce 21       mode: 'textareas',
A 22       editor_selector: 'mce_editor',
23       apply_source_formatting: true,
24       theme: 'advanced',
66df08 25       language: config.lang,
A 26       content_css: config.skin_path + '/editor_content.css',
e0a5ce 27       theme_advanced_toolbar_location: 'top',
A 28       theme_advanced_toolbar_align: 'left',
29       theme_advanced_buttons3: '',
ea3021 30       theme_advanced_statusbar_location: 'none',
2d889e 31       extended_valid_elements: 'font[face|size|color|style],span[id|class|align|style]',
e0a5ce 32       relative_urls: false,
A 33       remove_script_host: false,
2d889e 34       gecko_spellcheck: true,
A 35       convert_urls: false, // #1486944
8f142e 36       external_image_list: window.rcmail_editor_images,
2d889e 37       rc_client: rcmail
A 38     };
39
66df08 40   if (config.mode == 'identity')
2d889e 41     $.extend(conf, {
A 42       plugins: 'paste,tabfocus',
43       theme_advanced_buttons1: 'bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,separator,outdent,indent,charmap,hr,link,unlink,code,forecolor',
d675ab 44       theme_advanced_buttons2: 'fontselect,fontsizeselect'
b8ae50 45     });
4be86f 46   else { // mail compose
2d889e 47     $.extend(conf, {
5f8d59 48       plugins: 'paste,emotions,media,nonbreaking,table,searchreplace,visualchars,directionality,inlinepopups,tabfocus' + (config.spellcheck ? ',spellchecker' : ''),
e0a5ce 49       theme_advanced_buttons1: 'bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,ltr,rtl,blockquote,|,forecolor,backcolor,fontselect,fontsizeselect',
4be86f 50       theme_advanced_buttons2: 'link,unlink,table,|,emotions,charmap,image,media,|,code,search,undo,redo',
e0a5ce 51       spellchecker_languages: (rcmail.env.spellcheck_langs ? rcmail.env.spellcheck_langs : 'Dansk=da,Deutsch=de,+English=en,Espanol=es,Francais=fr,Italiano=it,Nederlands=nl,Polski=pl,Portugues=pt,Suomi=fi,Svenska=sv'),
ec86ad 52       spellchecker_rpc_url: '?_task=utils&_action=spell_html&_remote=1',
66df08 53       spellchecker_enable_learn_rpc: config.spelldict,
2d889e 54       accessibility_focus: false,
e0a5ce 55       oninit: 'rcmail_editor_callback'
b8ae50 56     });
2d889e 57
4be86f 58     // add handler for spellcheck button state update
A 59     conf.setup = function(ed) {
60       ed.onSetProgressState.add(function(ed, active) {
61         if (!active)
62           rcmail.spellcheck_state();
63       });
64     }
65   }
66
2d889e 67   // support external configuration settings e.g. from skin
A 68   if (window.rcmail_editor_settings)
69     $.extend(conf, window.rcmail_editor_settings);
70
71   tinyMCE.init(conf);
4ca10b 72 }
b8ae50 73
A 74 // react to real individual tinyMCE editor init
b2f3e6 75 function rcmail_editor_callback()
b8ae50 76 {
f7b2bf 77   var css = {},
AM 78     elem = rcube_find_object('_from'),
c288f9 79     fe = rcmail.env.compose_focus_elem;
A 80
7e263e 81   if (rcmail.env.default_font)
f7b2bf 82     css['font-family'] = rcmail.env.default_font;
7e263e 83
edc49e 84   if (rcmail.env.default_font_size)
f7b2bf 85     css['font-size'] = rcmail.env.default_font_size;
AM 86
87   if (css['font-family'] || css['font-size'])
88     $(tinyMCE.get(rcmail.env.composebody).getBody()).css(css);
edc49e 89
d3d1e3 90   if (elem && elem.type == 'select-one') {
1f019c 91     rcmail.change_identity(elem);
882b0f 92     // Focus previously focused element
43fb35 93     if (fe && fe.id != rcmail.env.composebody) {
ad9dac 94       // use setTimeout() for IE9 (#1488541)
AM 95       window.setTimeout(function() {
96         window.focus(); // for WebKit (#1486674)
97         fe.focus();
98       }, 10);
43fb35 99     }
882b0f 100   }
c288f9 101
5cff85 102   // set tabIndex and set focus to element that was focused before
c288f9 103   rcmail_editor_tabindex(fe && fe.id == rcmail.env.composebody);
eb9eff 104   // Trigger resize (needed for proper editor resizing in some browsers using default skin)
A 105   $(window).resize();
665cc5 106 }
A 107
108 // set tabIndex on tinyMCE editor
5cff85 109 function rcmail_editor_tabindex(focus)
665cc5 110 {
A 111   if (rcmail.env.task == 'mail') {
a01b3b 112     var editor = tinyMCE.get(rcmail.env.composebody);
4cdc70 113     if (editor) {
A 114       var textarea = editor.getElement();
115       var node = editor.getContentAreaContainer().childNodes[0];
116       if (textarea && node)
117         node.tabIndex = textarea.tabIndex;
5cff85 118       if (focus)
59c404 119         editor.getBody().focus();
4cdc70 120     }
665cc5 121   }
b8ae50 122 }
A 123
124 // switch html/plain mode
5821ff 125 function rcmail_toggle_editor(select, textAreaId, flagElement)
b8ae50 126 {
5821ff 127   var flag, ishtml;
A 128
129   if (select.tagName != 'SELECT')
130     ishtml = select.checked;
131   else
132     ishtml = select.value == 'html';
b8ae50 133
3940ba 134   var res = rcmail.command('toggle-editor', {id:textAreaId, mode:ishtml?'html':'plain'});
832594 135
3940ba 136   if (ishtml) {
4cdc70 137     // #1486593
5cff85 138     setTimeout("rcmail_editor_tabindex(true);", 500);
b8ae50 139     if (flagElement && (flag = rcube_find_object(flagElement)))
A 140       flag.value = '1';
087c7d 141   }
14d494 142   else if (res) {
b8ae50 143     if (flagElement && (flag = rcube_find_object(flagElement)))
A 144       flag.value = '0';
5cff85 145
bdf6de 146     if (rcmail.env.composebody)
A 147       rcube_find_object(rcmail.env.composebody).focus();
087c7d 148   }
14d494 149   else { // !res
A 150     if (select.tagName == 'SELECT')
151       select.value = 'html';
152     else if (select.tagName == 'INPUT')
153       select.checked = true;
154   }
087c7d 155 }
8f142e 156
A 157 // editor callbeck for images listing
158 function rcmail_editor_images()
159 {
160   var i, files = rcmail.env.attachments, list = [];
161
162   for (i in files) {
163     att = files[i];
164     if (att.complete && att.mimetype.indexOf('image/') == 0) {
165       list.push([att.name, rcmail.env.comm_path+'&_action=display-attachment&_file='+i+'&_id='+rcmail.env.compose_id]);
166     }
167   }
168
169   return list;
170 };