]> git.donarmstrong.com Git - roundcube.git/blob - program/js/editor.js
Imported Upstream version 0.2~alpha
[roundcube.git] / program / js / editor.js
1 /*
2  +-----------------------------------------------------------------------+
3  | RoundCube editor js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
6  | Copyright (C) 2006, RoundCube Dev, - Switzerland                      |
7  | Licensed under the GNU GPL                                            |
8  |                                                                       |
9  +-----------------------------------------------------------------------+
10  | Author: Eric Stadtherr <estadtherr@gmail.com>                         |
11  +-----------------------------------------------------------------------+
12
13  $Id: editor.js 000 2006-05-18 19:12:28Z roundcube $
14 */
15
16 // Initialize the message editor
17
18 function rcmail_editor_init(skin_path, editor_lang)
19   {
20   tinyMCE.init({ mode : "textareas",
21                  editor_selector : "mce_editor",
22                  accessibility_focus : false,
23                  apply_source_formatting : true,
24                  theme : "advanced",
25                  language : editor_lang,
26                  plugins : "emotions,media,nonbreaking,table,searchreplace,visualchars,directionality",
27                  theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,outdent,indent,separator,link,unlink,emotions,charmap,code,forecolor,backcolor,fontselect,fontsizeselect, separator,undo,redo,image,media,ltr,rtl",
28                  theme_advanced_buttons2 : "",
29                  theme_advanced_buttons3 : "",
30                  theme_advanced_toolbar_location : "top",
31                  theme_advanced_toolbar_align : "left",
32                  extended_valid_elements : "font[face|size|color|style],span[id|class|align|style]",
33                  content_css : skin_path + "/editor_content.css",
34                  external_image_list_url : "editor_images.js"
35                });
36   }
37
38 // Toggle between the HTML and Plain Text editors
39
40 function rcmail_toggle_editor(toggler)
41   {
42   var selectedEditor = toggler.value;
43
44   // determine the currently displayed editor
45   var htmlFlag = document.getElementsByName('_is_html')[0];
46   var isHtml = htmlFlag.value;
47
48   if (((selectedEditor == 'plain') && (isHtml == "0")) ||
49       ((selectedEditor == 'html') && (isHtml == "1")))
50     {
51     return;
52     }
53
54   // do the appropriate conversion
55
56   var composeElement = document.getElementById('compose-body');
57
58   if (selectedEditor == 'html')
59     {
60     var existingPlainText = composeElement.value;
61     var htmlText = "<pre>" + existingPlainText + "</pre>";
62     composeElement.value = htmlText;
63     tinyMCE.execCommand('mceAddControl', true, 'compose-body');
64     htmlFlag.value = "1";
65     }
66   else
67     {
68     rcmail.set_busy(true, 'converting');
69     var thisMCE = tinyMCE.get('compose-body');
70     var existingHtml = thisMCE.getContent();
71     rcmail_html2plain(existingHtml);
72     tinyMCE.execCommand('mceRemoveControl', true, 'compose-body');
73     htmlFlag.value = "0";
74     }
75   }
76
77 function rcmail_html2plain(htmlText)
78   {
79   var http_request = new rcube_http_request();
80
81   http_request.onerror = function(o) { rcmail_handle_toggle_error(o); };
82   http_request.oncomplete = function(o) { rcmail_set_text_value(o); };
83   var url = rcmail.env.bin_path+'html2text.php';
84   console.log('HTTP request: ' + url);
85   http_request.POST(url, htmlText, 'application/octet-stream');
86   }
87
88 function rcmail_set_text_value(httpRequest)
89   {
90   rcmail.set_busy(false);
91   var composeElement = document.getElementById('compose-body');
92   composeElement.value = httpRequest.get_text();
93   }
94
95 function rcmail_handle_toggle_error(httpRequest)
96   {
97   alert('html2text request returned with error ' + httpRequest.xmlhttp.status);
98   }