Added cell unfocus event canceller API

This commit is contained in:
Jonathan Frederic 2014-02-25 09:32:57 -08:00
parent a6aaa8a886
commit 10d79a9102
3 changed files with 55 additions and 6 deletions

View File

@ -48,7 +48,7 @@ var IPython = (function (IPython) {
this.cell_id = utils.uuid();
this._options = options;
// For JS VM engines optimisation, attributes should be all set (even
// For JS VM engines optimization, attributes should be all set (even
// to null) in the constructor, and if possible, if different subclass
// have new attributes with same name, they should be created in the
// same order. Easiest is to create and set to null in parent class.
@ -57,6 +57,10 @@ var IPython = (function (IPython) {
this.cell_type = this.cell_type || null;
this.code_mirror = null;
// This is a list of callbacks that are called when a cell's textual
// region is unfocused. If one of the callbacks returns True, the cell
// unfocus event will be ignored. Callbacks will be passed no arguments.
this.cancel_unfocus_callbacks = [];
this.create_element();
if (this.element !== null) {
@ -264,6 +268,17 @@ var IPython = (function (IPython) {
}
};
/**
* Check if this cell's unfocus event was legit.
*/
Cell.prototype.should_cancel_unfocus = function () {
// Try user registered callbacks.
for (var i=0; i<this.cancel_unfocus_callbacks.length; i++) {
if (this.cancel_unfocus_callbacks[i]()) { return true; }
}
return false;
};
/**
* Focus the cell in the DOM sense
* @method focus_cell

View File

@ -418,6 +418,17 @@ var IPython = (function (IPython) {
return cont;
};
/**
* Check if this cell's unfocus event was legit.
*/
CodeCell.prototype.should_cancel_unfocus = function () {
// Call base
if (IPython.Cell.prototype.should_cancel_unfocus.apply(this)) { return true; }
// Cancel this unfocus event if the cell completer is open.
return (this.completer && this.completer.is_visible());
};
/**
* Focus the editor area so a user can type
* @method focus_editor

View File

@ -55,6 +55,12 @@ var IPython = (function (IPython) {
this.notebook_name_blacklist_re = /[\/\\:]/;
this.nbformat = 3; // Increment this when changing the nbformat
this.nbformat_minor = 0; // Increment this when changing the nbformat
// This is a list of callbacks that are called when a cell's textual
// region is unfocused. If one of the callbacks returns True, the cell
// unfocus event will be ignored. Callbacks will be passed one argument,
// the cell instance.
this.cancel_unfocus_callbacks = [];
this.style();
this.create_elements();
this.bind_events();
@ -581,11 +587,8 @@ var IPython = (function (IPython) {
var cell = this.get_cell(index);
if (!cell) {return;}
// Only respect the blur event if the tooltip and autocompleter are
// not visible.
var tooltip_visible = IPython.tooltip && IPython.tooltip.is_visible();
var completer_visible = cell.completer && cell.completer.is_visible();
if (!tooltip_visible && !completer_visible) {
// Check if this unfocus event is legit.
if (!this.should_cancel_unfocus(cell)) {
// In Firefox the focus event is called before the blur event. In
// other words, two cells elements may be focused at any given time.
// This has been witnessed on Win7 x64 w/ FF 25. Here we only put the
@ -603,6 +606,26 @@ var IPython = (function (IPython) {
}
};
Notebook.prototype.should_cancel_unfocus = function (cell) {
// Determine whether or not the unfocus event should be aknowledged.
// If the tooltip is visible, ignore the unfocus.
var tooltip_visible = IPython.tooltip && IPython.tooltip.is_visible();
if (tooltip_visible) { return true; }
// Try user registered callbacks.
for (var i=0; i<this.cancel_unfocus_callbacks.length; i++) {
if (this.cancel_unfocus_callbacks[i](cell)) { return true; }
}
// Check the cell's should_cancel_unfocus method.
if (cell.should_cancel_unfocus !== undefined && cell.should_cancel_unfocus()) {
return true;
} else {
return false;
}
};
// Cell movement
/**