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

Revision 5134, 13.8 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo ExpressoMail?.

  • 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.saveFlag = 0;
7    this.signatures = false;
8    this.replyController = false;
9    this.newImageId = false;
10    this.plain = new Array;
11    this.editorReady = true;
12}
13
14// This code was written by Tyler Akins and has been placed in the
15// public domain.  It would be nice if you left this header intact.
16// Base64 code from Tyler Akins -- http://rumkin.com
17
18var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
19
20var ua = navigator.userAgent.toLowerCase();
21if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
22    var StringMaker = function () {
23        this.str = "";
24        this.length = 0;
25        this.append = function (s) {
26            this.str += s;
27            this.length += s.length;
28        }
29        this.prepend = function (s) {
30            this.str = s + this.str;
31            this.length += s.length;
32        }
33        this.toString = function () {
34            return this.str;
35        }
36    }
37} else {
38    var StringMaker = function () {
39        this.parts = [];
40        this.length = 0;
41        this.append = function (s) {
42            this.parts.push(s);
43            this.length += s.length;
44        }
45        this.prepend = function (s) {
46            this.parts.unshift(s);
47            this.length += s.length;
48        }
49        this.toString = function () {
50            return this.parts.join('');
51        }
52    }
53}
54
55cRichTextEditor.prototype.fromJSON = function( value )
56{
57    return (new Function( "return " + this.decode64( value )))();
58}
59
60cRichTextEditor.prototype.decode64 = function(input) {
61        if( typeof input === "undefined" ) return '';
62
63        var output = new StringMaker();
64        var chr1, chr2, chr3;
65        var enc1, enc2, enc3, enc4;
66        var i = 0;
67
68        // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
69        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
70
71        while (i < input.length) {
72                enc1 = keyStr.indexOf(input.charAt(i++));
73                enc2 = keyStr.indexOf(input.charAt(i++));
74                enc3 = keyStr.indexOf(input.charAt(i++));
75                enc4 = keyStr.indexOf(input.charAt(i++));
76
77                chr1 = (enc1 << 2) | (enc2 >> 4);
78                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
79                chr3 = ((enc3 & 3) << 6) | enc4;
80
81                output.append(String.fromCharCode(chr1));
82
83                if (enc3 != 64) {
84                        output.append(String.fromCharCode(chr2));
85                }
86                if (enc4 != 64) {
87                        output.append(String.fromCharCode(chr3));
88                }
89        }
90
91        return output.toString();
92}
93
94
95cRichTextEditor.prototype.loadEditor = function(ID) {
96       
97        var parentDiv = document.getElementById("body_position_" + ID);
98        var pObj = "body_" + ID;
99        var textArea = document.createElement("TEXTAREA");
100        textArea.id = pObj;
101        textArea.style.width = '100%';
102        parentDiv.appendChild(textArea);
103        RichTextEditor.plain[ID] = false;
104       
105        if(preferences.plain_text_editor == 1)
106          RichTextEditor.plain[ID] = true; 
107        else
108                  RichTextEditor.active(pObj);
109}
110
111cRichTextEditor.prototype.getSignaturesOptions = function() {
112       
113        if(RichTextEditor.signatures !== false)
114            return RichTextEditor.signatures;
115               
116        var signatures = this.fromJSON( preferences.signatures );
117        var signature_types = this.fromJSON( preferences.signature_types );
118
119        for( key in signatures )
120            if( !signature_types[key] )
121                signatures[key] = signatures[key].replace( /\n/g, "<br>" );
122                 
123        RichTextEditor.signatures = signatures;
124        return signatures;
125
126}
127
128cRichTextEditor.prototype.getSignatureDefault = function() {
129         
130        if(RichTextEditor.signatures === false)
131            RichTextEditor.signatures = RichTextEditor.getSignaturesOptions();
132         
133        if(!RichTextEditor.signatures || 
134           !RichTextEditor.signatures[preferences.signature_default || ""])
135        {
136          preferences.use_signature = "0"; //Desabilita o uso da assinatura
137          return '';
138        }
139       
140        return unescape(RichTextEditor.signatures[preferences.signature_default]);
141
142}
143
144cRichTextEditor.prototype.createImage = function(){
145        if (preferences.auto_save_draft == 1)
146        {
147                        autosave_time = 200000;
148                        clearTimeout(openTab.autosave_timer[currentTab]);
149        }
150               
151        var form = document.getElementById("attachment_window");
152       
153        if (form == null){
154                form = document.createElement("DIV");
155                form.id  = "attachment_window";
156                form.style.visibility = "hidden";
157                form.style.position = "absolute";
158                form.style.background = "#eeeeee";
159                form.style.left = "0px";
160                form.style.top  = "0px";
161                form.style.width = "0px";
162                form.style.height = "0px";
163                document.body.appendChild(form);
164        }
165                var form_upload = Element('form_upload');
166                if (form_upload == null)
167                        form_upload = document.createElement("DIV");
168                form_upload.id = "form_upload";
169                form_upload.style.position = "absolute";
170                form_upload.style.top = "5px";
171                form_upload.style.left = "5px";
172                form_upload.name = get_lang("Upload File");
173                form_upload.style.width = "450px";
174                form_upload.style.height = "75px";
175                form_upload.innerHTML = get_lang('Select the desired image file')+':<br>'+
176                '<input name="image_at" maxlength="255" size="40" id="inputFile_img" type="file"><br>' +
177                '<input title="' + get_lang('Include') + '"  value="' + get_lang('Include') + '"' +
178                'type="button" onclick="RichTextEditor.addInputFile();">&nbsp;' +
179                '<input title="' + get_lang('Close') + '"  value="' + get_lang('Close') + '"' +
180                ' type="button" onclick="win.close()">';
181                form.appendChild(form_upload);
182               
183                this.showWindow(form);
184}
185cRichTextEditor.prototype.showWindow = function (div){
186
187                if(! div) {
188                        return;
189                }
190               
191                if(! this.emwindow[div.id]) {
192                        div.style.width  =  div.firstChild.style.width;
193                        div.style.height = div.firstChild.style.height;
194                        div.style.zIndex = "10000";                     
195                        var title = div.firstChild.name;
196                        var wHeight = div.offsetHeight + "px";
197                        var wWidth =  div.offsetWidth   + "px";
198                        div.style.width = div.offsetWidth - 5;
199
200                        win = new dJSWin({
201                                id: 'win_'+div.id,
202                                content_id: div.id,
203                                width: wWidth,
204                                height: wHeight,
205                                title_color: '#3978d6',
206                                bg_color: '#eee',
207                                title: title,
208                                title_text_color: 'white',
209                                button_x_img: '../phpgwapi/images/winclose.gif',
210                                border: true});
211                       
212                        this.emwindow[div.id] = win;
213                        win.draw();
214                }
215                else
216                        win = this.emwindow[div.id];
217                win.open();     
218}
219cRichTextEditor.prototype.addInputFile = function()
220{
221        //Begin: Verify if the image extension is allowed.
222        var imgExtensions = new Array("jpeg", "jpg", "gif", "png", "bmp", "xbm", "tiff", "pcx");
223        var inputFile = document.getElementById('inputFile_img');       
224        if(!inputFile.value) return false;
225        var fileExtension = inputFile.value.split(".");
226        fileExtension = fileExtension[(fileExtension.length-1)];
227        var deniedExtension = true;
228        for(var i=0; i<imgExtensions.length; i++) {
229                if(imgExtensions[i].toUpperCase() == fileExtension.toUpperCase()) {
230                        deniedExtension = false;
231                        break;
232                }
233        }
234        if(deniedExtension) {
235                alert(get_lang('File extension forbidden or invalid file') + '.');
236                return false;
237        }
238        // End: Verify image extension.
239        var id =  currentTab;
240        divFiles = document.getElementById("divFiles_"+id);
241        var countDivFiles = divFiles.childNodes.length + 1;
242
243        var divFiles = document.getElementById('divFiles_'+id);
244        inputFile.id = 'inputFile_'+id +"_"+countDivFiles;
245        inputFile.name = 'file_'+countDivFiles;
246        divFile = document.createElement('DIV');
247        divFile.appendChild(inputFile);
248        divFiles.appendChild(divFile);
249
250        var form_upload = document.getElementById('form_upload');
251        form_upload.parentNode.removeChild(form_upload);
252        win.close();
253
254        RichTextEditor.saveFlag = 0; // See if save function finished
255        var save_link = document.getElementById("save_message_options_"+id);
256        save_link.onclick = function () {};
257        this.newImageId = new Date().getTime();
258        CKEDITOR.instances['body_'+id].insertHtml('<img id="'+this.newImageId+'" src=""/>');
259        save_msg(id,true);
260}
261
262cRichTextEditor.prototype.execPosInstance = function(inst) {
263         
264        var editor =  CKEDITOR.instances[inst];
265       
266       
267        var id = inst.replace('body_','');
268       
269        editor.document.on('keydown', function(event)
270        {
271            away = false;
272            var save_link = Element("save_message_options_"+id);
273            save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
274            save_link.className = 'message_options';
275        });
276       
277        //Adicionando atalhos de preferencias 
278//TODO: Não esta pegando o numero (keycode)
279//        if (preferences.use_shortcuts == '1')
280//        {
281//            CKEDITOR.instances[inst].document.on('keyup', function(event){
282//                    if(event.keyCode == 27){                         
283//                            delete_border(id,'false');}  //Tecla delete fechar aba         
284//            });
285//        } 
286       
287        // IM Module Enabled
288        if( window.parent.loadscript && loadscript.autoStatusIM )
289        {
290            CKEDITOR.instances[inst].document.on('keydown', function(event){
291                   loadscript.autoStatusIM;
292            });                 
293        }
294       
295        if (preferences.auto_save_draft == 1)
296        {
297            openTab.autosave_timer[id] = false;
298            var save_link = document.getElementById("save_message_options"+id);
299            CKEDITOR.instances[inst].document.on('keydown', function(event){
300               if (openTab.autosave_timer[id])
301                    clearTimeout(openTab.autosave_timer[id]);
302               openTab.autosave_timer[id] = setTimeout("save_msg("+id+")", autosave_time);
303                       
304            }); 
305        }
306        /////////////////////////////////////////////////////////////////////////////////////
307       
308   
309       
310       RichTextEditor.editorReady = true;
311}
312cRichTextEditor.prototype.setPlain = function (active,id){
313      RichTextEditor.plain[id] = active;
314      if(active === true)
315      {
316            CKEDITOR.instances['body_'+id].destroy();
317            var height = document.body.scrollHeight;
318            height -= 330;
319            $('#body_'+id).height(height);
320            $('#body_'+id).keydown(function(event) {
321                away = false;
322                var save_link = Element("save_message_options_"+id);
323                save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
324                save_link.className = 'message_options';
325            });
326
327      }   
328      else
329          RichTextEditor.active('body_'+id);
330}
331
332cRichTextEditor.prototype.getData = function (inst){ 
333    var id = inst.replace('body_','');
334   
335    if(RichTextEditor.plain[id] === true)
336        return $('#'+inst).val();
337    else
338        return CKEDITOR.instances[inst].getData();
339}
340cRichTextEditor.prototype.setData = function (id,data){
341   
342     if(this.plain[id] === true)
343        $('#'+id).val(data);
344     else
345        CKEDITOR.instances[id].setData(data)
346}
347cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion){
348   
349    if(recursion === undefined) recursion = 1;
350    else recursion++;   
351   
352     if(this.plain[id] === true)
353     {
354                 if($('#'+id) !== undefined)
355                        $('#'+id).val(data);
356         else
357                          setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
358         } 
359     else
360     {
361       if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined )   
362       {
363           var editor =   CKEDITOR.instances['body_'+id];   
364           editor.insertHtml('');
365           var selection = editor.getSelection();
366           if(selection !== undefined && selection !== null) var selectionRanges = selection.getRanges();
367
368           var fontSize = '';
369           var fontFamily = '';
370           
371           if(typeof(preferences.font_size_editor) !== 'undefined')
372               fontSize = 'font-size:' + preferences.font_size_editor;
373               
374           if(fontSize != '')
375               fontFamily = ';'
376           
377           if(typeof(preferences.font_family_editor) !== 'undefined')
378               fontFamily += 'font-family:' + preferences.font_family_editor + ';';
379           
380           var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>';
381         
382           editor.insertHtml(divBr+divBr+divBr+data);
383           
384           if(selection !== null) selection.selectRanges(selectionRanges);
385         
386          //Caso não for uma resposta votla o foco para o input to
387          if(reply === undefined)   
388                  setTimeout("$('#to_"+id+"').focus()",100);
389 
390       }
391       else if(recursion < 20)
392           setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
393     }
394}
395
396cRichTextEditor.prototype.destroy = function(id)
397{
398        //Remove Instancia do editor
399        if( CKEDITOR.instances[id] !== undefined )   
400             CKEDITOR.remove(CKEDITOR.instances[id]);
401}
402cRichTextEditor.prototype.active = function(id)
403{
404   
405   //Remove Instancia do editor caso ela exista
406    if( CKEDITOR.instances[id] !== undefined )   
407         CKEDITOR.remove(CKEDITOR.instances[id]);
408     
409    var height = document.body.scrollHeight;
410    height -= 425;
411     
412     $('#'+id).ckeditor(
413          function() {RichTextEditor.execPosInstance(id)},
414          {
415              toolbar:'mail',
416              height: height
417          });
418
419}
420cRichTextEditor.prototype.focus = function(id)
421{
422    if(RichTextEditor.plain[id]  === true)
423        $('#body_'+id).focus();
424    else
425        CKEDITOR.instances['body_'+id].focus();
426
427}
428//Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas
429cRichTextEditor.prototype.setEditable = function(id) {
430        if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
431        var element = CKEDITOR.instances['body_'+ id].document.getBody();
432        element.removeAttribute('contentEditable');
433        element.setAttribute('contentEditable','true');
434}
435//Build the Object
436RichTextEditor = new cRichTextEditor();
Note: See TracBrowser for help on using the repository browser.