source: trunk/expressoMail1_2/js/jscode/rich_text_editor.js @ 2521

Revision 2521, 18.0 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1009 - Correção de problemas no ExpressoMail? ao carregar imagens.

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