Switch to list view on mobile

Created on 13 October 2021, over 2 years ago
Updated 8 May 2023, about 1 year ago

Problem/Motivation

When the screen shrinks, or on mobile, the event display becomes too small to be useful. What I'd like is for the display to toggle to listYear when the screen size is small enough. I'm having trouble getting it going.

In mytheme.libraries.yml I've added:

calendarmobilize:
  js:
    resources/js/calendarmobilize.js: {}
  dependencies:
    - core/jquery
    - fullcalendar_view/fullcalendar

in mytheme.theme I've added:

function mytheme_page_attachments_alter(&$page) {
  // Get the current path
  $path = $current_path = \Drupal::service('path.current')->getPath();
  // Check if the alias is the one you want
  if ($path == '/calendar') {
  // Attach the library
    $page['#attached']['library'][] = 'mytheme/calendarmobilize';
  }
}

In mytheme/resources/js/calendarmobilize.js I've added:

(function ($) {
  console.log("document", $( window ).width() );
// shows the width of the page at the start
  window.addEventListener('resize', function (event){
    if ($(window).width() < 900){
      console.log("Mobile detected");
      $(".js-drupal-fullcalendar").fullCalendar('changeView', 'listYear');
    }
  });
}(jQuery));

On window resize, the console throws the error:

Uncaught TypeError: $(...).fullCalendar is not a function.

Am I not going about this the right way? What have people done to make sure their calendars work for mobile?

πŸ’¬ Support request
Status

Fixed

Version

5.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ed2908

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024