Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('autocomplete-list-keys', function (Y, NAME) {
2
 
3
/**
4
Mixes keyboard support into AutoCompleteList. By default, this module is not
5
loaded for iOS and Android devices.
6
 
7
@module autocomplete
8
@submodule autocomplete-list-keys
9
**/
10
 
11
 // keyCode constants.
12
var KEY_DOWN  = 40,
13
    KEY_ENTER = 13,
14
    KEY_ESC   = 27,
15
    KEY_TAB   = 9,
16
    KEY_UP    = 38;
17
 
18
function ListKeys() {
19
    Y.after(this._bindKeys, this, 'bindUI');
20
    this._initKeys();
21
}
22
 
23
ListKeys.prototype = {
24
    // -- Lifecycle Methods ----------------------------------------------------
25
 
26
    /**
27
    Initializes keyboard command mappings.
28
 
29
    @method _initKeys
30
    @protected
31
    @for AutoCompleteList
32
    **/
33
    _initKeys: function () {
34
        var keys        = {},
35
            keysVisible = {};
36
 
37
        // Register keyboard command handlers. _keys contains handlers that will
38
        // always be called; _keysVisible contains handlers that will only be
39
        // called when the list is visible.
40
        keys[KEY_DOWN] = this._keyDown;
41
 
42
        keysVisible[KEY_ENTER] = this._keyEnter;
43
        keysVisible[KEY_ESC]   = this._keyEsc;
44
        keysVisible[KEY_TAB]   = this._keyTab;
45
        keysVisible[KEY_UP]    = this._keyUp;
46
 
47
        this._keys        = keys;
48
        this._keysVisible = keysVisible;
49
    },
50
 
51
    destructor: function () {
52
        this._unbindKeys();
53
    },
54
 
55
    /**
56
    Binds keyboard events.
57
 
58
    @method _bindKeys
59
    @protected
60
    **/
61
    _bindKeys: function () {
62
        this._keyEvents = this._inputNode.on('keydown', this._onInputKey, this);
63
    },
64
 
65
    /**
66
    Unbinds keyboard events.
67
 
68
    @method _unbindKeys
69
    @protected
70
    **/
71
    _unbindKeys: function () {
72
        this._keyEvents && this._keyEvents.detach();
73
        this._keyEvents = null;
74
    },
75
 
76
    // -- Protected Methods ----------------------------------------------------
77
 
78
    /**
79
    Called when the down arrow key is pressed.
80
 
81
    @method _keyDown
82
    @protected
83
    **/
84
    _keyDown: function () {
85
        if (this.get('visible')) {
86
            this._activateNextItem();
87
        } else {
88
            this.show();
89
        }
90
    },
91
 
92
    /**
93
    Called when the enter key is pressed.
94
 
95
    @method _keyEnter
96
    @protected
97
    **/
98
    _keyEnter: function (e) {
99
        var item = this.get('activeItem');
100
 
101
        if (item) {
102
            this.selectItem(item, e);
103
        } else {
104
            // Don't prevent form submission when there's no active item.
105
            return false;
106
        }
107
    },
108
 
109
    /**
110
    Called when the escape key is pressed.
111
 
112
    @method _keyEsc
113
    @protected
114
    **/
115
    _keyEsc: function () {
116
        this.hide();
117
    },
118
 
119
    /**
120
    Called when the tab key is pressed.
121
 
122
    @method _keyTab
123
    @protected
124
    **/
125
    _keyTab: function (e) {
126
        var item;
127
 
128
        if (this.get('tabSelect')) {
129
            item = this.get('activeItem');
130
 
131
            if (item) {
132
                this.selectItem(item, e);
133
                return true;
134
            }
135
        }
136
 
137
        return false;
138
    },
139
 
140
    /**
141
    Called when the up arrow key is pressed.
142
 
143
    @method _keyUp
144
    @protected
145
    **/
146
    _keyUp: function () {
147
        this._activatePrevItem();
148
    },
149
 
150
    // -- Protected Event Handlers ---------------------------------------------
151
 
152
    /**
153
    Handles `inputNode` key events.
154
 
155
    @method _onInputKey
156
    @param {EventTarget} e
157
    @protected
158
    **/
159
    _onInputKey: function (e) {
160
        var handler,
161
            keyCode = e.keyCode;
162
 
163
        this._lastInputKey = keyCode;
164
 
165
        if (this.get('results').length) {
166
            handler = this._keys[keyCode];
167
 
168
            if (!handler && this.get('visible')) {
169
                handler = this._keysVisible[keyCode];
170
            }
171
 
172
            if (handler) {
173
                // A handler may return false to indicate that it doesn't wish
174
                // to prevent the default key behavior.
175
                if (handler.call(this, e) !== false) {
176
                    e.preventDefault();
177
                }
178
            }
179
        }
180
    }
181
};
182
 
183
Y.Base.mix(Y.AutoCompleteList, [ListKeys]);
184
 
185
 
186
}, '3.18.1', {"requires": ["autocomplete-list", "base-build"]});