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

Revision 6931, 18.3 KB checked in by eduardow, 12 years ago (diff)

Ticket #2987 - Inconsistencia excluindo com tela modal aberta - teclas de atalho.

  • 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        var selected_shortcut_msgs = '';
290        var tbody_box = Element('tbody_box');
291        all_messages = Element('tbody_box').childNodes;
292       
293        for ( var i=0; i < all_messages.length; i++ )
294        {
295                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
296                {
297                        selected_shortcut_msgs += all_messages[i].id + ',';
298                       
299                        if( all_messages[i].nextSibling )
300                                selMessageShortcut = all_messages[i].nextSibling.id + "-" + "down";
301                        else if(all_messages[i].previousSibling)
302                                selMessageShortcut = all_messages[i].previousSibling.id + "-" + "up";
303                }
304        }
305       
306        selected_shortcut_msgs = selected_shortcut_msgs.substring(0,(selected_shortcut_msgs.length-1));
307       
308        if ( Element('border_id_0').className === 'menu-sel' )
309        {
310                proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
311        }
312        else
313        {
314                exec_msg_action('delete');
315                select_msg(selMessageShortcut.substring(0, selMessageShortcut.indexOf("-")),
316                                   selMessageShortcut.substring(selMessageShortcut.indexOf("-")), true );
317               
318                proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
319        }
320       
321}
322,{'disable_in_input':true});
323
324shortcut.add("Ctrl+Up",function(){exec_msg_action('previous');/*select_msg('null', 'up');*/},{'disable_in_input':true});
325shortcut.add("Ctrl+Down",function(){exec_msg_action('next');/*select_msg('null', 'down');*/},{'disable_in_input':true});
326
327if (is_ie || is_webkit)
328{
329//**********************
330shortcut.add('up', function(e)
331        {
332               
333                var search_in_focus = false;
334                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
335                if ( search_win && search_win.style.visibility == 'visible' )
336                        search_in_focus = true;
337
338                if ( !search_in_focus && currentTab == 0 )
339                    select_msg('null', 'up');
340                // Ao usuario pressionar o 'up' em uma outra aba, não pode ser removido o 'up'.
341                                else{}
342                   // shortcut.remove('up');
343                                e.stopPropagation();
344                                e.preventDefault();
345        },{'disable_in_input':false});
346
347
348        shortcut.add('down', function(e)
349        {
350                var search_in_focus = false;
351                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
352                if ( search_win && search_win.style.visibility == 'visible' )
353                        search_in_focus = true;
354
355                if ( !search_in_focus && currentTab == 0 )
356                    select_msg('null', 'down');
357                                // Ao usuario pressionar o 'down' em uma outra aba, não pode ser removido o 'down'.
358                else{}
359                   // shortcut.remove('down');
360                                e.stopPropagation();
361                                e.preventDefault();
362        },{'disable_in_input':false});
363
364//****************
365
366        shortcut.add("Shift+down",function(){
367                if ( Element('border_id_0').className==='menu-sel' )
368                {
369                        if (shift_up_count > 0)
370                                unselect_top_msg();
371                        else
372                                select_bottom_msg();
373                }
374        },{'disable_in_input':true, 'propagate':false});
375       
376        shortcut.add("Shift+up",function(){
377                if ( Element('border_id_0').className==='menu-sel' )
378                {
379                        if (shift_down_count > 0)
380                                unselect_bottom_msg();
381                        else
382                                select_top_msg();
383                }
384        },{'disable_in_input':true, 'propagate':false});
385}
386else
387{
388        shortcut.add("Up",function(){
389                if (currentTab == 0)
390                  select_msg('null', 'up');
391        },{'disable_in_input':false});
392       
393        shortcut.add("Down",function(){
394
395                if (currentTab == 0)
396                  select_msg('null', 'down');
397               
398        },{'disable_in_input':false});
399       
400        shortcut.add("Shift+down",function(){
401                if ( Element('border_id_0').className==='menu-sel' )
402                {
403                        if (shift_up_count > 0)
404                                unselect_top_msg();
405                        else
406                                select_bottom_msg();
407                }
408        },{'type':'keypress','disable_in_input':true, 'propagate':false});
409       
410        shortcut.add("Shift+up",function(){
411                if ( Element('border_id_0').className==='menu-sel' )
412                {
413                        if (shift_down_count > 0)
414                                unselect_bottom_msg();
415                        else
416                                select_top_msg();
417                }
418        },{'type':'keypress', 'disable_in_input':true, 'propagate':false});
419}
420
421shortcut.add("return",function(){
422        if ( Element('border_id_0').className==='menu-sel' )
423        {
424                all_messages = Element('tbody_box').childNodes;
425                for (var i=0; i < all_messages.length; i++)
426                {
427                        if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
428                        {
429                                Element("td_from_" + all_messages[i].id).onclick();
430                                return;
431                        }
432                }
433        }
434},{'disable_in_input':true});
435
436shortcut.add("f9",function(){
437        Element("em_refresh_button").onclick();
438        return;
439},{'disable_in_input':true});
440
441function exec_msg_action(action)
442{
443        var msg_id = get_msg_id();
444        if (msg_id)
445        {
446                var msg_id = 'msg_opt_' + action + '_' + msg_id;
447                try {Element(msg_id).onclick();}
448                catch(e){/*alert(e);*/}
449        }
450        return;
451}
452
453function show_head_option()
454{
455        var msg_id = get_msg_id();
456    if (msg_id) {
457                var msg_id = 'option_hide_more_' + msg_id;
458                try {Element(msg_id).onclick();}
459                catch(e){/*alert(e);*/}
460    }
461        return;
462}
463
464function show_address_full()
465{
466        var toaddress = Element('div_toaddress_' + get_msg_id());       
467        var ccaddress = Element('div_ccaddress_' + get_msg_id());
468       
469        if(toaddress &&  '' == toaddress.style.display) {
470                show_div_address_full(get_msg_id(),'to');
471        }
472        else {
473                if(toaddress)
474                        toaddress.style.display = '';
475                var toaddress_full = Element('div_toaddress_full_' + get_msg_id());
476                if(toaddress_full)
477                        toaddress_full.style.display = 'none';
478        }               
479        if(ccaddress &&  '' == ccaddress.style.display) {
480                show_div_address_full(get_msg_id(),'cc');
481        }
482        else {
483                if(ccaddress)
484                        ccaddress.style.display = '';
485                var ccaddress_full = Element('div_ccaddress_full_' + get_msg_id());
486                if(ccaddress_full)
487                        ccaddress_full.style.display = 'none';
488        }
489        return;
490}
491
492function get_msg_id()
493{
494        children = Element('border_tr').childNodes;
495       
496        for (var i=0; i<children.length; i++)
497        {
498                if ( (children[i].nodeName==='TD') && (children[i].className==='menu-sel') && children[i].id != 'border_id_0')
499                {
500                        var border_selected = children[i];
501                        var msg_id = border_selected.id.replace("border_id_","");
502                        return msg_id;
503                }
504        }
505        return false;
506}
507
508function select_msg(msg_number, keyboard_action, force_msg_selection)
509{
510    /*
511          ** Se caso for limpado toda a caixa de email,
512          ** é adicionado um novo atalho de seleção.
513          ** main.js on function refrash and line 629.
514        */
515       
516        if(keyboard_action == "reload_msg"){
517           if( $("#tbody_box .selected_shortcut_msg").length == 0 )
518                $("#tbody_box tr:first").addClass("selected_shortcut_msg");     
519        }
520       
521        shift_up_count = 0;
522        shift_down_count = 0;
523
524        if (msg_number != 'null') {
525       
526                if(Element(msg_number)){
527                        unselect_all_msgs();
528                        add_className(Element(msg_number), 'selected_shortcut_msg');
529                }
530               
531        } else {
532                var scrollMain = Element('divScrollMain_0');
533                var selection_size = parseInt(preferences.line_height) + 10;
534               
535                        if( keyboard_action == 'down') {
536                       
537                                if(!Element("chk_box_select_all_messages").checked){
538                                       
539                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
540                                       
541                                                if($(this).hasClass("selected_shortcut_msg") && $(this).next().length){
542                                                        $(this).next().addClass("selected_shortcut_msg");
543                                                        $(this).removeClass("selected_shortcut_msg");
544                                                        return false;
545                                                }
546                                               
547                                        });
548                                       
549                                } else {
550                               
551                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
552                                               
553                                                if($(this).hasClass("current_selected") && $(this).next().length){
554                                                        $(this).removeClass("current_selected");
555                                                        $(this).removeClass("selected_shortcut_msg");
556                                                        $(this).next().addClass("current_selected");
557                                                        $(this).next().addClass("selected_shortcut_msg");
558                                                        return false;
559                                                }
560                                       
561                                        });
562                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
563                                                                if(!$(this).hasClass("current_selected"))
564                                                                        $(this).removeClass("selected_shortcut_msg");
565                                        });
566                                }
567                       
568                        } else if( keyboard_action == 'up') {
569                       
570                                if(!Element("chk_box_select_all_messages").checked){
571                               
572                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
573                                       
574                                                if($(this).hasClass("selected_shortcut_msg") && $(this).prev().length){
575                                                                $(this).prev().addClass("selected_shortcut_msg");
576                                                                $(this).removeClass("selected_shortcut_msg");
577                                                                return false;
578                                                }
579                                               
580                                        });
581                                       
582                                } else {
583                                       
584                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
585                                               
586                                                if($(this).hasClass("current_selected") && $(this).prev().length){
587                                                        $(this).removeClass("current_selected");
588                                                        $(this).removeClass("selected_shortcut_msg");
589                                                        $(this).prev().addClass("current_selected");
590                                                        $(this).prev().addClass("selected_shortcut_msg");
591                                                        return false;
592                                                }
593                                       
594                                        });
595                                        $("#divScrollMain_0").find("#tbody_box").find("tr").each(function(){
596                                                                if(!$(this).hasClass("current_selected"))
597                                                                        $(this).removeClass("selected_shortcut_msg");
598                                        });
599                               
600                                }
601                 
602                        }
603                return true;
604        }
605}
606
607function select_bottom_msg()
608{
609        all_messages = Element('tbody_box').childNodes;
610       
611        if ( exist_className(all_messages[all_messages.length-1], 'selected_shortcut_msg') )
612                return;
613       
614        for (var i=all_messages.length-1; i >=0; i--)
615        {
616                if ( (exist_className(all_messages[i], 'selected_shortcut_msg')) && (i+1 <= all_messages.length-1) )
617                {
618                        shift_down_count++;
619                        add_className(all_messages[i+1], 'selected_shortcut_msg');
620                        break;
621                }
622        }
623}
624
625function select_top_msg()
626{
627        all_messages = Element('tbody_box').childNodes;
628               
629        if ( exist_className(all_messages[0], 'selected_shortcut_msg') )
630                return;
631       
632        for (var i=0; i <=all_messages.length-1; i++)
633        {
634                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
635                {
636                        shift_up_count++;
637                        add_className(all_messages[i-1], 'selected_shortcut_msg');
638                        break;
639                }
640        }
641}
642
643function unselect_bottom_msg()
644{
645        all_messages = Element('tbody_box').childNodes;
646        for (var i=all_messages.length-1; i >=0; i--)
647        {
648                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
649                {
650                        shift_down_count--;
651                        remove_className(all_messages[i], 'selected_shortcut_msg');
652                        break;
653                }
654        }
655}
656
657function unselect_top_msg()
658{
659        all_messages = Element('tbody_box').childNodes;
660        for (var i=0; i <=all_messages.length-1; i++)
661        {
662                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
663                {
664                        shift_up_count--;
665                        remove_className(all_messages[i], 'selected_shortcut_msg');
666                        break;
667                }
668        }
669}
670
671function unselect_all_msgs()
672{
673        all_messages = Element('tbody_box').childNodes;
674        for (var i=0; i <=all_messages.length-1; i++)
675        {
676                remove_className(all_messages[i], 'selected_shortcut_msg');
677        }
678}
Note: See TracBrowser for help on using the repository browser.