Google Calendar Sync

The event calendar comes with third party calendar integration support through the integration plugin. This needs to be added to the download package or installed separately from a dedicated NPM package.

It includes everything you need to authenticate your users, get their Google calendars and provides functions for CRUD operations. It takes care of loading the events and converts them to the required format so that they can be displayed on the Mobiscroll calendar separately or mixed with other calendars and events.

Customizing the interaction, event workflows, whether the events are read-only is up to you. In this live demo, newly created events will land in the primary calendar of the authenticated user.

This example uses the integration plugin to connect to and load events from Google Calendar and it is not available in the trial. Give the live demo a try and see how it can be configured.
Get started with the agenda

Demo

Loading the live demo

Preview in full screen

Load and sync Google Calendar events in a agenda view — authenticate users, toggle calendars on/off, and create events using the plugin.

Code

mobiscroll.setOptions({
  locale:  ,
  theme: ' ',
  themeVariant: ' '
});

$(function () {
  var googleCalendarSync = mobiscroll.googleCalendarSync;
  var calendarIds = [];
  var myEvents = [];
  var startDate;
  var endDate;
  var loader;
  var timer;

  function isLoading(loading) {
    loader = loader || $('#demo-sync-google-cal-loader');
    loader.css('visibility', loading ? 'visible' : 'hidden');
  }

  function toggleButtons(loggedIn) {
    if (loggedIn) {
      $btnSignIn.hide();
      $btnCalPopup.show();
    } else {
      $btnSignIn.show();
      $btnCalPopup.hide();
    }
  }

  function toggleCalendar(checked, calendarId) {
    if (checked) {
      calendarIds.push(calendarId);
    } else {
      calendarIds = calendarIds.filter(function (id) {
        return id !== calendarId;
      });
    }
    if (calendarIds.length === 0) {
      myEvents = [];
      inst.setEvents(myEvents);
      return;
    }
    isLoading(true);
    googleCalendarSync
      .getEvents(calendarIds, startDate, endDate)
      .then(function (resp) {
        myEvents = resp;
        inst.setEvents(myEvents);
        isLoading(false);
      })
      .catch(function (error) {
        onError(error);
      });
  }

  function onError(resp) {
    mobiscroll.toast({
      message: resp.error ? resp.error : resp.result.error.message,
    });
  }

  function onSignedIn() {
    toggleButtons(true);
    calendarIds = [];
    googleCalendarSync
      .getCalendars()
      .then(function (calendars) {
        var calList = '';

        calendars.sort(function (c) {
          return c.primary ? -1 : 1;
        });

        calendars.forEach(function (c) {
          calList +=
            '<label>' +
            '<input mbsc-switch value="' +
            c.id +
            '" data-label="' +
            c.summary +
            '" type="checkbox" class="mds-google-cal-switch" checked />' +
            '</label>';

          calendarIds.push(c.id);
        });

        $calendarList.html(calList);
        mobiscroll.enhance($calendarList[0]);

        isLoading(true);

        return googleCalendarSync.getEvents(calendarIds, startDate, endDate);
      })
      .then(function (resp) {
        myEvents = resp;
        inst.setEvents(myEvents);
        isLoading(false);
      })
      .catch(onError);
  }

  function onSignedOut() {
    toggleButtons(false);
    calendarIds = [];
    myEvents = [];
    inst.setEvents([]);
    popup.close();
  }

  var inst = $('#demo-sync-google-cal')
    .mobiscroll()
    .eventcalendar({
      view: {
        agenda: { type: 'month' },
      },
      exclusiveEndDates: true,
      onPageLoading: function (args) {
        startDate = args.viewStart;
        endDate = args.viewEnd;
        clearTimeout(timer);
        timer = setTimeout(function () {
          if (googleCalendarSync.isSignedIn()) {
            isLoading(true);
            googleCalendarSync
              .getEvents(calendarIds, startDate, endDate)
              .then(function (resp) {
                myEvents = resp;
                inst.setEvents(myEvents);
                isLoading(false);
              })
              .catch(onError);
          }
        }, 200);
      },
      renderHeader: function () {
        return (
          '<div mbsc-calendar-nav></div>' +
          '<div id="demo-sync-google-cal-loader" class="mds-loader"></div>' +
          '<div class="mbsc-flex mbsc-flex-1-0 mbsc-justify-content-end">' +
          '<button mbsc-button id="demo-sync-google-cal-sign-in">Sync my Google calendars</button>' +
          '<button mbsc-button id="demo-sync-google-cal-popup-btn" style="display:none">My Calendars</button>' +
          '<button mbsc-button id="demo-sync-google-cal-today">Today</button></div>' +
          '</div>' +
          '<button mbsc-calendar-prev></button>' +
          '<button mbsc-calendar-next></button>'
        );
      },
    })
    .mobiscroll('getInst');

  var $btnSignIn = $('#demo-sync-google-cal-sign-in');
  var $btnSignOut = $('#demo-sync-google-cal-sign-out');
  var $btnCalPopup = $('#demo-sync-google-cal-popup-btn');
  var $btnCalToday = $('#demo-sync-google-cal-today');
  var $calendarList = $('#demo-sync-google-cal-list');

  var popup = $('#demo-sync-google-cal-popup')
    .mobiscroll()
    .popup({
      anchor: $btnCalPopup[0],
      contentPadding: false,
      display: 'anchored',
      scrollLock: false,
      showOverlay: false,
      touchUi: false,
      width: 400,
    })
    .mobiscroll('getInst');

  $btnCalPopup.on('click', function () {
    popup.open();
  });

  $btnCalToday.on('click', function () {
    inst.navigate(new Date());
  });

  $btnSignIn.on('click', function () {
    if (!googleCalendarSync.isSignedIn()) {
      googleCalendarSync.signIn().catch(onError);
    }
  });

  $btnSignOut.on('click', function () {
    googleCalendarSync.signOut().catch(onError);
  });

  $calendarList.on('change', '.mds-google-cal-switch', function (ev) {
    toggleCalendar(ev.target.checked, ev.target.value);
  });

  // Init Google Client
  googleCalendarSync.init({
    apiKey: '<YOUR_GOOGLE_API_KEY>',
    clientId: '<YOUR_GOOGLE_CLIENT_ID>',
    onSignedIn: onSignedIn,
    onSignedOut: onSignedOut,
  });
});
  

Duplicate Session

You have signed in on a different machine. To keep everything in sync, you have been signed out of this session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.

Duplicate Session

We noticed you are logged in on a different machine and have closed your old session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.