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

Revision 8160, 13.1 KB checked in by angelo, 11 years ago (diff)

Ticket #3453 - Problema ao inserir assinatura automaticamente no Expresso Mail

  • 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        if(!value)
58                return '';
59        return (new Function( "return " + this.decode64( value )))();
60}
61
62cRichTextEditor.prototype.decode64 = function(input) {
63        if( typeof input === "undefined" ) return '';
64
65        var output = new StringMaker();
66        var chr1, chr2, chr3;
67        var enc1, enc2, enc3, enc4;
68        var i = 0;
69
70        // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
71        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
72
73        while (i < input.length) {
74                enc1 = keyStr.indexOf(input.charAt(i++));
75                enc2 = keyStr.indexOf(input.charAt(i++));
76                enc3 = keyStr.indexOf(input.charAt(i++));
77                enc4 = keyStr.indexOf(input.charAt(i++));
78
79                chr1 = (enc1 << 2) | (enc2 >> 4);
80                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
81                chr3 = ((enc3 & 3) << 6) | enc4;
82
83                output.append(String.fromCharCode(chr1));
84
85                if (enc3 != 64) {
86                        output.append(String.fromCharCode(chr2));
87                }
88                if (enc4 != 64) {
89                        output.append(String.fromCharCode(chr3));
90                }
91        }
92
93        return output.toString();
94}
95
96
97cRichTextEditor.prototype.loadEditor = function(ID) {
98       
99        var parentDiv = document.getElementById("body_position_" + ID);
100        var pObj = "body_" + ID;
101        var textArea = document.createElement("TEXTAREA");
102        textArea.id = pObj;
103        textArea.style.width = '100%';
104        parentDiv.appendChild(textArea);
105        RichTextEditor.plain[ID] = false;
106       
107        if(preferences.plain_text_editor == 1)
108                {
109                        RichTextEditor.plain[ID] = true; 
110                        RichTextEditor.editorReady = true;
111                }
112        else
113                        RichTextEditor.active(pObj);
114}
115
116cRichTextEditor.prototype.loadEditor2 = function(ID) {     
117                var pObj = "body_" + ID;
118        RichTextEditor.plain[ID] = false;
119       
120        if(preferences.plain_text_editor == 1)
121                {
122                        RichTextEditor.plain[ID] = true; 
123                        RichTextEditor.editorReady = true;
124                }
125        else
126                        RichTextEditor.active(pObj);
127}
128
129
130cRichTextEditor.prototype.getSignaturesOptions = function() {
131       
132    if(RichTextEditor.signatures !== false)
133        return RichTextEditor.signatures;
134               
135        var signatures = RichTextEditor.normalizerSignature(this.fromJSON( preferences.signatures ));
136        var signature_types = RichTextEditor.normalizerSignature(this.fromJSON( preferences.signature_types ));
137
138        for( key in signatures )
139            if( !signature_types[key] )
140                    signatures[key] = signatures[key].replace( /\n/g, "<br>" );
141
142    RichTextEditor.signatures = signatures;
143    return signatures;
144
145}
146cRichTextEditor.prototype.normalizerSignature = function(values) {
147
148    var value = {};
149
150    for (key in values){
151
152        value[RichTextEditor.isEncoded64(key) ? RichTextEditor.decode64(key) : key] = values[key];
153    }
154
155    return value;
156
157}
158
159/*Verifica se a string input esta em Base64*/
160cRichTextEditor.prototype.isEncoded64 = function(input){
161var baseStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
162var encoded = true;
163        if ( (input.length % 4) != 0)
164                return false;
165        for(var i=0; i<input.length; i++){
166                if ( baseStr.indexOf(input[i]) < 0 ){
167                        encoded = false;
168                        break;
169                }
170        }
171        return encoded;
172}
173
174cRichTextEditor.prototype.getSignatureDefault = function() {
175
176    if(RichTextEditor.signatures === false){
177        RichTextEditor.signatures = RichTextEditor.getSignaturesOptions();
178        preferences.signature_default = preferences.signature;
179    }
180         
181    if(!RichTextEditor.signatures || !preferences.signature_default)
182    {
183      preferences.use_signature = "0"; //Desabilita o uso da assinatura
184      return '';
185    }
186
187    return unescape(preferences.signature_default);
188
189}
190
191
192cRichTextEditor.prototype.execPosInstance = function(inst) {
193     if(RichTextEditor.editorReady === false)
194     {
195        var editor =  CKEDITOR.instances[inst];
196        var id = inst.replace('body_','');
197        var content = $("#content_id_"+id)
198        editor.document.on('keydown', function(event)
199        {
200                away = false;
201                var save_link = content.find(".save");
202                save_link.unbind("click").click(function(){
203                        openTab.toPreserve[id] = true;save_msg(id);
204                });
205                save_link.button({ disabled: false });
206        });
207       
208       
209        // IM Module Enabled
210        if( window.parent.loadscript && loadscript.autoStatusIM )
211        {
212                CKEDITOR.instances[inst].document.on('keydown', function(event){
213                        loadscript.autoStatusIM;
214                });             
215        }
216
217        if (preferences.auto_save_draft == 1)
218        {
219            autoSaveControl.status[id] = true;
220            autoSaveControl.timer[id] = window.setInterval( "autoSave("+id+")" ,autosave_time); 
221
222            CKEDITOR.instances[inst].document.on('keydown', function(event){   
223                autoSaveControl.status[id] = false;
224            })
225        }
226       
227        $(".cke_editor").css("white-space", "normal");
228
229    if(typeof(preferences.font_size_editor) !== 'undefined')
230        $(editor.document.$.body).css("font-size",preferences.font_size_editor);
231    if(typeof(preferences.font_family_editor) !== 'undefined')
232        $(editor.document.$.body).css("font-family",preferences.font_family_editor);
233
234    RichTextEditor.editorReady = true;
235    }   
236}
237
238cRichTextEditor.prototype.setPlain = function (active,id){
239      RichTextEditor.plain[id] = active;
240          var content = $("#content_id_"+id);
241          //var div = $("<div>").attr("display", "none");
242      if(active === true)
243      {
244            CKEDITOR.instances['body_'+id].destroy();
245            var height = document.body.scrollHeight;
246            height -= 330;
247            //Insere o texto sem formatação no textarea
248            var text_body = remove_tags($('#body_'+id).val());
249            $('#body_'+id).val(text_body);
250           
251            $('#body_'+id).keydown(function(event) {
252                away = false;
253                save_link = content.find(".save")[0];
254                save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
255                                $("#save_message_options_"+id).button({ disabled: false });
256                //save_link.className = 'message_options';
257            });
258                        $("[name=textplain_rt_checkbox_"+id+"]").button({ disabled: false });
259
260            $('#body_'+id).on('keydown',function(){
261            $("#content_id_"+currentTab+" .save").button("enable");
262        });
263      }   
264      else{
265          RichTextEditor.active('body_'+id, id);
266          /*Insere somente quebras de linha para que o texto convertido não fique todo em uma linha só*/
267          var text_body = $('#body_'+id).val().replace(/[\n]+/g, '<br>');
268          $('#body_'+id).val(text_body);
269      }
270}
271
272cRichTextEditor.prototype.getData = function (inst){ 
273    var id = inst.replace('body_','');
274   
275    if(RichTextEditor.plain[id] === true)
276        return $('#'+inst).val();
277    else
278        return CKEDITOR.instances[inst].getData();
279}
280cRichTextEditor.prototype.setData = function (id,data){
281   
282        if(this.plain[id.replace('body_','')] === true)
283                $('#'+id).val(data);
284    else
285        CKEDITOR.instances[id].setData(data);
286}
287
288cRichTextEditor.prototype.dataReady = function(id,reply)
289{
290        var content = $("#content_id_"+id);
291        var input = content.find('.new-message-input.to:first');
292        if (this.plain[id]){
293                if (reply === 'forward')
294                        setTimeout(function(){input.focus();},400);
295        }
296        else{
297                CKEDITOR.instances['body_'+id].on('dataReady',function(e){
298                        if (reply === 'forward' ){     
299                                setTimeout(function(){
300                                                RichTextEditor.blur(id);
301                                                content.find('input[name="input_subject"]').focus();
302                                                input.focus();                                         
303                                },600.);       
304                        }
305                        else if (reply === 'new'){
306                                setTimeout(function(){
307                                                RichTextEditor.blur(id);
308                                                content.find('input[name="input_subject"]').focus();
309                                                input.focus();
310                                },500);
311
312                        };
313                });
314        }
315}
316
317cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion, callback){
318        var content = $("#content_id_"+id);
319        if(recursion === undefined){
320                recursion = 1;
321        }else{
322                recursion++;   
323        }
324        if(this.plain[id] === true){               
325                data =  data.replace( new RegExp('<pre>((.\n*)*)</pre>'),'$1');
326                if($('#'+id) !== undefined){
327                        $('#'+id).val(data);
328                        if (reply === undefined){       
329                                $('#to_'+id).focus();
330                        }
331                }
332                else{
333                        setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
334                }
335        } 
336        else{
337                if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined ){
338                        var editor =   CKEDITOR.instances['body_'+id];
339                        var selection = editor.getSelection();
340                        var fontSize = '';
341                        var fontFamily = '';
342                        if(typeof(preferences.font_size_editor) !== 'undefined')
343                                fontSize = 'font-size:' + preferences.font_size_editor;
344                        if(fontSize != '')
345                                fontFamily = ';'
346                        if(typeof(preferences.font_family_editor) !== 'undefined')
347                                fontFamily += 'font-family:' + preferences.font_family_editor + ';';
348                        var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>';
349                       
350                        if(selection !== undefined && selection !== null){
351                                var selectionRanges = selection.getRanges();
352                        }
353                        if(reply !== undefined){
354                                if(reply == 'edit')
355                                        editor.insertHtml(data);
356                                else
357                                        editor.insertHtml(divBr+data);
358                                editor.focus();
359                        }
360
361                        if(selection !== null){
362                                if(selectionRanges[selectionRanges.length-1] !== undefined){
363                                        selectionRanges[selectionRanges.length-1].setStart(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0);
364                                        selectionRanges[selectionRanges.length-1].setEnd(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0);
365                                }
366                                selection.selectRanges(selectionRanges);
367                        }
368                       
369                        if (is_webkit){
370                                $('#cke_contents_body_'+id+'>iframe').scrollTo(':first');
371                        }
372                        if(callback !== undefined)
373                                callback();
374                }
375                else if(recursion < 20){
376                        setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
377                }
378        }
379}
380
381cRichTextEditor.prototype.destroy = function(id)
382{
383        //Remove Instancia do editor
384        if( CKEDITOR.instances[id] !== undefined )   
385             CKEDITOR.remove(CKEDITOR.instances[id]);
386}
387cRichTextEditor.prototype.active = function(id, just_id)
388{
389   
390   //Remove Instancia do editor caso ela exista
391    if( CKEDITOR.instances[id] !== undefined )   
392         CKEDITOR.remove(CKEDITOR.instances[id]);
393     
394    var height = document.body.scrollHeight;
395     height -= 375;
396     $('#'+id).ckeditor(
397                function() {
398                        RichTextEditor.execPosInstance(id)
399                },
400                {
401                        toolbar:'mail',
402                        height: height
403                }
404        );
405        //$("[name=textplain_rt_checkbox_"+just_id+"]").button({ disabled: false });
406}
407cRichTextEditor.prototype.focus = function(id)
408{
409    if(RichTextEditor.plain[id]  === true)
410        $('#body_'+id).focus();
411    else
412        CKEDITOR.instances['body_'+id].focus();
413
414}
415
416cRichTextEditor.prototype.blur = function(id)
417{
418    if(RichTextEditor.plain[id]  === true)
419        $('#body_'+id).blur();
420    else{
421            var focusManager = new CKEDITOR.focusManager( CKEDITOR.instances['body_'+id] );
422                if (focusManager)
423                        focusManager.blur();
424        }
425}
426
427//Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas
428cRichTextEditor.prototype.setEditable = function(id) {
429        if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
430        var element = CKEDITOR.instances['body_'+ id].document.getBody();
431        element.removeAttribute('contentEditable');
432        element.setAttribute('contentEditable','true');
433}
434cRichTextEditor.prototype.keydown = function (id,rec){
435    if (rec === undefined) rec = 1;
436    rec++;
437    if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
438    var element = CKEDITOR.instances['body_'+ id]; 
439   
440    if(element.document){
441        element.document.on('keydown',function(){
442            $("#content_id_"+currentTab+" .save").button("enable");
443        });
444    } else {
445        if (rec <= 20)
446                setTimeout(function(){RichTextEditor.keydown(id,rec)},500);
447    }
448}
449//Build the Object
450RichTextEditor = new cRichTextEditor();
Note: See TracBrowser for help on using the repository browser.