source: branches/2.4/expressoMail1_2/js/shortcut.js @ 7160

Revision 7160, 18.8 KB checked in by eduardow, 12 years ago (diff)

Ticket #3091 - Inconsistencia ao excluir mensagem pelo atalho del.

  • Property svn:eol-style set to native
Line 
1/**
2 * http://www.openjs.com/scripts/events/keyboard_shortcuts/
3 * Version : 2.01.A
4 * By Binny V A
5 * License : BSD
6 */
7shortcut = {
8        'all_shortcuts':{},//All the shortcuts are stored in this array
9        'disabled': false,
10        'add': function(shortcut_combination,callback,opt) {
11                //Provide a set of default options
12                var default_options = {
13                        'type':'keydown',
14                        'propagate':false,
15                        'disable_in_input':false,
16                        'target':document,
17                        'keycode':false
18                }
19                if(!opt) opt = default_options;
20                else {
21                        for(var dfo in default_options) {
22                                if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
23                        }
24                }
25               
26                var ele = opt.target;
27                if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
28                var ths = this;
29                shortcut_combination = shortcut_combination.toLowerCase();
30
31                //The function to be called at keypress
32                var func = function(e) {
33                        e = e || window.event;
34                       
35                        if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
36                                var element;
37                                if(e.target) element=e.target;
38                                else if(e.srcElement) element=e.srcElement;
39                                if(element.nodeType==3) element=element.parentNode;
40
41                                if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
42                        }
43       
44                if(shortcut.disabled === true)
45                return;
46                        //Find Which key is pressed
47                        if (e.keyCode) code = e.keyCode;
48                        else if (e.which) code = e.which;
49                        var character = String.fromCharCode(code).toLowerCase();
50                       
51                        if(code == 188) character=","; //If the user presses , when the type is onkeydown
52                        if(code == 190) character="."; //If the user presses , when the type is onkeydown
53       
54                        var keys = shortcut_combination.split("+");
55                        //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
56                        var kp = 0;
57                       
58                        //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
59                        var shift_nums = {
60                                "`":"~",
61                                "1":"!",
62                                "2":"@",
63                                "3":"#",
64                                "4":"$",
65                                "5":"%",
66                                "6":"^",
67                                "7":"&",
68                                "8":"*",
69                                "9":"(",
70                                "0":")",
71                                "-":"_",
72                                "=":"+",
73                                ";":":",
74                                "'":"\"",
75                                ",":"<",
76                                ".":">",
77                                "/":"?",
78                                "\\":"|"
79                        }
80                        //Special Keys - and their codes
81                        var special_keys = {
82                                'esc':27,
83                                'escape':27,
84                                'tab':9,
85                                'space':32,
86                                'return':13,
87                                'enter':13,
88                                'backspace':8,
89       
90                                'scrolllock':145,
91                                'scroll_lock':145,
92                                'scroll':145,
93                                'capslock':20,
94                                'caps_lock':20,
95                                'caps':20,
96                                'numlock':144,
97                                'num_lock':144,
98                                'num':144,
99                               
100                                'pause':19,
101                                'break':19,
102                               
103                                'insert':45,
104                                'home':36,
105                                'delete':46,
106                                'end':35,
107                               
108                                'pageup':33,
109                                'page_up':33,
110                                'pu':33,
111       
112                                'pagedown':34,
113                                'page_down':34,
114                                'pd':34,
115       
116                                'left':37,
117                                'up':38,
118                                'right':39,
119                                'down':40,
120       
121                                'f1':112,
122                                'f2':113,
123                                'f3':114,
124                                'f4':115,
125                                'f5':116,
126                                'f6':117,
127                                'f7':118,
128                                'f8':119,
129                                'f9':120,
130                                'f10':121,
131                                'f11':122,
132                                'f12':123
133                        }
134       
135                        var modifiers = {
136                                shift: {wanted:false, pressed:false},
137                                ctrl : {wanted:false, pressed:false},
138                                alt  : {wanted:false, pressed:false},
139                                meta : {wanted:false, pressed:false}    //Meta is Mac specific
140                        };
141                       
142                        if(e.ctrlKey)   modifiers.ctrl.pressed = true;
143                        if(e.shiftKey)  modifiers.shift.pressed = true;
144                        if(e.altKey)    modifiers.alt.pressed = true;
145                        if(e.metaKey)   modifiers.meta.pressed = true;
146                       
147                        for(var i=0; k=keys[i],i<keys.length; i++) {
148                                //Modifiers
149                                if(k == 'ctrl' || k == 'control') {
150                                        kp++;
151                                        modifiers.ctrl.wanted = true;
152
153                                } else if(k == 'shift') {
154                                        kp++;
155                                        modifiers.shift.wanted = true;
156
157                                } else if(k == 'alt') {
158                                        kp++;
159                                        modifiers.alt.wanted = true;
160                                } else if(k == 'meta') {
161                                        kp++;
162                                        modifiers.meta.wanted = true;
163                                } else if(k.length > 1) { //If it is a special key
164                                        if(special_keys[k] == code) kp++;
165                                       
166                                } else if(opt['keycode']) {
167                                        if(opt['keycode'] == code) kp++;
168
169                                } else { //The special keys did not match
170                                        if(character == k) kp++;
171                                        else {
172                                                if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
173                                                        character = shift_nums[character];
174                                                        if(character == k) kp++;
175                                                }
176                                        }
177                                }
178                        }
179
180                        if(kp == keys.length &&
181                                                modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
182                                                modifiers.shift.pressed == modifiers.shift.wanted &&
183                                                modifiers.alt.pressed == modifiers.alt.wanted &&
184                                                modifiers.meta.pressed == modifiers.meta.wanted) {
185                                callback(e);
186       
187                                if(!opt['propagate']) { //Stop the event
188                                        //e.cancelBubble is supported by IE - this will kill the bubbling process.
189                                        if ( Element('border_id_0') && Element('border_id_0').className != 'menu-sel' ){
190                                                return false;
191                                        }
192                                        e.cancelBubble = true;
193                                        e.returnValue = false;
194       
195                                       
196                                        //e.stopPropagation works in Firefox.
197                                        if (e.stopPropagation) {
198                                        if ( Element('border_id_0') && Element('border_id_0').className != 'menu-sel' ){
199                                                return false;
200                                        }
201                                               
202                                                e.stopPropagation();
203                                                e.preventDefault();
204                                        }
205                                        return false;
206                                }
207                               
208                        }
209                }
210                this.all_shortcuts[shortcut_combination] = {
211                        'callback':func,
212                        'target':ele,
213                        'event': opt['type']
214                };
215                //Attach the function with the event
216                if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
217                else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
218                else ele['on'+opt['type']] = func;
219        },
220
221        //Remove the shortcut - just specify the shortcut and I will remove the binding
222        'remove':function(shortcut_combination) {
223                shortcut_combination = shortcut_combination.toLowerCase();
224                var binding = this.all_shortcuts[shortcut_combination];
225                delete(this.all_shortcuts[shortcut_combination])
226                if(!binding) return;
227                var type = binding['event'];
228                var ele = binding['target'];
229                var callback = binding['callback'];
230
231                if(ele.detachEvent) ele.detachEvent('on'+type, callback);
232                else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
233                else ele['on'+type] = false;
234        }
235}
236
237/* ExpressMail Functions */
238
239var shift_up_count = 0;
240var shift_down_count = 0;
241var selMessageShortcut = "";
242
243shortcut.add("N",function(e)
244{
245        // avoids problem related at ticket #1011
246        e.preventDefault();
247        var search_in_focus = false;
248        var search_win = document.getElementById( 'QuickCatalogSearch_window_QuickCatalogSearch' );
249        if ( search_win && search_win.style.visibility != 'hidden' )
250                search_in_focus = true;
251
252        if ( ! search_in_focus )
253                new_message("new","null");
254},{'disable_in_input':true});
255
256shortcut.add("Esc",function(){
257        var window_closed = false;
258        var search_win = document.getElementById( 'window_QuickCatalogSearch' );
259       
260        for(var window in arrayJSWin)
261        {
262                if (arrayJSWin[window].visible)
263                {
264                    window_closed = true;
265                    if(search_win.style.visibility == 'hidden'){
266                        arrayJSWin[window].close();
267                    }
268                }
269        }
270        if((search_win) && (search_win.style.visibility == 'visible')){
271            search_win.style.visibility = 'hidden';
272            win.close();
273       }       
274
275        if (!window_closed)
276                delete_border(get_msg_id(), 'false');
277},{'disable_in_input':false});
278
279shortcut.add("I",function(){print_all();},{'disable_in_input':true});
280shortcut.add("E",function(e){ if(e.preventDefault) e.preventDefault(); else event.returnValue = false; exec_msg_action('forward');},{'disable_in_input':true});
281shortcut.add("R",function(e){ if(e.preventDefault) e.preventDefault(); else event.returnValue = false; exec_msg_action('reply');},{'disable_in_input':true});
282shortcut.add("T",function(e){ if(e.preventDefault) e.preventDefault(); else event.returnValue = false; var msg_id = get_msg_id(); if(msg_id) new_message("reply_to_all_with_history",msg_id);},{'disable_in_input':true});
283shortcut.add("O",function(e){ if(e.preventDefault) e.preventDefault(); else event.returnValue = false; show_head_option();},{'disable_in_input':true});
284shortcut.add("M",function(e){ if(e.preventDefault) e.preventDefault(); else event.returnValue = false; show_address_full();},{'disable_in_input':true});
285
286shortcut.add("Delete",function(){
287       
288       
289        if(currentTab == 0){
290    var selected_shortcut_msgs = '';
291    var tbody_box = Element('tbody_box');
292    all_messages = Element('tbody_box').childNodes;
293     
294    for ( var i=0; i < all_messages.length; i++ )
295                {
296                        if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
297            {
298                    selected_shortcut_msgs += all_messages[i].id + ',';
299                     
300                    if( all_messages[i].nextSibling )
301                            selMessageShortcut = all_messages[i].nextSibling.id + "-" + "down";
302                    else if(all_messages[i].previousSibling)
303                            selMessageShortcut = all_messages[i].previousSibling.id + "-" + "up";
304            }
305    }
306     
307    selected_shortcut_msgs = selected_shortcut_msgs.substring(0,(selected_shortcut_msgs.length-1));
308     
309    if ( Element('border_id_0').className === 'menu-sel' )
310    {
311            proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
312    }
313    else
314    {
315            //exec_msg_action('delete');
316            select_msg(selMessageShortcut.substring(0, selMessageShortcut.indexOf("-")),
317                               selMessageShortcut.substring(selMessageShortcut.indexOf("-")), true );
318                       
319                        proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
320                }
321        }else{
322                proxy_mensagens.delete_msgs(openTab.imapBox[currentTab], currentTab.substring(0, selMessageShortcut.indexOf("_r")), 'null');
323       
324        }
325       
326}
327,{'disable_in_input':true});
328
329shortcut.add("Ctrl+Up",function(){exec_msg_action('previous');/*select_msg('null', 'up');*/},{'disable_in_input':true});
330shortcut.add("Ctrl+Down",function(){exec_msg_action('next');/*select_msg('null', 'down');*/},{'disable_in_input':true});
331
332if (is_ie || is_webkit)
333{
334//**********************
335shortcut.add('up', function(e)
336        {
337               
338                var search_in_focus = false;
339                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
340                if ( search_win && search_win.style.visibility == 'visible' )
341                        search_in_focus = true;
342
343                if ( !search_in_focus && currentTab == 0 )
344                    select_msg('null', 'up');
345                // Ao usuario pressionar o 'up' em uma outra aba, não pode ser removido o 'up'.
346                                else{}
347                   // shortcut.remove('up');
348                                e.stopPropagation();
349                                e.preventDefault();
350        },{'disable_in_input':false});
351
352
353        shortcut.add('down', function(e)
354        {
355                var search_in_focus = false;
356                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
357                if ( search_win && search_win.style.visibility == 'visible' )
358                        search_in_focus = true;
359
360                if ( !search_in_focus && currentTab == 0 )
361                    select_msg('null', 'down');
362                                // Ao usuario pressionar o 'down' em uma outra aba, não pode ser removido o 'down'.
363                else{}
364                   // shortcut.remove('down');
365                                e.stopPropagation();
366                                e.preventDefault();
367        },{'disable_in_input':false});
368
369//****************
370
371        shortcut.add("Shift+down",function(){
372                if ( Element('border_id_0').className==='menu-sel' )
373                {
374                        if (shift_up_count > 0)
375                                unselect_top_msg();
376                        else
377                                select_bottom_msg();
378                }
379        },{'disable_in_input':true, 'propagate':false});
380       
381        shortcut.add("Shift+up",function(){
382                if ( Element('border_id_0').className==='menu-sel' )
383                {
384                        if (shift_down_count > 0)
385                                unselect_bottom_msg();
386                        else
387                                select_top_msg();
388                }
389        },{'disable_in_input':true, 'propagate':false});
390}
391else
392{
393        shortcut.add("Up",function(){
394                if (currentTab == 0)
395                  select_msg('null', 'up');
396        },{'disable_in_input':false});
397       
398        shortcut.add("Down",function(){
399
400                if (currentTab == 0)
401                  select_msg('null', 'down');
402               
403        },{'disable_in_input':false});
404       
405        shortcut.add("Shift+down",function(){
406                if ( Element('border_id_0').className==='menu-sel' )
407                {
408                        if (shift_up_count > 0)
409                                unselect_top_msg();
410                        else
411                                select_bottom_msg();
412                }
413        },{'type':'keypress','disable_in_input':true, 'propagate':false});
414       
415        shortcut.add("Shift+up",function(){
416                if ( Element('border_id_0').className==='menu-sel' )
417                {
418                        if (shift_down_count > 0)
419                                unselect_bottom_msg();
420                        else
421                                select_top_msg();
422                }
423        },{'type':'keypress', 'disable_in_input':true, 'propagate':false});
424}
425
426shortcut.add("return",function(){
427        if ( Element('border_id_0').className==='menu-sel' )
428        {
429                all_messages = Element('tbody_box').childNodes;
430                for (var i=0; i < all_messages.length; i++)
431                {
432                        if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
433                        {
434                                Element("td_from_" + all_messages[i].id).onclick();
435                                return;
436                        }
437                }
438        }
439},{'disable_in_input':true});
440
441shortcut.add("f9",function(){
442        Element("em_refresh_button").onclick();
443        return;
444},{'disable_in_input':true});
445
446function exec_msg_action(action)
447{
448        var msg_id = get_msg_id();
449        if (msg_id)
450        {
451                var msg_id = 'msg_opt_' + action + '_' + msg_id;
452                try {Element(msg_id).onclick();}
453                catch(e){/*alert(e);*/}
454        }
455        return;
456}
457
458function show_head_option()
459{
460        var msg_id = get_msg_id();
461    if (msg_id) {
462                var msg_id = 'option_hide_more_' + msg_id;
463                try {Element(msg_id).onclick();}
464                catch(e){/*alert(e);*/}
465    }
466        return;
467}
468
469function show_address_full()
470{
471        var toaddress = Element('div_toaddress_' + get_msg_id());       
472        var ccaddress = Element('div_ccaddress_' + get_msg_id());
473       
474        if(toaddress &&  '' == toaddress.style.display) {
475                show_div_address_full(get_msg_id(),'to');
476        }
477        else {
478                if(toaddress)
479                        toaddress.style.display = '';
480                var toaddress_full = Element('div_toaddress_full_' + get_msg_id());
481                if(toaddress_full)
482                        toaddress_full.style.display = 'none';
483        }               
484        if(ccaddress &&  '' == ccaddress.style.display) {
485                show_div_address_full(get_msg_id(),'cc');
486        }
487        else {
488                if(ccaddress)
489                        ccaddress.style.display = '';
490                var ccaddress_full = Element('div_ccaddress_full_' + get_msg_id());
491                if(ccaddress_full)
492                        ccaddress_full.style.display = 'none';
493        }
494        return;
495}
496
497function get_msg_id()
498{
499        children = Element('border_tr').childNodes;
500       
501        for (var i=0; i<children.length; i++)
502        {
503                if ( (children[i].nodeName==='TD') && (children[i].className==='menu-sel') && children[i].id != 'border_id_0')
504                {
505                        var border_selected = children[i];
506                        var msg_id = border_selected.id.replace("border_id_","");
507                        return msg_id;
508                }
509        }
510        return false;
511}
512
513function select_msg(msg_number, keyboard_action, force_msg_selection)
514{
515    /*
516          ** Se caso for limpado toda a caixa de email,
517          ** é adicionado um novo atalho de seleção.
518          ** main.js on function refrash and line 629.
519        */
520       
521        if(keyboard_action == "reload_msg"){
522           if( $("#tbody_box .selected_shortcut_msg").length == 0 )
523                $("#tbody_box tr:first").addClass("selected_shortcut_msg");     
524        }
525       
526        shift_up_count = 0;
527        shift_down_count = 0;
528
529        if (msg_number != 'null') {
530       
531                if(Element(msg_number)){
532                        unselect_all_msgs();
533                        add_className(Element(msg_number), 'selected_shortcut_msg');
534                }
535               
536        } else {
537                var scrollMain = Element('divScrollMain_0');
538                var selection_size = parseInt(preferences.line_height) + 10;
539               
540                        if( keyboard_action == 'down') {
541                       
542                                if(!Element("chk_box_select_all_messages").checked){
543                                       
544                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
545                                       
546                                                if($(this).hasClass("selected_shortcut_msg") && $(this).next().length){
547                                                        $(this).next().addClass("selected_shortcut_msg");
548                                                        $(this).removeClass("selected_shortcut_msg");
549                                                        return false;
550                                                }
551                                               
552                                        });
553                                       
554                                } else {
555                               
556                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
557                                               
558                                                if($(this).hasClass("current_selected") && $(this).next().length){
559                                                        $(this).removeClass("current_selected");
560                                                        $(this).removeClass("selected_shortcut_msg");
561                                                        $(this).next().addClass("current_selected");
562                                                        $(this).next().addClass("selected_shortcut_msg");
563                                                        return false;
564                                                }
565                                       
566                                        });
567                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
568                                                                if(!$(this).hasClass("current_selected"))
569                                                                        $(this).removeClass("selected_shortcut_msg");
570                                        });
571                                }
572                       
573                        } else if( keyboard_action == 'up') {
574                       
575                                if(!Element("chk_box_select_all_messages").checked){
576                               
577                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
578                                       
579                                                if($(this).hasClass("selected_shortcut_msg") && $(this).prev().length){
580                                                                $(this).prev().addClass("selected_shortcut_msg");
581                                                                $(this).removeClass("selected_shortcut_msg");
582                                                                return false;
583                                                }
584                                               
585                                        });
586                                       
587                                } else {
588                                       
589                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
590                                               
591                                                if($(this).hasClass("current_selected") && $(this).prev().length){
592                                                        $(this).removeClass("current_selected");
593                                                        $(this).removeClass("selected_shortcut_msg");
594                                                        $(this).prev().addClass("current_selected");
595                                                        $(this).prev().addClass("selected_shortcut_msg");
596                                                        return false;
597                                                }
598                                       
599                                        });
600                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
601                                                                if(!$(this).hasClass("current_selected"))
602                                                                        $(this).removeClass("selected_shortcut_msg");
603                                        });
604                               
605                                }
606                 
607                        }
608                return true;
609        }
610}
611
612function select_bottom_msg()
613{
614        all_messages = Element('tbody_box').childNodes;
615       
616        if ( exist_className(all_messages[all_messages.length-1], 'selected_shortcut_msg') )
617                return;
618       
619        for (var i=all_messages.length-1; i >=0; i--)
620        {
621                if ( (exist_className(all_messages[i], 'selected_shortcut_msg')) && (i+1 <= all_messages.length-1) )
622                {
623                        shift_down_count++;
624                        add_className(all_messages[i+1], 'selected_shortcut_msg');
625                        break;
626                }
627        }
628}
629
630function select_top_msg()
631{
632        all_messages = Element('tbody_box').childNodes;
633               
634        if ( exist_className(all_messages[0], 'selected_shortcut_msg') )
635                return;
636       
637        for (var i=0; i <=all_messages.length-1; i++)
638        {
639                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
640                {
641                        shift_up_count++;
642                        add_className(all_messages[i-1], 'selected_shortcut_msg');
643                        break;
644                }
645        }
646}
647
648function unselect_bottom_msg()
649{
650        all_messages = Element('tbody_box').childNodes;
651        for (var i=all_messages.length-1; i >=0; i--)
652        {
653                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
654                {
655                        shift_down_count--;
656                        remove_className(all_messages[i], 'selected_shortcut_msg');
657                        break;
658                }
659        }
660}
661
662function unselect_top_msg()
663{
664        all_messages = Element('tbody_box').childNodes;
665        for (var i=0; i <=all_messages.length-1; i++)
666        {
667                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
668                {
669                        shift_up_count--;
670                        remove_className(all_messages[i], 'selected_shortcut_msg');
671                        break;
672                }
673        }
674}
675
676function unselect_all_msgs()
677{
678        all_messages = Element('tbody_box').childNodes;
679        for (var i=0; i <=all_messages.length-1; i++)
680        {
681                remove_className(all_messages[i], 'selected_shortcut_msg');
682        }
683}
Note: See TracBrowser for help on using the repository browser.