mobility-toolbox-js
Version:
Toolbox for JavaScript applications in the domains of mobility and logistics.
208 lines (207 loc) • 8.09 kB
JavaScript
import debounce from 'lodash.debounce';
let deprecated = () => { };
if (typeof window !== 'undefined' &&
new URLSearchParams(window.location.search).get('deprecated')) {
deprecated = debounce((message) => {
// eslint-disable-next-line no-console
console.warn(message);
}, 1000);
}
const onChildrenChange = (obj, oldValue) => {
// Set the parent property
(oldValue || []).forEach((child) => {
child.set('parent', undefined);
});
(obj.get('children') || []).forEach((child) => {
child.set('parent', obj);
});
};
/**
* obj function defines properties taht were used in mbt v2.
* They are all marked als deprecated.
* @param obj
* @returns
*/
const defineDeprecatedProperties = (obj, options) => {
if (options.properties) {
deprecated("Deprecated. Don't use properties options. Pass the values directly in options object.");
obj.setProperties(options.properties);
}
// Update parent property
obj.on('propertychange', (evt) => {
if (evt.key === 'children') {
onChildrenChange(evt.target, evt.oldValue);
}
if (evt.key === 'map') {
const map = evt.target.get(evt.key);
if (map) {
(evt.target.get('children') || []).forEach((child) => {
map.addLayer(child);
});
}
else if (evt.oldValue) {
(evt.target.get('children') || []).forEach((child) => {
var _a;
(_a = evt.oldValue) === null || _a === void 0 ? void 0 : _a.removeLayer(child);
});
}
}
});
// Save options for cloning
obj.set('options', options);
// Force triggering the on children property change event
obj.set('children', [...(options.children || [])]);
Object.defineProperties(obj, {
children: {
/** @deprecated */
get: () => {
deprecated("Layer.children is deprecated. Use the Layer.get('children') method instead.");
return obj.get('children') || [];
},
/** @deprecated */
set: (newValue) => {
deprecated("Layer.children is deprecated. Use the Layer.set('children', children) method instead.");
obj.set('children', newValue || []);
},
},
copyrights: {
/** @deprecated */
get: () => {
deprecated('Layer.copyrights is deprecated. Get the attributions from the source object');
return obj.get('copyrights');
},
/** @deprecated */
set: (newCopyrights) => {
deprecated('Layer.copyrights is deprecated. Set the attributions to the source object.');
const arrValue = newCopyrights && !Array.isArray(newCopyrights)
? [newCopyrights]
: newCopyrights;
obj.set('copyrights', arrValue || []);
},
},
disabled: {
/** @deprecated */
get() {
deprecated("Layer.disabled is deprecated. Use the Layer.get('disabled') method instead.");
return obj.get('disabled');
},
/** @deprecated */
set(newValue) {
deprecated("Layer.disabled is deprecated. Use the Layer.set('disabled', newValue) method instead.");
obj.set('disabled', newValue);
},
},
group: {
/** @deprecated */
get() {
deprecated("Layer.group is deprecated. Use the Layer.get('group') method instead.");
return obj.get('group');
},
},
hitTolerance: {
/** @deprecated */
get() {
deprecated('Layer.hitTolerance is deprecated. Pass the hitTolerance when you request the features.');
return obj.get('hitTolerance') || 5;
},
/** @deprecated */
set(newValue) {
deprecated('Layer.hitTolerance is deprecated. Pass the hitTolerance when you request the features.');
obj.set('hitTolerance', newValue);
},
},
key: {
/** @deprecated */
get() {
deprecated('Layer.key is deprecated. Use the Layer.get("key") method instead.');
return obj.get('key') || obj.get('name');
},
},
map: {
/** @deprecated */
get() {
deprecated('Layer.map is deprecated. Use the Layer.get("map") method instead.');
return obj.getMapInternal();
},
},
name: {
/** @deprecated */
get() {
deprecated("Layer.name is deprecated. Use the Layer.get('name') method instead.");
return obj.get('name');
},
},
olLayer: {
/** @deprecated */
get() {
deprecated("Layer.olLayer is deprecated. mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. obj getter is only a redirect to the current 'this' object.");
return obj;
},
/** @deprecated */
set() {
deprecated('Layer.olLayer is deprecated. mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. obj setter has no effect.');
},
},
olListenersKeys: {
/** @deprecated */
get() {
deprecated('Layer.olListenersKeys is deprecated. Use the Layer.olEventsKeys instead.');
//@ts-expect-error Property just there for backward compatibility
return obj.olEventsKeys || [];
},
set(newValue) {
deprecated('Layer.olListenersKeys is deprecated. Use the Layer.olEventsKeys instead.');
//@ts-expect-error Property just there for backward compatibility
obj.olEventsKeys = newValue;
},
},
options: {
/** @deprecated */
get() {
deprecated('Layer.options is deprecated. Use the Layer.get("options") method instead.');
return obj.get('options');
},
set(newValue) {
deprecated('Layer.options is deprecated. Use the Layer.set("options", newValue) method instead.');
return obj.set('options', newValue);
},
},
parent: {
/** @deprecated */
get() {
deprecated("Layer.parent is deprecated. Use the Layer.get('parent') method instead.");
return obj.get('parent');
},
/** @deprecated */
set(newValue) {
deprecated("Layer.parent is deprecated. Use the Layer.set('parent', parent) method instead.");
obj.set('parent', newValue);
},
},
properties: {
/** @deprecated */
get() {
deprecated('Layer.properties is deprecated. Use the Layer.getProperties() method instead.');
return obj.getProperties();
},
/** @deprecated */
set(newValue) {
deprecated('Layer.properties is deprecated. Use the Layer.setProperties(newValue) method instead.');
obj.setProperties(newValue);
},
},
visible: {
/** @deprecated */
get() {
deprecated('Layer.visible is deprecated. Use the Layer.getVisible() method instead.');
return obj.getVisible();
},
/** @deprecated */
set(newValue) {
deprecated('Layer.visible is deprecated. Use the Layer.setVisible(newValue) method instead.');
obj.setVisible(newValue);
},
},
});
};
export default defineDeprecatedProperties;