mirror of
https://github.com/jquery/jquery-ui.git
synced 2025-01-07 20:34:24 +00:00
Widget: Added _off() for removing event handlers. Fixes #7795 - Widget: _on and _off.
This commit is contained in:
parent
00d4beb0ca
commit
ff39bed57a
@ -767,6 +767,83 @@ test( "_on() to common element", function() {
|
|||||||
$( document ).trigger( "customevent" );
|
$( document ).trigger( "customevent" );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test( "_off() - single event", function() {
|
||||||
|
expect( 3 );
|
||||||
|
|
||||||
|
$.widget( "ui.testWidget", {} );
|
||||||
|
var shouldTriggerWidget, shouldTriggerOther,
|
||||||
|
element = $( "#widget" ),
|
||||||
|
widget = element.testWidget().data( "testWidget" );
|
||||||
|
widget._on( element, { foo: function() {
|
||||||
|
ok( shouldTriggerWidget, "foo called from _on" );
|
||||||
|
}});
|
||||||
|
element.bind( "foo", function() {
|
||||||
|
ok( shouldTriggerOther, "foo called from bind" );
|
||||||
|
});
|
||||||
|
shouldTriggerWidget = true;
|
||||||
|
shouldTriggerOther = true;
|
||||||
|
element.trigger( "foo" );
|
||||||
|
shouldTriggerWidget = false;
|
||||||
|
widget._off( element, "foo" );
|
||||||
|
element.trigger( "foo" );
|
||||||
|
});
|
||||||
|
|
||||||
|
test( "_off() - multiple events", function() {
|
||||||
|
expect( 6 );
|
||||||
|
|
||||||
|
$.widget( "ui.testWidget", {} );
|
||||||
|
var shouldTriggerWidget, shouldTriggerOther,
|
||||||
|
element = $( "#widget" ),
|
||||||
|
widget = element.testWidget().data( "testWidget" );
|
||||||
|
widget._on( element, {
|
||||||
|
foo: function() {
|
||||||
|
ok( shouldTriggerWidget, "foo called from _on" );
|
||||||
|
},
|
||||||
|
bar: function() {
|
||||||
|
ok( shouldTriggerWidget, "bar called from _on" );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
element.bind( "foo bar", function( event ) {
|
||||||
|
ok( shouldTriggerOther, event.type + " called from bind" );
|
||||||
|
});
|
||||||
|
shouldTriggerWidget = true;
|
||||||
|
shouldTriggerOther = true;
|
||||||
|
element.trigger( "foo" );
|
||||||
|
element.trigger( "bar" );
|
||||||
|
shouldTriggerWidget = false;
|
||||||
|
widget._off( element, "foo bar" );
|
||||||
|
element.trigger( "foo" );
|
||||||
|
element.trigger( "bar" );
|
||||||
|
});
|
||||||
|
|
||||||
|
test( "_off() - all events", function() {
|
||||||
|
expect( 6 );
|
||||||
|
|
||||||
|
$.widget( "ui.testWidget", {} );
|
||||||
|
var shouldTriggerWidget, shouldTriggerOther,
|
||||||
|
element = $( "#widget" ),
|
||||||
|
widget = element.testWidget().data( "testWidget" );
|
||||||
|
widget._on( element, {
|
||||||
|
foo: function() {
|
||||||
|
ok( shouldTriggerWidget, "foo called from _on" );
|
||||||
|
},
|
||||||
|
bar: function() {
|
||||||
|
ok( shouldTriggerWidget, "bar called from _on" );
|
||||||
|
}
|
||||||
|
});
|
||||||
|
element.bind( "foo bar", function( event ) {
|
||||||
|
ok( shouldTriggerOther, event.type + " called from bind" );
|
||||||
|
});
|
||||||
|
shouldTriggerWidget = true;
|
||||||
|
shouldTriggerOther = true;
|
||||||
|
element.trigger( "foo" );
|
||||||
|
element.trigger( "bar" );
|
||||||
|
shouldTriggerWidget = false;
|
||||||
|
widget._off( element );
|
||||||
|
element.trigger( "foo" );
|
||||||
|
element.trigger( "bar" );
|
||||||
|
});
|
||||||
|
|
||||||
test( "._hoverable()", function() {
|
test( "._hoverable()", function() {
|
||||||
$.widget( "ui.testWidget", {
|
$.widget( "ui.testWidget", {
|
||||||
_create: function() {
|
_create: function() {
|
||||||
|
3
ui/jquery.ui.accordion.js
vendored
3
ui/jquery.ui.accordion.js
vendored
@ -219,8 +219,7 @@ $.widget( "ui.accordion", {
|
|||||||
|
|
||||||
if ( key === "event" ) {
|
if ( key === "event" ) {
|
||||||
if ( this.options.event ) {
|
if ( this.options.event ) {
|
||||||
this.headers.unbind(
|
this._off( this.headers, this.options.event );
|
||||||
this.options.event.split( " " ).join( ".accordion " ) + ".accordion" );
|
|
||||||
}
|
}
|
||||||
this._setupEvents( value );
|
this._setupEvents( value );
|
||||||
}
|
}
|
||||||
|
32
ui/jquery.ui.button.js
vendored
32
ui/jquery.ui.button.js
vendored
@ -54,8 +54,8 @@ $.widget( "ui.button", {
|
|||||||
},
|
},
|
||||||
_create: function() {
|
_create: function() {
|
||||||
this.element.closest( "form" )
|
this.element.closest( "form" )
|
||||||
.unbind( "reset.button" )
|
.unbind( "reset" + this.eventNamespace )
|
||||||
.bind( "reset.button", formResetHandler );
|
.bind( "reset" + this.eventNamespace, formResetHandler );
|
||||||
|
|
||||||
if ( typeof this.options.disabled !== "boolean" ) {
|
if ( typeof this.options.disabled !== "boolean" ) {
|
||||||
this.options.disabled = !!this.element.prop( "disabled" );
|
this.options.disabled = !!this.element.prop( "disabled" );
|
||||||
@ -79,7 +79,7 @@ $.widget( "ui.button", {
|
|||||||
this.buttonElement
|
this.buttonElement
|
||||||
.addClass( baseClasses )
|
.addClass( baseClasses )
|
||||||
.attr( "role", "button" )
|
.attr( "role", "button" )
|
||||||
.bind( "mouseenter.button", function() {
|
.bind( "mouseenter" + this.eventNamespace, function() {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -88,13 +88,13 @@ $.widget( "ui.button", {
|
|||||||
$( this ).addClass( "ui-state-active" );
|
$( this ).addClass( "ui-state-active" );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind( "mouseleave.button", function() {
|
.bind( "mouseleave" + this.eventNamespace, function() {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$( this ).removeClass( hoverClass );
|
$( this ).removeClass( hoverClass );
|
||||||
})
|
})
|
||||||
.bind( "click.button", function( event ) {
|
.bind( "click" + this.eventNamespace, function( event ) {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
event.stopImmediatePropagation();
|
event.stopImmediatePropagation();
|
||||||
@ -102,16 +102,16 @@ $.widget( "ui.button", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.element
|
this.element
|
||||||
.bind( "focus.button", function() {
|
.bind( "focus" + this.eventNamespace, function() {
|
||||||
// no need to check disabled, focus won't be triggered anyway
|
// no need to check disabled, focus won't be triggered anyway
|
||||||
that.buttonElement.addClass( focusClass );
|
that.buttonElement.addClass( focusClass );
|
||||||
})
|
})
|
||||||
.bind( "blur.button", function() {
|
.bind( "blur" + this.eventNamespace, function() {
|
||||||
that.buttonElement.removeClass( focusClass );
|
that.buttonElement.removeClass( focusClass );
|
||||||
});
|
});
|
||||||
|
|
||||||
if ( toggleButton ) {
|
if ( toggleButton ) {
|
||||||
this.element.bind( "change.button", function() {
|
this.element.bind( "change" + this.eventNamespace, function() {
|
||||||
if ( clickDragged ) {
|
if ( clickDragged ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -121,7 +121,7 @@ $.widget( "ui.button", {
|
|||||||
// prevents issue where button state changes but checkbox/radio checked state
|
// prevents issue where button state changes but checkbox/radio checked state
|
||||||
// does not in Firefox (see ticket #6970)
|
// does not in Firefox (see ticket #6970)
|
||||||
this.buttonElement
|
this.buttonElement
|
||||||
.bind( "mousedown.button", function( event ) {
|
.bind( "mousedown" + this.eventNamespace, function( event ) {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -129,7 +129,7 @@ $.widget( "ui.button", {
|
|||||||
startXPos = event.pageX;
|
startXPos = event.pageX;
|
||||||
startYPos = event.pageY;
|
startYPos = event.pageY;
|
||||||
})
|
})
|
||||||
.bind( "mouseup.button", function( event ) {
|
.bind( "mouseup" + this.eventNamespace, function( event ) {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -140,7 +140,7 @@ $.widget( "ui.button", {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( this.type === "checkbox" ) {
|
if ( this.type === "checkbox" ) {
|
||||||
this.buttonElement.bind( "click.button", function() {
|
this.buttonElement.bind( "click" + this.eventNamespace, function() {
|
||||||
if ( options.disabled || clickDragged ) {
|
if ( options.disabled || clickDragged ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -148,7 +148,7 @@ $.widget( "ui.button", {
|
|||||||
that.buttonElement.attr( "aria-pressed", that.element[0].checked );
|
that.buttonElement.attr( "aria-pressed", that.element[0].checked );
|
||||||
});
|
});
|
||||||
} else if ( this.type === "radio" ) {
|
} else if ( this.type === "radio" ) {
|
||||||
this.buttonElement.bind( "click.button", function() {
|
this.buttonElement.bind( "click" + this.eventNamespace, function() {
|
||||||
if ( options.disabled || clickDragged ) {
|
if ( options.disabled || clickDragged ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -166,7 +166,7 @@ $.widget( "ui.button", {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.buttonElement
|
this.buttonElement
|
||||||
.bind( "mousedown.button", function() {
|
.bind( "mousedown" + this.eventNamespace, function() {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -176,13 +176,13 @@ $.widget( "ui.button", {
|
|||||||
lastActive = null;
|
lastActive = null;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.bind( "mouseup.button", function() {
|
.bind( "mouseup" + this.eventNamespace, function() {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$( this ).removeClass( "ui-state-active" );
|
$( this ).removeClass( "ui-state-active" );
|
||||||
})
|
})
|
||||||
.bind( "keydown.button", function(event) {
|
.bind( "keydown" + this.eventNamespace, function(event) {
|
||||||
if ( options.disabled ) {
|
if ( options.disabled ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ $.widget( "ui.button", {
|
|||||||
$( this ).addClass( "ui-state-active" );
|
$( this ).addClass( "ui-state-active" );
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind( "keyup.button", function() {
|
.bind( "keyup" + this.eventNamespace, function() {
|
||||||
$( this ).removeClass( "ui-state-active" );
|
$( this ).removeClass( "ui-state-active" );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
6
ui/jquery.ui.dialog.js
vendored
6
ui/jquery.ui.dialog.js
vendored
@ -224,7 +224,7 @@ $.widget("ui.dialog", {
|
|||||||
if ( this.overlay ) {
|
if ( this.overlay ) {
|
||||||
this.overlay.destroy();
|
this.overlay.destroy();
|
||||||
}
|
}
|
||||||
this.uiDialog.unbind( "keypress.ui-dialog" );
|
this._off( this.uiDialog, "keypress" );
|
||||||
|
|
||||||
if ( this.options.hide ) {
|
if ( this.options.hide ) {
|
||||||
this.uiDialog.hide( this.options.hide, function() {
|
this.uiDialog.hide( this.options.hide, function() {
|
||||||
@ -310,7 +310,7 @@ $.widget("ui.dialog", {
|
|||||||
|
|
||||||
// prevent tabbing out of modal dialogs
|
// prevent tabbing out of modal dialogs
|
||||||
if ( options.modal ) {
|
if ( options.modal ) {
|
||||||
uiDialog.bind( "keydown.ui-dialog", function( event ) {
|
this._on( uiDialog, { keydown: function( event ) {
|
||||||
if ( event.keyCode !== $.ui.keyCode.TAB ) {
|
if ( event.keyCode !== $.ui.keyCode.TAB ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -326,7 +326,7 @@ $.widget("ui.dialog", {
|
|||||||
last.focus( 1 );
|
last.focus( 1 );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
// set focus to the first tabbable element in the content area or the first button
|
// set focus to the first tabbable element in the content area or the first button
|
||||||
|
4
ui/jquery.ui.menu.js
vendored
4
ui/jquery.ui.menu.js
vendored
@ -44,7 +44,7 @@ $.widget( "ui.menu", {
|
|||||||
})
|
})
|
||||||
// need to catch all clicks on disabled menu
|
// need to catch all clicks on disabled menu
|
||||||
// not possible through _on
|
// not possible through _on
|
||||||
.bind( "click.menu", $.proxy(function( event ) {
|
.bind( "click" + this.eventNamespace, $.proxy(function( event ) {
|
||||||
if ( this.options.disabled ) {
|
if ( this.options.disabled ) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
@ -168,7 +168,7 @@ $.widget( "ui.menu", {
|
|||||||
this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
|
this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
|
||||||
|
|
||||||
// unbind currentEventTarget click event handler
|
// unbind currentEventTarget click event handler
|
||||||
$( currentEventTarget ).unbind( "click.menu" );
|
this._off( $( currentEventTarget ), "click" );
|
||||||
},
|
},
|
||||||
|
|
||||||
_keydown: function( event ) {
|
_keydown: function( event ) {
|
||||||
|
6
ui/jquery.ui.tabs.js
vendored
6
ui/jquery.ui.tabs.js
vendored
@ -58,7 +58,7 @@ $.widget( "ui.tabs", {
|
|||||||
.addClass( "ui-tabs ui-widget ui-widget-content ui-corner-all" )
|
.addClass( "ui-tabs ui-widget ui-widget-content ui-corner-all" )
|
||||||
.toggleClass( "ui-tabs-collapsible", options.collapsible )
|
.toggleClass( "ui-tabs-collapsible", options.collapsible )
|
||||||
// Prevent users from focusing disabled tabs via click
|
// Prevent users from focusing disabled tabs via click
|
||||||
.delegate( ".ui-tabs-nav > li", "mousedown.tabs", function( event ) {
|
.delegate( ".ui-tabs-nav > li", "mousedown" + this.eventNamespace, function( event ) {
|
||||||
if ( $( this ).is( ".ui-state-disabled" ) ) {
|
if ( $( this ).is( ".ui-state-disabled" ) ) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ $.widget( "ui.tabs", {
|
|||||||
// We don't have to worry about focusing the previously focused
|
// We don't have to worry about focusing the previously focused
|
||||||
// element since clicking on a non-focusable element should focus
|
// element since clicking on a non-focusable element should focus
|
||||||
// the body anyway.
|
// the body anyway.
|
||||||
.delegate( ".ui-tabs-anchor", "focus.tabs", function() {
|
.delegate( ".ui-tabs-anchor", "focus" + this.eventNamespace, function() {
|
||||||
if ( $( this ).closest( "li" ).is( ".ui-state-disabled" ) ) {
|
if ( $( this ).closest( "li" ).is( ".ui-state-disabled" ) ) {
|
||||||
this.blur();
|
this.blur();
|
||||||
}
|
}
|
||||||
@ -480,7 +480,7 @@ $.widget( "ui.tabs", {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.anchors.add( this.tabs ).add( this.panels ).unbind( ".tabs" );
|
this._off( this.anchors.add( this.tabs ).add( this.panels ) );
|
||||||
this._on( this.anchors, events );
|
this._on( this.anchors, events );
|
||||||
this._on( this.tabs, { keydown: "_tabKeydown" } );
|
this._on( this.tabs, { keydown: "_tabKeydown" } );
|
||||||
this._on( this.panels, { keydown: "_panelKeydown" } );
|
this._on( this.panels, { keydown: "_panelKeydown" } );
|
||||||
|
2
ui/jquery.ui.tooltip.js
vendored
2
ui/jquery.ui.tooltip.js
vendored
@ -233,7 +233,7 @@ $.widget( "ui.tooltip", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
target.removeData( "tooltip-open" );
|
target.removeData( "tooltip-open" );
|
||||||
target.unbind( "mouseleave.tooltip focusout.tooltip keyup.tooltip" );
|
this._off( target, "mouseleave focusout keyup" );
|
||||||
|
|
||||||
this.closing = true;
|
this.closing = true;
|
||||||
this._trigger( "close", event, { tooltip: tooltip } );
|
this._trigger( "close", event, { tooltip: tooltip } );
|
||||||
|
5
ui/jquery.ui.widget.js
vendored
5
ui/jquery.ui.widget.js
vendored
@ -387,6 +387,11 @@ $.Widget.prototype = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_off: function( element, eventName ) {
|
||||||
|
eventName = (eventName || "").split( " " ).join( this.eventNamespace + " " ) + this.eventNamespace;
|
||||||
|
element.unbind( eventName ).undelegate( eventName );
|
||||||
|
},
|
||||||
|
|
||||||
_delay: function( handler, delay ) {
|
_delay: function( handler, delay ) {
|
||||||
function handlerProxy() {
|
function handlerProxy() {
|
||||||
return ( typeof handler === "string" ? instance[ handler ] : handler )
|
return ( typeof handler === "string" ? instance[ handler ] : handler )
|
||||||
|
Loading…
Reference in New Issue
Block a user