Features

  • Zoom using HTML5 slider control
  • Zoom using buttons: [+] and [-]
  • Custom zoom levels with defined properties (scale, visible days, time headers...)
  • Includes a trial version of DayPilot Pro for JavaScript (see License below)

License

Licensed for testing and evaluation purposes. Please see the license agreement included in the sample project. You can use the source code of the tutorial if you are a licensed user of DayPilot Pro for JavaScript. Buy a license.

Live Demo

Scheduler Zoom Levels

javascript-scheduler-zoom-levels.png

The first steps is to define the zoom levels for the JavaScript Scheduler component that we want to make available in our application. The users will be able to change the Scheduler zoom level using an HTML5 slider control and (+)/(-) buttons that we add later.

The zoomLevels property is holds an array of objects that specify the zoom level properties:

  • name (custom zoom level name displayed to the user)
  • scale (Scheduler scale unit)
  • cellWidth (cell width in pixels)
  • cellDuration (cell duration which is used for custom scale)
  • timeHeaders (time header rows)
  • startDate (calculated start date)
  • days (calculated number of visible days)

Each object can also have arbitrary properties at the top level (like name in this case). There properties will be ignored by the Scheduler.

The properties items value can be either the target value (string, number, etc.) or a function. Using a function lets you calculate the return value using the Scheduler state (args.date holds the reference date, args.level holds the target level index);

dp.zoomLevels = [
  {
    name: "Year",
    properties: {
      scale: "Day",
      cellWidth: 40,
      timeHeaders: [{groupBy: "Year"}, {groupBy: "Month", format: "MMMM"}, {groupBy: "Day", format: "d"}],
      startDate: function (args) { return args.date.firstDayOfYear(); },
      days: function (args) { return args.date.daysInYear(); },
    }
  },
  {
    name: "Month",
    properties: {
      scale: "CellDuration",
      cellDuration: 720,
      cellWidth: 40,
      timeHeaders: [{groupBy: "Month"}, {groupBy: "Day", format: "ddd d"}, {groupBy: "Cell", format: "tt"}],
      startDate: function(args) { return args.date.firstDayOfMonth(); },
      days: function(args) { return args.date.daysInMonth();},
    }
  },
  {
    name: "Week",
    properties: {
      scale: "Hour",
      cellWidth: 40,
      timeHeaders: [{groupBy: "Month"}, {groupBy: "Day", format: "dddd d"}, {groupBy: "Hour"}],
      startDate: function(args) { return args.date.firstDayOfWeek(); },
      days: function() { return 7; },
    }
  },
  {
    name: "Hour",
    properties: {
      scale: "CellDuration",
      cellDuration: 15,
      cellWidth: 40,
      timeHeaders: [{groupBy: "Day", format: "dddd MMMM d, yyyy"}, {groupBy: "Hour"}, {groupBy: "Cell"}],
      startDate: function(args) { return args.date.getDatePart(); },
      days: function() { return 1; },
    }
  },
];

Applying Zoom Level

The applyLevel() function calls zoom.setActive() method to apply the selected zoom level (specified using index parameter) to the JavaScript Scheduler component.

When the Scheduler is updated to display the new zoom level it scrolls to the reference date/time. The reference date is calculated using zoomPosition property before the zoom level is changed. We are using "middle" which means the date which was at the middle point of the Scheduler viewport will remain in the middle after the zoom level change.

The applyLevel() function also displays the current zoom level name using the <span> label element.

dp.zoomPosition = "middle";
dp.zoomLevels = [ ... ];

function applyLevel(index, date) {
  dp.zoom.setActive(index, "left");
  elements.range.value = index;
  elements.label.innerText = "Current zoom level: " + dp.zoom.levels[index].name;
}

Slider Control for Changing Scheduler Zoom Level

javascript-scheduler-zoom-slider-control.png

HTML5 slider control:

<input type="range" min="0" max="3" step="1" id="zoom-level" />

Event handler:

var elements = {
  range: document.getElementById("zoom-level"),
  // ...
};

elements.range.addEventListener("input", function(ev) {
  var val = elements.range.value;
  applyLevel(val);
});

Zoom Buttons

javascript-scheduler-zoom-buttons.png

HTML5:

<div class="controls">
  <button id="minus">-</button>
  <button id="plus">+</button>
</div>

CSS Styling:

.controls button {
  width: 25px;
  height: 25px;

  background-color: #3c78d8;
  color: white;
  border: 0;
  cursor: pointer;
}
.controls button:focus {
  outline: none;
}

Event handlers:

var elements = {
  plus: document.getElementById("plus"),
  minus: document.getElementById("minus"),
  // ...
};

elements.plus.addEventListener("click", function(ev) {
  if (elements.range.value < dp.zoomLevels.length - 1) {
    elements.range.value++;
    applyLevel(elements.range.value);
  }
});

elements.minus.addEventListener("click", function(ev) {
  if (elements.range.value > 0) {
    elements.range.value--;
    applyLevel(elements.range.value);
  }
});