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

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