Skip to content

Instantly share code, notes, and snippets.

@jviereck
Created March 11, 2010 20:17
Show Gist options
  • Save jviereck/329601 to your computer and use it in GitHub Desktop.
Save jviereck/329601 to your computer and use it in GitHub Desktop.
var SC = require('sproutcore/runtime').SC;
exports.ReFocus = {
_rf_view: null,
setup: function() {
SC.RootResponder.responder.addObserver('hasFocus', this, function() {
if (SC.RootResponder.responder.get('hasFocus') &&
!SC.none(exports.ReFocus._rf_view)) {
if (SC.none(SC.RootResponder.responder.keyPane.firstResponder)) {
console.log(SC.RootResponder.responder.keyPane);
exports.ReFocus._rf_view.becomeFirstResponder();
}
}
});
},
willLoseKeyResponderTo: function(responder) {
arguments.callee.base.apply(this, arguments);
exports.ReFocus._rf_view = null;
// If the responder is null then the focus is lost to an non-SC-component.
// This can happen, because
// 1) another DOM element on the page
// OR
// 2) the entire page/window lost the focus
//
if (SC.none(responder)) {
setTimeout(function() {
if (!SC.RootResponder.responder.hasFocus) {
exports.ReFocus._rf_view = this;
}
}.bind(this), 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment