Deeper integration of undoTypes-"condition"-function
Instead of getting called from within the "on"-function, the "condition"-function now gets called by the addToStack function. That way its better suitable for custom undoTypes or changed undoTypes, as the authors of these undoTypes don't have to manually integrate it into their implementation.
This commit is contained in:
parent
7df7d2ae2f
commit
a78d7c8bf8
@ -253,6 +253,23 @@
|
||||
manager.trigger(which, manager);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function checks whether an UndoAction should be created or not. Therefore
|
||||
* it checks whether a "condition" property is set in the undoTypes-object of
|
||||
* the specific event type. If not, it returns true. If it's set and a boolean, it
|
||||
* returns it. If it's a function, it returns its result, converting it to a true
|
||||
* boolean. If it's set, but neither a boolean nor a function, it returns true.
|
||||
*
|
||||
* @param {Object} undoTypesType The object within the UndoTypes that holds the function for this event type (i.e. "change")
|
||||
* @param {Arguments} args The arguments the "condition" function is called with
|
||||
* @return {Boolean} True, if an UndoAction should be created
|
||||
*/
|
||||
function validateUndoActionCreation (undoTypesType, args) {
|
||||
var condition = undoTypesType.condition, type = typeof condition;
|
||||
return type === "function" ? !!apply(condition, undoTypesType, args) :
|
||||
type === "boolean" ? condition : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an Undo-Action to the stack.
|
||||
*
|
||||
@ -263,7 +280,8 @@
|
||||
* @return {undefined}
|
||||
*/
|
||||
function addToStack(stack, type, args, undoTypes) {
|
||||
if (stack.track && !stack.isCurrentlyUndoRedoing && type in undoTypes) {
|
||||
if (stack.track && !stack.isCurrentlyUndoRedoing && type in undoTypes &&
|
||||
validateUndoActionCreation(undoTypes[type], args)) {
|
||||
var res = apply(undoTypes[type]["on"], undoTypes[type], args), diff;
|
||||
if (hasKeys(res, "object", "before", "after")) {
|
||||
res.type = type;
|
||||
@ -289,7 +307,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
function returnTrue() {return true;}
|
||||
|
||||
/**
|
||||
* Predefined UndoTypes object with default handlers for the most common events.
|
||||
@ -309,9 +326,7 @@
|
||||
}
|
||||
collection.add(model, data.options);
|
||||
},
|
||||
"condition": returnTrue,
|
||||
"on": function (model, collection, options) {
|
||||
if (this.condition(model, collection, options)) {
|
||||
return {
|
||||
object: collection,
|
||||
before: undefined,
|
||||
@ -319,7 +334,6 @@
|
||||
options: _.clone(options)
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
"remove": {
|
||||
"undo": function (collection, model, ignore, data) {
|
||||
@ -332,9 +346,7 @@
|
||||
"redo": function (collection, model, ignore, data) {
|
||||
collection.remove(model, data.options);
|
||||
},
|
||||
"condition": returnTrue,
|
||||
"on": function (model, collection, options) {
|
||||
if (this.condition(model,collection, options)) {
|
||||
return {
|
||||
object: collection,
|
||||
before: model,
|
||||
@ -342,7 +354,6 @@
|
||||
options: _.clone(options)
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
"change": {
|
||||
"undo": function (model, before, after) {
|
||||
@ -359,9 +370,7 @@
|
||||
model.set(after);
|
||||
}
|
||||
},
|
||||
"condition": returnTrue,
|
||||
"on": function (model, options) {
|
||||
if (this.condition(model, options)) {
|
||||
var
|
||||
changedAttributes = model.changedAttributes(),
|
||||
previousAttributes = _.pick(model.previousAttributes(), _.keys(changedAttributes));
|
||||
@ -371,7 +380,6 @@
|
||||
after: changedAttributes
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
"reset": {
|
||||
"undo": function (collection, before, after) {
|
||||
|
Loading…
Reference in New Issue
Block a user