2021-12-02 01:41:56 +01:00
|
|
|
/*
|
2021-12-25 23:39:34 +01:00
|
|
|
* Copyright [2021] [wisemapping]
|
2021-12-02 01:41:56 +01:00
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2022-07-09 03:34:52 +02:00
|
|
|
import { $assert, $defined } from '@wisemapping/core-js';
|
|
|
|
import { Group, ElementClass, Point } from '@wisemapping/web2d';
|
2021-12-13 22:30:37 +01:00
|
|
|
import IconGroupRemoveTip from './IconGroupRemoveTip';
|
2022-10-31 06:17:01 +01:00
|
|
|
import ImageIcon from './ImageIcon';
|
2022-02-23 04:12:04 +01:00
|
|
|
import SizeType from './SizeType';
|
|
|
|
import FeatureModel from './model/FeatureModel';
|
2023-01-03 02:43:16 +01:00
|
|
|
import Icon from './Icon';
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
const ORDER_BY_TYPE = new Map<string, number>();
|
2022-01-02 23:16:18 +01:00
|
|
|
ORDER_BY_TYPE.set('icon', 0);
|
|
|
|
ORDER_BY_TYPE.set('note', 1);
|
|
|
|
ORDER_BY_TYPE.set('link', 2);
|
2021-12-27 20:42:32 +01:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
class IconGroup {
|
2023-01-03 02:43:16 +01:00
|
|
|
private _icons: Icon[];
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2022-12-10 20:15:36 +01:00
|
|
|
private _group: Group;
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
private _removeTip: IconGroupRemoveTip;
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2023-01-14 07:33:17 +01:00
|
|
|
private _iconSize: SizeType | null;
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
private _topicId: number;
|
2022-03-05 17:10:03 +01:00
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
constructor(topicId: number, iconSize: number) {
|
|
|
|
this._topicId = topicId;
|
2021-12-13 22:30:37 +01:00
|
|
|
this._icons = [];
|
2021-12-19 17:31:29 +01:00
|
|
|
this._group = new Group({
|
2021-12-13 22:30:37 +01:00
|
|
|
width: 0,
|
|
|
|
height: iconSize,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
coordSizeWidth: 0,
|
|
|
|
coordSizeHeight: 100,
|
|
|
|
});
|
2022-02-23 04:12:04 +01:00
|
|
|
this._removeTip = new IconGroupRemoveTip(this._group);
|
2021-12-13 22:30:37 +01:00
|
|
|
this.seIconSize(iconSize, iconSize);
|
|
|
|
this._registerListeners();
|
2023-01-14 07:33:17 +01:00
|
|
|
this._iconSize = null;
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
setPosition(x: number, y: number): void {
|
2021-12-13 22:30:37 +01:00
|
|
|
this._group.setPosition(x, y);
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
getPosition(): Point {
|
2021-12-13 22:30:37 +01:00
|
|
|
return this._group.getPosition();
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
getNativeElement(): ElementClass {
|
2021-12-13 22:30:37 +01:00
|
|
|
return this._group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** */
|
2022-02-23 04:12:04 +01:00
|
|
|
getSize(): SizeType {
|
2021-12-13 22:30:37 +01:00
|
|
|
return this._group.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** */
|
2022-02-23 04:12:04 +01:00
|
|
|
seIconSize(width: number, height: number) {
|
2021-12-27 20:42:32 +01:00
|
|
|
this._iconSize = {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
};
|
2021-12-13 22:30:37 +01:00
|
|
|
this._resize(this._icons.length);
|
|
|
|
}
|
|
|
|
|
2023-01-03 02:43:16 +01:00
|
|
|
addIcon(icon: Icon, remove: boolean): void {
|
2021-12-13 22:30:37 +01:00
|
|
|
$defined(icon, 'icon is not defined');
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2021-12-27 20:42:32 +01:00
|
|
|
// Order could have change, need to re-add all.
|
|
|
|
const icons = this._icons.slice();
|
|
|
|
this._icons.forEach((i) => {
|
|
|
|
this._removeIcon(i);
|
|
|
|
});
|
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
icon.setGroup(this);
|
2021-12-27 20:42:32 +01:00
|
|
|
icons.push(icon);
|
2022-07-09 03:34:52 +02:00
|
|
|
this._icons = icons.sort(
|
|
|
|
(a, b) =>
|
2022-12-10 07:39:53 +01:00
|
|
|
ORDER_BY_TYPE.get(a.getModel().getType())! - ORDER_BY_TYPE.get(b.getModel().getType())!,
|
2022-07-09 03:34:52 +02:00
|
|
|
);
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2021-12-27 20:42:32 +01:00
|
|
|
// Add all the nodes back ...
|
2021-12-13 22:30:37 +01:00
|
|
|
this._resize(this._icons.length);
|
2021-12-27 20:42:32 +01:00
|
|
|
this._icons.forEach((i, index) => {
|
|
|
|
this._positionIcon(i, index);
|
2022-10-31 06:17:01 +01:00
|
|
|
const imageShape = i.getElement();
|
2021-12-27 20:42:32 +01:00
|
|
|
this._group.append(imageShape);
|
|
|
|
});
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
// Register event for the group ..
|
|
|
|
if (remove) {
|
|
|
|
this._removeTip.decorate(this._topicId, icon);
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:14:15 +01:00
|
|
|
|
2023-01-03 02:43:16 +01:00
|
|
|
private _findIconFromModel(iconModel: FeatureModel): Icon {
|
|
|
|
let result: Icon | null = null;
|
2021-12-05 17:14:15 +01:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
this._icons.forEach((icon) => {
|
|
|
|
const elModel = icon.getModel();
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
if (elModel.getId() === iconModel.getId()) {
|
|
|
|
result = icon;
|
2021-12-02 01:41:56 +01:00
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result == null) {
|
2021-12-14 18:06:09 +01:00
|
|
|
throw new Error(`Icon can no be found:${iconModel.getId()} Icons:${this._icons}`);
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** */
|
2022-02-23 04:12:04 +01:00
|
|
|
removeIconByModel(featureModel: FeatureModel) {
|
2021-12-13 22:30:37 +01:00
|
|
|
$assert(featureModel, 'featureModel can not be null');
|
|
|
|
|
|
|
|
const icon = this._findIconFromModel(featureModel);
|
|
|
|
this._removeIcon(icon);
|
|
|
|
}
|
|
|
|
|
2023-01-03 02:43:16 +01:00
|
|
|
private _removeIcon(icon: Icon) {
|
2021-12-13 22:30:37 +01:00
|
|
|
this._removeTip.close(0);
|
2022-10-31 06:17:01 +01:00
|
|
|
this._group.removeChild(icon.getElement());
|
2021-12-13 22:30:37 +01:00
|
|
|
|
2021-12-14 18:06:09 +01:00
|
|
|
this._icons = this._icons.filter((i) => i !== icon);
|
2021-12-13 22:30:37 +01:00
|
|
|
this._resize(this._icons.length);
|
|
|
|
const me = this;
|
|
|
|
|
|
|
|
// Add all again ...
|
|
|
|
this._icons.forEach((elem, i) => {
|
|
|
|
me._positionIcon(elem, i);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** */
|
2022-02-23 04:12:04 +01:00
|
|
|
moveToFront(): void {
|
2021-12-13 22:30:37 +01:00
|
|
|
this._group.moveToFront();
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
private _registerListeners() {
|
2021-12-13 22:30:37 +01:00
|
|
|
this._group.addEvent('click', (event) => {
|
|
|
|
// Avoid node creation ...
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._group.addEvent('dblclick', (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:04 +01:00
|
|
|
private _resize(iconsLength: number) {
|
2023-01-14 07:33:17 +01:00
|
|
|
if (this._iconSize) {
|
|
|
|
this._group.setSize(iconsLength * this._iconSize.width, this._iconSize.height);
|
2021-12-13 22:30:37 +01:00
|
|
|
|
2023-01-14 07:33:17 +01:00
|
|
|
const iconSize = ImageIcon.SIZE + IconGroup.ICON_PADDING * 2;
|
|
|
|
this._group.setCoordSize(iconsLength * iconSize, iconSize);
|
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-03 02:43:16 +01:00
|
|
|
private _positionIcon(icon: Icon, order: number) {
|
2022-10-31 06:17:01 +01:00
|
|
|
const iconSize = ImageIcon.SIZE + IconGroup.ICON_PADDING * 2;
|
|
|
|
icon
|
|
|
|
.getElement()
|
|
|
|
.setPosition(iconSize * order + IconGroup.ICON_PADDING, IconGroup.ICON_PADDING);
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
2022-02-23 04:12:04 +01:00
|
|
|
|
|
|
|
static ICON_PADDING = 5;
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
2021-12-02 01:41:56 +01:00
|
|
|
|
2022-01-02 23:16:18 +01:00
|
|
|
export default IconGroup;
|