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

Revision 139, 12.2 KB checked in by niltonneto, 16 years ago (diff)

Vide ocorrencias no Trac.

  • 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;
228shortcut.add("N",function(){ new_message("new","null"); },{'disable_in_input':true});
229
230shortcut.add("Esc",function(){
231        var window_closed = false;
232        for(var window in arrayJSWin)
233        {
234                if (arrayJSWin[window].visible)
235                {
236                        arrayJSWin[window].close();
237                        window_closed = true;
238                }
239        }
240        if (!window_closed)
241                delete_border(get_msg_id(), 'false');
242},{'disable_in_input':false});
243
244shortcut.add("I",function(){ exec_msg_action('print'); },{'disable_in_input':true});
245shortcut.add("E",function(){ exec_msg_action('forward'); },{'disable_in_input':true});
246shortcut.add("R",function(){ exec_msg_action('reply'); },{'disable_in_input':true});
247shortcut.add("Delete",function(){
248        if ( Element('border_id_0').className==='menu-sel' )
249        {
250                var selected_msgs = '';
251                var current_folder = get_current_folder();
252                all_messages = Element('tbody_box').childNodes;
253               
254                for (var i=0; i < all_messages.length; i++)
255                        if (all_messages[i].style.backgroundColor != '')
256                                selected_msgs += all_messages[i].id + ',';
257               
258                selected_msgs = selected_msgs.substring(0,(selected_msgs.length-1));
259                delete_msgs(current_folder, selected_msgs, 'null');
260        }
261        else
262                exec_msg_action('delete');
263}
264,{'disable_in_input':true});
265
266shortcut.add("Ctrl+Up",function(){ exec_msg_action('previous'); },{'disable_in_input':true});
267shortcut.add("Ctrl+Down",function(){ exec_msg_action('next'); },{'disable_in_input':true});
268if (is_ie){
269        shortcut.add("down",function(){ select_msg('null', 'down'); },{'disable_in_input':false});
270        shortcut.add("up",function(){ select_msg('null', 'up'); },{'disable_in_input':false});
271        shortcut.add("Shift+down",function(){
272                if (shift_up_count > 0)
273                        unselect_top_msg();
274                else
275                        select_bottom_msg();
276        },{'disable_in_input':false, 'propagate':false});
277        shortcut.add("Shift+up",function(){
278                if (shift_down_count > 0)
279                        unselect_bottom_msg();
280                else
281                        select_top_msg();
282        },{'disable_in_input':false, 'propagate':false});
283}
284else{
285        shortcut.add("down",function(){ select_msg('null', 'down'); },{'type':'keypress', 'disable_in_input':false});
286        shortcut.add("up",function(){ select_msg('null', 'up'); },{'type':'keypress', 'disable_in_input':false});
287        shortcut.add("Shift+down",function(){
288                if (shift_up_count > 0)
289                        unselect_top_msg();
290                else
291                        select_bottom_msg();
292        },{'type':'keypress','disable_in_input':false, 'propagate':false});
293        shortcut.add("Shift+up",function(){
294                if (shift_down_count > 0)
295                        unselect_bottom_msg();
296                else
297                        select_top_msg();
298        },{'type':'keypress','disable_in_input':false, 'propagate':false});
299}
300
301shortcut.add("return",function(){
302        if ( Element('border_id_0').className==='menu-sel' )
303        {
304                all_messages = Element('tbody_box').childNodes;
305                for (var i=0; i < all_messages.length; i++)
306                {
307                        if (all_messages[i].style.backgroundColor != '')
308                        {
309                                Element("td_who_" + all_messages[i].id).onclick();
310                                return;
311                        }
312                }
313        }
314}
315,{'disable_in_input':true});
316
317shortcut.add("f9",function(){
318        Element("em_refresh_button").onclick();
319        return;
320},{'disable_in_input':true});
321
322function exec_msg_action(action)
323{
324        var msg_id = get_msg_id();
325        if (msg_id)
326        {
327                var msg_id = 'msg_opt_' + action + '_' + msg_id;
328                try {Element(msg_id).onclick();}
329                catch(e){/*alert(e);*/}
330        }
331        return;
332}
333
334function get_msg_id()
335{
336        children = Element('border_tr').childNodes;
337       
338        for (var i=0; i<children.length; i++)
339        {
340                if ( (children[i].nodeName==='TD') && (children[i].className==='menu-sel') && children[i].id != 'border_id_0')
341                {
342                        var border_selected = children[i];
343                        var msg_id = border_selected.id.replace("border_id_","");
344                        return msg_id;
345                }
346        }
347        return false;
348}
349
350function select_msg(msg_number, keyboard_action)
351{
352        if ( Element('border_id_0').className != 'menu-sel' )
353                return;
354       
355        shift_up_count = 0;
356        shift_down_count = 0;
357
358        if (msg_number != 'null')
359        {
360                var selected_msg = Element(msg_number);
361                all_messages = selected_msg.parentNode.childNodes;
362                for (var i=0; i < all_messages.length; i++)
363                {
364                        if (all_messages[i].style.backgroundColor != '')
365                        {
366                                all_messages[i].style.color = '';
367                                all_messages[i].style.backgroundColor = '';
368                                break;
369                        }
370                }
371                selected_msg.style.color = 'white';
372                selected_msg.style.backgroundColor = '#5194d2';
373        }
374        else
375        {
376                all_messages = Element('tbody_box').childNodes;
377                if (keyboard_action == 'down')
378                {
379                        if (all_messages[all_messages.length-1].style.backgroundColor != '')
380                                return false;
381                       
382                        for (var i=all_messages.length-1; i >=0; i--)
383                        {
384                                if (all_messages[i].style.backgroundColor != '')
385                                {
386                                        all_messages[i+1].style.color = 'white';
387                                        all_messages[i+1].style.backgroundColor = '#5194d2';
388                                        break;
389                                }
390                        }
391                        for (; i>=0; i--)
392                        {
393                                all_messages[i].style.color = '';
394                                all_messages[i].style.backgroundColor = '';
395                        }
396                        return true;
397                }
398                else
399                {
400                        if (all_messages[0].style.backgroundColor != '')
401                                return false;
402                       
403                        for (var i=0; i < all_messages.length; i++)
404                        {
405                                if (all_messages[i].style.backgroundColor != '')
406                                {
407                                        all_messages[i-1].style.color = 'white';
408                                        all_messages[i-1].style.backgroundColor = '#5194d2';
409                                        break;
410                                }
411                        }
412                        for (; i< all_messages.length; i++)
413                        {
414                                all_messages[i].style.color = '';
415                                all_messages[i].style.backgroundColor = '';
416                        }
417                        return true;
418                }
419        }
420}
421
422function select_bottom_msg()
423{
424        all_messages = Element('tbody_box').childNodes;
425       
426        if (all_messages[all_messages.length-1].style.backgroundColor != '')
427                return;
428       
429        for (var i=all_messages.length-1; i >=0; i--)
430        {
431                if ((all_messages[i].style.backgroundColor != '') && (i+1 <= all_messages.length-1))
432                {
433                        shift_down_count++;
434                        all_messages[i+1].style.color = 'white';
435                        all_messages[i+1].style.backgroundColor = '#5194d2';
436                        break;
437                }
438        }
439}
440
441function select_top_msg()
442{
443        all_messages = Element('tbody_box').childNodes;
444       
445        if (all_messages[0].style.backgroundColor != '')
446                return;
447       
448        for (var i=0; i <=all_messages.length-1; i++)
449        {
450                if (all_messages[i].style.backgroundColor != '')
451                {
452                        shift_up_count++;
453                        all_messages[i-1].style.color = 'white';
454                        all_messages[i-1].style.backgroundColor = '#5194d2';
455                        break;
456                }
457        }
458}
459
460function unselect_bottom_msg()
461{
462        all_messages = Element('tbody_box').childNodes;
463        for (var i=all_messages.length-1; i >=0; i--)
464        {
465                if (all_messages[i].style.backgroundColor != '')
466                {
467                        shift_down_count--;
468                        all_messages[i].style.color = '';
469                        all_messages[i].style.backgroundColor = '';
470                        break;
471                }
472        }
473}
474
475function unselect_top_msg()
476{
477        all_messages = Element('tbody_box').childNodes;
478        for (var i=0; i <=all_messages.length-1; i++)
479        {
480                if (all_messages[i].style.backgroundColor != '')
481                {
482                        shift_up_count--;
483                        all_messages[i].style.color = '';
484                        all_messages[i].style.backgroundColor = '';
485                        break;
486                }
487        }
488}
Note: See TracBrowser for help on using the repository browser.