fabric.js/src/mixins/stateful.mixin.js

45 lines
1.2 KiB
JavaScript

/*
Depends on `stateProperties`
*/
fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prototype */ {
/**
* Returns true if object state (one of its state properties) was changed
* @return {Boolean} true if instance' state has changed since `{@link fabric.Object#saveState}` was called
*/
hasStateChanged: function() {
return this.stateProperties.some(function(prop) {
return this.get(prop) !== this.originalState[prop];
}, this);
},
/**
* Saves state of an object
* @param {Object} [options] Object with additional `stateProperties` array to include when saving state
* @return {fabric.Object} thisArg
*/
saveState: function(options) {
this.stateProperties.forEach(function(prop) {
this.originalState[prop] = this.get(prop);
}, this);
if (options && options.stateProperties) {
options.stateProperties.forEach(function(prop) {
this.originalState[prop] = this.get(prop);
}, this);
}
return this;
},
/**
* Setups state of an object
* @return {fabric.Object} thisArg
*/
setupState: function() {
this.originalState = { };
this.saveState();
return this;
}
});