source: branches/2.2/expressoMail1_2/js/rich_text_editor.js @ 3130

Revision 3130, 19.1 KB checked in by niltonneto, 14 years ago (diff)

Ticket #1111 - Corrigido problema ao editar/imprimir mensagens com tags <pre>.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1function cRichTextEditor(){
2        this.emwindow   = new Array;
3        this.editor = "body_1";
4        this.table = "";
5        this.id = "1";
6        this.buildEditor();
7        this.saveFlag = 0;
8}
9
10cRichTextEditor.prototype.loadEditor = function(ID) {
11        var _this = this;
12        _this.id = ID;
13        parentDiv = document.getElementById("body_position_"+this.id);
14        this.editor = "body_"+this.id;
15
16        if(this.table.parentNode)
17                this.table.parentNode.removeChild(this.table);
18       
19        if( parentDiv.firstChild )
20        {
21                if (!parentDiv.firstChild.hasChildNodes())
22                        parentDiv.insertBefore(this.table,parentDiv.firstChild);
23        }
24        else
25                parentDiv.appendChild(this.table);
26
27        if(!Element(this.editor))
28        {
29                this.createElementEditor(this.editor);
30        }
31        else
32        {
33                Element("viewsource_rt_checkbox").checked=false;
34        }
35
36        document.getElementById('fontname').selectedIndex = 0;
37        document.getElementById('fontsize').selectedIndex = 0;
38}
39
40cRichTextEditor.prototype.createElementEditor = function(pObj)
41{
42                iframe = document.createElement("IFRAME");
43                iframe.id = pObj;
44                iframe.name = pObj;
45                iframe.width = "99%";
46                iframe.height = 300;
47                iframe.setAttribute("unselectable","on");
48                iframe.setAttribute("tabIndex","1");
49
50                config_events( iframe, 'onload', function( )
51                {
52                        iframe.contentWindow.document.designMode = "on";
53                        if ( iframe.contentWindow.document.documentElement )
54                                iframe.contentWindow.document.documentElement.style.background = '#fff';
55                });
56
57                var checkbox = document.createElement("INPUT");
58                checkbox.id = 'viewsource_rt_checkbox';
59                checkbox.type = "checkbox";
60                checkbox.setAttribute("tabIndex","-1");
61                checkbox.onclick = function () {RichTextEditor.viewsource(this.checked)};
62                var text = document.createTextNode(get_lang('View HTML source') + '.');
63                parentDiv.appendChild(iframe);
64                parentDiv.appendChild(checkbox);
65                parentDiv.appendChild(text);
66}
67
68cRichTextEditor.prototype.loadStyle = function(tag, css_file) {
69        var theRules = new Array();
70        var stylePRE = "";     
71        for(var s = 0; s < document.styleSheets.length; s++) {
72                if(document.styleSheets[s].href != null &&
73                                document.styleSheets[s].href.match("templates/"+template+"/"+css_file)){                       
74                        if (document.styleSheets[s].cssRules)
75                                theRules = document.styleSheets[s].cssRules;
76                        else if (document.styleSheets[s].rules)
77                                theRules = document.styleSheets[s].rules;
78                        break;
79                }
80        }
81        for(var s = 0;s < theRules.length; s++){
82                if(theRules[s].selectorText.toLowerCase() == tag.toLowerCase()){                       
83                        stylePRE = theRules[s].style;
84                        break;
85                }
86        }
87        var _body = Element(this.editor);
88        var i_doc = (document.all) ? _body.contentWindow.document: _body.contentDocument;
89        var hh1 = i_doc.getElementsByTagName('head')[0];
90        // For IE
91        if(typeof(hh1) == 'undefined'){
92                hh1 = i_doc.createElement("head");
93                i_doc.appendChild(hh1);
94        }
95        var ss1 = i_doc.createElement('style');
96        ss1.setAttribute("type", "text/css");
97        var def = tag.toLowerCase()+' {'+stylePRE.cssText+'}';
98        if (ss1.styleSheet) {
99            ss1.styleSheet.cssText = def;
100        } else {
101            var tt1 = i_doc.createTextNode(def);
102            ss1.appendChild(tt1);
103        }
104        hh1.appendChild(ss1);
105}
106
107cRichTextEditor.prototype.viewsource = function(source) {
108        var html;
109        var mainField = document.getElementById(this.editor).contentWindow;
110        if (source) {
111                if (is_ie){
112                        connector.loadScript('html2xhtml');
113                        html = frames[this.editor].document.body;
114                        var xhtml = get_xhtml(html, 'en', 'iso-8859-1');
115                        frames[this.editor].document.body.innerText = xhtml;
116                        document.getElementById("table_richtext_toolbar").style.visibility="hidden";
117                }
118                else{
119                        html = document.createTextNode(document.getElementById(this.editor).contentWindow.document.body.innerHTML);
120                        document.getElementById(this.editor).contentWindow.document.body.innerHTML = "";
121                        html = document.getElementById(this.editor).contentWindow.document.importNode(html,false);
122                        document.getElementById(this.editor).contentWindow.document.body.appendChild(html);
123                        document.getElementById("table_richtext_toolbar").style.visibility="hidden";
124                }               
125        } else {
126                if (is_ie){
127                        var output = escape(frames[this.editor].document.body.innerText);
128                        output = output.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
129                        output = output.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
130                        frames[this.editor].document.body.innerHTML = unescape(output);
131                        document.getElementById("table_richtext_toolbar").style.visibility="visible"; 
132                }
133                else{
134                        html = document.getElementById(this.editor).contentWindow.document.body.ownerDocument.createRange();
135                        html.selectNodeContents(document.getElementById(this.editor).contentWindow.document.body);
136                        document.getElementById(this.editor).contentWindow.document.body.innerHTML = html.toString();
137                        document.getElementById("table_richtext_toolbar").style.visibility="visible"; 
138                }
139        }
140}
141
142cRichTextEditor.prototype.buildEditor = function() {
143        this.table = document.createElement("TABLE");
144        this.table.id = "table_richtext_toolbar";
145        this.table.className = "richtext_toolbar";
146        this.table.width = "100%";
147        var tbody = document.createElement("TBODY");
148        var tr = document.createElement("TR");
149        var td = document.createElement("TD");
150        var div_button_rt = document.createElement("DIV");
151       
152        selectBox=document.createElement("SELECT");
153        selectBox.id="fontname";
154        selectBox.setAttribute("tabIndex","-1");
155        selectBox.onchange = function () {RichTextEditor.Select("fontname");};
156        selectBox.className = 'select_richtext';
157        var option1 = new Option(get_lang('Font'), 'Font');
158        var option2 = new Option('Arial', 'Arial');
159        var option3 = new Option('Courier', 'Courier');
160        var option4 = new Option('Times New Roman', 'Times');
161        if (is_ie){
162                selectBox.add(option1);
163                selectBox.add(option2);
164                selectBox.add(option3);
165                selectBox.add(option4);
166        }
167        else{
168                selectBox.add(option1, null);
169                selectBox.add(option2, null);
170                selectBox.add(option3, null);
171                selectBox.add(option4, null);
172        }
173        div_button_rt.appendChild(selectBox);
174
175        selectBox=document.createElement("SELECT");
176        selectBox.id="fontsize";
177        selectBox.setAttribute("tabIndex","-1");
178        selectBox.setAttribute("unselectable","on");
179        selectBox.className = 'select_richtext';
180        selectBox.onchange = function () {RichTextEditor.Select("fontsize");};
181        var option1 = new Option(get_lang('Size'), 'Size');
182        var option2 = new Option('1 (8 pt)','1' );
183        var option3 = new Option('2 (10 pt)','2');
184        var option4 = new Option('3 (12 pt)','3');
185        var option5 = new Option('4 (14 pt)','4');
186        var option6 = new Option('5 (18 pt)','5');
187        var option7 = new Option('6 (24 pt)','6');
188        var option8 = new Option('7 (36 pt)','7');
189        if (is_ie){
190                selectBox.add(option1);
191                selectBox.add(option2);
192                selectBox.add(option3);
193                selectBox.add(option4);
194                selectBox.add(option5);
195                selectBox.add(option6);
196                selectBox.add(option7);
197                selectBox.add(option8);
198        }
199        else{
200                selectBox.add(option1, null);
201                selectBox.add(option2, null);
202                selectBox.add(option3, null);
203                selectBox.add(option4, null);
204                selectBox.add(option5, null);
205                selectBox.add(option6, null);
206                selectBox.add(option7, null);
207                selectBox.add(option8, null);   
208        }
209        div_button_rt.appendChild(selectBox);
210       
211        var buttons = ['bold', 'italic', 'underline', 'forecolor', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull',
212                                   'undo', 'redo', 'insertorderedlist', 'insertunorderedlist', 'outdent', 'indent', 'link', 'image', 'table', 'signature'];
213
214        for (var i=0; i<buttons.length; i++){
215                var img = document.createElement("IMG");
216                img.id = buttons[i];
217                img.className = 'imagebutton';
218                img.align = 'center';
219                img.src = './templates/'+template+'/images/'+buttons[i]+'.gif';
220                img.title = get_lang(buttons[i]);
221                img.style.cursor = 'pointer';
222
223                if (buttons[i] == 'forecolor')
224                        img.onclick = function () {RichTextEditor.show_pc('forecolor')};
225                else if (buttons[i] == 'link')
226                        img.onclick = function () {RichTextEditor.createLink();};
227                else if (buttons[i] == 'image')
228                        img.onclick = function () {RichTextEditor.createImage();};
229                else if (buttons[i] == 'table')
230                        img.onclick = function () {RichTextEditor.createTable();};
231                else
232                        img.onclick = function () {RichTextEditor.editorCommand(this.id,'');};
233               
234                img.onmouseover = function () {this.style.border="outset 2px";};
235                img.onmouseout = function () {this.style.border="solid 2px #C0C0C0";};
236                div_button_rt.appendChild(img);
237        }
238
239        td.appendChild(div_button_rt);
240        tr.appendChild(td);
241        tbody.appendChild(tr);
242        this.table.appendChild(tbody);
243}
244
245cRichTextEditor.prototype.editorCommand = function(command, option) {
246        try {
247                var mainField = document.getElementById(this.editor).contentWindow;
248                mainField.focus();
249                var signature = preferences.type_signature == 'html' ? preferences.signature : preferences.signature.replace(/\n/g, "<br>");
250                if (command == 'signature'){
251                        if (is_ie){
252                                var sel = document.selection;
253                                if (sel!=null)
254                                {
255                                    var rng = sel.createRange();
256                                    if (rng!=null)
257                                        rng.pasteHTML(signature);
258                                }
259                        }
260                        else{
261                                mainField.document.execCommand('inserthtml', false, signature);
262                        }
263                }
264                else if (command == 'CreateLink')
265                        mainField.document.execCommand('CreateLink', false, option);
266                else if (command == 'Table'){
267                        if (is_ie){
268                                var sel = document.selection;
269                                if (sel!=null)
270                                {
271                                    var rng = sel.createRange();
272                                    if (rng!=null)
273                                rng.pasteHTML(option);
274                                }
275                        }
276                        else
277                                mainField.document.execCommand('inserthtml', false, option);
278                        }
279                else if (command == 'Image')
280                        mainField.document.execCommand('InsertImage', false, option);
281                else
282                        mainField.document.execCommand(command, false, option);
283                //mainField.focus();
284    } catch (e) {/* alert(e);*/ }
285}
286
287cRichTextEditor.prototype.createLink = function(){
288        var mainField = document.getElementById(this.editor).contentWindow;
289        if (is_ie){
290                if ((mainField.document.selection.createRange().text) == ''){
291                                alert(get_lang('Chose the text you want transform in link before.'));
292                        return;
293                }
294        }
295        else{
296                if (mainField.window.getSelection() == ''){
297                                alert(get_lang('Chose the text you want transform in link before.'));
298                        return;
299                }
300        }
301                var szURL = prompt(get_lang('Enter with link URL:'), 'http://');
302        if ((szURL != null) && (szURL != "")){
303                this.editorCommand("CreateLink", szURL);
304        }
305}
306
307// It include the image file in emails body
308// It saves and attach in drafts folder and open it
309cRichTextEditor.prototype.addInputFile = function()
310{
311        //Begin: Verify if the image extension is allowed.
312        var imgExtensions = new Array("jpeg", "jpg", "gif", "png", "bmp", "xbm", "tiff", "pcx");
313        var inputFile = document.getElementById('inputFile_img');       
314        if(!inputFile.value) return false;
315        var fileExtension = inputFile.value.split(".");
316        fileExtension = fileExtension[(fileExtension.length-1)];
317        var deniedExtension = true;
318        for(var i=0; i<imgExtensions.length; i++) {
319                if(imgExtensions[i].toUpperCase() == fileExtension.toUpperCase()) {
320                        deniedExtension = false;
321                        break;
322                }
323        }
324        if(deniedExtension) {
325                alert(get_lang('File extension forbidden or invalid file') + '.');
326                return false;
327        }
328        // End: Verify image extension.
329        var id = this.editor.substr(5); // border_id
330        divFiles = document.getElementById("divFiles_"+id);
331        var countDivFiles = divFiles.childNodes.length + 1;
332
333        var divFiles = document.getElementById('divFiles_'+id);
334        inputFile.id = 'inputFile_'+id +"_"+countDivFiles;
335        inputFile.name = 'file_'+countDivFiles;
336        divFile = document.createElement('DIV');
337        divFile.appendChild(inputFile);
338        divFiles.appendChild(divFile);
339
340        var form_upload = document.getElementById('form_upload');
341        form_upload.parentNode.removeChild(form_upload);
342        win.close();
343
344        RichTextEditor.saveFlag = 0; // See if save function finished
345        var save_link = document.getElementById("save_message_options_"+id);
346        //save_link.onclick = function () {};
347        save_msg(id,true);
348        setTimeout("RichTextEditor.insertImgHtml("+id+")",1000);
349}
350
351cRichTextEditor.prototype.insertImgHtml = function (id){
352        if (RichTextEditor.saveFlag == 0)
353                setTimeout("RichTextEditor.insertImgHtml("+id+")",500);
354        else
355                if (RichTextEditor.saveFlag == 1)
356                        this.editorCommand('Image', './inc/show_embedded_attach.php?msg_folder=INBOX/'+draftsfolder+'&msg_num='+openTab.imapUid[id]+'&msg_part='+(openTab.countFile[id]+1));
357                        // this.editorCommand('Image', '.inc/gotodownload.php?msg_folder="+msg_folder+"&msg_number="+msg_number+"&idx_file="+idx_file+"&msg_part="+msg_part+params'));
358}
359
360cRichTextEditor.prototype.insertTableHtml = function (){
361        var id = this.editor.substr(5); // border_id
362        var     rows = document.getElementById('rows').value;
363        var     cols = document.getElementById('cols').value;
364        var border = document.getElementById('border').value;
365        var insertTable = '<table border="'+border+'px"><tbody>';
366        for (var i = 0; i < rows; i++){
367                insertTable += "<tr>"; 
368                for (var j = 0; j < cols; j++)
369                        insertTable += "<td>&nbsp;</td>";       
370                insertTable += "</tr>";
371        }
372        insertTable += "</tbody></table>";
373        this.editorCommand('Table', insertTable);
374}
375
376cRichTextEditor.prototype.createTable = function(){
377        var form = document.getElementById("table_window");
378        if (form == null){
379                form = document.createElement("DIV");
380                form.id  = "table_window";
381                form.style.visibility = "hidden";
382                form.style.position = "absolute";
383                form.style.background = "#eeeeee";
384                form.style.left = "0px";
385                form.style.top  = "0px";
386                form.style.width = "0px";
387                form.style.height = "0px";
388                document.body.appendChild(form);
389        }
390               
391                var form_table = document.createElement("DIV");
392                form_table.id = "form_table";
393                form_table.style.position = "absolute";
394                form_table.style.top = "5px";
395                form_table.style.left = "5px";
396                form_table.style.width = "190px";
397                form_table.style.height = "90px";
398                form_table.name = get_lang("Insert Table");             
399                form_table.innerHTML = get_lang('Select the table size')+':<br><br><table cellspacing="0"><tbody><tr><td align="center">'+
400                                                                get_lang('Rows')+':</td><td></td><td align="center">'+get_lang('Cols')+':</td><td></td><td align="center">'+get_lang('Border')+':</td></tr>'+
401                                                                        '<tr><td align="right"><input type="text" readonly="true" id="rows" size="2" maxlength="2" value="1"></input></td><td align="left"><img src="templates/'+template+'/images/plus.png" onclick="javascript:RichTextEditor.incrementField(\'rows\');"></img><br><img src="templates/'+template+'/images/minus.png" onclick="javascript:RichTextEditor.decrementField(\'rows\');"></img></td>'+
402                                                                        '<td align="right"><input type="text" readonly="true" id="cols" size="2" maxlength="2" value="1"></input></td><td align="left"><img src="templates/'+template+'/images/plus.png" onclick="javascript:RichTextEditor.incrementField(\'cols\');"></img><br><img src="templates/'+template+'/images/minus.png" onclick="javascript:RichTextEditor.decrementField(\'cols\');"></img></td>'+
403                                                                        '<td align="right"><input type="text" readonly="true" id="border" size="2" maxlength="2" value="1"></input></td><td align="left"><img src="templates/'+template+'/images/plus.png" onclick="javascript:RichTextEditor.incrementField(\'border\');"></img><br><img src="templates/'+template+'/images/minus.png" onclick="javascript:RichTextEditor.decrementField(\'border\');"></img></td>'+
404                                                                        '</tr></tbody></table>'+
405                                                                        '&nbsp;&nbsp;&nbsp;<input title="'+get_lang('Close')+'"  value="' + get_lang('Close') + '" type="button" onclick="win.close()">&nbsp;'+
406                                                                        '<input title="' + get_lang('Include') + '"  value="' + get_lang('Include') + '" type="button" onclick="RichTextEditor.insertTableHtml();win.close();">';       
407                form.appendChild(form_table);
408               
409                this.showWindow(form);
410                }
411
412cRichTextEditor.prototype.incrementField = function(id_val){
413        var field_text = document.getElementById(id_val);
414        field_text.value = parseInt(field_text.value)+1;
415}
416
417cRichTextEditor.prototype.decrementField = function(id_val){
418        var field_text = document.getElementById(id_val);
419        if (parseInt(field_text.value) > 0)
420                field_text.value = parseInt(field_text.value)-1;
421}
422
423cRichTextEditor.prototype.createImage = function(){
424        if (preferences.auto_save_draft == 1){
425                        autosave_time = 200000;
426                        clearTimeout(openTab.autosave_timer[currentTab]);
427                }
428        var form = document.getElementById("attachment_window");
429        if (form == null){
430                form = document.createElement("DIV");
431                form.id  = "attachment_window";
432                form.style.visibility = "hidden";
433                form.style.position = "absolute";
434                form.style.background = "#eeeeee";
435                form.style.left = "0px";
436                form.style.top  = "0px";
437                form.style.width = "0px";
438                form.style.height = "0px";
439                document.body.appendChild(form);
440        }
441                var form_upload = Element('form_upload');
442                if (form_upload == null)
443                        form_upload = document.createElement("DIV");
444                form_upload.id = "form_upload";
445                form_upload.style.position = "absolute";
446                form_upload.style.top = "5px";
447                form_upload.style.left = "5px";
448                form_upload.name = get_lang("Upload File");
449                form_upload.style.width = "450px";
450                form_upload.style.height = "75px";
451                form_upload.innerHTML = get_lang('Select the desired image file')+':<br>'+
452                                                                '<input name="image_at" maxlength="255" size="50" id="inputFile_img" type="file"><br>' +
453                                                                '<input title="' + get_lang('Include') + '"  value="' + get_lang('Include') + '"' + 'type="button" onclick="RichTextEditor.addInputFile();">&nbsp;' +
454                                                                '<input title="' + get_lang('Close') + '"  value="' + get_lang('Close') + '"' +
455                                                                ' type="button" onclick="win.close()">';
456                form.appendChild(form_upload);
457               
458                this.showWindow(form);
459}
460cRichTextEditor.prototype.showWindow = function (div){
461
462                if(! div) {
463                        return;
464                }
465               
466                if(! this.emwindow[div.id]) {
467                        div.style.width  =  div.firstChild.style.width;
468                        div.style.height = div.firstChild.style.height;
469                        div.style.zIndex = "10000";                     
470                        var title = div.firstChild.name;
471                        var wHeight = div.offsetHeight + "px";
472                        var wWidth =  div.offsetWidth   + "px";
473                        div.style.width = div.offsetWidth - 5;
474
475                        win = new dJSWin({
476                                id: 'win_'+div.id,
477                                content_id: div.id,
478                                width: wWidth,
479                                height: wHeight,
480                                title_color: '#3978d6',
481                                bg_color: '#eee',
482                                title: title,
483                                title_text_color: 'white',
484                                button_x_img: '../phpgwapi/images/winclose.gif',
485                                border: true });
486                       
487                        this.emwindow[div.id] = win;
488                        win.draw();
489                }
490                else
491                        win = this.emwindow[div.id];
492                win.open();     
493}
494
495cRichTextEditor.prototype.Select = function(selectname)
496{
497        var mainField = Element(this.editor).contentWindow;
498        var cursel = document.getElementById(selectname).selectedIndex;
499
500        if (cursel != 0) {
501                var selected = document.getElementById(selectname).options[cursel].value;
502                mainField.document.execCommand(selectname, false, selected);
503                document.getElementById(selectname).selectedIndex = cursel;
504        }
505        mainField.focus();
506}
507
508cRichTextEditor.prototype.show_pc = function(command)
509{
510        connector.loadScript("color_palette");
511        ColorPalette.loadPalette(this.id);
512        if (ColorPalette.div.style.visibility != "visible")
513                ColorPalette.div.style.visibility="visible";
514        else
515                this.hide_pc();
516}
517
518cRichTextEditor.prototype.hide_pc = function()
519{
520        document.getElementById("palettecolor").style.visibility="hidden";
521}
522
523cRichTextEditor.prototype.getOffsetTop = function(elm) {
524  var mOffsetTop = elm.offsetTop;1
525  var mOffsetParent = elm.offsetParent;
526  while(mOffsetParent){
527    mOffsetTop += mOffsetParent.offsetTop;
528    mOffsetParent = mOffsetParent.offsetParent;
529  }
530  return mOffsetTop;
531}
532
533cRichTextEditor.prototype.getOffsetLeft = function(elm) {
534  var mOffsetLeft = elm.offsetLeft;
535  var mOffsetParent = elm.offsetParent;
536  while(mOffsetParent){
537    mOffsetLeft += mOffsetParent.offsetLeft;
538    mOffsetParent = mOffsetParent.offsetParent;
539  }
540  return mOffsetLeft;
541}
542
543//Build the Object
544RichTextEditor = new cRichTextEditor();
Note: See TracBrowser for help on using the repository browser.