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

Revision 320, 16.7 KB checked in by niltonneto, 16 years ago (diff)

Versionamento 1.222
Ver changelog de alterações no Trac.

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