Sha256: 9dcbf3106e2e95838f6fb7bff07eebc5cec977a77e4ed4f56e174e884ffadf92

Contents?: true

Size: 1.96 KB

Versions: 10

Compression:

Stored size: 1.96 KB

Contents

import { Controller } from '@hotwired/stimulus'

export default class OembedController extends Controller {
  static values = {
    url: String
  }

  connect() {
    const loadedAttr = this.element.getAttribute('loaded')

    if (loadedAttr && loadedAttr === 'loaded') {
      return
    }

    fetch(this.urlValue)
      .then((response) => {
        if (response.ok) return response.text()
        throw new Error(`HTTP error, status = ${response.status}`)
      })
      .then((body) => {
        const oEmbedEndPoint = this.findOEmbedEndPoint(body)
        if (!oEmbedEndPoint || oEmbedEndPoint.length === 0) {
          console.warn(`No oEmbed endpoint found in <head> at ${this.urlValue}`)
          return
        }
        this.loadEndPoint(oEmbedEndPoint)
      }).catch(error => {
        console.error(error)
      })
  }

  // We are choosing not to make this class static so that downstream classes
  // can override it and access values to populate extraParams.
  // E.g. https://github.com/sul-dlss/vt-arclight/blob/main/app/javascript/controllers/sul_embed_controller.js
  findOEmbedEndPoint(body, extraParams = {}) { // eslint-disable-line class-methods-use-this
    // Parse out link elements so image assets are not loaded
    const template = document.createElement('template')
    template.innerHTML = body.match(/<link .*>/g).join('')

    // Look for a link element containing the oEmbed endpoint; bail out if none
    const endpoint = template.content.querySelector('link[rel="alternate"][type="application/json+oembed"]')
      ?.getAttribute('href')
    if (!endpoint) return ''

    // Serialize any extra params and append them to the endpoint
    const qs = new URLSearchParams(extraParams).toString()
    return `${endpoint}&${qs}`
  }

  loadEndPoint(oEmbedEndPoint) {
    fetch(oEmbedEndPoint)
      .then((response) => response.json())
      .then((json) => {
        this.element.innerHTML = json.html
        this.element.setAttribute('loaded', 'loaded')
      })
  }
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
arclight-1.4.0 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.3.0 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.2.0 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.1.3 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.1.2 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.1.1 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.1.0 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.0.1 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.0.0 app/assets/javascripts/arclight/oembed_controller.js
arclight-1.0.0.beta6 app/assets/javascripts/arclight/oembed_controller.js