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

Revision 5745, 10.1 KB checked in by cristiano, 12 years ago (diff)

Ticket #2469 - Trocada regex que limpa o css e problemas com envio de email em text/plain

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[2]1function cRichTextEditor(){
[5083]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;
[2]12}
13
[5083]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
[320]17
[5083]18var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
[2]19
[5083]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    }
[320]53}
54
[5083]55cRichTextEditor.prototype.fromJSON = function( value )
[320]56{
[5083]57    return (new Function( "return " + this.decode64( value )))();
58}
[790]59
[5083]60cRichTextEditor.prototype.decode64 = function(input) {
61        if( typeof input === "undefined" ) return '';
[4309]62
[5083]63        var output = new StringMaker();
64        var chr1, chr2, chr3;
65        var enc1, enc2, enc3, enc4;
66        var i = 0;
[790]67
[5083]68        // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
69        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
[4291]70
[5083]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++));
[4291]76
[5083]77                chr1 = (enc1 << 2) | (enc2 >> 4);
78                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
79                chr3 = ((enc3 & 3) << 6) | enc4;
[2]80
[5083]81                output.append(String.fromCharCode(chr1));
[3130]82
[5083]83                if (enc3 != 64) {
84                        output.append(String.fromCharCode(chr2));
[2]85                }
[5083]86                if (enc4 != 64) {
87                        output.append(String.fromCharCode(chr3));
[2]88                }
89        }
[5083]90
91        return output.toString();
[2]92}
93
[4291]94
[5083]95cRichTextEditor.prototype.loadEditor = function(ID) {
[2]96       
[5083]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)
[5417]106                {
107                        RichTextEditor.plain[ID] = true; 
108                        RichTextEditor.editorReady = true;
109                }
[5083]110        else
[5417]111                        RichTextEditor.active(pObj);
[2]112}
113
[5083]114cRichTextEditor.prototype.getSignaturesOptions = function() {
115       
116        if(RichTextEditor.signatures !== false)
117            return RichTextEditor.signatures;
118               
119        var signatures = this.fromJSON( preferences.signatures );
120        var signature_types = this.fromJSON( preferences.signature_types );
[2]121
[5083]122        for( key in signatures )
123            if( !signature_types[key] )
124                signatures[key] = signatures[key].replace( /\n/g, "<br>" );
125                 
126        RichTextEditor.signatures = signatures;
127        return signatures;
[2]128
[271]129}
130
[5083]131cRichTextEditor.prototype.getSignatureDefault = function() {
132         
133        if(RichTextEditor.signatures === false)
134            RichTextEditor.signatures = RichTextEditor.getSignaturesOptions();
135         
136        if(!RichTextEditor.signatures || 
137           !RichTextEditor.signatures[preferences.signature_default || ""])
138        {
139          preferences.use_signature = "0"; //Desabilita o uso da assinatura
140          return '';
141        }
142       
143        return unescape(RichTextEditor.signatures[preferences.signature_default]);
[4472]144
[271]145}
146
147
148
[5083]149cRichTextEditor.prototype.execPosInstance = function(inst) {
[5459]150     if(RichTextEditor.editorReady === false)
151     {
[5417]152        var editor =  CKEDITOR.instances[inst];
153        var id = inst.replace('body_','');
154        editor.document.on('keydown', function(event)
155        {
156                away = false;
157                var save_link = Element("save_message_options_"+id);
158                save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
159                $("#save_message_options_"+id).button({ disabled: false });
160        });
[5083]161       
162       
163        // IM Module Enabled
164        if( window.parent.loadscript && loadscript.autoStatusIM )
165        {
[5417]166                CKEDITOR.instances[inst].document.on('keydown', function(event){
167                        loadscript.autoStatusIM;
168                });             
[2]169        }
[5459]170
[5417]171        if (preferences.auto_save_draft == 1)
[5083]172        {
[5459]173            autoSaveControl.status[id] = true;
174            autoSaveControl.timer[id] = window.setInterval( "autoSave("+id+")" ,autosave_time); 
175
176            CKEDITOR.instances[inst].document.on('keydown', function(event){   
177                autoSaveControl.status[id] = false;
178            })
179        }
180       
[5417]181        $(".cke_editor").css("white-space", "normal");
[5459]182        RichTextEditor.editorReady = true;
183     } 
[2]184}
[5459]185
[5083]186cRichTextEditor.prototype.setPlain = function (active,id){
187      RichTextEditor.plain[id] = active;
188      if(active === true)
189      {
190            CKEDITOR.instances['body_'+id].destroy();
191            var height = document.body.scrollHeight;
192            height -= 330;
193            $('#body_'+id).height(height);
194            $('#body_'+id).keydown(function(event) {
195                away = false;
196                var save_link = Element("save_message_options_"+id);
197                save_link.onclick = function onclick() {openTab.toPreserve[id] = true;save_msg(id);} ;
[5417]198                                $("#save_message_options_"+id).button({ disabled: false });
199                //save_link.className = 'message_options';
[5083]200            });
[5417]201                        $("[name=textplain_rt_checkbox_"+id+"]").button({ disabled: false });
[5083]202      }   
203      else
[5417]204          RichTextEditor.active('body_'+id, id);
[2]205}
206
[5083]207cRichTextEditor.prototype.getData = function (inst){ 
208    var id = inst.replace('body_','');
209   
210    if(RichTextEditor.plain[id] === true)
211        return $('#'+inst).val();
212    else
213        return CKEDITOR.instances[inst].getData();
[2]214}
[5083]215cRichTextEditor.prototype.setData = function (id,data){
216   
217     if(this.plain[id] === true)
218        $('#'+id).val(data);
219     else
220        CKEDITOR.instances[id].setData(data)
221}
222cRichTextEditor.prototype.setInitData = function (id,data,reply,recursion){
223   
224    if(recursion === undefined) recursion = 1;
225    else recursion++;   
226   
227     if(this.plain[id] === true)
[5745]228     {               
229                data =  data.replace( new RegExp('<pre>((.\n*)*)</pre>'),'$1');
230               
231                if($('#'+id) !== undefined){
[5083]232                        $('#'+id).val(data);
[5485]233                 if (reply === undefined)       
234                        $('#to_'+id).focus();
235         }
[5083]236         else
237                          setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
238         } 
239     else
240     {
241       if( RichTextEditor.editorReady === true && CKEDITOR.instances['body_'+id] !== undefined )   
242       {
243           var editor =   CKEDITOR.instances['body_'+id];   
244           editor.insertHtml('');
245           var selection = editor.getSelection();
246           if(selection !== undefined && selection !== null) var selectionRanges = selection.getRanges();
[2]247
[5134]248           var fontSize = '';
249           var fontFamily = '';
[5083]250           
[5134]251           if(typeof(preferences.font_size_editor) !== 'undefined')
252               fontSize = 'font-size:' + preferences.font_size_editor;
253               
254           if(fontSize != '')
255               fontFamily = ';'
256           
257           if(typeof(preferences.font_family_editor) !== 'undefined')
258               fontFamily += 'font-family:' + preferences.font_family_editor + ';';
259           
260           var divBr = '<div style="'+fontSize+fontFamily+'"><br type="_moz"></div>';
[5083]261         
[5485]262                    if(reply === undefined){
263                                editor.on('insertHtml',function setFocus(e){
264                                                        setTimeout("$('#to_"+id+"').focus()",100);
[5496]265                                                        editor.removeListener('insertHtml', setFocus);
[5485]266                                                }
267                                );
[5158]268                                editor.insertHtml(divBr+divBr+data);
[5485]269                        }
[5158]270                        else if(reply == 'edit')
271                                editor.insertHtml(data);
272                        else
273                                editor.insertHtml(divBr+data);
[5134]274           
275           if(selection !== null) selection.selectRanges(selectionRanges);
[5385]276                   
277                   //Coloca o scroll do editor no inicio
278                   if (is_webkit){
279                                $('#cke_contents_body_'+id+'>iframe').scrollTo(':first');
280                   }
[5083]281          //Caso não for uma resposta votla o foco para o input to
[5485]282          //if(reply === undefined)   
283            //      setTimeout("$('#to_"+id+"').focus()",100);
[5083]284 
285       }
286       else if(recursion < 20)
287           setTimeout(function() {RichTextEditor.setInitData(id,data,reply,recursion); }, 500);
288     }
[2]289}
290
[5083]291cRichTextEditor.prototype.destroy = function(id)
292{
293        //Remove Instancia do editor
294        if( CKEDITOR.instances[id] !== undefined )   
295             CKEDITOR.remove(CKEDITOR.instances[id]);
[2]296}
[5417]297cRichTextEditor.prototype.active = function(id, just_id)
[5083]298{
299   
300   //Remove Instancia do editor caso ela exista
301    if( CKEDITOR.instances[id] !== undefined )   
302         CKEDITOR.remove(CKEDITOR.instances[id]);
303     
[5134]304    var height = document.body.scrollHeight;
[5460]305     height -= 375;
[5083]306     $('#'+id).ckeditor(
307          function() {RichTextEditor.execPosInstance(id)},
308          {
[5134]309              toolbar:'mail',
310              height: height
[5083]311          });
[5417]312        $("[name=textplain_rt_checkbox_"+just_id+"]").button({ disabled: false });
[5083]313}
314cRichTextEditor.prototype.focus = function(id)
315{
316    if(RichTextEditor.plain[id]  === true)
317        $('#body_'+id).focus();
318    else
319        CKEDITOR.instances['body_'+id].focus();
320
321}
322//Função reseta o atributo contentEditable para resolver bug de cursor ao trocar abas
323cRichTextEditor.prototype.setEditable = function(id) {
324        if( CKEDITOR.instances['body_'+ id] === undefined ) return;   
325        var element = CKEDITOR.instances['body_'+ id].document.getBody();
326        element.removeAttribute('contentEditable');
327        element.setAttribute('contentEditable','true');
328}
[2]329//Build the Object
[320]330RichTextEditor = new cRichTextEditor();
Note: See TracBrowser for help on using the repository browser.