source: branches/2.5/expressoMail1_2/js/rich_text_editor.js @ 8158

Revision 8158, 12.8 KB checked in by douglas, 11 years ago (diff)

Ticket #3418 - Problema ao salvar assinatura com titulo contendo acentos

  • 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        var value = {}; 
148        for (key in values){
149                value[RichTextEditor.decode64(key)] = values[key];
150        }
151        return value;
152}
153
154cRichTextEditor.prototype.getSignatureDefault = function() {
155         
156        if(RichTextEditor.signatures === false){
157            RichTextEditor.signatures = RichTextEditor.getSignaturesOptions();
158            preferences.signature_default = RichTextEditor.decode64(preferences.signature_default);
159        }     
160         
161        if(!RichTextEditor.signatures || 
162           !RichTextEditor.signatures[preferences.signature_default || ""])
163        {
164          preferences.use_signature = "0"; //Desabilita o uso da assinatura
165          return '';
166        }
167       
168        return unescape(RichTextEditor.signatures[preferences.signature_default]);
169
170}
171
172
173
174cRichTextEditor.prototype.execPosInstance = function(inst) {
175     if(RichTextEditor.editorReady === false)
176     {
177        var editor =  CKEDITOR.instances[inst];
178        var id = inst.replace('body_','');
179        var content = $("#content_id_"+id)
180        editor.document.on('keydown', function(event)
181        {
182                away = false;
183                var save_link = content.find(".save");
184                save_link.unbind("click").click(function(){
185                        openTab.toPreserve[id] = true;save_msg(id);
186                });
187                save_link.button({ disabled: false });
188        });
189       
190       
191        // IM Module Enabled
192        if( window.parent.loadscript && loadscript.autoStatusIM )
193        {
194                CKEDITOR.instances[inst].document.on('keydown', function(event){
195                        loadscript.autoStatusIM;
196                });             
197        }
198
199        if (preferences.auto_save_draft == 1)
200        {
201            autoSaveControl.status[id] = true;
202            autoSaveControl.timer[id] = window.setInterval( "autoSave("+id+")" ,autosave_time); 
203
204            CKEDITOR.instances[inst].document.on('keydown', function(event){   
205                autoSaveControl.status[id] = false;
206            })
207        }
208       
209        $(".cke_editor").css("white-space", "normal");
210
211    if(typeof(preferences.font_size_editor) !== 'undefined')
212        $(editor.document.$.body).css("font-size",preferences.font_size_editor);
213    if(typeof(preferences.font_family_editor) !== 'undefined')
214        $(editor.document.$.body).css("font-family",preferences.font_family_editor);
215
216    RichTextEditor.editorReady = true;
217    }   
218}
219
220cRichTextEditor.prototype.setPlain = function (active,id){
221      RichTextEditor.plain[id] = active;
222          var content = $("#content_id_"+id);
223          //var div = $("<div>").attr("display", "none");
224      if(active === true)
225      {
226            CKEDITOR.instances['body_'+id].destroy();
227            var height = document.body.scrollHeight;
228            height -= 330;
229            //Insere o texto sem formatação no textarea
230            var text_body = remove_tags($('#body_'+id).val());
231            $('#body_'+id).val(text_body);
232           
233            $('#body_'+id).keydown(function(event) {
234                away = false;
235                save_link = content.find(".save")[0];
236                save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
237                                $("#save_message_options_"+id).button({ disabled: false });
238                //save_link.className = 'message_options';
239            });
240                        $("[name=textplain_rt_checkbox_"+id+"]").button({ disabled: false });
241
242            $('#body_'+id).on('keydown',function(){
243            $("#content_id_"+currentTab+" .save").button("enable");
244        });
245      }   
246      else{
247          RichTextEditor.active('body_'+id, id);
248          /*Insere somente quebras de linha para que o texto convertido não fique todo em uma linha só*/
249          var text_body = $('#body_'+id).val().replace(/[\n]+/g, '<br>');
250          $('#body_'+id).val(text_body);
251      }
252}
253
254cRichTextEditor.prototype.getData = function (inst){ 
255    var id = inst.replace('body_','');
256   
257    if(RichTextEditor.plain[id] === true)
258        return $('#'+inst).val();
259    else
260        return CKEDITOR.instances[inst].getData();
261}
262cRichTextEditor.prototype.setData = function (id,data){
263   
264        if(this.plain[id.replace('body_','')] === true)
265                $('#'+id).val(data);
266    else
267        CKEDITOR.instances[id].setData(data);
268}
269
270cRichTextEditor.prototype.dataReady = function(id,reply)
271{
272        var content = $("#content_id_"+id);
273        var input = content.find('.new-message-input.to:first');
274        if (this.plain[id]){
275                if (reply === 'forward')
276                        setTimeout(function(){input.focus();},400);
277        }
278        else{
279                CKEDITOR.instances['body_'+id].on('dataReady',function(e){
280                        if (reply === 'forward' ){     
281                                setTimeout(function(){
282                                                RichTextEditor.blur(id);
283                                                content.find('input[name="input_subject"]').focus();
284                                                input.focus();                                         
285                                },600.);       
286                        }
287                        else if (reply === 'new'){
288                                setTimeout(function(){
289                                                RichTextEditor.blur(id);
290                                                content.find('input[name="input_subject"]').focus();
291                                                input.focus();
292                                },500);
293
294                        };
295                });
296        }
297}
298
299cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion, callback){
300        var content = $("#content_id_"+id);
301        if(recursion === undefined){
302                recursion = 1;
303        }else{
304                recursion++;   
305        }
306        if(this.plain[id] === true){               
307                data =  data.replace( new RegExp('<pre>((.\n*)*)</pre>'),'$1');
308                if($('#'+id) !== undefined){
309                        $('#'+id).val(data);
310                        if (reply === undefined){       
311                                $('#to_'+id).focus();
312                        }
313                }
314                else{
315                        setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
316                }
317        } 
318        else{
319                if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined ){
320                        var editor =   CKEDITOR.instances['body_'+id];
321                        var selection = editor.getSelection();
322                        var fontSize = '';
323                        var fontFamily = '';
324                        if(typeof(preferences.font_size_editor) !== 'undefined')
325                                fontSize = 'font-size:' + preferences.font_size_editor;
326                        if(fontSize != '')
327                                fontFamily = ';'
328                        if(typeof(preferences.font_family_editor) !== 'undefined')
329                                fontFamily += 'font-family:' + preferences.font_family_editor + ';';
330                        var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>';
331                       
332                        if(selection !== undefined && selection !== null){
333                                var selectionRanges = selection.getRanges();
334                        }
335                        if(reply !== undefined){
336                                if(reply == 'edit')
337                                        editor.insertHtml(data);
338                                else
339                                        editor.insertHtml(divBr+data);
340                                editor.focus();
341                        }
342
343                        if(selection !== null){
344                                if(selectionRanges[selectionRanges.length-1] !== undefined){
345                                        selectionRanges[selectionRanges.length-1].setStart(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0);
346                                        selectionRanges[selectionRanges.length-1].setEnd(selectionRanges[selectionRanges.length-1].getTouchedStartNode().getParents()[1].getChild(0), 0);
347                                }
348                                selection.selectRanges(selectionRanges);
349                        }
350                       
351                        if (is_webkit){
352                                $('#cke_contents_body_'+id+'>iframe').scrollTo(':first');
353                        }
354                        if(callback !== undefined)
355                                callback();
356                }
357                else if(recursion < 20){
358                        setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
359                }
360        }
361}
362
363cRichTextEditor.prototype.destroy = function(id)
364{
365        //Remove Instancia do editor
366        if( CKEDITOR.instances[id] !== undefined )   
367             CKEDITOR.remove(CKEDITOR.instances[id]);
368}
369cRichTextEditor.prototype.active = function(id, just_id)
370{
371   
372   //Remove Instancia do editor caso ela exista
373    if( CKEDITOR.instances[id] !== undefined )   
374         CKEDITOR.remove(CKEDITOR.instances[id]);
375     
376    var height = document.body.scrollHeight;
377     height -= 375;
378     $('#'+id).ckeditor(
379                function() {
380                        RichTextEditor.execPosInstance(id)
381                },
382                {
383                        toolbar:'mail',
384                        height: height
385                }
386        );
387        //$("[name=textplain_rt_checkbox_"+just_id+"]").button({ disabled: false });
388}
389cRichTextEditor.prototype.focus = function(id)
390{
391    if(RichTextEditor.plain[id]  === true)
392        $('#body_'+id).focus();
393    else
394        CKEDITOR.instances['body_'+id].focus();
395
396}
397
398cRichTextEditor.prototype.blur = function(id)
399{
400    if(RichTextEditor.plain[id]  === true)
401        $('#body_'+id).blur();
402    else{
403            var focusManager = new CKEDITOR.focusManager( CKEDITOR.instances['body_'+id] );
404                if (focusManager)
405                        focusManager.blur();
406        }
407}
408
409//Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas
410cRichTextEditor.prototype.setEditable = function(id) {
411        if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
412        var element = CKEDITOR.instances['body_'+ id].document.getBody();
413        element.removeAttribute('contentEditable');
414        element.setAttribute('contentEditable','true');
415}
416cRichTextEditor.prototype.keydown = function (id,rec){
417    if (rec === undefined) rec = 1;
418    rec++;
419    if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
420    var element = CKEDITOR.instances['body_'+ id]; 
421   
422    if(element.document){
423        element.document.on('keydown',function(){
424            $("#content_id_"+currentTab+" .save").button("enable");
425        });
426    } else {
427        if (rec <= 20)
428                setTimeout(function(){RichTextEditor.keydown(id,rec)},500);
429    }
430}
431//Build the Object
432RichTextEditor = new cRichTextEditor();
Note: See TracBrowser for help on using the repository browser.