Thomas Bruederli
2012-11-17 6ddb16d181e285d4f0ef0ef55bdd0ba787f1b583
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',
90550b 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 {
c288f9 77   var elem = rcube_find_object('_from'),
A 78     fe = rcmail.env.compose_focus_elem;
79
7e263e 80   if (rcmail.env.default_font)
A 81     $(tinyMCE.get(rcmail.env.composebody).getBody()).css('font-family', rcmail.env.default_font);
82
762565 83   if (elem && elem.type == 'select-one' && !rcmail.env.opened_extwin) {
1f019c 84     rcmail.change_identity(elem);
882b0f 85     // Focus previously focused element
43fb35 86     if (fe && fe.id != rcmail.env.composebody) {
ad9dac 87       // use setTimeout() for IE9 (#1488541)
AM 88       window.setTimeout(function() {
89         window.focus(); // for WebKit (#1486674)
90         fe.focus();
91       }, 10);
43fb35 92     }
882b0f 93   }
c288f9 94
5cff85 95   // set tabIndex and set focus to element that was focused before
c288f9 96   rcmail_editor_tabindex(fe && fe.id == rcmail.env.composebody);
eb9eff 97   // Trigger resize (needed for proper editor resizing in some browsers using default skin)
A 98   $(window).resize();
665cc5 99 }
A 100
101 // set tabIndex on tinyMCE editor
5cff85 102 function rcmail_editor_tabindex(focus)
665cc5 103 {
A 104   if (rcmail.env.task == 'mail') {
a01b3b 105     var editor = tinyMCE.get(rcmail.env.composebody);
4cdc70 106     if (editor) {
A 107       var textarea = editor.getElement();
108       var node = editor.getContentAreaContainer().childNodes[0];
109       if (textarea && node)
110         node.tabIndex = textarea.tabIndex;
5cff85 111       if (focus)
59c404 112         editor.getBody().focus();
4cdc70 113     }
665cc5 114   }
b8ae50 115 }
A 116
117 // switch html/plain mode
5821ff 118 function rcmail_toggle_editor(select, textAreaId, flagElement)
b8ae50 119 {
5821ff 120   var flag, ishtml;
A 121
122   if (select.tagName != 'SELECT')
123     ishtml = select.checked;
124   else
125     ishtml = select.value == 'html';
b8ae50 126
3940ba 127   var res = rcmail.command('toggle-editor', {id:textAreaId, mode:ishtml?'html':'plain'});
832594 128
3940ba 129   if (ishtml) {
4cdc70 130     // #1486593
5cff85 131     setTimeout("rcmail_editor_tabindex(true);", 500);
b8ae50 132     if (flagElement && (flag = rcube_find_object(flagElement)))
A 133       flag.value = '1';
087c7d 134   }
14d494 135   else if (res) {
b8ae50 136     if (flagElement && (flag = rcube_find_object(flagElement)))
A 137       flag.value = '0';
5cff85 138
bdf6de 139     if (rcmail.env.composebody)
A 140       rcube_find_object(rcmail.env.composebody).focus();
087c7d 141   }
14d494 142   else { // !res
A 143     if (select.tagName == 'SELECT')
144       select.value = 'html';
145     else if (select.tagName == 'INPUT')
146       select.checked = true;
147   }
087c7d 148 }
8f142e 149
A 150 // editor callbeck for images listing
151 function rcmail_editor_images()
152 {
153   var i, files = rcmail.env.attachments, list = [];
154
155   for (i in files) {
156     att = files[i];
157     if (att.complete && att.mimetype.indexOf('image/') == 0) {
158       list.push([att.name, rcmail.env.comm_path+'&_action=display-attachment&_file='+i+'&_id='+rcmail.env.compose_id]);
159     }
160   }
161
162   return list;
163 };