source: trunk/expressoMail1_2/js/shortcut.js @ 5891

Revision 5891, 16.8 KB checked in by thiago, 12 years ago (diff)

Ticket #2599 - O problema era que ainda não tinha sido tratado na shortcut.js

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