Event = require 'ecl/dist/event'
module.exports = class Component extends require './shape'
Event = require 'ecl/dist/event'
module.exports = class Component extends require './shape'
constructor: ->
super
Register validating listeners for each type from the list.
for type in ['grab', 'release', 'drag']
Localize the type variable.
do (type) =>
Add capturing listener which will cancel and stop the event if the event type is not enabled.
@addListener type:type, capture:yes, listener: (event) ->
event.cancel().stop() unless @events?[type]
Add normal listener which will cancel and stop the event if the event type is not enabled.
@addListener type:type, capture:no, listener: (event) ->
event.cancel().stop() unless @events?[type]
Register extendable listeners for each type from the list.
for type in ['grab', 'release', 'drag']
Add capturing extendable listener
@addListener type, this["#{type}CaptureListener"], yes
Add normal extendable listener
@addListener type, this["#{type}Listener"], no
Capture mousemove event passing throught this component
mousemoveCaptureListener: (event) ->
super
Proceed only for valid events that have
if event
$ = @___runtime
Reset component state.
@state = null
If component is grabbed then set the state to active.
if $.grab
@state = 'active'
Create drag Event and broadcast it to this component if drag event is enabled,
if @events?.drag
@broadcastEvent new Event
type:'drag', x:event.x, y:event.y
setting the drag offset.
offsetX: event.x - ($.dragEvent?.x or $.grabEvent?.x or 0)
offsetY: event.y - ($.dragEvent?.y or $.grabEvent?.y or 0)
Otherwise, set the state to the name of the first matching region in order.
else if regions = event.regions
@state = (if regions.hover then 'hover' else if regions.normal then 'normal')
return this
Capture mousedown events passing through this shape
mousedownCaptureListener: (event) ->
super
Proceed only for valid events that have any matching regions
if event and regions = event.regions
Set the state to the first matching region name in order
for name in ['active', 'hover', 'normal'] when regions[name]
state = name
break
If any state was set, assign it to the component and set the component as the event target.
if (@state = state or null) then event.target = this
return this
Capture mouseup events passing through this component
mouseupCaptureListener: (event) ->
super
Proceed only for valid events
if event
$ = @___runtime
Set the state to the first matching region name in order (if any)
if regions = event.regions
for name in ['active', 'hover', 'normal'] when regions[name]
state = name
break
If any state was set then assign it to the component and set the component as the event target.
if (@state = state or null) then event.target = this
If grab event preceeded then clear it.
if $.grab
$.grab = false
Create and broadcast the release event to this component if release event is enabled.
if @events?.release
@broadcastEvent $.releaseEvent = new Event
type:'release', x:event.x, y:event.y
return this
mousedownListener: (event) ->
super
Proceed only for valid event
if event
If component is in active state and grab event is enabled then grab this component.
if (@state is 'active') and @events?.grab
($ = @___runtime).grab = true
Create grab event and deliver it to this component.
$.dragEvent = null
@broadcastEvent new Event
type:'grab', x:event.x, y:event.y
return this
dragCaptureListener: (event) ->
Proceed only for valid event
if event
Save the event and localize event coordinates.
@___runtime.dragEvent = event
@localizeEventCoordinates event
return this
grabCaptureListener: (event) ->
Proceed only for valid event
if event
Save the event and localize event coordinates.
@___runtime.grabEvent = event
@localizeEventCoordinates event
return this
releaseCaptureListener: (event) ->
Proceed only for valid event
if event
Save the event and localize event coordinates.
@___runtime.releaseEvent = event
@localizeEventCoordinates event
return this
dragListener: -> this
grabListener: -> this
releaseListener: -> this