diff --git a/src/canvas.class.js b/src/canvas.class.js index ec584969..8d9de4a4 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -25,7 +25,6 @@ * * @fires before:selection:cleared * @fires selection:cleared - * @fires selection:created * * @fires path:created * @fires mouse:down @@ -1430,7 +1429,7 @@ if (object === currentActiveObject) { return this; } - if (this._setActiveObject(object)) { + if (this._setActiveObject(object, e)) { currentActiveObject && currentActiveObject.fire('deselected', { e: e }); this.fire('object:selected', { target: object, e: e }); object.fire('selected', { e: e }); @@ -1440,14 +1439,15 @@ /** * @private - * @param {Object} object + * @param {Object} object to set as active + * @param {Event} [e] Event (passed along when firing "object:selected") */ - _setActiveObject: function(object) { + _setActiveObject: function(object, e) { var active = this._activeObject; - if (active === object) { + if (active === object || object.onSelect({ e: e })) { return false; } - if (this._discardActiveObject()) { + if (this._discardActiveObject(e)) { this._activeObject = object; object.set('active', true); return true; @@ -1458,11 +1458,11 @@ /** * @private */ - _discardActiveObject: function() { + _discardActiveObject: function(e) { var obj = this._activeObject; if (obj && obj.onDeselect && typeof obj.onDeselect === 'function') { // onDeselect return TRUE to cancel selection; - if (obj.onDeselect()) { + if (obj.onDeselect({ e: e })) { return false; } obj.set('active', false); @@ -1484,7 +1484,7 @@ var activeObject = this._activeObject; if (activeObject) { this.fire('before:selection:cleared', { target: activeObject, e: e }); - if (this._discardActiveObject()) { + if (this._discardActiveObject(e)) { this.fire('selection:cleared', { e: e }); activeObject.fire('deselected', { e: e }); } diff --git a/src/mixins/itext_behavior.mixin.js b/src/mixins/itext_behavior.mixin.js index fad2bfa7..2d0c2f02 100644 --- a/src/mixins/itext_behavior.mixin.js +++ b/src/mixins/itext_behavior.mixin.js @@ -15,10 +15,10 @@ this.mouseMoveHandler = this.mouseMoveHandler.bind(this); }, - onDeselect: function() { + onDeselect: function(options) { this.isEditing && this.exitEditing(); this.selected = false; - this.callSuper('onDeselect'); + fabric.Object.prototype.onDeselect.call(this, options); }, /** diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index 682ace71..cfe3b43f 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -924,6 +924,28 @@ // implemented by sub-classes, as needed. }, + /** + * This callback function is called every time _discardActiveObject or _setActiveObject + * try to to deselect this object. If the function returns true, the process is cancelled + * @param {Object} [options] options sent from the upper functions + * @param {Event} [options.e] event if the process is generated by an event + */ + onDeselect: function() { + // implemented by sub-classes, as needed. + }, + + + /** + * This callback function is called every time _discardActiveObject or _setActiveObject + * try to to select this object. If the function returns true, the process is cancelled + * @param {Object} [options] options sent from the upper functions + * @param {Event} [options.e] event if the process is generated by an event + */ + onSelect: function() { + // implemented by sub-classes, as needed. + }, + + /** * Retrieves viewportTransform from Object's canvas if possible * @method getViewportTransform