gitcoinco/code_fund_ads

View on GitHub
app/views/advertisements/show.js.erb

Summary

Maintainability
Test Coverage
(function () {
  let timeout;

  function init(attempts) {
    clearTimeout(timeout)
    attempts = attempts || 1
    if (attempts > 3) return
    if (typeof CodeFundAd === 'undefined') {
      timeout = setTimeout(function () { init(attempts + 1) }, 350)
      return
    }
    new CodeFundAd(<%= advertisement_mustache_template_options.to_json.html_safe %>)
  }

  const codefundThemeName = '<%= theme_name.to_s %>'
  const codefundStylesheetId = 'codefund-style'
  const codefundScriptId = 'codefund-script'

  if (!document.getElementById(codefundStylesheetId) && codefundThemeName !== 'unstyled') {
    const stylesheet = document.createElement('link')
    stylesheet.setAttribute('id', codefundStylesheetId)
    stylesheet.setAttribute('rel', 'stylesheet')
    stylesheet.setAttribute('media', 'all')
    stylesheet.setAttribute('href', '<%= asset_pack_url "code_fund_ad.css" %>')
    stylesheet.addEventListener('load', init)
    document.head.appendChild(stylesheet)
  }

  if (document.getElementById(codefundScriptId)) {
    init()
  } else {
    const script = document.createElement('script')
    script.setAttribute('id', codefundScriptId)
    script.setAttribute('type', 'text/javascript')
    script.setAttribute('src', '<%= asset_pack_url "code_fund_ad.js" %>')
    script.addEventListener('load', init)
    document.head.appendChild(script)
  }
})()