Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Created July 17, 2011 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junaidpv/1087017 to your computer and use it in GitHub Desktop.
Save junaidpv/1087017 to your computer and use it in GitHub Desktop.
Using .live() method of jQuery.
Index: ext.narayam.core.js
--- ext.narayam.core.js Base (BASE)
+++ ext.narayam.core.js Locally Modified (Based On LOCAL)
@@ -137,6 +137,14 @@
return text;
}
+ function changeVisual( $element ) {
+ if ( enabled ) {
+ $element.addClass( 'narayam-input' );
+ } else {
+ $element.removeClass( 'narayam-input' );
+ }
+ }
+
/**
* Keydown event handler. Handles shortcut key presses
* @param e Event object
@@ -148,6 +156,7 @@
return false; // Not in original code -- does this belong here?
} else if ( isShortcutKey( e ) ) {
that.toggle();
+ changeVisual( $( this ));
e.stopPropagation();
return false;
}
@@ -223,6 +232,15 @@
return false;
}
+ function onfocus( e ) {
+ $( this ).data( 'narayamKeyBuffer', '' );
+ changeVisual( $( this ) );
+ }
+
+ function onblur( e ) {
+ $( this ).removeClass( 'narayam-input' );
+ }
+
/**
* Change handler for the scheme dropdown. Updates the current scheme
* based on the new selection in the dropdown.
@@ -240,14 +258,20 @@
* or an array of DOM elements, or a single DOM element, or a selector
*/
this.addInputs = function( inputs ) {
+ if (typeof( inputs ) == "string" ) {
+ $( inputs )
+ .live('keydown', onkeydown)
+ .live('keypress', onkeypress)
+ .live('focus', onfocus)
+ .live('blur', onblur);
+ } else {
var $newInputs = $( inputs );
$inputs = $inputs.add( $newInputs );
$newInputs
.bind( 'keydown.narayam', onkeydown )
.bind( 'keypress.narayam', onkeypress )
- .data( 'narayamKeyBuffer', '' );
- if ( enabled ) {
- $newInputs.addClass( 'narayam-input' );
+ .bind( 'focus', onfocus)
+ .bind( 'blur', onblur);
}
};
@@ -268,7 +292,6 @@
*/
this.disable = function() {
if ( enabled ) {
- $inputs.removeClass( 'narayam-input' );
$.cookie( 'narayam-enabled', '0', { 'path': '/', 'expires': 30 } );
$( '#narayam-toggle' ).attr( 'checked', false );
enabled = false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment