source: trunk/expressoMail1_2/js/rich_text_editor.js @ 1296

Revision 1296, 17.9 KB checked in by amuller, 15 years ago (diff)

Ticket #519 - Reutiliza formulario da inserção de imagens

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