2023-04-04 15:43:43 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
function clearTextSelection() {
|
|
|
|
if (window.getSelection) {
|
2023-04-14 11:19:40 +03:00
|
|
|
if (window.getSelection().empty) {
|
2023-04-04 15:43:43 +03:00
|
|
|
window.getSelection().empty();
|
2023-04-14 11:19:40 +03:00
|
|
|
} else if (window.getSelection().removeAllRanges) {
|
2023-04-04 15:43:43 +03:00
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ItemComponent extends Component {
|
|
|
|
@service dropdown;
|
|
|
|
|
|
|
|
get selectionList() {
|
|
|
|
return this.args.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
get id() {
|
|
|
|
return this.args.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isSelected() {
|
|
|
|
return this.selectionList.isSelected(this.id);
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:19:40 +03:00
|
|
|
/**
|
|
|
|
* We use the mouse down event because it allows us to cancel any text selection using preventDefault
|
|
|
|
*/
|
2023-04-04 15:43:43 +03:00
|
|
|
@action
|
2023-04-14 11:19:40 +03:00
|
|
|
onMouseDown(event) {
|
2023-04-11 17:32:11 +03:00
|
|
|
if (!this.selectionList.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-03 17:59:57 +03:00
|
|
|
|
|
|
|
// If event target has data-ignore-select or one of its partens, then ignore the event
|
|
|
|
if (event.target.closest('[data-ignore-select]')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:43:43 +03:00
|
|
|
const shiftKey = event.shiftKey;
|
|
|
|
const ctrlKey = event.ctrlKey || event.metaKey;
|
|
|
|
|
|
|
|
if (ctrlKey) {
|
|
|
|
this.selectionList.toggleItem(this.id);
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
clearTextSelection();
|
|
|
|
} else if (shiftKey) {
|
|
|
|
try {
|
|
|
|
this.selectionList.shiftItem(this.id);
|
|
|
|
} catch (e) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
clearTextSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:19:40 +03:00
|
|
|
@action
|
|
|
|
onClick(event) {
|
|
|
|
if (!this.selectionList.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-03 17:59:57 +03:00
|
|
|
|
|
|
|
// If event target has data-ignore-select or one of its partens, then ignore the event
|
|
|
|
if (event.target.closest('[data-ignore-select]')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-14 11:19:40 +03:00
|
|
|
const shiftKey = event.shiftKey;
|
|
|
|
const ctrlKey = event.ctrlKey || event.metaKey;
|
|
|
|
|
|
|
|
if (!ctrlKey && !shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
clearTextSelection();
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:43:43 +03:00
|
|
|
@action
|
|
|
|
onContextMenu(event) {
|
2023-04-11 17:32:11 +03:00
|
|
|
if (!this.selectionList.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-03 17:59:57 +03:00
|
|
|
|
|
|
|
// If event target has data-ignore-select or one of its partens, then ignore the event
|
|
|
|
if (event.target.closest('[data-ignore-select]')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:43:43 +03:00
|
|
|
let x = event.clientX;
|
|
|
|
let y = event.clientY;
|
|
|
|
|
|
|
|
if (this.isSelected) {
|
|
|
|
this.dropdown.toggleDropdown('context-menu', this, {left: x, top: y, selectionList: this.selectionList});
|
|
|
|
} else {
|
2023-04-14 11:19:40 +03:00
|
|
|
this.selectionList.clearSelection();
|
|
|
|
this.selectionList.toggleItem(this.id);
|
2023-04-14 12:12:30 +03:00
|
|
|
this.selectionList.clearOnNextUnfreeze();
|
2023-04-14 11:19:40 +03:00
|
|
|
this.dropdown.toggleDropdown('context-menu', this, {left: x, top: y, selectionList: this.selectionList});
|
2023-04-04 15:43:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|