barra-brasil.coffee 2.89 KB
  divBarra = document.getElementById("barra-brasil")
  if divBarra
    divBarra.removeAttribute "style"
  # conteudoBarra definido no barra-brasil-1.yaml e atribuido pelo assetgen
    divBarra.innerHTML = conteudoBarra
    head = document.getElementsByTagName("head")[0]

  !((window) ->

    $q = (q, res) ->
      if document.querySelectorAll
        res = document.querySelectorAll(q)
      else
        d = document
        a = d.styleSheets[0] or d.createStyleSheet()
        a.addRule q, 'f:b'
        l = d.all
        b = 0
        c = []
        f = l.length
        while b < f
          l[b].currentStyle.f and c.push(l[b])
          b++
        a.removeRule 0
        res = c
      res

    addEventListener = (evt, fn) ->
      if window.addEventListener then document.getElementById("logovlibras").addEventListener(evt, fn, false) else if window.attachEvent then document.getElementById("logovlibras").attachEvent('on' + evt, fn) else (@['on' + evt] = fn)
      return

    _has = (obj, key) ->
      Object::hasOwnProperty.call obj, key

    images = new Array
    query = $q('img.barralazy')

    processScroll = ->
      i = 0
      while i < images.length
        if elementInViewport(images[i])
          loadImage images[i], ->
            images.splice i, i
            return
        i++
      return

    loadImage = (el, fn) ->
      img = new Image
      src = el.getAttribute('data-src')

      img.onload = ->
        if ! !el.parent
          el.parent.replaceChild img, el
        else
          el.src = src
        if fn then fn() else null
        return

      img.src = src
      return

    elementInViewport = (el) ->
      rect = el.getBoundingClientRect()
      rect.top >= 0 and rect.left >= 0 and rect.top <= (window.innerHeight or document.documentElement.clientHeight)

    i = 0
    while i < query.length
      images.push query[i]
      i++

    addEventListener 'click', processScroll
    addEventListener 'mouseover', processScroll
    addEventListener 'focus', processScroll
    return
  )(this)

  loadVlibras = ->
    window.VLibras.Widget 'https://vlibras.gov.br/app2', 'https://vlibras.gov.br/config/default_logo.json'

  divLibras = document.createElement 'div'
  divLibras.innerHTML = '<div vw class=\"enabled\"><div vw-access-button class=\"active\"></div><div vw-plugin-wrapper><div class=\"vw-plugin-top-wrapper\"></div><div class=\"vw-links\"><ul></ul></div></div></div>'
  document.body.appendChild divLibras

  script = document.createElement 'script'
  script.setAttribute 'src', 'https://vlibras.gov.br/app2/vlibras-plugin.js'
  script.type = "text/javascript"
  script.onload = loadVlibras
  document.body.appendChild script

  window._barrabrasil =
    insere_css: (css) ->
        style = document.createElement("style")
        style.setAttribute "type","text/css"
        style.setAttribute "media","all"
        style.appendChild document.createTextNode(css)

        head.appendChild style