Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions notebook/static/notebook/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,22 @@ define(function(require){
}
}
},
'cell-style-centered' : {
help: 'select cell style centered ',
icon: 'cell-style',
help_index : 'el',
handler : function (env) {
env.notebook.set_cell_style('center');
}
},
'cell-style-split' : {
help: 'select cell style split ',
icon: 'cell-style',
help_index : 'el',
handler : function (env) {
env.notebook.set_cell_style('right');
}
},
'show-command-palette': {
help_index : 'aa',
help: 'open the command palette',
Expand Down
30 changes: 30 additions & 0 deletions notebook/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ define([
options = options || {};
this.keyboard_manager = options.keyboard_manager;
this.events = options.events;
this.cell_style = options.cell_style;

var config = utils.mergeopt(Cell, options.config);
// superclass default overwrite our default

Expand All @@ -56,6 +58,7 @@ define([
this.anchor = false;
this.rendered = false;
this.mode = 'command';


// Metadata property
var that = this;
Expand All @@ -81,6 +84,7 @@ define([

// load this from metadata later ?
this.user_highlight = 'auto';



var _local_cm_config = {};
Expand Down Expand Up @@ -181,6 +185,16 @@ define([
}
};

Cell.prototype.get_cell_style_html = function(cell_style){
if (cell_style == "right")
{return "float:right; width:50%;";}
else if (cell_style == "left")
{return "float:left; width:50%;";}
else if (cell_style == "center")
{return "width:100%;";}
return cell_style
};

/**
* Subclasses can implement override bind_events.
* Be careful to call the parent method when overwriting as it fires event.
Expand Down Expand Up @@ -350,6 +364,16 @@ define([
}
};

/**
* Garbage collects unused attachments in this cell
* @method remove_unused_attachments
*/
Cell.prototype.remove_unused_attachments = function () {
// Cell subclasses which support attachments should override this
// and keep them when needed
this.attachments = {};
};

/**
* Delegates keyboard shortcut handling to either Jupyter keyboard
* manager when in command mode, or CodeMirror when in edit mode
Expand Down Expand Up @@ -483,6 +507,7 @@ define([
// deepcopy the metadata so copied cells don't share the same object
data.metadata = JSON.parse(JSON.stringify(this.metadata));
data.cell_type = this.cell_type;
data.cell_style = this.cell_style || "width=100%;";
return data;
};

Expand Down Expand Up @@ -746,6 +771,11 @@ define([
this.element = cell;
};

UnrecognizedCell.prototype.remove_unused_attachments = function () {
// Do nothing to avoid removing attachments from a possible future
// attachment-supporting cell type
};

UnrecognizedCell.prototype.bind_events = function () {
Cell.prototype.bind_events.apply(this, arguments);
var cell = this;
Expand Down
23 changes: 19 additions & 4 deletions notebook/static/notebook/js/codecell.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define([
this.events = options.events;
this.tooltip = options.tooltip;
this.config = options.config;
this.cell_style = options.cell_style;
this.class_config = new configmod.ConfigWithDefaults(this.config,
CodeCell.config_defaults, 'CodeCell');

Expand All @@ -110,7 +111,8 @@ define([
Cell.apply(this,[{
config: $.extend({}, CodeCell.options_default),
keyboard_manager: options.keyboard_manager,
events: this.events}]);
events: this.events,
cell_style:this.cell_style}]);

// Attributes we want to override in this subclass.
this.cell_type = "code";
Expand Down Expand Up @@ -146,14 +148,16 @@ define([
CodeCell.msg_cells = {};

CodeCell.prototype = Object.create(Cell.prototype);

/** @method create_element */
CodeCell.prototype.create_element = function () {
Cell.prototype.create_element.apply(this, arguments);
var that = this;

var cell = $('<div></div>').addClass('cell code_cell');
cell.attr('tabindex','2');
var cell_style_html = Cell.prototype.get_cell_style_html.apply(this, [this.cell_style]);

cell.attr({'tabindex':'2', 'style':cell_style_html});

var input = $('<div></div>').addClass('input');
this.input = input;
Expand Down Expand Up @@ -512,16 +516,25 @@ define([
this.code_mirror.clearHistory();
this.auto_highlight();
}
if (data.cell_style !== undefined){
this.cell_style = data.cell_style ;
}
else {this.cell_style = 'width=100%;';}

this.set_input_prompt(data.execution_count);
this.output_area.trusted = data.metadata.trusted || false;

this.output_area.fromJSON(data.outputs, data.metadata);
}
};




CodeCell.prototype.toJSON = function () {
var data = Cell.prototype.toJSON.apply(this);
data.source = this.get_text();

// is finite protect against undefined and '*' value
if (isFinite(this.input_prompt_number)) {
data.execution_count = this.input_prompt_number;
Expand All @@ -530,17 +543,19 @@ define([
}
var outputs = this.output_area.toJSON();
data.outputs = outputs;

data.metadata.trusted = this.output_area.trusted;
data.metadata.collapsed = this.output_area.collapsed;
if (this.output_area.scroll_state === 'auto') {
delete data.metadata.scrolled;
} else {
data.metadata.scrolled = this.output_area.scroll_state;
}

return data;
};

/**
/** `
* handle cell level logic when the cell is unselected
* @method unselect
* @return is the action being taken
Expand Down
2 changes: 2 additions & 0 deletions notebook/static/notebook/js/menubar.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ define([
'#copy_cell_attachments': 'copy-cell-attachments',
'#paste_cell_attachments': 'paste-cell-attachments',
'#insert_image': 'insert-image',
'#cell_style_centered':'cell-style-centered',
'#cell_style_split':'cell-style-split',
};

for(var idx in id_actions_dict){
Expand Down
66 changes: 55 additions & 11 deletions notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ define(function (require) {
this.keyboard_manager = options.keyboard_manager;
this.contents = options.contents;
this.save_widget = options.save_widget;
this.cell_style = "center";

this.tooltip = new tooltip.Tooltip(this.events);
this.ws_url = options.ws_url;
this._session_starting = false;
this.last_modified = null;

// debug 484
this._last_modified = 'init';
// Firefox workaround
Expand Down Expand Up @@ -170,14 +173,15 @@ define(function (require) {
// 'above', 'below', or 'selected' to get the value from another cell.
default_cell_type: 'code'
};

/**
* Create an HTML and CSS representation of the notebook.
*/
Notebook.prototype.create_elements = function () {
var that = this;
this.element.attr('tabindex','-1');
this.container = $("<div/>").addClass("container").attr("id", "notebook-container");
this.container = $("<div/>").addClass("container").attr({"id":"notebook-container"});

// We add this end_space div to the end of the notebook div to:
// i) provide a margin between the last cell and the end of the notebook
// ii) to prevent the div from scrolling up when the last cell is being
Expand Down Expand Up @@ -303,7 +307,7 @@ define(function (require) {
var new_height = app_height - pager_height - splitter_height;
that.element.animate({height : new_height + 'px'}, time);
};

this.element.bind('expand_pager', function (event, extrap) {
var time = (extrap !== undefined) ? ((extrap.duration !== undefined ) ? extrap.duration : 'fast') : 'fast';
expand_time(time);
Expand Down Expand Up @@ -1115,6 +1119,15 @@ define(function (require) {
}
};

Notebook.prototype.set_cell_style = function(cell_style){
this.cell_style = cell_style
};

Notebook.prototype.cycle_cell_style_side = function(cycle_style){
if (this.cell_style == "right"){this.cell_style = "left";}
else if (this.cell_style == "left"){this.cell_style ="right"}
};

/**
* Insert a cell so that after insertion the cell is at given index.
*
Expand All @@ -1131,6 +1144,7 @@ define(function (require) {
*/
Notebook.prototype.insert_cell_at_index = function(type, index){


var ncells = this.ncells();
index = Math.min(index, ncells);
index = Math.max(index, 0);
Expand Down Expand Up @@ -1158,7 +1172,8 @@ define(function (require) {
config: this.config,
keyboard_manager: this.keyboard_manager,
notebook: this,
tooltip: this.tooltip
tooltip: this.tooltip,
cell_style: this.cell_style,
};
switch(type) {
case 'code':
Expand Down Expand Up @@ -1239,6 +1254,9 @@ define(function (require) {
if (index === null || index === undefined) {
index = Math.min(this.get_selected_index(index), this.get_anchor_index());
}

this.cycle_cell_style_side()

return this.insert_cell_at_index(type, index);
};

Expand All @@ -1254,6 +1272,9 @@ define(function (require) {
if (index === null || index === undefined) {
index = Math.max(this.get_selected_index(index), this.get_anchor_index());
}

this.cycle_cell_style_side()

return this.insert_cell_at_index(type, index+1);
};

Expand Down Expand Up @@ -2403,9 +2424,19 @@ define(function (require) {
};

/**
Move the unused attachments garbage collection logic to TextCell.toJSON.
* Garbage collects unused attachments in all the cells
*/
Notebook.prototype.remove_unused_attachments = function() {
var cells = this.get_cells();
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
cell.remove_unused_attachments();
}
};

/**
* Load a notebook from JSON (.ipynb).
*
*
* @param {object} data - JSON representation of a notebook
*/
Notebook.prototype.fromJSON = function (data) {
Expand Down Expand Up @@ -2437,6 +2468,7 @@ define(function (require) {
var new_cell = null;
for (i=0; i<ncells; i++) {
cell_data = new_cells[i];
this.cell_style = cell_data.cell_style || 'center'
new_cell = this.insert_cell_at_index(cell_data.cell_type, i);
new_cell.fromJSON(cell_data);
if (new_cell.cell_type === 'code' && !new_cell.output_area.trusted) {
Expand Down Expand Up @@ -2468,7 +2500,7 @@ define(function (require) {
if (cell.cell_type === 'code' && !cell.output_area.trusted) {
trusted = false;
}
cell_array[i] = cell.toJSON(true);
cell_array[i] = cell.toJSON();
}
var data = {
cells: cell_array,
Expand Down Expand Up @@ -2517,12 +2549,17 @@ define(function (require) {
* Save this notebook on the server. This becomes a notebook instance's
* .save_notebook method *after* the entire notebook has been loaded.
*
* manual_save will be true if the save was manually trigered by the user
*/
Notebook.prototype.save_notebook = function (check_last_modified) {
Notebook.prototype.save_notebook = function (check_last_modified,
manual_save) {
if (check_last_modified === undefined) {
check_last_modified = true;
}

if (manual_save === undefined) {
manual_save = false;
}

var error;
if (!this._fully_loaded) {
error = new Error("Load failed, save is disabled");
Expand All @@ -2538,6 +2575,13 @@ define(function (require) {
// the notebook as needed.
this.events.trigger('before_save.Notebook');

// Garbage collect unused attachments. Only do this for manual save
// to avoid removing unused attachments while the user is editing if
// an autosave gets triggered in the midle of an edit
if (manual_save) {
this.remove_unused_attachments();
}

// Create a JSON model to be sent to the server.
var model = {
type : "notebook",
Expand Down Expand Up @@ -2721,7 +2765,7 @@ define(function (require) {
var parent = utils.url_path_split(this.notebook_path)[0];
var p;
if (this.dirty) {
p = this.save_notebook(true);
p = this.save_notebook(true, true);
} else {
p = Promise.resolve();
}
Expand Down Expand Up @@ -2991,7 +3035,7 @@ define(function (require) {
*/
Notebook.prototype.save_checkpoint = function () {
this._checkpoint_after_save = true;
this.save_notebook(true);
this.save_notebook(true, true);
};

/**
Expand Down
Loading