1
YUI.add('widget-autohide', function (Y, NAME) {
4
* A widget-level extension that provides ability to hide widget when
5
* certain events occur.
7
* @module widget-autohide
8
* @author eferraiuolo, tilomitra
13
var WIDGET_AUTOHIDE = 'widgetAutohide',
14
AUTOHIDE = 'autohide',
15
CLICK_OUTSIDE = 'clickoutside',
16
FOCUS_OUTSIDE = 'focusoutside',
17
DOCUMENT = 'document',
22
RENDERED = "rendered",
23
BOUNDING_BOX = "boundingBox",
27
getCN = Y.ClassNameManager.getClassName;
30
* The WidgetAutohide class provides the hideOn attribute which can
31
* be used to hide the widget when certain events occur.
33
* @class WidgetAutohide
34
* @param {Object} config User configuration object
36
function WidgetAutohide(config) {
37
Y.after(this._bindUIAutohide, this, BIND_UI);
38
Y.after(this._syncUIAutohide, this, SYNC_UI);
41
if (this.get(RENDERED)) {
42
this._bindUIAutohide();
43
this._syncUIAutohide();
49
* Static property used to define the default attribute
50
* configuration introduced by WidgetAutohide.
56
WidgetAutohide.ATTRS = {
63
* @description An array of objects corresponding to the nodes, events, and keycodes to hide the widget on.
64
* The implementer can supply an array of objects, with each object having the following properties:
65
* <p>eventName: (string, required): The eventName to listen to.</p>
66
* <p>node: (Y.Node, optional): The Y.Node that will fire the event (defaults to the boundingBox of the widget)</p>
67
* <p>keyCode: (string, optional): If listening for key events, specify the keyCode</p>
68
* <p>By default, this attribute consists of one object which will cause the widget to hide if the
69
* escape key is pressed.</p>
72
validator: Y.Lang.isArray,
73
valueFn : function() {
76
node: Y.one(DOCUMENT),
85
WidgetAutohide.prototype = {
86
// *** Instance Members *** //
88
_uiHandlesAutohide : null,
90
// *** Lifecycle Methods *** //
92
destructor : function () {
94
this._detachUIHandlesAutohide();
98
* Binds event listeners to the widget.
100
* This method in invoked after bindUI is invoked for the Widget class
101
* using YUI's aop infrastructure.
103
* @method _bindUIAutohide
106
_bindUIAutohide : function () {
108
this.after(VISIBLE+CHANGE, this._afterHostVisibleChangeAutohide);
109
this.after("hideOnChange", this._afterHideOnChange);
113
* Syncs up the widget based on its current state. In particular, removes event listeners if
114
* widget is not visible, and attaches them otherwise.
116
* This method in invoked after syncUI is invoked for the Widget class
117
* using YUI's aop infrastructure.
119
* @method _syncUIAutohide
122
_syncUIAutohide : function () {
124
this._uiSetHostVisibleAutohide(this.get(VISIBLE));
127
// *** Private Methods *** //
130
* Removes event listeners if widget is not visible, and attaches them otherwise.
132
* @method _uiSetHostVisibleAutohide
135
_uiSetHostVisibleAutohide : function (visible) {
138
//this._attachUIHandlesAutohide();
139
Y.later(1, this, '_attachUIHandlesAutohide');
141
this._detachUIHandlesAutohide();
146
* Iterates through all objects in the hideOn attribute and creates event listeners.
148
* @method _attachUIHandlesAutohide
151
_attachUIHandlesAutohide : function () {
153
if (this._uiHandlesAutohide) { return; }
155
var bb = this.get(BOUNDING_BOX),
156
hide = Y.bind(this.hide,this),
159
hideOn = this.get('hideOn'),
161
o = {node: undefined, ev: undefined, keyCode: undefined};
163
//push all events on which the widget should be hidden
164
for (; i < hideOn.length; i++) {
166
o.node = hideOn[i].node;
167
o.ev = hideOn[i].eventName;
168
o.keyCode = hideOn[i].keyCode;
170
//no keycode or node defined
171
if (!o.node && !o.keyCode && o.ev) {
172
uiHandles.push(bb.on(o.ev, hide));
175
//node defined, no keycode (not a keypress)
176
else if (o.node && !o.keyCode && o.ev) {
177
uiHandles.push(o.node.on(o.ev, hide));
180
//node defined, keycode defined, event defined (its a key press)
181
else if (o.node && o.keyCode && o.ev) {
182
uiHandles.push(o.node.on(o.ev, hide, o.keyCode));
190
this._uiHandlesAutohide = uiHandles;
194
* Detaches all event listeners created by this extension
196
* @method _detachUIHandlesAutohide
199
_detachUIHandlesAutohide : function () {
201
Y.each(this._uiHandlesAutohide, function(h){
204
this._uiHandlesAutohide = null;
208
* Default function called when the visibility of the widget changes. Determines
209
* whether to attach or detach event listeners based on the visibility of the widget.
211
* @method _afterHostVisibleChangeAutohide
214
_afterHostVisibleChangeAutohide : function (e) {
216
this._uiSetHostVisibleAutohide(e.newVal);
220
* Default function called when hideOn Attribute is changed. Remove existing listeners and create new listeners.
222
* @method _afterHideOnChange
225
_afterHideOnChange : function(e) {
226
this._detachUIHandlesAutohide();
228
if (this.get(VISIBLE)) {
229
this._attachUIHandlesAutohide();
234
Y.WidgetAutohide = WidgetAutohide;
237
}, '@VERSION@', {"requires": ["base-build", "event-key", "event-outside", "widget"]});