Permalink
Browse files

Clean up context and only add dom events to dom nodes

We used to just pass along every event to attacht to the dom, but a lot were Framer events like change:x. I now filter these out.
  • Loading branch information...
koenbok committed Jul 17, 2015
1 parent db96a5e commit 5c47bd5212995c74516dec0582ca053e6ce6ceff
Showing with 32 additions and 20 deletions.
  1. +13 −12 framer/Context.coffee
  2. +15 −7 framer/EventManager.coffee
  3. +1 −1 framer/Framer.coffee
  4. +3 −0 framer/Utils.coffee
View
@@ -44,7 +44,7 @@ class exports.Context extends BaseClass
@_rootElement.__cancelAppendChild = true
# Create a fresh root element:
@_rootElement = @_createRootElement()
@_createRootElement()
@_delayTimers?.map (timer) -> window.clearTimeout(timer)
@_delayIntervals?.map (timer) -> window.clearInterval(timer)
@@ -65,7 +65,7 @@ class exports.Context extends BaseClass
@reset()
if @_rootElement.parentNode
@_rootElement.parentNode.removeChild(@_rootElement)
@_rootElement.__cancelAppendChild = true
Utils.domCompleteCancel(@_appendRootElement)
getRootElement: ->
@_rootElement
@@ -90,18 +90,19 @@ class exports.Context extends BaseClass
_createRootElement: ->
element = document.createElement("div")
element.id = "FramerContextRoot-#{@_name}"
element.classList.add("framerContext")
@_rootElement = document.createElement("div")
@_rootElement.id = "FramerContextRoot-#{@_name}"
@_rootElement.classList.add("framerContext")
parentElement = @_parentLayer?._element
Framer.Loop.once "render", ->
parentElement ?= document.body
if not element.__cancelAppendChild
parentElement.appendChild(element)
if @_parentLayer
@_appendRootElement()
else
Utils.domComplete(@_appendRootElement)
element
_appendRootElement: =>
parentElement = @_parentLayer?._element
parentElement ?= document.body
parentElement.appendChild(@_rootElement)
run: (f) ->
previousContext = Framer.CurrentContext
View
@@ -1,3 +1,5 @@
Utils = require "./Utils"
EventManagerIdCounter = 0
class EventManagerElement
@@ -6,21 +8,27 @@ class EventManagerElement
@_events = {}
addEventListener: (eventName, listener) ->
# Filter out all the events that are not dom valid
if not Utils.domValidEvent(@element, eventName)
return
@_events[eventName] ?= []
@_events[eventName].push listener
@_events[eventName].push(listener)
@element.addEventListener(eventName, listener)
removeEventListener: (event, listener) ->
removeEventListener: (eventName, listener) ->
return unless @_events
return unless @_events[event]
@_events[event] = _.without @_events[event], listener
@element.removeEventListener(event, listener)
return unless @_events[eventName]
@_events[eventName] = _.without @_events[eventName], listener
@element.removeEventListener(eventName, listener)
return
removeAllEventListeners: (eventName) ->
events = if eventName then [eventName] else _.keys(@_events)
for eventName in events
View
@@ -41,7 +41,7 @@ Framer.Debug = (require "./Debug").Debug
Framer.Extras = require "./Extras/Extras"
Framer.Loop = new Framer.AnimationLoop()
Utils.domComplete Framer.Loop.start
Utils.domComplete(Framer.Loop.start)
window.Framer = Framer if window
View
@@ -420,6 +420,9 @@ Utils.domComplete = (f) ->
Utils.domCompleteCancel = (f) ->
__domComplete = _.without __domComplete, f
Utils.domValidEvent = (element, eventName) ->
return typeof(element["on#{eventName.toLowerCase()}"]) isnt "undefined"
Utils.domLoadScript = (url, callback) ->
script = document.createElement "script"

0 comments on commit 5c47bd5

Please sign in to comment.