Sha256: 87a0194c1881139bf14f50ed1f3f9d7856b4f1e23a51489d102aa9cc6ce89d32

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

$(document).ready(function() {
  function isValidDateTime(value) {
    return !isNaN(new Date(value).getTime());
  }

  function showError(message) {
    $('#published-at-error').text(message);
    $('.error').removeClass('hidden');
  }

  function hideError() {
    $('.error').addClass('hidden');
  }

  function validateScheduledStatus(publishedTime, currentTime) {
    if (!isValidDateTime(publishedTime)) {
      showError('Select a valid Date & Time.');
      return false;
    }
    if (publishedTime < currentTime) {
      showError('Scheduled Date & Time cannot be in the past.');
      return false;
    }

    hideError();
    return true;
  }

  function validatePublishedStatus(publishedTime, currentTime) {
    if (publishedTime > currentTime) {
      showError('Published Date & Time cannot be in the future. Clear the date and time to publish now, or set the status to Scheduled.');
      return false;
    }

    hideError();
    return true;
  }

  function validateDateTime(status) {
    const publishedAt = $('#page_published_at').val();
    const publishedTime = new Date(publishedAt);
    const currentTime = new Date();

    if (status === '90') {
      return validateScheduledStatus(publishedTime, currentTime);
    }

    if (status === '100') {
      return validatePublishedStatus(publishedTime, currentTime);
    }

    return true;
  }

  $('#save-button, #save-and-continue-button').on('click', function(event) {
    const status = $('#page_status_id').val();

    if (status && !validateDateTime(status)) {
      event.preventDefault();
      event.stopImmediatePropagation();
    }
  });
});

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trusty-cms-7.0.8 app/assets/javascripts/admin/validations/scheduled_status_validation.js
trusty-cms-7.0.7 app/assets/javascripts/admin/validations/scheduled_status_validation.js
trusty-cms-7.0.6 app/assets/javascripts/admin/validations/scheduled_status_validation.js