@syncfusion/ej2-schedule
Version:
Flexible scheduling library with more built-in features and enhanced customization options similar to outlook and google calendar, allowing the users to plan and manage their appointments with efficient data-binding support.
460 lines (459 loc) • 16.4 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createElement, remove, isNullOrUndefined } from '@syncfusion/ej2-base';
/**
* Schedule common utilities
*/
export var WEEK_LENGTH = 7;
export var DEFAULT_WEEKS = 6;
export var MS_PER_DAY = 86400000;
export var MS_PER_MINUTE = 60000;
/**
* Method to get height from element
*
* @param {Element} container Accepts the DOM element
* @param {string} elementClass Accepts the element class
* @param {boolean} isTransformed Accepts the boolean value that indicates the status of the transform style applied to the element
* @returns {number} Returns the height of the element
*/
export function getElementHeightFromClass(container, elementClass, isTransformed) {
var height = 0;
var el = createElement('div', { className: elementClass }).cloneNode();
el.style.visibility = 'hidden';
el.style.position = 'absolute';
container.appendChild(el);
height = getElementHeight(el, isTransformed);
remove(el);
return height;
}
/**
* Method to get width from element
*
* @param {Element} container Accepts the DOM element
* @param {string} elementClass Accepts the element class
* @param {boolean} isTransformed Accepts the boolean value that indicates the status of the transform style applied to the element
* @returns {number} Returns the width of the element
*/
export function getElementWidthFromClass(container, elementClass, isTransformed) {
var width = 0;
var el = createElement('div', { className: elementClass }).cloneNode();
el.style.visibility = 'hidden';
el.style.position = 'absolute';
container.appendChild(el);
width = getElementWidth(el, isTransformed);
remove(el);
return width;
}
/**
* Method to get translateY value
*
* @param {HTMLElement | Element} element Accepts the DOM element
* @returns {number} Returns the translateY value of given element
*/
export function getTranslateY(element) {
var style = getComputedStyle(element);
return window.WebKitCSSMatrix ?
new WebKitCSSMatrix(style.webkitTransform).m42 : 0;
}
/**
* Method to get translateX value
*
* @param {HTMLElement | Element} element Accepts the DOM element
* @returns {number} Returns the translateX value of given element
*/
export function getTranslateX(element) {
var style = getComputedStyle(element);
return window.WebKitCSSMatrix ?
new WebKitCSSMatrix(style.webkitTransform).m41 : 0;
}
/**
* Method to get week first date
*
* @param {Date} date Accepts the date object
* @param {number} firstDayOfWeek Accepts the first day of week number
* @returns {Date} Returns the date object
*/
export function getWeekFirstDate(date, firstDayOfWeek) {
var date1 = new Date(date.getTime());
firstDayOfWeek = (firstDayOfWeek - date1.getDay() + 7 * (-1)) % 7;
return new Date(date1.setDate(date1.getDate() + (isNaN(firstDayOfWeek) ? 0 : firstDayOfWeek)));
}
/**
* Method to get week last date
*
* @param {Date} date Accepts the date object
* @param {number} firstDayOfWeek Accepts the first day of week number
* @returns {Date} Returns the date object
*/
export function getWeekLastDate(date, firstDayOfWeek) {
var weekFirst = getWeekFirstDate(date, firstDayOfWeek);
var weekLast = new Date(weekFirst.getFullYear(), weekFirst.getMonth(), weekFirst.getDate() + 6);
return new Date(weekLast.getTime());
}
/**
* Method to get first date of month
*
* @param {Date} date Accepts the date object
* @returns {Date} Returns the date object
*/
export function firstDateOfMonth(date) {
return new Date(date.getFullYear(), date.getMonth(), 1);
}
/**
* Method to get last date of month
*
* @param {Date} date Accepts the date object
* @returns {Date} Returns the date object
*/
export function lastDateOfMonth(date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0);
}
/**
* Method to get week number
*
* @param {Date} date Accepts the date object
* @returns {number} Returns the week number
*/
export function getWeekNumber(date) {
var date1 = new Date(date.getFullYear(), 0, 1).valueOf();
var currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()).valueOf();
var dayOfYear = ((currentDate - date1 + MS_PER_DAY) / MS_PER_DAY);
return Math.ceil(dayOfYear / 7);
}
/**
* Method to get week middle date
*
* @param {Date} weekFirst Accepts the week first date object
* @param {Date} weekLast Accepts the week last date object
* @returns {Date} Returns the date object
*/
export function getWeekMiddleDate(weekFirst, weekLast) {
return new Date(weekLast.valueOf() - ((weekLast.valueOf() - weekFirst.valueOf()) / 2));
}
/**
* Method to set time to date object
*
* @param {Date} date Accepts the date object
* @param {number} time Accepts the milliseconds
* @returns {Date} Returns the date object
*/
export function setTime(date, time) {
var tzOffsetBefore = date.getTimezoneOffset();
var d = new Date(date.getTime() + time);
var tzOffsetDiff = d.getTimezoneOffset() - tzOffsetBefore;
date.setTime(d.getTime() + tzOffsetDiff * MS_PER_MINUTE);
return date;
}
/**
* Method the reset hours in date object
*
* @param {Date} date Accepts the date object
* @returns {Date} Returns the date object
*/
export function resetTime(date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}
/**
* Method to get milliseconds from date object
*
* @param {Date} date Accepts the date object
* @returns {number} Returns the milliseconds from date object
*/
export function getDateInMs(date) {
var localOffset = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).getTimezoneOffset();
var dateOffset = date.getTimezoneOffset();
var timezoneOffset = dateOffset - localOffset;
return ((date.getTime() - new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).getTime())
- (timezoneOffset * 60 * 1000));
}
/**
* Method to get date count between two dates
*
* @param {Date} startDate Accepts the date object
* @param {Date} endDate Accepts the date object
* @returns {number} Returns the date count
*/
export function getDateCount(startDate, endDate) {
return Math.ceil((endDate.getTime() - startDate.getTime()) / MS_PER_DAY);
}
/**
* Method to add no of days in date object
*
* @param {Date} date Accepts the date object
* @param {number} noOfDays Accepts the number of days count
* @returns {Date} Returns the date object
*/
export function addDays(date, noOfDays) {
date = new Date('' + date);
return new Date(date.setDate(date.getDate() + noOfDays));
}
/**
* Method to add no of months in date object
*
* @param {Date} date Accepts the date object
* @param {number} noOfMonths Accepts the number of month count
* @returns {Date} Returns the date object
*/
export function addMonths(date, noOfMonths) {
date = new Date('' + date);
var day = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() + noOfMonths);
date.setDate(Math.min(day, getMaxDays(date)));
return date;
}
/**
* Method to add no of years in date object
*
* @param {Date} date Accepts the date object
* @param {number} noOfYears Accepts the number of month count
* @returns {Date} Returns the date object
*/
export function addYears(date, noOfYears) {
date = new Date('' + date);
var day = date.getDate();
date.setDate(1);
date.setFullYear(date.getFullYear() + noOfYears);
date.setDate(Math.min(day, getMaxDays(date)));
return date;
}
/**
* Method to get start and end hours
*
* @param {Date} date Accepts the date object
* @param {Date} startHour Accepts the start hour date object
* @param {Date} endHour Accepts the end hour date object
* @returns {Object} Returns the start and end hour date objects
*/
export function getStartEndHours(date, startHour, endHour) {
var date1 = new Date(date.getTime());
date1.setHours(startHour.getHours());
date1.setMinutes(startHour.getMinutes());
date1.setSeconds(startHour.getSeconds());
var date2 = new Date(date.getTime());
if (endHour.getHours() === 0) {
date2 = addDays(date2, 1);
}
else {
date2.setHours(endHour.getHours());
date2.setMinutes(endHour.getMinutes());
date2.setSeconds(endHour.getSeconds());
}
return { startHour: date1, endHour: date2 };
}
/**
* Method to get month last date
*
* @param {Date} date Accepts the date object
* @returns {number} Returns the month last date
*/
export function getMaxDays(date) {
return lastDateOfMonth(date).getDate();
}
/**
* Method to get days count between two dates
*
* @param {Date} startDate Accepts the date object
* @param {Date} endDate Accepts the date object
* @returns {number} Returns the days count
*/
export function getDaysCount(startDate, endDate) {
var strTime = resetTime(new Date(startDate));
var endTime = resetTime(new Date(endDate));
return Math.round((endTime.getTime() - strTime.getTime()) / MS_PER_DAY);
}
/**
* Method to get date object from date string
*
* @param {string} date Accepts the date string
* @returns {Date} Returns the date object
*/
export function getDateFromString(date) {
return date.indexOf('Date') !== -1 ? new Date(parseInt(date.match(/\d+/g).toString(), 10)) :
date.indexOf('T') !== -1 ? new Date(date) : new Date(date.replace(/-/g, '/'));
}
/** @private */
var scrollWidth = null;
/** @private */
var pixelRatio = null;
/**
* Method to get scrollbar width
*
* @returns {number} Returns the scrollbar width
* @private
*/
export function getScrollBarWidth() {
if (scrollWidth !== null) {
return scrollWidth;
}
if (pixelRatio === null) {
pixelRatio = window.devicePixelRatio || window.screen.availWidth / document.documentElement.clientWidth;
}
var divNode = createElement('div');
var value = 0;
divNode.style.cssText = 'width:100px;height: 100px;overflow: scroll;position: absolute;top: -9999px;';
document.body.appendChild(divNode);
var ratio = (devicePixelRatio) ? (devicePixelRatio.toFixed(2) === '1.10' || devicePixelRatio <= 1) ?
Math.ceil(devicePixelRatio % 1) : Math.floor(devicePixelRatio % 1) : 0;
value = (divNode.offsetWidth - divNode.clientWidth - ratio) | 0;
document.body.removeChild(divNode);
return scrollWidth = value;
}
/**
* Method to reset scrollbar width
*
* @private
* @returns {void}
*/
export function resetScrollbarWidth() {
var zoomPixelRatio = window.devicePixelRatio || window.screen.availWidth / document.documentElement.clientWidth;
if (pixelRatio && pixelRatio !== zoomPixelRatio) {
scrollWidth = null;
pixelRatio = zoomPixelRatio;
}
}
/**
* Method to find the index from data collection
*
* @param {Object} data Accepts the data as object
* @param {string} field Accepts the field name
* @param {string} value Accepts the value name
* @param {Object} event Accepts the data as object
* @param {Object[]} resourceCollection Accepts the data collections
* @returns {number} Returns the index number
*/
// eslint-disable-next-line max-len
export function findIndexInData(data, field, value, event, resourceCollection) {
for (var i = 0, length_1 = data.length; i < length_1; i++) {
if (data[parseInt(i.toString(), 10)]["" + field] === value) {
if (event) {
var field_1 = resourceCollection.slice(-2)[0].field;
var res = (event["" + field_1] instanceof Array ? event["" + field_1] : [event["" + field_1]]);
var resData = res.join(',');
if (resData.includes(data[parseInt(i.toString(), 10)][resourceCollection.slice(-1)[0].groupIDField])) {
return i;
}
}
else {
return i;
}
}
}
return -1;
}
/**
* Method to get element outer height
*
* @param {HTMLElement} element Accepts the DOM element
* @returns {number} Returns the outer height of the given element
*/
export function getOuterHeight(element) {
var style = getComputedStyle(element);
return element.offsetHeight + (parseInt(style.marginTop, 10) || 0) + (parseInt(style.marginBottom, 10) || 0);
}
/**
* Method to remove child elements
*
* @param {HTMLElement | Element} element Accepts the DOM element
* @returns {void}
*/
export function removeChildren(element) {
var elementChildren = [].slice.call(element.children);
for (var _i = 0, elementChildren_1 = elementChildren; _i < elementChildren_1.length; _i++) {
var elementChild = elementChildren_1[_i];
element.removeChild(elementChild);
}
}
/**
* Method to check DST is present or not in date object
*
* @param {Date} date Accepts the date object
* @returns {boolean} Returns the boolean value for either DST is present or not
*/
export function isDaylightSavingTime(date) {
var jan = new Date(date.getFullYear(), 0, 1);
var jul = new Date(date.getFullYear(), 6, 1);
return date.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
/**
* Method to get UTC time value from date
*
* @param {Date} date Accepts the date
* @returns {number} Returns the UTC time value
*/
export function getUniversalTime(date) {
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var milliseconds = date.getMilliseconds();
return Date.UTC(year, month, day, hours, minutes, seconds, milliseconds);
}
/**
* Method to check the device
*
* @returns {boolean} Returns the boolean value for either device is present or not.
*/
export function isMobile() {
return window.navigator.userAgent.toLowerCase().indexOf('mobi') > -1;
}
/**
* Method to check the IPad device
*
* @returns {boolean} Returns the boolean value for either IPad device is present or not.
*/
export function isIPadDevice() {
return window.navigator.userAgent.toLowerCase().indexOf('ipad') > -1;
}
/**
* Method to capitalize the first word in string
*
* @param {string} inputString Accepts the string value
* @param {string} type Accepts the string type
* @returns {string} Returns the output string
*/
export function capitalizeFirstWord(inputString, type) {
if (type === 'multiple') {
inputString = inputString.split(' ').map(function (e) { return e.charAt(0).toLocaleUpperCase() + e.substring(1); }).join(' ');
}
else if (type === 'single') {
if (inputString[0] >= '0' && inputString[0] <= '9') {
var array = inputString.match(/[a-zA-Z]/);
inputString = isNullOrUndefined(array) ? inputString :
inputString.slice(0, array.index) + inputString[array.index].toLocaleUpperCase() + inputString.slice(array.index + 1);
}
inputString = inputString[0].toLocaleUpperCase() + inputString.slice(1);
}
return inputString;
}
/**
* Method to get element cell width
*
* @param {HTMLElement} element Accepts the DOM element
* @param {boolean} isTransformed Accepts the boolean value that indicates the status of the transform style applied to the element
* @returns {number} Returns the width of the given element
*/
export function getElementWidth(element, isTransformed) {
return isTransformed ? element.offsetWidth : element.getBoundingClientRect().width;
}
/**
* Method to get element cell Height
*
* @param {HTMLElement} element Accepts the DOM element
* @param {boolean} isTransformed Accepts the boolean value that indicates the status of the transform style applied to the element
* @returns {number} Returns the Height of the given element
*/
export function getElementHeight(element, isTransformed) {
return isTransformed ? element.offsetHeight : element.getBoundingClientRect().height;
}
/**
* Method to get element cell Top
*
* @param {HTMLElement} element Accepts the DOM element
* @param {boolean} isTransformed Accepts the boolean value that indicates the status of the transform style applied to the element
* @returns {number} Returns the top value of the given element
*/
export function getElementTop(element, isTransformed) {
return isTransformed ? element.offsetTop : element.getBoundingClientRect().top;
}