Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added undo for removeRow() #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
62 changes: 52 additions & 10 deletions src/sensei-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
plugin.events.trigger = function (event) {
var args = Array.prototype.slice.call(arguments, 1);
if (_.has(this._events, event)) {
// alert("event:"+event);
var events = this._events[event];
_.each(events, function (e) {
var cbk = _.bind(e["callback"], e["context"]);
Expand Down Expand Up @@ -166,7 +167,8 @@
plugin.render = function () {

// render row actions
plugin.rowElements = {};
plugin.rowElements = {}
;
_.each(plugin.rowActions, function (rowAction) {
rowAction.initialize();
var rowEl = "<div>" + rowAction.rowElement() + "</div>";
Expand Down Expand Up @@ -473,6 +475,15 @@
// get row data for event
var data = plugin.getRowData($row);

// stores the original row and records the row id
var edit = {
"type":"row",
"previousState": data,
"index": row
};
// save the state prior to edit
plugin.addEdit(edit);

// trigger row:remove event before actual removal
// could be used to persist changes in db
plugin.events.trigger("row:remove", data, row, $row);
Expand Down Expand Up @@ -677,6 +688,7 @@

// stores the original content and records the cell row and column
var edit = {
"type":"cell",
"previousState": plugin.getCellData($td),
"currentState": val,
"row": plugin.getRowData(plugin.getCellRow($td))["id"],
Expand Down Expand Up @@ -897,19 +909,49 @@
if (e.ctrlKey || e.metaKey) {
var edit = plugin.undo();

if (('row' in edit) && ('column' in edit)) {
if (edit.type === 'cell') {
if (('row' in edit) && ('column' in edit)) {

var row = plugin.getRowByIndex(edit.row - 1);
var element = plugin.getCellFromRowByIndex(row, edit.column);
var row = plugin.getRowByIndex(edit.row - 1);
var element = plugin.getCellFromRowByIndex(row, edit.column);

// set value from editor to the active cell
element.html($("<div>").text(edit.previousState));
// set value from editor to the active cell
element.html($("<div>").text(edit.previousState));

// trigger editor:save event
var data = {};
data[element.data("column")] = edit.previousState;
plugin.events.trigger("editor:save", data, element);
// trigger editor:save event
var data = {};
data[element.data("column")] = edit.previousState;
plugin.events.trigger("editor:save", data, element);

}
} else if (edit.type === 'row') {
if ('index' in edit) {

var rowIndex = edit.index;
var data = edit.previousState;

// recreate the row from row data
var $newRow = $(plugin.renderRow(data, false, true));

// insert the row at its previous position
var totalRows = plugin.getRows().length - (plugin.emptyRow?1:0);
if (rowIndex == 0 || totalRows == 0) {
// if it was the first row in the table
// or the table is currently empty
var $tbody = plugin.$el.find(".sensei-grid-tbody");
$tbody.prepend($newRow);
}
else {
var prevRowIndex = Math.min(totalRows,rowIndex - 1);
var prevRow = $(plugin.getRowByIndex(prevRowIndex));

$newRow.insertAfter(prevRow);
}

// trigger row:create event
plugin.events.trigger("row:create", $newRow);

}
}
}
break;
Expand Down