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

Revision 8166, 13.4 KB checked in by douglas, 11 years ago (diff)

Ticket #3455 - Redefinicao de preferencias ao salvar assinatura

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