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

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