global element events. automatically focus on click.

This commit is contained in:
Christopher Jeffrey 2013-06-09 15:21:58 -05:00
parent b04f8d04b2
commit a19d9baf48
2 changed files with 36 additions and 14 deletions

View File

@ -190,6 +190,10 @@ Screen.prototype._listenMouse = function(el, hover) {
self.program.disableMouse();
});
//this.on('element click', function(el) {
// el.focus();
//});
this.program.on('mouse', function(data) {
var i = 0, left, top, el;
for (; i < self.clickable.length; i++) {
@ -200,12 +204,16 @@ Screen.prototype._listenMouse = function(el, hover) {
if (data.x > left && data.x <= left + el.width
&& data.y > top && data.y <= top + el.height) {
el.emit('mouse', data);
self.emit('element mouse', el, data);
if (data.action === 'mouseup') {
el.emit('click', data);
self.emit('element click', el, data);
} else if (data.action === 'movement') {
el.emit('hover', data);
self.emit('element hover', el, data);
}
el.emit(data.action, data);
self.emit('element ' + data.action, data);
}
}
self.emit('mouse', data);
@ -218,12 +226,12 @@ Screen.prototype._listenKeys = function(el) {
if (el) {
if (!~this.input.indexOf(el)) {
if (this._listenedMouse) {
//this._listenMouse(el);
el.on('click', el.focus.bind(el));
}
this.input.push(el);
}
//if (this.mouse)
//el.on('click', function() {
// el.focus();
//});
}
if (this._listenedKeys) return;
@ -525,6 +533,7 @@ function Element(options) {
Element.prototype.__proto__ = Node.prototype;
// TODO: Possibly move this to Node for screen.on('mouse', ...).
Element._addListener = Element.prototype.addListener;
Element.prototype.on =
Element.prototype.addListener = function(type, listener) {
@ -545,6 +554,23 @@ Element.prototype.addListener = function(type, listener) {
return Element._addListener.apply(this, arguments);
};
/*
Element._emit = Element.prototype.emit;
Element.prototype.emit = function(type) {
var args = Array.prototype.slice.call(arguments)
, ret = Element._emit.apply(this, args);
if (this.screen) {
args.shift();
args.unshift(this);
args.unshift('element ' + type);
this.screen.emit.apply(this.screen, args);
}
return ret;
};
*/
Element.prototype.hide = function() {
var ret = this.render(true);
this.hidden = true;

View File

@ -103,10 +103,6 @@ list.on('keypress', function(ch, key) {
}
});
list.on('click', function() {
list.focus();
});
var progress = new blessed.ProgressBar({
fg: 4,
bg: -1,
@ -152,10 +148,6 @@ var stext = new blessed.ScrollableText({
bottom: 0
});
stext.on('click', function() {
stext.focus();
});
screen.append(stext);
stext.on('keypress', function(ch, key) {
if (key.name === 'up' || key.name === 'k') {
@ -219,12 +211,16 @@ screen.on('keypress', function(ch, key) {
list.focus();
//screen.on('element click', function(el) {
// el.focus();
//});
screen.render();
setInterval(function() {
stext.toggle();
progress.toggle();
screen.render();
}, 1000);
}, 2000);
(function fill() {
if (progress.filled === 100) {