Sha256: 4a20d27fa0f58d86364f089ced70ff8ac01330de128fd0de98adff24332831a3

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

import $ from 'jquery'

import { init } from '../core/events'
import { i18n } from '../core/i18n'

function setupDatePicker (selectedDates, dateStr, instance) {
  addClearButton(instance)
  enableDirectEntry(instance)
}

function addClearButton (instance) {
  const $input = $(instance.input)

  if ($input.data('allow-clear')) {
    $('<a href="#">')
      .on('click', function (e) {
        e.preventDefault()

        const isDisabled = $input.is(':disabled') || $input.hasClass('disabled')

        if (!isDisabled) {
          instance.clear()
        }
      })
      .addClass('clear-datepicker')
      .insertBefore(instance.altInput)
  }
}

function enableDirectEntry (instance) {
  const $altInput = $(instance.altInput)

  $altInput.on('input', function (e) {
    const value = $altInput.val()

    const parsedDate = instance.parseDate(value, instance.config.altFormat)
    const formattedDate = instance.formatDate(parsedDate, instance.config.altFormat)

    if (value === formattedDate) {
      instance.setDate(value, true, instance.config.altFormat)
    }
  })
}

init(function (root) {
  $(root).find('input[type="date"][data-picker="true"]').flatpickr({
    allowInput: true,
    altInput: true,
    altFormat: i18n['admin.datepicker.formats.date'] || 'm/d/Y',
    onReady: setupDatePicker
  })

  $(root).find('input[type="datetime"][data-picker="true"], input[type="datetime-local"][data-picker="true"]').flatpickr({
    enableTime: true,
    allowInput: true,
    altInput: true,
    altFormat: i18n['admin.datepicker.formats.datetime'] || 'm/d/Y h:i K',
    onReady: setupDatePicker
  })

  $(root).find('input[type="time"][data-picker="true"]').flatpickr({
    enableTime: true,
    noCalendar: true,
    allowInput: true,
    altInput: true,
    altFormat: i18n['admin.datepicker.formats.time'] || 'h:i K',
    onReady: setupDatePicker
  })
})

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
trestle-0.9.8 frontend/js/components/datepicker.js
trestle-0.9.7 frontend/js/components/datepicker.js
trestle-0.9.6 frontend/js/components/datepicker.js
trestle-0.9.5 frontend/js/components/datepicker.js
trestle-0.9.4 frontend/js/components/datepicker.js
trestle-0.9.3 frontend/js/components/datepicker.js
trestle-0.9.2 frontend/js/components/datepicker.js
trestle-0.9.1 frontend/js/components/datepicker.js
trestle-0.9.0 frontend/js/components/datepicker.js