github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/assets/scripts/cirrus.js (about)

     1  ;(function (w, d) {
     2    // On android, change the address bar color to match the page background
     3    const paperColor = getComputedStyle(d.body).getPropertyValue(
     4      '--paperBackgroundColor',
     5    )
     6    if (paperColor) {
     7      const themeColor = d.querySelector('meta[name=theme-color]')
     8      themeColor.setAttribute('content', paperColor)
     9    }
    10  
    11    // Add an helper function to display an error on a form input
    12    w.showError = (field, message) => {
    13      let tooltip = field.querySelector('.invalid-tooltip')
    14      let input = field.querySelector('input')
    15      let submit = input.form.querySelector('[type=submit]')
    16      let error = 'The Cozy server is unavailable. Do you have network?'
    17      if (message) {
    18        error = '' + message
    19      }
    20  
    21      if (tooltip) {
    22        tooltip.classList.remove('d-none')
    23        tooltip.lastChild.textContent = error
    24      } else {
    25        tooltip = d.createElement('div')
    26        tooltip.classList.add('invalid-tooltip', 'mb-1')
    27        const arrow = d.createElement('div')
    28        arrow.classList.add('tooltip-arrow')
    29        tooltip.appendChild(arrow)
    30        const icon = d.createElement('span')
    31        icon.classList.add('icon', 'icon-alert', 'bg-danger')
    32        tooltip.appendChild(icon)
    33        tooltip.append(error)
    34        field.appendChild(tooltip)
    35      }
    36  
    37      submit.removeAttribute('disabled')
    38      input.removeAttribute('disabled')
    39      input.classList.add('is-invalid')
    40      input.select()
    41    }
    42  
    43    // Use the browser history for managing cancel links
    44    const cancel = w.document.querySelector('a.cancel')
    45    if (cancel) {
    46      cancel.addEventListener('click', function (event) {
    47        if (w.history.length > 1) {
    48          event.preventDefault()
    49          w.history.back()
    50        }
    51      })
    52    }
    53  
    54    const expand = w.document.querySelector('a.expand')
    55    if (expand) {
    56      expand.addEventListener('click', function (event) {
    57        event.preventDefault()
    58        expand.classList.toggle('expanded')
    59  
    60        if (expand.getAttribute('aria-expanded') === 'true') {
    61          expand.setAttribute('aria-expanded', 'false')
    62        } else {
    63          expand.setAttribute('aria-expanded', 'true')
    64        }
    65      })
    66    }
    67  })(window, document)