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

Revision 7663, 12.3 KB checked in by angelo, 11 years ago (diff)

Ticket #3211 - Problema com posicao do cursor ao encaminhar mensagem

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