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

Revision 5250, 16.3 KB checked in by gustavo, 12 years ago (diff)

Ticket #2381 - Sincronizar seleção de mensagens com a navegação entre mensagens

  • 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                                        e.cancelBubble = true;
187                                        e.returnValue = false;
188       
189                                        //e.stopPropagation works in Firefox.
190                                        if (e.stopPropagation) {
191                                                e.stopPropagation();
192                                                e.preventDefault();
193                                        }
194                                        return false;
195                                }
196                        }
197                }
198                this.all_shortcuts[shortcut_combination] = {
199                        'callback':func,
200                        'target':ele,
201                        'event': opt['type']
202                };
203                //Attach the function with the event
204                if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
205                else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
206                else ele['on'+opt['type']] = func;
207        },
208
209        //Remove the shortcut - just specify the shortcut and I will remove the binding
210        'remove':function(shortcut_combination) {
211                shortcut_combination = shortcut_combination.toLowerCase();
212                var binding = this.all_shortcuts[shortcut_combination];
213                delete(this.all_shortcuts[shortcut_combination])
214                if(!binding) return;
215                var type = binding['event'];
216                var ele = binding['target'];
217                var callback = binding['callback'];
218
219                if(ele.detachEvent) ele.detachEvent('on'+type, callback);
220                else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
221                else ele['on'+type] = false;
222        }
223}
224
225/* ExpressMail Functions */
226var shift_up_count = 0;
227var shift_down_count = 0;
228var selMessageShortcut = "";
229
230shortcut.add("N",function()
231{
232        // avoids problem related at ticket #1011
233        var search_in_focus = false;
234        var search_win = document.getElementById( 'QuickCatalogSearch_window_QuickCatalogSearch' );
235        if ( search_win && search_win.style.visibility != 'hidden' )
236                search_in_focus = true;
237
238        if ( ! search_in_focus )
239                new_message("new","null");
240},{'disable_in_input':true});
241
242shortcut.add("Esc",function(){
243        var window_closed = false;
244        var search_win = document.getElementById( 'window_QuickCatalogSearch' );
245       
246        for(var window in arrayJSWin)
247        {
248                if (arrayJSWin[window].visible)
249                {
250                    window_closed = true;
251                    if(search_win.style.visibility == 'hidden'){
252                        arrayJSWin[window].close();
253                    }
254                }
255        }
256        if((search_win) && (search_win.style.visibility == 'visible')){
257            search_win.style.visibility = 'hidden';
258            win.close();
259       }       
260
261        if (!window_closed)
262                delete_border(get_msg_id(), 'false');
263},{'disable_in_input':false});
264
265shortcut.add("I",function(){print_all();},{'disable_in_input':true});
266shortcut.add("E",function(){exec_msg_action('forward');},{'disable_in_input':true});
267shortcut.add("R",function(){exec_msg_action('reply');},{'disable_in_input':true});
268shortcut.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});
269shortcut.add("O",function(){show_head_option();},{'disable_in_input':true});
270shortcut.add("M",function(){show_address_full();},{'disable_in_input':true});
271
272shortcut.add("Delete",function(){
273       
274        var selected_shortcut_msgs = '';
275        var tbody_box = Element('tbody_box');
276        all_messages = Element('tbody_box').childNodes;
277       
278        for ( var i=0; i < all_messages.length; i++ )
279        {
280                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
281                {
282                        selected_shortcut_msgs += all_messages[i].id + ',';
283                       
284                        if( all_messages[i].nextSibling )
285                                selMessageShortcut = all_messages[i].nextSibling.id + "-" + "down";
286                        else if(all_messages[i].previousSibling)
287                                selMessageShortcut = all_messages[i].previousSibling.id + "-" + "up";
288                }
289        }
290       
291        selected_shortcut_msgs = selected_shortcut_msgs.substring(0,(selected_shortcut_msgs.length-1));
292       
293        if ( Element('border_id_0').className === 'menu-sel' )
294        {
295                proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
296        }
297        else
298        {
299                exec_msg_action('delete');
300                select_msg(selMessageShortcut.substring(0, selMessageShortcut.indexOf("-")),
301                                   selMessageShortcut.substring(selMessageShortcut.indexOf("-")), true );
302               
303                proxy_mensagens.delete_msgs(current_folder, selected_shortcut_msgs, 'null');
304        }
305}
306,{'disable_in_input':true});
307
308shortcut.add("Ctrl+Up",function(){exec_msg_action('previous');select_msg('null', 'up');},{'disable_in_input':true});
309shortcut.add("Ctrl+Down",function(){exec_msg_action('next');select_msg('null', 'down');},{'disable_in_input':true});
310
311if (is_ie)
312{
313//**********************
314shortcut.add('up', function()
315        {
316                var search_in_focus = false;
317                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
318                if ( search_win && search_win.style.visibility == 'visible' )
319                        search_in_focus = true;
320
321                if ( !search_in_focus )
322                    select_msg('null', 'up');
323                else
324                    shortcut.remove('up');
325                                e.stopPropagation();
326                                e.preventDefault();
327        },{'disable_in_input':false});
328
329
330        shortcut.add('down', function()
331        {
332                var search_in_focus = false;
333                var search_win = document.getElementById( 'window_QuickCatalogSearch' );
334                if ( search_win && search_win.style.visibility == 'visible' )
335                        search_in_focus = true;
336
337                if ( !search_in_focus )
338                    select_msg('null', 'down');
339                else
340                    shortcut.remove('down');
341                                e.stopPropagation();
342                                e.preventDefault();
343        },{'disable_in_input':false});
344
345//****************
346
347        shortcut.add("Shift+down",function(){
348                if ( Element('border_id_0').className==='menu-sel' )
349                {
350                        if (shift_up_count > 0)
351                                unselect_top_msg();
352                        else
353                                select_bottom_msg();
354                }
355        },{'disable_in_input':true, 'propagate':false});
356       
357        shortcut.add("Shift+up",function(){
358                if ( Element('border_id_0').className==='menu-sel' )
359                {
360                        if (shift_down_count > 0)
361                                unselect_bottom_msg();
362                        else
363                                select_top_msg();
364                }
365        },{'disable_in_input':true, 'propagate':false});
366}
367else
368{
369        shortcut.add("Up",function(){select_msg('null', 'up');},{'disable_in_input':false});
370        shortcut.add("Down",function(){select_msg('null', 'down');},{'disable_in_input':false});
371       
372        shortcut.add("Shift+down",function(){
373                if ( Element('border_id_0').className==='menu-sel' )
374                {
375                        if (shift_up_count > 0)
376                                unselect_top_msg();
377                        else
378                                select_bottom_msg();
379                }
380        },{'type':'keypress','disable_in_input':true, 'propagate':false});
381       
382        shortcut.add("Shift+up",function(){
383                if ( Element('border_id_0').className==='menu-sel' )
384                {
385                        if (shift_down_count > 0)
386                                unselect_bottom_msg();
387                        else
388                                select_top_msg();
389                }
390        },{'type':'keypress', 'disable_in_input':true, 'propagate':false});
391}
392
393shortcut.add("return",function(){
394        if ( Element('border_id_0').className==='menu-sel' )
395        {
396                all_messages = Element('tbody_box').childNodes;
397                for (var i=0; i < all_messages.length; i++)
398                {
399                        if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
400                        {
401                                Element("td_from_" + all_messages[i].id).onclick();
402                                return;
403                        }
404                }
405        }
406},{'disable_in_input':true});
407
408shortcut.add("f9",function(){
409        Element("em_refresh_button").onclick();
410        return;
411},{'disable_in_input':true});
412
413function exec_msg_action(action)
414{
415        var msg_id = get_msg_id();
416        if (msg_id)
417        {
418                var msg_id = 'msg_opt_' + action + '_' + msg_id;
419                try {Element(msg_id).onclick();}
420                catch(e){/*alert(e);*/}
421        }
422        return;
423}
424
425function show_head_option()
426{
427        var msg_id = get_msg_id();
428    if (msg_id) {
429                var msg_id = 'option_hide_more_' + msg_id;
430                try {Element(msg_id).onclick();}
431                catch(e){/*alert(e);*/}
432    }
433        return;
434}
435
436function show_address_full()
437{
438        var toaddress = Element('div_toaddress_' + get_msg_id());       
439        var ccaddress = Element('div_ccaddress_' + get_msg_id());
440       
441        if(toaddress &&  '' == toaddress.style.display) {
442                show_div_address_full(get_msg_id(),'to');
443        }
444        else {
445                if(toaddress)
446                        toaddress.style.display = '';
447                var toaddress_full = Element('div_toaddress_full_' + get_msg_id());
448                if(toaddress_full)
449                        toaddress_full.style.display = 'none';
450        }               
451        if(ccaddress &&  '' == ccaddress.style.display) {
452                show_div_address_full(get_msg_id(),'cc');
453        }
454        else {
455                if(ccaddress)
456                        ccaddress.style.display = '';
457                var ccaddress_full = Element('div_ccaddress_full_' + get_msg_id());
458                if(ccaddress_full)
459                        ccaddress_full.style.display = 'none';
460        }
461        return;
462}
463
464function get_msg_id()
465{
466        children = Element('border_tr').childNodes;
467       
468        for (var i=0; i<children.length; i++)
469        {
470                if ( (children[i].nodeName==='TD') && (children[i].className==='menu-sel') && children[i].id != 'border_id_0')
471                {
472                        var border_selected = children[i];
473                        var msg_id = border_selected.id.replace("border_id_","");
474                        return msg_id;
475                }
476        }
477        return false;
478}
479
480function select_msg(msg_number, keyboard_action, force_msg_selection)
481{
482        //Select msg, even if the focus isn't the border_id_0
483        if ( (Element('border_id_0').className != 'menu-sel') && (!force_msg_selection) )
484                return;
485
486        shift_up_count = 0;
487        shift_down_count = 0;
488
489        if (msg_number != 'null')
490        {
491                if(Element(msg_number)){
492                        unselect_all_msgs();
493                        add_className(Element(msg_number), 'selected_shortcut_msg');
494                }
495        }
496        else
497        {
498                var scrollMain = Element('divScrollMain_0');
499                var selection_size = parseInt(preferences.line_height) + 10;
500
501                if( keyboard_action == 'down')
502                {
503                        for (var i=all_messages.length-1; i >=0; i--)
504                        {
505                                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
506                                {
507                                        add_className(all_messages[i+1], 'selected_shortcut_msg')
508                                        var mailPos = i;
509                                        break;
510                                }
511                        }
512                       
513                        for (; i>=0; i--)
514                        {
515                                if( all_messages[i].nextSibling )
516                                        remove_className(all_messages[i], 'selected_shortcut_msg');
517                        }
518                        if ((mailPos) && (parseInt(scrollMain.style.height.substr(0,scrollMain.style.height.length-2)) + scrollMain.scrollTop) < (all_messages[mailPos].offsetTop + selection_size))
519                                scrollMain.scrollTop += selection_size;
520
521                }
522                else if( keyboard_action == 'up')
523                {
524                        for (var i=0; i < all_messages.length; i++)
525                        {
526                                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
527                                {
528                                        if( all_messages[i].previousSibling )
529                                        {
530                                                add_className(all_messages[i-1], 'selected_shortcut_msg')
531                                                var mailPos = i;
532                                                break;
533                                        }
534                                }
535                        }
536                       
537                        for (; i< all_messages.length; i++)
538                        {
539                                remove_className(all_messages[i], 'selected_shortcut_msg')
540                        }
541                if (typeof(all_messages[mailPos]) != 'undefined' && all_messages[mailPos].offsetTop <= scrollMain.scrollTop)
542                        scrollMain.scrollTop -= selection_size+10;
543                }
544
545                return true;
546        }
547}
548
549function select_bottom_msg()
550{
551        all_messages = Element('tbody_box').childNodes;
552       
553        if ( exist_className(all_messages[all_messages.length-1], 'selected_shortcut_msg') )
554                return;
555       
556        for (var i=all_messages.length-1; i >=0; i--)
557        {
558                if ( (exist_className(all_messages[i], 'selected_shortcut_msg')) && (i+1 <= all_messages.length-1) )
559                {
560                        shift_down_count++;
561                        add_className(all_messages[i+1], 'selected_shortcut_msg');
562                        break;
563                }
564        }
565}
566
567function select_top_msg()
568{
569        all_messages = Element('tbody_box').childNodes;
570               
571        if ( exist_className(all_messages[0], 'selected_shortcut_msg') )
572                return;
573       
574        for (var i=0; i <=all_messages.length-1; i++)
575        {
576                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
577                {
578                        shift_up_count++;
579                        add_className(all_messages[i-1], 'selected_shortcut_msg');
580                        break;
581                }
582        }
583}
584
585function unselect_bottom_msg()
586{
587        all_messages = Element('tbody_box').childNodes;
588        for (var i=all_messages.length-1; i >=0; i--)
589        {
590                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
591                {
592                        shift_down_count--;
593                        remove_className(all_messages[i], 'selected_shortcut_msg');
594                        break;
595                }
596        }
597}
598
599function unselect_top_msg()
600{
601        all_messages = Element('tbody_box').childNodes;
602        for (var i=0; i <=all_messages.length-1; i++)
603        {
604                if ( exist_className(all_messages[i], 'selected_shortcut_msg') )
605                {
606                        shift_up_count--;
607                        remove_className(all_messages[i], 'selected_shortcut_msg');
608                        break;
609                }
610        }
611}
612
613function unselect_all_msgs()
614{
615        all_messages = Element('tbody_box').childNodes;
616        for (var i=0; i <=all_messages.length-1; i++)
617        {
618                remove_className(all_messages[i], 'selected_shortcut_msg');
619        }
620}
Note: See TracBrowser for help on using the repository browser.