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

Revision 7766, 12.5 KB checked in by thiago, 11 years ago (diff)

Ticket #3307 - Problema ao editar uma nova mensagem.......

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