Skip to content

Commit

Permalink
Merge pull request #5146 from jdfreder/modal-fix
Browse files Browse the repository at this point in the history
Dual mode bug fixes.
  • Loading branch information
ellisonbg committed Feb 28, 2014
2 parents 4b9f156 + 8b012de commit edfc0fa
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 197 deletions.
2 changes: 1 addition & 1 deletion IPython/html/static/components
89 changes: 41 additions & 48 deletions IPython/html/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -57,7 +57,6 @@ var IPython = (function (IPython) {
this.cell_type = this.cell_type || null;
this.code_mirror = null;


this.create_element();
if (this.element !== null) {
this.element.data("cell", this);
Expand All @@ -76,20 +75,16 @@ var IPython = (function (IPython) {

// FIXME: Workaround CM Bug #332 (Safari segfault on drag)
// by disabling drag/drop altogether on Safari
// https://github.com/marijnh/CodeMirror/issues/332

// https://github.com/marijnh/CodeMirror/issues/332
if (utils.browser[0] == "Safari") {
Cell.options_default.cm_config.dragDrop = false;
}

Cell.prototype.mergeopt = function(_class, options, overwrite){
options = options || {};
overwrite = overwrite || {};
return $.extend(true, {}, _class.options_default, options, overwrite)

}


return $.extend(true, {}, _class.options_default, options, overwrite);
};

/**
* Empty. Subclasses must implement create_element.
Expand Down Expand Up @@ -118,8 +113,7 @@ var IPython = (function (IPython) {
} else {
this.element.addClass('command_mode');
}
}

};

/**
* Subclasses can implement override bind_events.
Expand All @@ -133,12 +127,12 @@ var IPython = (function (IPython) {
that.element.click(function (event) {
if (!that.selected) {
$([IPython.events]).trigger('select.Cell', {'cell':that});
};
}
});
that.element.focusin(function (event) {
if (!that.selected) {
$([IPython.events]).trigger('select.Cell', {'cell':that});
};
}
});
if (this.code_mirror) {
this.code_mirror.on("change", function(cm, change) {
Expand All @@ -152,17 +146,9 @@ var IPython = (function (IPython) {
}
if (this.code_mirror) {
this.code_mirror.on('blur', function(cm, change) {
if (that.mode === 'edit') {
setTimeout(function () {
var isf = IPython.utils.is_focused;
var trigger = true;
if (isf('div#tooltip') || isf('div.completions')) {
trigger = false;
}
if (trigger) {
$([IPython.events]).trigger('command_mode.Cell', {cell: that});
}
}, 1);
// Check if this unfocus event is legit.
if (!that.should_cancel_blur()) {
$([IPython.events]).trigger('command_mode.Cell', {cell: that});
}
});
}
Expand Down Expand Up @@ -273,32 +259,38 @@ var IPython = (function (IPython) {
} else {
return false;
}
}
};

/**
* Determine whether or not the unfocus event should be aknowledged.
*
* @method should_cancel_blur
*
* @return results {bool} Whether or not to ignore the cell's blur event.
**/
Cell.prototype.should_cancel_blur = function () {
return false;
};

/**
* Focus the cell in the DOM sense
* @method focus_cell
*/
Cell.prototype.focus_cell = function () {
this.element.focus();
}
};

/**
* Focus the editor area so a user can type
*
* NOTE: If codemirror is focused via a mouse click event, you don't want to
* call this because it will cause a page jump.
* @method focus_editor
*/
Cell.prototype.focus_editor = function () {
var that = this;
this.refresh();
// Only focus the CM editor if it is not focused already. This prevents jumps
// related to the previous prompt position.
setTimeout(function () {
var isf = IPython.utils.is_focused;
if (!isf(that.element.find('div.CodeMirror'))) {
that.code_mirror.focus();
}
}, 1);
}
this.code_mirror.focus();
};

/**
* Refresh codemirror instance
Expand Down Expand Up @@ -375,7 +367,7 @@ var IPython = (function (IPython) {
var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
text = text.replace(/^\n+/, '').replace(/\n+$/, '');
return text;
}
};


/**
Expand All @@ -386,7 +378,7 @@ var IPython = (function (IPython) {
var cursor = this.code_mirror.getCursor();
var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
var end = {line:last_line_num, ch:last_line_len}
var end = {line:last_line_num, ch:last_line_len};
var text = this.code_mirror.getRange(cursor, end);
text = text.replace(/^\n+/, '').replace(/\n+$/, '');
return text;
Expand Down Expand Up @@ -430,32 +422,33 @@ var IPython = (function (IPython) {
**/
Cell.prototype._auto_highlight = function (modes) {
//Here we handle manually selected modes
if( this.user_highlight != undefined && this.user_highlight != 'auto' )
var mode;
if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
{
var mode = this.user_highlight;
mode = this.user_highlight;
CodeMirror.autoLoadMode(this.code_mirror, mode);
this.code_mirror.setOption('mode', mode);
return;
}
var current_mode = this.code_mirror.getOption('mode', mode);
var first_line = this.code_mirror.getLine(0);
// loop on every pairs
for( var mode in modes) {
var regs = modes[mode]['reg'];
for(mode in modes) {
var regs = modes[mode].reg;
// only one key every time but regexp can't be keys...
for(var i=0; i<regs.length; i++) {
// here we handle non magic_modes
if(first_line.match(regs[i]) != null) {
if(first_line.match(regs[i]) !== null) {
if(current_mode == mode){
return;
}
if (mode.search('magic_') != 0) {
if (mode.search('magic_') !== 0) {
this.code_mirror.setOption('mode', mode);
CodeMirror.autoLoadMode(this.code_mirror, mode);
return;
}
var open = modes[mode]['open']|| "%%";
var close = modes[mode]['close']|| "%%end";
var open = modes[mode].open || "%%";
var close = modes[mode].close || "%%end";
var mmode = mode;
mode = mmode.substr(6);
if(current_mode == mode){
Expand All @@ -482,14 +475,14 @@ var IPython = (function (IPython) {
}
}
// fallback on default
var default_mode
var default_mode;
try {
default_mode = this._options.cm_config.mode;
} catch(e) {
default_mode = 'text/plain';
}
if( current_mode === default_mode){
return
return;
}
this.code_mirror.setOption('mode', default_mode);
};
Expand Down
21 changes: 14 additions & 7 deletions IPython/html/static/notebook/js/codecell.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,20 @@ var IPython = (function (IPython) {
return false;
};

CodeCell.prototype.edit_mode = function () {
var cont = IPython.Cell.prototype.edit_mode.apply(this);
if (cont) {
this.focus_editor();
}
return cont;
}
/**
* Determine whether or not the unfocus event should be aknowledged.
*
* @method should_cancel_blur
*
* @return results {bool} Whether or not to ignore the cell's blur event.
**/
CodeCell.prototype.should_cancel_blur = function () {
// Cancel this unfocus event if the base wants to cancel or the cell
// completer is open or the tooltip is open.
return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
(this.completer && this.completer.is_visible()) ||
(IPython.tooltip && IPython.tooltip.is_visible());
};

CodeCell.prototype.select_all = function () {
var start = {line: 0, ch: 0};
Expand Down
7 changes: 7 additions & 0 deletions IPython/html/static/notebook/js/completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var IPython = (function (IPython) {


var Completer = function (cell) {
this._visible = false;
this.cell = cell;
this.editor = cell.code_mirror;
var that = this;
Expand All @@ -84,6 +85,10 @@ var IPython = (function (IPython) {
});
};

Completer.prototype.is_visible = function () {
// Return whether or not the completer is visible.
return this._visible;
};

Completer.prototype.startCompletion = function () {
// call for a 'first' completion, that will set the editor and do some
Expand Down Expand Up @@ -225,6 +230,7 @@ var IPython = (function (IPython) {
.attr('multiple', 'true')
.attr('size', Math.min(10, this.raw_result.length));
this.complete.append(this.sel);
this._visible = true;
$('body').append(this.complete);

// After everything is on the page, compute the postion.
Expand Down Expand Up @@ -282,6 +288,7 @@ var IPython = (function (IPython) {
};

Completer.prototype.close = function () {
this._visible = false;
if (this.done) return;
this.done = true;
$('.completions').remove();
Expand Down

0 comments on commit edfc0fa

Please sign in to comment.