~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/event/event-key.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
7
 
*/
8
 
YUI.add('event-key', function(Y) {
9
 
 
10
 
/**
11
 
 * Functionality to listen for one or more specific key combinations.
12
 
 * @module event
13
 
 * @submodule event-key
14
 
 */
15
 
 
16
 
/**
17
 
 * Add a key listener.  The listener will only be notified if the
18
 
 * keystroke detected meets the supplied specification.  The
19
 
 * spec consists of the key event type, followed by a colon,
20
 
 * followed by zero or more comma separated key codes, followed
21
 
 * by zero or more modifiers delimited by a plus sign.  Ex:
22
 
 * press:12,65+shift+ctrl
23
 
 * @event key
24
 
 * @for YUI
25
 
 * @param type {string} 'key'
26
 
 * @param fn {function} the function to execute
27
 
 * @param id {string|HTMLElement|collection} the element(s) to bind
28
 
 * @param spec {string} the keyCode and modifier specification
29
 
 * @param o optional context object
30
 
 * @param args 0..n additional arguments to provide to the listener.
31
 
 * @return {Event.Handle} the detach handle
32
 
 */
33
 
Y.Env.evt.plugins.key = {
34
 
 
35
 
    on: function(type, fn, id, spec, o) {
36
 
        var a = Y.Array(arguments, 0, true), parsed, etype, criteria, ename;
37
 
 
38
 
        parsed = spec && spec.split(':');
39
 
 
40
 
        if (!spec || spec.indexOf(':') == -1 || !parsed[1]) {
41
 
            a[0] = 'key' + ((parsed && parsed[0]) || 'press');
42
 
            return Y.on.apply(Y, a);
43
 
        }
44
 
 
45
 
        // key event type: 'down', 'up', or 'press'
46
 
        etype = parsed[0];
47
 
 
48
 
        // list of key codes optionally followed by modifiers
49
 
        criteria = (parsed[1]) ? parsed[1].split(/,|\+/) : null;
50
 
 
51
 
        // the name of the custom event that will be created for the spec
52
 
        ename = (Y.Lang.isString(id) ? id : Y.stamp(id)) + spec;
53
 
 
54
 
        ename = ename.replace(/,/g, '_');
55
 
 
56
 
        if (!Y.getEvent(ename)) {
57
 
 
58
 
            // subscribe spec validator to the DOM event
59
 
            Y.on(type + etype, function(e) {
60
 
 
61
 
                
62
 
                var passed = false, failed = false, i, crit, critInt;
63
 
 
64
 
                for (i=0; i<criteria.length; i=i+1) {
65
 
                    crit = criteria[i]; 
66
 
                    critInt = parseInt(crit, 10);
67
 
 
68
 
                    // pass this section if any supplied keyCode 
69
 
                    // is found
70
 
                    if (Y.Lang.isNumber(critInt)) {
71
 
 
72
 
                        if (e.charCode === critInt) {
73
 
                            passed = true;
74
 
                        } else {
75
 
                            failed = true;
76
 
                        }
77
 
 
78
 
                    // only check modifier if no keyCode was specified
79
 
                    // or the keyCode check was successful.  pass only 
80
 
                    // if every modifier passes
81
 
                    } else if (passed || !failed) {
82
 
                        passed = (e[crit + 'Key']);
83
 
                        failed = !passed;
84
 
                    }                    
85
 
                }
86
 
 
87
 
                // fire spec custom event if spec if met
88
 
                if (passed) {
89
 
                    Y.fire(ename, e);
90
 
                }
91
 
 
92
 
            }, id);
93
 
 
94
 
        }
95
 
 
96
 
        // subscribe supplied listener to custom event for spec validator
97
 
        // remove element and spec.
98
 
        a.splice(2, 2);
99
 
        a[0] = ename;
100
 
 
101
 
        return Y.on.apply(Y, a);
102
 
    }
103
 
};
104
 
 
105
 
 
106
 
}, '3.2.0' ,{requires:['node-base']});