Skip to content

Commit

Permalink
Merge pull request ipython#3371 from minrk/dirty-event
Browse files Browse the repository at this point in the history
make setting the notebook dirty flag an event
  • Loading branch information
Carreau committed Jun 2, 2013
2 parents 03222a6 + 8600c80 commit 2a99bb6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
9 changes: 6 additions & 3 deletions IPython/frontend/html/notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ IPython.utils = (function (IPython) {
"$1<a target=\"_blank\" href=\"$2$3\">$2$3</a>");
}

// some keycodes that seem to be platform/browser independant
var keycodes ={
// some keycodes that seem to be platform/browser independent
var keycodes = {
BACKSPACE: 8,
TAB : 9,
ENTER : 13,
Expand All @@ -253,7 +253,10 @@ IPython.utils = (function (IPython) {
DOWN_ARROW: 40,
DOWNARROW: 40,
DOWN : 40,
COMMAND : 91,
// all three of these keys may be COMMAND on OS X:
LEFT_SUPER : 91,
RIGHT_SUPER : 92,
COMMAND : 93,
};


Expand Down
5 changes: 5 additions & 0 deletions IPython/frontend/html/notebook/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ var IPython = (function (IPython) {
$([IPython.events]).trigger('select.Cell', {'cell':that});
}
});
if (this.code_mirror) {
this.code_mirror.on("change", function(cm, change) {
$([IPython.events]).trigger("set_dirty.Notebook", {value: true});
});
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ var IPython = (function (IPython) {
CodeCell.prototype._handle_execute_reply = function (content) {
this.set_input_prompt(content.execution_count);
this.element.removeClass("running");
$([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
$([IPython.events]).trigger('set_dirty.Notebook', {value: true});
}

/**
Expand Down
59 changes: 37 additions & 22 deletions IPython/frontend/html/notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var IPython = (function (IPython) {
this.undelete_index = null;
this.undelete_below = false;
this.paste_enabled = false;
this.dirty = false;
this.set_dirty(false);
this.metadata = {};
this._checkpoint_after_save = false;
this.last_checkpoint = null;
Expand Down Expand Up @@ -308,10 +308,10 @@ var IPython = (function (IPython) {
} else if (that.control_key_active) {
that.control_key_active = false;
return true;
};
}
return true;
});

var collapse_time = function(time){
var app_height = $('#ipython-main-app').height(); // content height
var splitter_height = $('div#pager_splitter').outerHeight(true);
Expand Down Expand Up @@ -353,6 +353,21 @@ var IPython = (function (IPython) {
});
};

/**
* Set the dirty flag, and trigger the set_dirty.Notebook event
*
* @method set_dirty
*/
Notebook.prototype.set_dirty = function (value) {
if (value === undefined) {
value = true;
}
if (this.dirty == value) {
return;
}
$([IPython.events]).trigger('set_dirty.Notebook', {value: value});
};

/**
* Scroll the top of the page to a given cell.
*
Expand Down Expand Up @@ -645,7 +660,7 @@ var IPython = (function (IPython) {
pivot.before(tomove);
this.select(i-1);
};
this.dirty = true;
this.set_dirty(true);
};
return this;
};
Expand All @@ -669,7 +684,7 @@ var IPython = (function (IPython) {
this.select(i+1);
};
};
this.dirty = true;
this.set_dirty();
return this;
};

Expand Down Expand Up @@ -700,7 +715,7 @@ var IPython = (function (IPython) {
this.undelete_index = i;
this.undelete_below = false;
};
this.dirty = true;
this.set_dirty(true);
};
return this;
};
Expand Down Expand Up @@ -740,7 +755,7 @@ var IPython = (function (IPython) {
if(this._insert_element_at_index(cell.element,index)){
cell.render();
this.select(this.find_cell_index(cell));
this.dirty = true;
this.set_dirty(true);
}
}
return cell;
Expand Down Expand Up @@ -779,7 +794,7 @@ var IPython = (function (IPython) {

if (this.undelete_index !== null && index <= this.undelete_index) {
this.undelete_index = this.undelete_index + 1;
this.dirty = true;
this.set_dirty(true);
}
return true;
};
Expand Down Expand Up @@ -855,7 +870,7 @@ var IPython = (function (IPython) {
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_element.remove();
this.dirty = true;
this.set_dirty(true);
};
};
};
Expand Down Expand Up @@ -884,7 +899,7 @@ var IPython = (function (IPython) {
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_element.remove();
this.dirty = true;
this.set_dirty(true);
};
};
};
Expand Down Expand Up @@ -914,7 +929,7 @@ var IPython = (function (IPython) {
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_element.remove();
this.dirty = true;
this.set_dirty(true);
};
};
};
Expand Down Expand Up @@ -949,7 +964,7 @@ var IPython = (function (IPython) {
// to this state, instead of a blank cell
target_cell.code_mirror.clearHistory();
source_element.remove();
this.dirty = true;
this.set_dirty(true);
};
$([IPython.events]).trigger('selected_cell_type_changed.Notebook',
{'cell_type':'heading',level:level}
Expand Down Expand Up @@ -1177,7 +1192,7 @@ var IPython = (function (IPython) {
Notebook.prototype.collapse = function (index) {
var i = this.index_or_selected(index);
this.get_cell(i).collapse();
this.dirty = true;
this.set_dirty(true);
};

/**
Expand All @@ -1189,7 +1204,7 @@ var IPython = (function (IPython) {
Notebook.prototype.expand = function (index) {
var i = this.index_or_selected(index);
this.get_cell(i).expand();
this.dirty = true;
this.set_dirty(true);
};

/** Toggle whether a cell's output is collapsed or expanded.
Expand All @@ -1200,7 +1215,7 @@ var IPython = (function (IPython) {
Notebook.prototype.toggle_output = function (index) {
var i = this.index_or_selected(index);
this.get_cell(i).toggle_output();
this.dirty = true;
this.set_dirty(true);
};

/**
Expand Down Expand Up @@ -1228,7 +1243,7 @@ var IPython = (function (IPython) {
}
};
// this should not be set if the `collapse` key is removed from nbformat
this.dirty = true;
this.set_dirty(true);
};

/**
Expand All @@ -1246,7 +1261,7 @@ var IPython = (function (IPython) {
}
};
// this should not be set if the `collapse` key is removed from nbformat
this.dirty = true;
this.set_dirty(true);
};

/**
Expand All @@ -1264,7 +1279,7 @@ var IPython = (function (IPython) {
}
};
// this should not be set if the `collapse` key is removed from nbformat
this.dirty = true;
this.set_dirty(true);
};

/**
Expand All @@ -1283,7 +1298,7 @@ var IPython = (function (IPython) {
cells[i].set_input_prompt();
}
};
this.dirty = true;
this.set_dirty(true);
};


Expand Down Expand Up @@ -1376,7 +1391,7 @@ var IPython = (function (IPython) {
that.select(cell_index+1);
};
};
this.dirty = true;
this.set_dirty(true);
};

/**
Expand Down Expand Up @@ -1624,7 +1639,7 @@ var IPython = (function (IPython) {
* @param {jqXHR} xhr jQuery Ajax object
*/
Notebook.prototype.save_notebook_success = function (start, data, status, xhr) {
this.dirty = false;
this.set_dirty(false);
$([IPython.events]).trigger('notebook_saved.Notebook');
this._update_autosave_interval(start);
if (this._checkpoint_after_save) {
Expand Down Expand Up @@ -1703,7 +1718,7 @@ var IPython = (function (IPython) {
if (this.ncells() === 0) {
this.insert_cell_below('code');
};
this.dirty = false;
this.set_dirty(false);
this.select(0);
this.scroll_to_top();
if (data.orig_nbformat !== undefined && data.nbformat !== data.orig_nbformat) {
Expand Down
16 changes: 10 additions & 6 deletions IPython/frontend/html/notebook/static/notebook/js/savewidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ var IPython = (function (IPython) {
$(this).removeClass("ui-state-hover");
});
$([IPython.events]).on('notebook_loaded.Notebook', function () {
that.set_last_saved();
that.update_notebook_name();
that.update_document_title();
});
$([IPython.events]).on('notebook_saved.Notebook', function () {
that.set_last_saved();
that.update_notebook_name();
that.update_document_title();
});
$([IPython.events]).on('notebook_save_failed.Notebook', function () {
that.set_save_status('Last Save Failed!');
that.set_save_status('Autosave Failed!');
});
$([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
that.set_last_checkpoint(data[0]);
Expand All @@ -63,6 +61,9 @@ var IPython = (function (IPython) {
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
that.set_last_checkpoint(data);
});
$([IPython.events]).on('set_dirty.Notebook', function (event, data) {
that.set_autosaved(data.value);
});
};


Expand Down Expand Up @@ -148,9 +149,12 @@ var IPython = (function (IPython) {
);
}

SaveWidget.prototype.set_last_saved = function () {
var d = new Date();
this.set_save_status('(autosaved: '+d.format('mmm dd HH:MM') + ')');
SaveWidget.prototype.set_autosaved = function (dirty) {
if (dirty) {
this.set_save_status("(unsaved changes)");
} else {
this.set_save_status("(autosaved)");
}
};


Expand Down

0 comments on commit 2a99bb6

Please sign in to comment.