UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

107 lines (106 loc) 3.7 kB
/** * DevExtreme (esm/__internal/scheduler/view_model/m_appointment_data_source.js) * Version: 25.2.7 * Build date: Tue May 05 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { Deferred } from "../../../core/utils/deferred"; const STORE_EVENTS = { updating: "updating", push: "push" }; export class AppointmentDataSource { constructor(dataSource) { this.setDataSource(dataSource); this.updatedAppointmentKeys = [] } get keyName() { const store = this.dataSource.store(); return store.key() } get isDataSourceInit() { return Boolean(this.dataSource) } getStoreKey(target) { const store = this.dataSource.store(); return store.keyOf(target) } setDataSource(dataSource) { this.dataSource = dataSource; this.cleanState(); this.initStoreChangeHandlers() } initStoreChangeHandlers() { const { dataSource: dataSource } = this; const store = null === dataSource || void 0 === dataSource ? void 0 : dataSource.store(); if (store) { store.on(STORE_EVENTS.updating, key => { const keyName = store.key(); if (keyName) { this.updatedAppointmentKeys.push({ key: keyName, value: key }) } else { this.updatedAppointment = key } }); store.on(STORE_EVENTS.push, pushItems => { const items = dataSource.items(); const keyName = store.key(); pushItems.forEach(pushItem => { const itemExists = 0 !== items.filter(item => item[keyName] === pushItem.key).length; if (itemExists) { this.updatedAppointmentKeys.push({ key: keyName, value: pushItem.key }) } else { const { data: data } = pushItem; data && items.push(data) } }); dataSource.load() }) } } getUpdatedAppointment() { return this.updatedAppointment } getUpdatedAppointmentKeys() { return this.updatedAppointmentKeys } cleanState() { this.updatedAppointment = null; this.updatedAppointmentKeys = [] } add(rawAppointment) { return this.dataSource.store().insert(rawAppointment).done(() => this.dataSource.load()) } update(target, data) { const key = this.getStoreKey(target); const d = new Deferred; this.dataSource.store().update(key, data).done(result => this.dataSource.load().done(() => d.resolve(result)).fail(d.reject)).fail(d.reject); return d.promise() } remove(rawAppointment) { const key = this.getStoreKey(rawAppointment); return this.dataSource.store().remove(key).done(() => this.dataSource.load()) } destroy() { var _this$dataSource; const store = null === (_this$dataSource = this.dataSource) || void 0 === _this$dataSource ? void 0 : _this$dataSource.store(); if (store) { store.off(STORE_EVENTS.updating); store.off(STORE_EVENTS.push) } } }