Created
May 9, 2012 16:45
-
-
Save ThomasLomas/2646535 to your computer and use it in GitHub Desktop.
Meteor And Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////// Helpers for in-place editing ////////// | |
// Returns an event_map key for attaching "ok/cancel" events to | |
// a text input (given by selector) | |
var okcancel_events = function (selector) { | |
return 'keyup '+selector+', keydown '+selector+', focusout '+selector; | |
}; | |
// Creates an event handler for interpreting "escape", "return", and "blur" | |
// on a text field and calling "ok" or "cancel" callbacks. | |
var make_okcancel_handler = function (options) { | |
var ok = options.ok || function () {}; | |
var cancel = options.cancel || function () {}; | |
return function (evt) { | |
if (evt.type === "keydown" && evt.which === 27) { | |
// escape = cancel | |
cancel.call(this, evt); | |
} else if (evt.type === "keyup" && evt.which === 13) { | |
// blur/return/enter = ok/submit if non-empty | |
var value = String(evt.target.value || ""); | |
if (value) | |
ok.call(this, value, evt); | |
else | |
cancel.call(this, evt); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment