~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/ext-2.2.1/source/util/KeyNav.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 2.2.1
 
3
 * Copyright(c) 2006-2009, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
10
 * @class Ext.KeyNav
 
11
 * <p>Provides a convenient wrapper for normalized keyboard navigation.  KeyNav allows you to bind
 
12
 * navigation keys to function calls that will get called when the keys are pressed, providing an easy
 
13
 * way to implement custom navigation schemes for any UI component.</p>
 
14
 * <p>The following are all of the possible keys that can be implemented: enter, left, right, up, down, tab, esc,
 
15
 * pageUp, pageDown, del, home, end.  Usage:</p>
 
16
 <pre><code>
 
17
var nav = new Ext.KeyNav("my-element", {
 
18
    "left" : function(e){
 
19
        this.moveLeft(e.ctrlKey);
 
20
    },
 
21
    "right" : function(e){
 
22
        this.moveRight(e.ctrlKey);
 
23
    },
 
24
    "enter" : function(e){
 
25
        this.save();
 
26
    },
 
27
    scope : this
 
28
});
 
29
</code></pre>
 
30
 * @constructor
 
31
 * @param {Mixed} el The element to bind to
 
32
 * @param {Object} config The config
 
33
 */
 
34
Ext.KeyNav = function(el, config){
 
35
    this.el = Ext.get(el);
 
36
    Ext.apply(this, config);
 
37
    if(!this.disabled){
 
38
        this.disabled = true;
 
39
        this.enable();
 
40
    }
 
41
};
 
42
 
 
43
Ext.KeyNav.prototype = {
 
44
    /**
 
45
     * @cfg {Boolean} disabled
 
46
     * True to disable this KeyNav instance (defaults to false)
 
47
     */
 
48
    disabled : false,
 
49
    /**
 
50
     * @cfg {String} defaultEventAction
 
51
     * The method to call on the {@link Ext.EventObject} after this KeyNav intercepts a key.  Valid values are
 
52
     * {@link Ext.EventObject#stopEvent}, {@link Ext.EventObject#preventDefault} and
 
53
     * {@link Ext.EventObject#stopPropagation} (defaults to 'stopEvent')
 
54
     */
 
55
    defaultEventAction: "stopEvent",
 
56
    /**
 
57
     * @cfg {Boolean} forceKeyDown
 
58
     * Handle the keydown event instead of keypress (defaults to false).  KeyNav automatically does this for IE since
 
59
     * IE does not propagate special keys on keypress, but setting this to true will force other browsers to also
 
60
     * handle keydown instead of keypress.
 
61
     */
 
62
    forceKeyDown : false,
 
63
 
 
64
    // private
 
65
    prepareEvent : function(e){
 
66
        var k = e.getKey();
 
67
        var h = this.keyToHandler[k];
 
68
        //if(h && this[h]){
 
69
        //    e.stopPropagation();
 
70
        //}
 
71
        if(Ext.isSafari2 && h && k >= 37 && k <= 40){
 
72
            e.stopEvent();
 
73
        }
 
74
    },
 
75
 
 
76
    // private
 
77
    relay : function(e){
 
78
        var k = e.getKey();
 
79
        var h = this.keyToHandler[k];
 
80
        if(h && this[h]){
 
81
            if(this.doRelay(e, this[h], h) !== true){
 
82
                e[this.defaultEventAction]();
 
83
            }
 
84
        }
 
85
    },
 
86
 
 
87
    // private
 
88
    doRelay : function(e, h, hname){
 
89
        return h.call(this.scope || this, e);
 
90
    },
 
91
 
 
92
    // possible handlers
 
93
    enter : false,
 
94
    left : false,
 
95
    right : false,
 
96
    up : false,
 
97
    down : false,
 
98
    tab : false,
 
99
    esc : false,
 
100
    pageUp : false,
 
101
    pageDown : false,
 
102
    del : false,
 
103
    home : false,
 
104
    end : false,
 
105
 
 
106
    // quick lookup hash
 
107
    keyToHandler : {
 
108
        37 : "left",
 
109
        39 : "right",
 
110
        38 : "up",
 
111
        40 : "down",
 
112
        33 : "pageUp",
 
113
        34 : "pageDown",
 
114
        46 : "del",
 
115
        36 : "home",
 
116
        35 : "end",
 
117
        13 : "enter",
 
118
        27 : "esc",
 
119
        9  : "tab"
 
120
    },
 
121
 
 
122
        /**
 
123
         * Enable this KeyNav
 
124
         */
 
125
        enable: function(){
 
126
                if(this.disabled){
 
127
            if(this.forceKeyDown || Ext.isIE || Ext.isSafari3 || Ext.isAir){
 
128
                this.el.on("keydown", this.relay,  this);
 
129
            }else{
 
130
                this.el.on("keydown", this.prepareEvent,  this);
 
131
                this.el.on("keypress", this.relay,  this);
 
132
            }
 
133
                    this.disabled = false;
 
134
                }
 
135
        },
 
136
 
 
137
        /**
 
138
         * Disable this KeyNav
 
139
         */
 
140
        disable: function(){
 
141
                if(!this.disabled){
 
142
                    if(this.forceKeyDown || Ext.isIE || Ext.isSafari3 || Ext.isAir){
 
143
                this.el.un("keydown", this.relay, this);
 
144
            }else{
 
145
                this.el.un("keydown", this.prepareEvent, this);
 
146
                this.el.un("keypress", this.relay, this);
 
147
            }
 
148
                    this.disabled = true;
 
149
                }
 
150
        }
 
151
};
 
 
b'\\ No newline at end of file'