Allow to save only the partial model changes, undo-redo operation
within the edit/create dialog/panel using the backbone.undo.js.pull/3/head
parent
c1503ade47
commit
c1db54b2c2
|
@ -19,3 +19,4 @@ Backbone 1.1.2 MIT http://backbonejs.org
|
|||
font-Awesome 4.3 SIL OFL http://fortawesome.github.io/Font-Awesome/
|
||||
font-mfizz 1.2 MIT http://fizzed.com/oss/font-mfizz
|
||||
backgrid.js 0.3.5 MIT http://backgridjs.com/
|
||||
backbone.undo 0.2 MIT http://backbone.undojs.com/
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
define(
|
||||
['jquery', 'underscore', 'underscore.string', 'pgadmin', 'pgadmin.browser.menu',
|
||||
'backbone', 'alertify', 'backform', 'pgadmin.backform', 'wcdocker',
|
||||
'pgadmin.alertifyjs'],
|
||||
'pgadmin.alertifyjs', 'backbone.undo'],
|
||||
function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
||||
|
||||
var pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
|
||||
|
@ -147,7 +147,9 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
.success(function(res, msg, xhr) {
|
||||
// We got the latest attributes of the
|
||||
// object. Render the view now.
|
||||
newModel.startNewSession();
|
||||
view.render();
|
||||
$(el).focus();
|
||||
})
|
||||
.error(function(jqxhr, error, message) {
|
||||
// TODO:: We may not want to continue from here
|
||||
|
@ -160,7 +162,9 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
});
|
||||
} else {
|
||||
// Yay - render the view now!
|
||||
newModel.startNewSession();
|
||||
view.render();
|
||||
$(el).focus();
|
||||
}
|
||||
}
|
||||
return view;
|
||||
|
@ -650,6 +654,32 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
|
||||
// Show contents before buttons
|
||||
j.prepend(content);
|
||||
|
||||
// Register the Ctrl/Meta+Z -> for Undo operation
|
||||
// and Ctrl+Shift+Z/Ctrl+Y -> Redo operation in the edit/create
|
||||
// dialog.
|
||||
content.on('keydown', function(e) {
|
||||
switch (e.keyCode) {
|
||||
case 90:
|
||||
if ((e['ctrlKey'] || e['metaKey'])) {
|
||||
if (e['shiftKey']) {
|
||||
view && view.model && view.model.redo();
|
||||
} else {
|
||||
view && view.model && view.model.undo();
|
||||
}
|
||||
e.preventDefault();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 89:
|
||||
if ((e['ctrlKey'] || e['metaKey']) && !e['shiftKey']) {
|
||||
view && view.model && view.model.redo();
|
||||
e.preventDefault();
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
content.focus();
|
||||
},
|
||||
closePanel = function() {
|
||||
// Closing this panel
|
||||
|
@ -798,6 +828,173 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
});
|
||||
},
|
||||
Collection: Backbone.Collection.extend({
|
||||
// Model collection
|
||||
initialize: function(attributes, options) {
|
||||
var self = this;
|
||||
|
||||
options = options || {};
|
||||
self.sessAttrs = {
|
||||
'changed': [],
|
||||
'added': [],
|
||||
'deleted': []
|
||||
};
|
||||
self.handler = options.handler;
|
||||
self.trackChanges = false;
|
||||
|
||||
self.undoMgr = new Backbone.UndoManager({
|
||||
register: self, track: true
|
||||
});
|
||||
|
||||
if (self.handler && self.handler.undoMgr) {
|
||||
self.handler.undoMgr.merge(self.undoMgr);
|
||||
}
|
||||
|
||||
|
||||
self.on('add', self.onModelAdd);
|
||||
self.on('remove', self.onModelRemove);
|
||||
self.on('change', self.onModelChange);
|
||||
},
|
||||
startNewSession: function() {
|
||||
var self = this;
|
||||
|
||||
self.trackChanges = true;
|
||||
self.sessAttrs = {
|
||||
'changed': [],
|
||||
'added': [],
|
||||
'deleted': []
|
||||
};
|
||||
|
||||
self.undoMgr.clear();
|
||||
|
||||
_.each(self.models, function(m) {
|
||||
if ('startNewSession' in m && _.isFunction(m.startNewSession)) {
|
||||
m.startNewSession();
|
||||
}
|
||||
});
|
||||
},
|
||||
onChange: function() {
|
||||
var self = this;
|
||||
|
||||
if (self.handler && 'onChange' in self.handler &&
|
||||
_.isFunction(self.handler.onChange)) {
|
||||
return self.handler.onChange();
|
||||
}
|
||||
return true;
|
||||
},
|
||||
sessChanged: function() {
|
||||
return (
|
||||
this.sessAttrs['changed'].length > 0 ||
|
||||
this.sessAttrs['added'].length > 0 ||
|
||||
this.sessAttrs['deleted'].length > 0
|
||||
);
|
||||
},
|
||||
toJSON: function(session) {
|
||||
var self = this,
|
||||
onlyChanged = (typeof(session) != "undefined" &&
|
||||
session == true);
|
||||
|
||||
if (!onlyChanged) {
|
||||
return Backbone.Collection.prototype.toJSON.call(self);
|
||||
} else {
|
||||
var res = {};
|
||||
|
||||
res['added'] = [];
|
||||
_.each(this.sessAttrs['added'], function(o) {
|
||||
res['added'].push(o.toJSON());
|
||||
});
|
||||
if (res['added'].length == 0) {
|
||||
delete res['added'];
|
||||
}
|
||||
res['changed'] = [];
|
||||
_.each(self.sessAttrs['changed'], function(o) {
|
||||
res['changed'].push(o.toJSON(true));
|
||||
});
|
||||
if (res['changed'].length == 0) {
|
||||
delete res['changed'];
|
||||
}
|
||||
res['deleted'] = [];
|
||||
_.each(self.sessAttrs['deleted'], function(o) {
|
||||
res['deleted'].push(o.toJSON(false, true));
|
||||
});
|
||||
if (res['deleted'].length == 0) {
|
||||
delete res['deleted'];
|
||||
}
|
||||
|
||||
return (_.size(res) == 0 ? null : res);
|
||||
}
|
||||
},
|
||||
onModelAdd: function(obj) {
|
||||
|
||||
if (!this.trackChanges)
|
||||
return true;
|
||||
|
||||
var self = this, idx = _.indexOf(self.sessAttrs['deleted'], obj);
|
||||
|
||||
// Hmm.. - it was originally deleted from this collection, we should
|
||||
// remove it from the 'deleted' list.
|
||||
if (idx >= 0) {
|
||||
self.sessAttrs['deleted'].splice(idx, 1);
|
||||
|
||||
// It has been changed originally!
|
||||
if ((!'sessChanged' in obj) || obj.sessChanged()) {
|
||||
self.sessAttrs['changed'].push(obj);
|
||||
}
|
||||
return self.onChange();
|
||||
}
|
||||
self.sessAttrs['added'].push(obj);
|
||||
return self.onChange();
|
||||
},
|
||||
onModelRemove: function(obj) {
|
||||
|
||||
if (!this.trackChanges)
|
||||
return true;
|
||||
|
||||
var self = this, idx = _.indexOf(self.sessAttrs['added'], obj);
|
||||
|
||||
// Hmm - it was newly added, we can safely remove it.
|
||||
if (idx >= 0) {
|
||||
self.sessAttrs['added'].splice(idx, 1);
|
||||
return self.onChange();
|
||||
}
|
||||
// Hmm - it was changed in this session, we should remove it from the
|
||||
// changed models.
|
||||
idx = _.indexOf(self.sessAttrs['changed'], obj);
|
||||
if (idx >= 0) {
|
||||
self.sessAttrs['changed'].splice(idx, 1);
|
||||
}
|
||||
self.sessAttrs['deleted'].push(obj);
|
||||
return self.onChange();
|
||||
},
|
||||
onModelChange: function(obj) {
|
||||
|
||||
if (!this.trackChanges || obj instanceof pgBrowser.Node.Model)
|
||||
return true;
|
||||
|
||||
var self = this, idx = _.indexOf(self.sessAttrs['added'], obj);
|
||||
|
||||
// It was newly added model, we don't need to add into the changed
|
||||
// list.
|
||||
if (idx >= 0) {
|
||||
return true;
|
||||
}
|
||||
idx = _.indexOf(self.sessAttrs['changed'], obj);
|
||||
if (!'sessChanged' in obj) {
|
||||
if (idx > 0) {
|
||||
return true;
|
||||
}
|
||||
self.sessAttrs['changed'].push(obj);
|
||||
return self.onChange();
|
||||
}
|
||||
if (idx > 0) {
|
||||
if (!obj.sessChanged()) {
|
||||
self.sessAttrs['changed'].splice(idx, 1);
|
||||
return self.onChange();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
self.sessAttrs['changed'].push(obj);
|
||||
return self.onChange();
|
||||
}
|
||||
}),
|
||||
// Base class for Node Model
|
||||
Model: Backbone.Model.extend({
|
||||
|
@ -833,6 +1030,36 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
initialize: function(attributes, options) {
|
||||
var self = this;
|
||||
|
||||
options = options || {};
|
||||
self.sessAttrs = {};
|
||||
self.origSessAttrs = {};
|
||||
self.objects = [];
|
||||
self.handler = (options.handler ||
|
||||
(self.collection && self.collection.handler));
|
||||
self.trackChanges = false;
|
||||
|
||||
/*
|
||||
* A object in pgBrowser.Node.Collection does not require a separate
|
||||
* Undo manager.
|
||||
*/
|
||||
if (self.collection && self.collection.undoMgr) {
|
||||
self.undoMgr = self.collection.undoMgr;
|
||||
} else {
|
||||
self.undoMgr = new Backbone.UndoManager({
|
||||
register: self, track: true
|
||||
});
|
||||
|
||||
/*
|
||||
* Merged Undo stack should be kept at main handler
|
||||
*/
|
||||
if (self.handler && self.handler.undoMgr) {
|
||||
self.handler.undoMgr.merge(self.undoMgr);
|
||||
}
|
||||
}
|
||||
|
||||
self.onChangeData = options.onChangeData;
|
||||
self.onChangeCallback = options.onChangeCallback;
|
||||
|
||||
if (this.schema && _.isArray(this.schema)) {
|
||||
_.each(this.schema, function(s) {
|
||||
var obj = null;
|
||||
|
@ -841,26 +1068,160 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) {
|
|||
if (_.isString(s.model) &&
|
||||
s.model in pgBrowser.Nodes) {
|
||||
var node = pgBrowser.Nodes[s.model];
|
||||
obj = new (node.Collection)(null, {model: node.model});
|
||||
obj = new (node.Collection)(null, {
|
||||
model: node.model,
|
||||
handler: self.handler || self
|
||||
});
|
||||
} else {
|
||||
obj = new (pgBrowser.Node.Collection)(null, {model: s.model});
|
||||
obj = new (pgBrowser.Node.Collection)(null, {
|
||||
model: s.model,
|
||||
handler: self.handler || self
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'model':
|
||||
if (_.isString(s.model) &&
|
||||
s.model in pgBrowser.Nodes[s.model]) {
|
||||
obj = new (pgBrowser.Nodes[s.model].Model)(null);
|
||||
obj = new (pgBrowser.Nodes[s.model].Model)(
|
||||
null, {handler: self.handler || self}
|
||||
);
|
||||
} else {
|
||||
obj = new (s.model)(null);
|
||||
obj = new (s.model)(null, {handler: self.handler || self});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
obj.name = s.id;
|
||||
self.objects.push(s.id);
|
||||
self.set(s.id, obj, {silent: true});
|
||||
});
|
||||
}
|
||||
},
|
||||
onChange: function() {
|
||||
var self = this;
|
||||
|
||||
if (self.handler && 'onChange' in self.handler &&
|
||||
_.isFunction(self.handler.onChange)) {
|
||||
return self.handler.onChange();
|
||||
}
|
||||
if (self.onChangeCallback && _.isFunction(self.onChangeCallback)) {
|
||||
return self.onChangeCallback(self, self.onChangeData);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
sessChanged: function() {
|
||||
var self = this;
|
||||
|
||||
return (_.size(self.sessAttrs) > 0 ||
|
||||
_.some(self.objects, function(o) {
|
||||
return self.get(o).sessChanged();
|
||||
}));
|
||||
},
|
||||
set: function(key, val, options) {
|
||||
var res = Backbone.Model.prototype.set.call(this, key, val, options);
|
||||
|
||||
if (key != null && res && this.trackChanges) {
|
||||
var attrs;
|
||||
var self = this, unChanged = [], handler = self.handler || self;
|
||||
|
||||
attrChanged = function(v, k) {
|
||||
if (k in self.objects) {
|
||||
return;
|
||||
}
|
||||
if (self.origSessAttrs[k] == v) {
|
||||
delete self.sessAttrs[k];
|
||||
} else {
|
||||
self.sessAttrs[k] = v;
|
||||
}
|
||||
};
|
||||
|
||||
// Handle both `"key", value` and `{key: value}` -style arguments.
|
||||
if (typeof key === 'object') {
|
||||
_.each(key, attrChanged);
|
||||
} else {
|
||||
attrChanged(val, key);
|
||||
}
|
||||
|
||||
handler.onChange();
|
||||
return true;
|
||||
}
|
||||
return res;
|
||||
},
|
||||
toJSON: function(session, idOnly) {
|
||||
var self = this, res, isNew = self.isNew();
|
||||
|
||||
session = (typeof(session) != "undefined" && session == true && isNew == false);
|
||||
idOnly = (typeof(idOnly) != "undefined" && idOnly == true);
|
||||
|
||||
if (!session && !idOnly) {
|
||||
res = Backbone.Model.prototype.toJSON.call(this, arguments);
|
||||
} else {
|
||||
res = {};
|
||||
res[self.idAttribute || '_id'] = self.get(self.idAttribute || '_id');
|
||||
|
||||
if (idOnly) {
|
||||
return res;
|
||||
}
|
||||
res = _.extend(res, self.sessAttrs);
|
||||
}
|
||||
|
||||
_.each(self.objects, function(o) {
|
||||
res[o] = (self.get(o)).toJSON(session);
|
||||
});
|
||||
return res;
|
||||
},
|
||||
startNewSession: function() {
|
||||
var self = this;
|
||||
|
||||
self.trackChanges = true;
|
||||
self.sessAttrs = {};
|
||||
self.origSessAttrs = _.clone(this.attributes);
|
||||
self.undoMgr.clear();
|
||||
self.handler = (self.handler ||
|
||||
(self.collection && self.collection.handler));
|
||||
|
||||
var res = false;
|
||||
|
||||
_.each(self.objects, function(o) {
|
||||
var obj = self.get(o);
|
||||
|
||||
delete self.origSessAttrs[o];
|
||||
|
||||
if ('startNewSession' in obj && _.isFunction(obj.startNewSession)) {
|
||||
obj.startNewSession();
|
||||
} else {
|
||||
self.undoMgr.register(obj);
|
||||
}
|
||||
});
|
||||
|
||||
if (!self.handler) {
|
||||
self.onChange();
|
||||
}
|
||||
},
|
||||
canUndo: function() {
|
||||
if (this.undoMgr) {
|
||||
return this.undoMgr.isAvailable('undo');
|
||||
}
|
||||
return false;
|
||||
},
|
||||
canRedo: function() {
|
||||
if (this.undoMgr) {
|
||||
return this.undoMgr.isAvailable('redo');
|
||||
}
|
||||
return false;
|
||||
},
|
||||
undo: function() {
|
||||
if (this.undoMgr) {
|
||||
return this.undoMgr.undo(true);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
redo: function() {
|
||||
if (this.undoMgr) {
|
||||
return this.undoMgr.redo(true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
|
|
@ -0,0 +1,830 @@
|
|||
/*!
|
||||
* Backbone.Undo.js v0.2
|
||||
*
|
||||
* Copyright (c)2013 Oliver Sartun
|
||||
* Released under the MIT License
|
||||
*
|
||||
* Documentation and full license available at
|
||||
* https://github.com/osartun/Backbone.Undo.js
|
||||
*/
|
||||
|
||||
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
// AMD support
|
||||
define(["underscore", "backbone"], factory);
|
||||
} else if (typeof exports !== 'undefined') {
|
||||
// CommonJS support
|
||||
module.exports = factory;
|
||||
} else {
|
||||
// Non-modular execution
|
||||
factory(_, Backbone);
|
||||
}
|
||||
})(function (_, Backbone) {
|
||||
|
||||
var core_slice = Array.prototype.slice;
|
||||
|
||||
/**
|
||||
* As call is faster than apply, this is a faster version of apply as it uses call.
|
||||
*
|
||||
* @param {Function} fn The function to execute
|
||||
* @param {Object} ctx The context the function should be called in
|
||||
* @param {Array} args The array of arguments that should be applied to the function
|
||||
* @return Forwards whatever the called function returns
|
||||
*/
|
||||
function apply (fn, ctx, args) {
|
||||
return args.length <= 4 ?
|
||||
fn.call(ctx, args[0], args[1], args[2], args[3]) :
|
||||
fn.apply(ctx, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses slice on an array or an array-like object.
|
||||
*
|
||||
* @param {Array|Object} arr The array or array-like object.
|
||||
* @param {Number} [index] The index from where the array should be sliced. Default is 0.
|
||||
* @return {Array} The sliced array
|
||||
*/
|
||||
function slice (arr, index) {
|
||||
return core_slice.call(arr, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an object has one or more specific keys. The keys
|
||||
* don't have to be an owned property.
|
||||
* You can call this function either this way:
|
||||
* hasKeys(obj, ["a", "b", "c"])
|
||||
* or this way:
|
||||
* hasKeys(obj, "a", "b", "c")
|
||||
*
|
||||
* @param {Object} obj The object to check on
|
||||
* @param {Array} keys The keys to check for
|
||||
* @return {Boolean} True, if the object has all those keys
|
||||
*/
|
||||
function hasKeys (obj, keys) {
|
||||
if (obj == null) return false;
|
||||
if (!_.isArray(keys)) {
|
||||
keys = slice(arguments, 1);
|
||||
}
|
||||
return _.all(keys, function (key) {
|
||||
return key in obj;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a number that is unique per call stack. The number gets
|
||||
* changed after the call stack has been completely processed.
|
||||
*
|
||||
* @return {number} MagicFusionIndex
|
||||
*/
|
||||
var getMagicFusionIndex = (function () {
|
||||
// If you add several models to a collection or set several
|
||||
// attributes on a model all in sequence and yet all for
|
||||
// example in one function, then several Undo-Actions are
|
||||
// generated.
|
||||
// If you want to undo your last action only the last model
|
||||
// would be removed from the collection or the last set
|
||||
// attribute would be changed back to its previous value.
|
||||
// To prevent that we have to figure out a way to combine
|
||||
// all those actions that happened "at the same time".
|
||||
// Timestamps aren't exact enough. A complex routine could
|
||||
// run several milliseconds and in that time produce a lot
|
||||
// of actions with different timestamps.
|
||||
// Instead we take advantage of the single-threadedness of
|
||||
// JavaScript:
|
||||
|
||||
var callstackWasIndexed = false, magicFusionIndex = -1;
|
||||
function indexCycle() {
|
||||
magicFusionIndex++;
|
||||
callstackWasIndexed = true;
|
||||
_.defer(function () {
|
||||
// Here comes the magic. With a Timeout of 0
|
||||
// milliseconds this function gets called whenever
|
||||
// the current callstack is completed
|
||||
callstackWasIndexed = false;
|
||||
})
|
||||
}
|
||||
return function () {
|
||||
if (!callstackWasIndexed) {
|
||||
indexCycle();
|
||||
}
|
||||
return magicFusionIndex;
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* To prevent binding a listener several times to one
|
||||
* object, we register the objects in an ObjectRegistry
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function ObjectRegistry () {
|
||||
// This uses two different ways of storing
|
||||
// objects: In case the object has a cid
|
||||
// (which Backbone objects typically have)
|
||||
// it uses this cid as an index. That way
|
||||
// the Array's length attribute doesn't
|
||||
// change and the object isn't an item
|
||||
// in the array, but an object-property.
|
||||
// Otherwise it's added to the Array as an
|
||||
// item.
|
||||
// That way we can use the fast property-
|
||||
// lookup and only have to fall back to
|
||||
// iterating over the array in case
|
||||
// non-Backbone-objects are registered.
|
||||
this.registeredObjects = [];
|
||||
// To return a list of all registered
|
||||
// objects in the 'get' method we have to
|
||||
// store the objects that have a cid in
|
||||
// an additional array.
|
||||
this.cidIndexes = [];
|
||||
}
|
||||
ObjectRegistry.prototype = {
|
||||
/**
|
||||
* Returns whether the object is already registered in this ObjectRegistry or not.
|
||||
*
|
||||
* @this {ObjectRegistry}
|
||||
* @param {Object} obj The object to check
|
||||
* @return {Boolean} True if the object is already registered
|
||||
*/
|
||||
isRegistered: function (obj) {
|
||||
// This is where we get a performance boost
|
||||
// by using the two different ways of storing
|
||||
// objects.
|
||||
return obj && obj.cid ? this.registeredObjects[obj.cid] : _.contains(this.registeredObjects, obj);
|
||||
},
|
||||
/**
|
||||
* Registers an object in this ObjectRegistry.
|
||||
*
|
||||
* @this {ObjectRegistry}
|
||||
* @param {Object} obj The object to register
|
||||
* @return {undefined}
|
||||
*/
|
||||
register: function (obj) {
|
||||
if (!this.isRegistered(obj)) {
|
||||
if (obj && obj.cid) {
|
||||
this.registeredObjects[obj.cid] = obj;
|
||||
this.cidIndexes.push(obj.cid);
|
||||
} else {
|
||||
this.registeredObjects.push(obj);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
/**
|
||||
* Unregisters an object from this ObjectRegistry.
|
||||
*
|
||||
* @this {ObjectRegistry}
|
||||
* @param {Object} obj The object to unregister
|
||||
* @return {undefined}
|
||||
*/
|
||||
unregister: function (obj) {
|
||||
if (this.isRegistered(obj)) {
|
||||
if (obj && obj.cid) {
|
||||
delete this.registeredObjects[obj.cid];
|
||||
this.cidIndexes.splice(_.indexOf(this.cidIndexes, obj.cid), 1);
|
||||
} else {
|
||||
var i = _.indexOf(this.registeredObjects, obj);
|
||||
this.registeredObjects.splice(i, 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
/**
|
||||
* Returns an array of all objects that are currently in this ObjectRegistry.
|
||||
*
|
||||
* @return {Array} An array of all the objects which are currently in the ObjectRegistry
|
||||
*/
|
||||
get: function () {
|
||||
return (_.map(this.cidIndexes, function (cid) {return this.registeredObjects[cid];}, this)).concat(this.registeredObjects);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds or unbinds the "all"-listener for one or more objects.
|
||||
*
|
||||
* @param {String} which Either "on" or "off"
|
||||
* @param {Object[]} objects Array of the objects on which the "all"-listener should be bound / unbound to
|
||||
* @param {Function} [fn] The function that should be bound / unbound. Optional in case of "off"
|
||||
* @param {Object} [ctx] The context the function should be called in
|
||||
* @return {undefined}
|
||||
*/
|
||||
function onoff(which, objects, fn, ctx) {
|
||||
for (var i = 0, l = objects.length, obj; i < l; i++) {
|
||||
obj = objects[i];
|
||||
if (!obj) continue;
|
||||
if (which === "on") {
|
||||
if (!ctx.objectRegistry.register(obj)) {
|
||||
// register returned false, so obj was already registered
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (!ctx.objectRegistry.unregister(obj)) {
|
||||
// unregister returned false, so obj wasn't registered
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (_.isFunction(obj[which])) {
|
||||
obj[which]("all", fn, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the undo/redo-function for a specific action.
|
||||
*
|
||||
* @param {String} which Either "undo" or "redo"
|
||||
* @param {Object} action The Action's attributes
|
||||
* @return {undefined}
|
||||
*/
|
||||
function actionUndoRedo (which, action) {
|
||||
var type = action.type, undoTypes = action.undoTypes, fn = !undoTypes[type] || undoTypes[type][which];
|
||||
if (_.isFunction(fn)) {
|
||||
fn(action.object, action.before, action.after, action.options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The main undo/redo function.
|
||||
*
|
||||
* @param {String} which Either "undo" or "redo"
|
||||
* @param {UndoManager} manager The UndoManager-instance on which an "undo"/"redo"-Event is triggered afterwards
|
||||
* @param {UndoStack} stack The UndoStack on which we perform
|
||||
* @param {Boolean} magic If true, undoes / redoes all actions with the same magicFusionIndex
|
||||
* @param {Boolean} everything If true, undoes / redoes every action that had been tracked
|
||||
* @return {undefined}
|
||||
*/
|
||||
function managerUndoRedo (which, manager, stack, magic, everything) {
|
||||
if (stack.isCurrentlyUndoRedoing ||
|
||||
(which === "undo" && stack.pointer === -1) ||
|
||||
(which === "redo" && stack.pointer === stack.length - 1)) {
|
||||
// We're either currently in an undo- / redo-process or
|
||||
// we reached the end of the stack
|
||||
return;
|
||||
}
|
||||
stack.isCurrentlyUndoRedoing = true;
|
||||
var action, actions, isUndo = which === "undo";
|
||||
if (everything) {
|
||||
// Undo / Redo all steps until you reach the stack's beginning / end
|
||||
actions = isUndo && stack.pointer === stack.length - 1 || // If at the stack's end calling undo
|
||||
!isUndo && stack.pointer === -1 ? // or at the stack's beginning calling redo
|
||||
_.clone(stack.models) : // => Take all the models. Otherwise:
|
||||
core_slice.apply(stack.models, isUndo ? [0, stack.pointer] : [stack.pointer, stack.length - 1]);
|
||||
} else {
|
||||
// Undo / Redo only one step
|
||||
action = stack.at(isUndo ? stack.pointer : stack.pointer + 1);
|
||||
actions = magic ? stack.where({"magicFusionIndex": action.get("magicFusionIndex")}) : [action];
|
||||
}
|
||||
|
||||
stack.pointer += (isUndo ? -1 : 1) * actions.length;
|
||||
while (action = isUndo ? actions.pop() : actions.shift()) {
|
||||
// Here we're calling the Action's undo / redo method
|
||||
action[which]();
|
||||
}
|
||||
stack.isCurrentlyUndoRedoing = false;
|
||||
|
||||
manager.trigger(which, manager);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 into a boolean.
|
||||
* Otherwise 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.
|
||||
*
|
||||
* @param {UndoStack} stack The undostack the action should be added to.
|
||||
* @param {String} type The event type (i.e. "change")
|
||||
* @param {Arguments} args The arguments passed to the undoTypes' "on"-handler
|
||||
* @param {OwnedUndoTypes} undoTypes The undoTypes-object which has the "on"-handler
|
||||
* @return {undefined}
|
||||
*/
|
||||
function addToStack(stack, type, args, undoTypes) {
|
||||
if (stack.track && !stack.isCurrentlyUndoRedoing && type in undoTypes &&
|
||||
validateUndoActionCreation(undoTypes[type], args)) {
|
||||
// An UndoAction should be created
|
||||
var res = apply(undoTypes[type]["on"], undoTypes[type], args), diff;
|
||||
if (hasKeys(res, "object", "before", "after")) {
|
||||
res.type = type;
|
||||
res.magicFusionIndex = getMagicFusionIndex();
|
||||
res.undoTypes = undoTypes;
|
||||
if (stack.pointer < stack.length - 1) {
|
||||
// New Actions must always be added to the end of the stack.
|
||||
// If the pointer is not pointed to the last action in the
|
||||
// stack, presumably because actions were undone before, then
|
||||
// all following actions must be discarded
|
||||
var diff = stack.length - stack.pointer - 1;
|
||||
while (diff--) {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
stack.pointer = stack.length;
|
||||
stack.add(res);
|
||||
if (stack.length > stack.maximumStackLength) {
|
||||
stack.shift();
|
||||
stack.pointer--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Predefined UndoTypes object with default handlers for the most common events.
|
||||
* @type {Object}
|
||||
*/
|
||||
var UndoTypes = {
|
||||
"add": {
|
||||
"undo": function (collection, ignore, model, options) {
|
||||
// Undo add = remove
|
||||
collection.remove(model, options);
|
||||
},
|
||||
"redo": function (collection, ignore, model, options) {
|
||||
// Redo add = add
|
||||
if (options.index) {
|
||||
options.at = options.index;
|
||||
}
|
||||
collection.add(model, options);
|
||||
},
|
||||
"on": function (model, collection, options) {
|
||||
return {
|
||||
object: collection,
|
||||
before: undefined,
|
||||
after: model,
|
||||
options: _.clone(options)
|
||||
};
|
||||
}
|
||||
},
|
||||
"remove": {
|
||||
"undo": function (collection, model, ignore, options) {
|
||||
if ("index" in options) {
|
||||
options.at = options.index;
|
||||
}
|
||||
collection.add(model, options);
|
||||
},
|
||||
"redo": function (collection, model, ignore, options) {
|
||||
collection.remove(model, options);
|
||||
},
|
||||
"on": function (model, collection, options) {
|
||||
return {
|
||||
object: collection,
|
||||
before: model,
|
||||
after: undefined,
|
||||
options: _.clone(options)
|
||||
};
|
||||
}
|
||||
},
|
||||
"change": {
|
||||
"undo": function (model, before, after, options) {
|
||||
if (_.isEmpty(before)) {
|
||||
_.each(_.keys(after), model.unset, model);
|
||||
} else {
|
||||
model.set(before);
|
||||
if (options && options.unsetData && options.unsetData.before && options.unsetData.before.length) {
|
||||
_.each(options.unsetData.before, model.unset, model);
|
||||
}
|
||||
}
|
||||
},
|
||||
"redo": function (model, before, after, options) {
|
||||
if (_.isEmpty(after)) {
|
||||
_.each(_.keys(before), model.unset, model);
|
||||
} else {
|
||||
model.set(after);
|
||||
if (options && options.unsetData && options.unsetData.after && options.unsetData.after.length) {
|
||||
_.each(options.unsetData.after, model.unset, model);
|
||||
}
|
||||
}
|
||||
},
|
||||
"on": function (model, options) {
|
||||
var
|
||||
afterAttributes = model.changedAttributes(),
|
||||
keysAfter = _.keys(afterAttributes),
|
||||
previousAttributes = _.pick(model.previousAttributes(), keysAfter),
|
||||
keysPrevious = _.keys(previousAttributes),
|
||||
unsetData = (options || (options = {})).unsetData = {
|
||||
after: [],
|
||||
before: []
|
||||
};
|
||||
|
||||
if (keysAfter.length != keysPrevious.length) {
|
||||
// There are new attributes or old attributes have been unset
|
||||
if (keysAfter.length > keysPrevious.length) {
|
||||
// New attributes have been added
|
||||
_.each(keysAfter, function (val) {
|
||||
if (!(val in previousAttributes)) {
|
||||
unsetData.before.push(val);
|
||||
}
|
||||
}, this);
|
||||
} else {
|
||||
// Old attributes have been unset
|
||||
_.each(keysPrevious, function (val) {
|
||||
if (!(val in afterAttributes)) {
|
||||
unsetData.after.push(val);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
return {
|
||||
object: model,
|
||||
before: previousAttributes,
|
||||
after: afterAttributes,
|
||||
options: _.clone(options)
|
||||
};
|
||||
}
|
||||
},
|
||||
"reset": {
|
||||
"undo": function (collection, before, after) {
|
||||
collection.reset(before);
|
||||
},
|
||||
"redo": function (collection, before, after) {
|
||||
collection.reset(after);
|
||||
},
|
||||
"on": function (collection, options) {
|
||||
return {
|
||||
object: collection,
|
||||
before: options.previousModels,
|
||||
after: _.clone(collection.models)
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Every UndoManager instance has an own undoTypes object
|
||||
* which is an instance of OwnedUndoTypes. OwnedUndoTypes'
|
||||
* prototype is the global UndoTypes object. Changes to the
|
||||
* global UndoTypes object take effect on every instance of
|
||||
* UndoManager as the object is its prototype. And yet every
|
||||
* local UndoTypes object can be changed individually.
|
||||
*
|
||||
* @constructor
|
||||
*/
|
||||
function OwnedUndoTypes () {}
|
||||
OwnedUndoTypes.prototype = UndoTypes;
|
||||
|
||||
/**
|
||||
* Adds, changes or removes an undo-type from an UndoTypes-object.
|
||||
* You can call it this way:
|
||||
* manipulateUndoType (1, "reset", {"on": function () {}}, undoTypes)
|
||||
* or this way to perform bulk actions:
|
||||
* manipulateUndoType (1, {"reset": {"on": function () {}}}, undoTypes)
|
||||
* In case of removing undo-types you can pass an Array for performing
|
||||
* bulk actions:
|
||||
* manipulateUndoType(2, ["reset", "change"], undoTypes)
|
||||
*
|
||||
* @param {Number} manipType Indicates the kind of action to execute: 0 for add, 1 for change, 2 for remove
|
||||
* @param {String|Object|Array} undoType The type of undoType that should be added/changed/removed. Can be an object / array to perform bulk actions
|
||||
* @param {Object} [fns] Object with the functions to add / change. Is optional in case you passed an object as undoType that contains these functions
|
||||
* @param {OwnedUndoTypes|UndoTypes} undoTypesInstance The undoTypes object to act on
|
||||
* @return {undefined}
|
||||
*/
|
||||
function manipulateUndoType (manipType, undoType, fns, undoTypesInstance) {
|
||||
// manipType, passed by the calling function
|
||||
// 0: add
|
||||
// 1: change
|
||||
// 2: remove
|
||||
if (typeof undoType === "object") {
|
||||
// bulk action. Iterate over this data.
|
||||
return _.each(undoType, function (val, key) {
|
||||
if (manipType === 2) { // remove
|
||||
// undoType is an array
|
||||
manipulateUndoType (manipType, val, fns, undoTypesInstance);
|
||||
} else {
|
||||
// undoType is an object
|
||||
manipulateUndoType (manipType, key, val, fns);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
switch (manipType) {
|
||||
case 0: // add
|
||||
if (hasKeys(fns, "undo", "redo", "on") && _.all(_.pick(fns, "undo", "redo", "on"), _.isFunction)) {
|
||||
undoTypesInstance[undoType] = fns;
|
||||
}
|
||||
break;
|
||||
case 1: // change
|
||||
if (undoTypesInstance[undoType] && _.isObject(fns)) {
|
||||
// undoTypeInstance[undoType] may be a prototype's property
|
||||
// So, if we did this _.extend(undoTypeInstance[undoType], fns)
|
||||
// we would extend the object on the prototype which means
|
||||
// that this change would have a global effect
|
||||
// Instead we just want to manipulate this instance. That's why
|
||||
// we're doing this:
|
||||
undoTypesInstance[undoType] = _.extend({}, undoTypesInstance[undoType], fns);
|
||||
}
|
||||
break;
|
||||
case 2: // remove
|
||||
delete undoTypesInstance[undoType];
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiating "Action" creates the UndoActions that
|
||||
* are collected in an UndoStack. It holds all relevant
|
||||
* data to undo / redo an action and has an undo / redo
|
||||
* method.
|
||||
*/
|
||||
var Action = Backbone.Model.extend({
|
||||
defaults: {
|
||||
type: null, // "add", "change", "reset", etc.
|
||||
object: null, // The object on which the action occurred
|
||||
before: null, // The previous values which were changed with this action
|
||||
after: null, // The values after this action
|
||||
magicFusionIndex: null // The magicFusionIndex helps to combine
|
||||
// all actions that occurred "at the same time" to undo/redo them altogether
|
||||
},
|
||||
/**
|
||||
* Undoes this action.
|
||||
* @param {OwnedUndoTypes|UndoTypes} undoTypes The undoTypes object which contains the "undo"-handler that should be used
|
||||
* @return {undefined}
|
||||
*/
|
||||
undo: function (undoTypes) {
|
||||
actionUndoRedo("undo", this.attributes);
|
||||
},
|
||||
/**
|
||||
* Redoes this action.
|
||||
* @param {OwnedUndoTypes|UndoTypes} undoTypes The undoTypes object which contains the "redo"-handler that should be used
|
||||
* @return {undefined}
|
||||
*/
|
||||
redo: function (undoTypes) {
|
||||
actionUndoRedo("redo", this.attributes);
|
||||
}
|
||||
}),
|
||||
/**
|
||||
* An UndoStack is a collection of UndoActions in
|
||||
* chronological order.
|
||||
*/
|
||||
UndoStack = Backbone.Collection.extend({
|
||||
model: Action,
|
||||
pointer: -1, // The pointer indicates the index where we are located within the stack. We start at -1
|
||||
track: false,
|
||||
isCurrentlyUndoRedoing: false,
|
||||
maximumStackLength: Infinity,
|
||||
setMaxLength: function (val) {
|
||||
this.maximumStackLength = val;
|
||||
}
|
||||
}),
|
||||
/**
|
||||
* An instance of UndoManager can keep track of
|
||||
* changes to objects and helps to undo them.
|
||||
*/
|
||||
UndoManager = Backbone.Model.extend({
|
||||
defaults: {
|
||||
maximumStackLength: Infinity,
|
||||
track: false
|
||||
},
|
||||
/**
|
||||
* The constructor function.
|
||||
* @param {attr} [attr] Object with parameters. The available parameters are:
|
||||
* - maximumStackLength {number} Set the undo-stack's maximum size
|
||||
* - track {boolean} Start tracking changes right away
|
||||
* @return {undefined}
|
||||
*/
|
||||
initialize: function (attr) {
|
||||
this.stack = new UndoStack;
|
||||
this.objectRegistry = new ObjectRegistry();
|
||||
this.undoTypes = new OwnedUndoTypes();
|
||||
|
||||
// sync the maximumStackLength attribute with our stack
|
||||
this.stack.setMaxLength(this.get("maximumStackLength"));
|
||||
this.on("change:maximumStackLength", function (model, value) {
|
||||
this.stack.setMaxLength(value);
|
||||
}, this);
|
||||
|
||||
// Start tracking, if attr.track == true
|
||||
if (attr && attr.track) {
|
||||
this.startTracking();
|
||||
}
|
||||
|
||||
// Register objects passed in the "register" attribute
|
||||
if (attr && attr.register) {
|
||||
if (_.isArray(attr.register) || _.isArguments(attr.register)) {
|
||||
apply(this.register, this, attr.register);
|
||||
} else {
|
||||
this.register(attr.register);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Starts tracking. Changes of registered objects won't be processed until you've called this function
|
||||
* @return {undefined}
|
||||
*/
|
||||
startTracking: function () {
|
||||
this.set("track", true);
|
||||
this.stack.track = true;
|
||||
},
|
||||
/**
|
||||
* Stops tracking. Afterwards changes of registered objects won't be processed.
|
||||
* @return {undefined}
|
||||
*/
|
||||
stopTracking: function () {
|
||||
this.set("track", false);
|
||||
this.stack.track = false;
|
||||
},
|
||||
/**
|
||||
* Return the state of the tracking
|
||||
* @return {boolean}
|
||||
*/
|
||||
isTracking: function () {
|
||||
return this.get("track");
|
||||
},
|
||||
/**
|
||||
* This is the "all"-handler which is bound to registered
|
||||
* objects. It creates an UndoAction from the event and adds
|
||||
* it to the stack.
|
||||
*
|
||||
* @param {String} type The event type
|
||||
* @return {undefined}
|
||||
*/
|
||||
_addToStack: function (type) {
|
||||
addToStack(this.stack, type, slice(arguments, 1), this.undoTypes);
|
||||
},
|
||||
/**
|
||||
* Registers one or more objects to track their changes.
|
||||
* @param {...Object} obj The object or objects of which changes should be tracked
|
||||
* @return {undefined}
|
||||
*/
|
||||
register: function () {
|
||||
onoff("on", arguments, this._addToStack, this);
|
||||
},
|
||||
/**
|
||||
* Unregisters one or more objects.
|
||||
* @param {...Object} obj The object or objects of which changes shouldn't be tracked any longer
|
||||
* @return {undefined}
|
||||
*/
|
||||
unregister: function () {
|
||||
onoff("off", arguments, this._addToStack, this);
|
||||
},
|
||||
/**
|
||||
* Unregisters all previously registered objects.
|
||||
* @return {undefined}
|
||||
*/
|
||||
unregisterAll: function () {
|
||||
apply(this.unregister, this, this.objectRegistry.get());
|
||||
},
|
||||
/**
|
||||
* Undoes the last action or the last set of actions in case 'magic' is true.
|
||||
* @param {Boolean} [magic] If true, all actions that happened basically at the same time are undone together
|
||||
* @return {undefined}
|
||||
*/
|
||||
undo: function (magic) {
|
||||
managerUndoRedo("undo", this, this.stack, magic);
|
||||
},
|
||||
|
||||
/**
|
||||
* Undoes all actions ever tracked by the undo manager
|
||||
* @return {undefined}
|
||||
*/
|
||||
undoAll: function () {
|
||||
managerUndoRedo("undo", this, this.stack, false, true);
|
||||
},
|
||||
|
||||
/**
|
||||
* Redoes a previously undone action or a set of actions.
|
||||
* @param {Boolean} [magic] If true, all actions that happened basically at the same time are redone together
|
||||
* @return {undefined}
|
||||
*/
|
||||
redo: function (magic) {
|
||||
managerUndoRedo("redo", this, this.stack, magic);
|
||||
},
|
||||
|
||||
/**
|
||||
* Redoes all actions ever tracked by the undo manager
|
||||
* @return {undefined}
|
||||
*/
|
||||
redoAll: function () {
|
||||
managerUndoRedo("redo", this, this.stack, false, true);
|
||||
},
|
||||
/**
|
||||
* Checks if there's an action in the stack that can be undone / redone
|
||||
* @param {String} type Either "undo" or "redo"
|
||||
* @return {Boolean} True if there is a set of actions which can be undone / redone
|
||||
*/
|
||||
isAvailable: function (type) {
|
||||
var s = this.stack, l = s.length;
|
||||
|
||||
switch (type) {
|
||||
case "undo": return l > 0 && s.pointer > -1;
|
||||
case "redo": return l > 0 && s.pointer < l - 1;
|
||||
default: return false;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Sets the stack-reference to the stack of another undoManager.
|
||||
* @param {UndoManager} undoManager The undoManager whose stack-reference is set to this stack
|
||||
* @return {undefined}
|
||||
*/
|
||||
merge: function (undoManager) {
|
||||
// This sets the stack-reference to the stack of another
|
||||
// undoManager so that the stack of this other undoManager
|
||||
// is used by two different managers.
|
||||
// This enables to set up a main-undoManager and besides it
|
||||
// several others for special, exceptional cases (by using
|
||||
// instance-based custom UndoTypes). Models / collections
|
||||
// which need this special treatment are only registered at
|
||||
// those special undoManagers. Those special ones are then
|
||||
// merged into the main-undoManager to write on its stack.
|
||||
// That way it's easier to manage exceptional cases.
|
||||
var args = _.isArray(undoManager) ? undoManager : slice(arguments), manager;
|
||||
while (manager = args.pop()) {
|
||||
if (manager instanceof UndoManager &&
|
||||
manager.stack instanceof UndoStack) {
|
||||
// set the stack reference to our stack
|
||||
manager.stack = this.stack;
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Add an UndoType to this specific UndoManager-instance.
|
||||
* @param {String} type The event this UndoType is made for
|
||||
* @param {Object} fns An object of functions that are called to generate the data for an UndoAction or to process it. Must have the properties "undo", "redo" and "on". Can have the property "condition".
|
||||
* @return {undefined}
|
||||
*/
|
||||
addUndoType: function (type, fns) {
|
||||
manipulateUndoType(0, type, fns, this.undoTypes);
|
||||
},
|
||||
/**
|
||||
* Overwrite properties of an existing UndoType for this specific UndoManager-instance.
|
||||
* @param {String} type The event the UndoType is made for
|
||||
* @param {Object} fns An object of functions that are called to generate the data for an UndoAction or to process it. It extends the existing object.
|
||||
* @return {undefined}
|
||||
*/
|
||||
changeUndoType: function (type, fns) {
|
||||
manipulateUndoType(1, type, fns, this.undoTypes);
|
||||
},
|
||||
/**
|
||||
* Remove one or more UndoTypes of this specific UndoManager-instance to fall back to the global UndoTypes.
|
||||
* @param {String|Array} type The event the UndoType that should be removed is made for. You can also pass an array of events.
|
||||
* @return {undefined}
|
||||
*/
|
||||
removeUndoType: function (type) {
|
||||
manipulateUndoType(2, type, undefined, this.undoTypes);
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all actions from the stack.
|
||||
* @return {undefined}
|
||||
*/
|
||||
clear: function() {
|
||||
this.stack.reset();
|
||||
this.stack.pointer = -1;
|
||||
}
|
||||
});
|
||||
|
||||
_.extend(UndoManager, {
|
||||
/**
|
||||
* Change the UndoManager's default attributes
|
||||
* @param {Object} defaultAttributes An object with the new default values.
|
||||
* @return {undefined}
|
||||
*/
|
||||
defaults: function (defaultAttributes) {
|
||||
_.extend(UndoManager.prototype.defaults, defaultAttributes);
|
||||
},
|
||||
/**
|
||||
* Add an UndoType to the global UndoTypes-object.
|
||||
* @param {String} type The event this UndoType is made for
|
||||
* @param {Object} fns An object of functions that are called to generate the data for an UndoAction or to process it. Must have the properties "undo", "redo" and "on". Can have the property "condition".
|
||||
* @return {undefined}
|
||||
*/
|
||||
"addUndoType": function (type, fns) {
|
||||
manipulateUndoType(0, type, fns, UndoTypes);
|
||||
},
|
||||
/**
|
||||
* Overwrite properties of an existing UndoType in the global UndoTypes-object.
|
||||
* @param {String} type The event the UndoType is made for
|
||||
* @param {Object} fns An object of functions that are called to generate the data for an UndoAction or to process it. It extends the existing object.
|
||||
* @return {undefined}
|
||||
*/
|
||||
"changeUndoType": function (type, fns) {
|
||||
manipulateUndoType(1, type, fns, UndoTypes)
|
||||
},
|
||||
/**
|
||||
* Remove one or more UndoTypes of this specific UndoManager-instance to fall back to the global UndoTypes.
|
||||
* @param {String|Array} type The event the UndoType that should be removed is made for. You can also pass an array of events.
|
||||
* @return {undefined}
|
||||
*/
|
||||
"removeUndoType": function (type) {
|
||||
manipulateUndoType(2, type, undefined, UndoTypes);
|
||||
}
|
||||
})
|
||||
|
||||
Backbone.UndoManager = UndoManager;
|
||||
|
||||
});
|
File diff suppressed because one or more lines are too long
|
@ -77,6 +77,7 @@
|
|||
"bootstrap.datepicker": "{{ url_for('static', filename='js/' + ('bootstrap-datepicker' if config.DEBUG else 'bootstrap-datepicker.min')) }}",
|
||||
backform: "{{ url_for('static', filename='js/backform') }}",
|
||||
backgrid: "{{ url_for('static', filename='js/backgrid/' + ('backgrid' if config.DEBUG else 'backgrid.min')) }}",
|
||||
"backbone.undo": "{{ url_for('static', filename='js/' + ('backbone.undo' if config.DEBUG else 'backbone.undo.min')) }}",
|
||||
"pgadmin.backgrid": "{{ url_for('static', filename='js/backgrid/backgrid.pgadmin') }}",
|
||||
'pgadmin.backform': "{{ url_for('static', filename='js/backform.pgadmin') }}"{% for script in current_app.javascripts %},
|
||||
'{{ script.name }}': "{{ script.path }}"{% endfor %}
|
||||
|
|
Loading…
Reference in New Issue