source: companies/serpro/expressoMail1_2/js/rich_text_editor.js @ 903

Revision 903, 17.4 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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