Sha256: b70181d665c38652a0a345590a176269beb02e1163a3f48a5fb9cf8e7842c607

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

{elementMatchesSelector} = LocalTime

class LocalTime.PageObserver
  constructor: (@selector, @callback) ->

  start: ->
    unless @started
      @observeWithMutationObserver() or @observeWithMutationEvent()
      @started = true

  observeWithMutationObserver: ->
    if MutationObserver?
      observer = new MutationObserver @processMutations
      observer.observe(document.documentElement, childList: true, subtree: true)
      true

  observeWithMutationEvent: ->
    addEventListener("DOMNodeInserted", @processInsertion, false)
    true

  findSignificantElements: (element) ->
    elements = []
    if element?.nodeType is Node.ELEMENT_NODE
      elements.push(element) if elementMatchesSelector(element, @selector)
      elements.push(element.querySelectorAll(@selector)...)
    elements

  processMutations: (mutations) =>
    elements = []
    for mutation in mutations
      switch mutation.type
        when "childList"
          for node in mutation.addedNodes
            elements.push(@findSignificantElements(node)...)
    @notify(elements)

  processInsertion: (event) =>
    elements = @findSignificantElements(event.target)
    @notify(elements)

  notify: (elements) ->
    if elements?.length
      @callback?(elements)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_auction-0.0.6 lib/assets/javascripts/src/local-time/page_observer.coffee
solidus_auction-0.0.5 lib/assets/javascripts/src/local-time/page_observer.coffee
solidus_auction-0.0.4 lib/assets/javascripts/src/local-time/page_observer.coffee
solidus_auction-0.0.3 lib/assets/javascripts/src/local-time/page_observer.coffee
solidus_auction-0.0.1 lib/assets/javascripts/src/local-time/page_observer.coffee