UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

238 lines (237 loc) • 7.58 kB
/** * DevExtreme (cjs/__internal/ui/diagram/diagram.items_option.js) * Version: 26.1.4 * Build date: Fri Jul 24 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _data = require("../../../common/data"); var _component = require("../../../core/component"); var _extend = require("../../../core/utils/extend"); const ItemsOptionBase = _component.Component.inherit({}).include(_data.DataHelperMixin); class ItemsOption extends ItemsOptionBase { constructor(diagramWidget) { super(); this._diagramWidget = diagramWidget; this._resetCache() } _dataSourceChangedHandler(newItems, e) { this._resetCache(); this._items = newItems.map(item => (0, _extend.extend)(true, {}, item)); this._dataSourceItems = newItems.slice(); if (null !== e && void 0 !== e && e.changes) { const internalChanges = e.changes.filter(change => change.internalChange); const externalChanges = e.changes.filter(change => !change.internalChange); if (internalChanges.length) { this._reloadContentByChanges(internalChanges, false) } if (externalChanges.length) { this._reloadContentByChanges(externalChanges, true) } } else { this._diagramWidget._onDataSourceChanged() } } _dataSourceLoadingChangedHandler(isLoading) { if (isLoading && !this._dataSource.isLoaded()) { this._diagramWidget._showLoadingIndicator() } else { this._diagramWidget._hideLoadingIndicator() } } _prepareData(dataObj) { for (const key in dataObj) { if (!Object.prototype.hasOwnProperty.call(dataObj, key)) { continue } if (void 0 === dataObj[key]) { dataObj[key] = null } } return dataObj } insert(data, callback, errorCallback) { this._resetCache(); const store = this._getStore(); store.insert(this._prepareData(data)).done((data, key) => { store.push([{ type: "insert", key: key, data: data, internalChange: true }]); if (callback) { callback(data) } this._resetCache() }).fail(error => { if (errorCallback) { errorCallback(error) } this._resetCache() }) } update(key, data, callback, errorCallback) { const store = this._getStore(); const storeKey = this._getStoreKey(store, key, data); store.update(storeKey, this._prepareData(data)).done((data, key) => { store.push([{ type: "update", key: key, data: data, internalChange: true }]); if (callback) { callback(key, data) } }).fail(error => { if (errorCallback) { errorCallback(error) } }) } remove(key, data, callback, errorCallback) { this._resetCache(); const store = this._getStore(); const storeKey = this._getStoreKey(store, key, data); store.remove(storeKey).done(key => { store.push([{ type: "remove", key: key, internalChange: true }]); if (callback) { callback(key) } this._resetCache() }).fail(error => { if (errorCallback) { errorCallback(error) } this._resetCache() }) } findItem(itemKey) { if (!this._items) { return null } return this._getItemByKey(itemKey) } getItems() { return this._items } hasItems() { return !!this._items } _reloadContentByChanges(changes, isExternalChanges) { changes = changes.map(change => (0, _extend.extend)(change, { internalKey: this._getInternalKey(change.key) })); this._diagramWidget._reloadContentByChanges(changes, isExternalChanges) } _getItemByKey(key) { this._ensureCache(); const cache = this._cache; const index = this._getIndexByKey(key); return cache.items[index] } _getIndexByKey(key) { this._ensureCache(); const cache = this._cache; if ("object" === typeof key) { for (let i = 0, { length: length } = cache.keys; i < length; i += 1) { if (cache.keys[i] === key) { return i } } } else { const keySet = cache.keySet || cache.keys.reduce((accumulator, key, index) => { accumulator[key] = index; return accumulator }, {}); if (!cache.keySet) { cache.keySet = keySet } return keySet[key] } return -1 } _ensureCache() { const cache = this._cache; if (!cache.keys) { cache.keys = []; cache.items = []; this._fillCache(cache, this._items) } } _fillCache(cache, items) { if (!(null !== items && void 0 !== items && items.length)) { return } const keyExpr = this._getKeyExpr(); if (keyExpr) { items.forEach(item => { cache.keys.push(keyExpr(item)); cache.items.push(item) }) } const itemsExpr = this._getItemsExpr(); if (itemsExpr) { items.forEach(item => this._fillCache(cache, itemsExpr(item))) } const containerChildrenExpr = this._getContainerChildrenExpr(); if (containerChildrenExpr) { items.forEach(item => this._fillCache(cache, containerChildrenExpr(item))) } } _getKeyExpr() { throw "Not Implemented" } _getItemsExpr() {} _getContainerChildrenExpr() {} _initDataSource() { var _this$_dataSource; super._initDataSource(); null === (_this$_dataSource = this._dataSource) || void 0 === _this$_dataSource || _this$_dataSource.paginate(false) } _dataSourceOptions() { return { paginate: false } } _getStore() { var _this$_dataSource2; return null === (_this$_dataSource2 = this._dataSource) || void 0 === _this$_dataSource2 ? void 0 : _this$_dataSource2.store() } _getStoreKey(store, internalKey, data) { let storeKey = store.keyOf(data); if (storeKey === data) { const keyExpr = this._getKeyExpr(); this._dataSourceItems.forEach(item => { if (keyExpr(item) === internalKey) { storeKey = item } }) } return storeKey } _getInternalKey(storeKey) { if ("object" === typeof storeKey) { const keyExpr = this._getKeyExpr(); return keyExpr(storeKey) } return storeKey } _resetCache() { this._cache = {} } } var _default = exports.default = ItemsOption;