Introduce Item._itemHandlers to remove code redundancy.

This commit is contained in:
Jürg Lehni 2016-01-27 11:38:14 +01:00
parent ed8a904b74
commit 3276616f53
2 changed files with 39 additions and 32 deletions

View file

@ -67,8 +67,44 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
selected: false,
clipMask: false,
data: {}
},
}
},
new function() { // // Scope to inject various item event handlers
var handles = ['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'];
return Base.each(handles,
function(name) {
this._events[name] = {
install: function(type) {
this.getView()._countItemEvent(type, 1);
},
uninstall: function(type) {
this.getView()._countItemEvent(type, -1);
}
};
}, {
_events: {
onFrame: {
install: function() {
this.getView()._animateItem(this, true);
},
uninstall: function() {
this.getView()._animateItem(this, false);
}
},
// Only for external sources, e.g. Raster
onLoad: {},
onError: {}
},
statics: {
_itemHandlers: handles
}
}
);
}, /** @lends Item# */{
initialize: function Item() {
// Do nothing, but declare it for named constructors.
},
@ -121,35 +157,6 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
return hasProps;
},
_events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'],
function(name) {
this[name] = {
install: function(type) {
this.getView()._countItemEvent(type, 1);
},
uninstall: function(type) {
this.getView()._countItemEvent(type, -1);
}
};
}, {
onFrame: {
install: function() {
this.getView()._animateItem(this, true);
},
uninstall: function() {
this.getView()._animateItem(this, false);
}
},
// Only for external sources, e.g. Raster
onLoad: {},
onError: {}
}
),
_serialize: function(options, dictionary) {
var props = {},
that = this;

View file

@ -136,8 +136,8 @@ var View = Base.extend(Emitter, /** @lends View# */{
return true;
},
_events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove',
'onMouseDrag', 'onMouseEnter', 'onMouseLeave'],
_events: Base.each(
Item._itemHandlers.concat(['onResize', 'onKeyDown', 'onKeyUp']),
function(name) {
this[name] = {};
}, {