@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
138 lines (137 loc) • 5.41 kB
JavaScript
import * as InternalRedux from '../../Redux/ActionsReducers/InternalRedux';
import ArrayExtensions from '../Extensions/ArrayExtensions';
export class AnnotationsService {
api;
adaptable;
isListeningToEvents;
constructor(api) {
this.api = api;
this.isListeningToEvents = false;
this.adaptable = api.internalApi.getAdaptableInstance();
this.adaptable.api.eventApi.on('AdaptableReady', () => this.checkListenToEvents());
}
destroy() { }
getAdaptableState() {
return this.api.internalApi.getAdaptableState();
}
dispatchAction(action) {
this.api.internalApi.dispatchReduxAction(action);
}
static isSameAddress(a, b) {
if (!a || !b) {
return false;
}
if (a?.ColumnId === b?.ColumnId && a?.PrimaryKeyValue === b?.PrimaryKeyValue) {
return true;
}
if ((typeof a.PrimaryKeyValue === 'number' && typeof b.PrimaryKeyValue === 'string') ||
(typeof b.PrimaryKeyValue === 'string' && typeof a.PrimaryKeyValue === 'number')) {
return (a.PrimaryKeyValue.toString() === b.PrimaryKeyValue.toString() && a.ColumnId === b.ColumnId);
}
return false;
}
checkListenToEvents() {
if (!this.isListeningToEvents && this.shouldListenToEvents()) {
this.setUpEventListeners();
this.isListeningToEvents = true;
}
}
shouldListenToEvents() {
return (ArrayExtensions.IsNotNullOrEmpty(this.api.noteApi.getAllNotes()) ||
this.api.internalApi.getModuleService().isAdapTableModulePresent('Comment'));
}
setUpEventListeners() {
this.adaptable._on('MouseEnter', (event) => this.handleMouseEnter(event));
this.api.eventApi.on('CellSelectionChanged', (params) => {
const cells = params.selectedCellInfo.gridCells;
if (ArrayExtensions.hasOneItem(cells)) {
const selectedCell = cells[0];
if (selectedCell.column?.columnId && selectedCell.primaryKeyValue) {
const selectedCellAddress = {
PrimaryKeyValue: selectedCell.primaryKeyValue,
ColumnId: selectedCell.column.columnId,
};
this.handleCellSelected(selectedCellAddress);
}
}
});
}
getCellPositionFromEvent(event) {
const target = event.target;
if (!target?.classList?.contains?.('ag-cell')) {
return null;
}
try {
const PrimaryKeyValue = target.parentElement.getAttribute('row-id');
return {
PrimaryKeyValue,
ColumnId: target.getAttribute('col-id'),
};
}
catch (e) {
return null;
}
}
handleMouseEnter(event) {
const editMode = this.getEditMode();
if (editMode) {
return;
}
const cellPosition = this.getCellPositionFromEvent(event);
const openCellAddress = this.getOpenCellAddress();
if (!cellPosition && openCellAddress) {
this.hidePopup();
return;
}
if (AnnotationsService.isSameAddress(openCellAddress, cellPosition)) {
return;
}
const cellNote = this.api.noteApi.getNoteForCell(cellPosition);
const cellComments = this.api.commentApi.getCommentThreadForCell(cellPosition);
const openNoteMode = this.api.optionsApi.getNoteOptions()?.showNoteAction ?? 'hover';
const showCommentMode = this.api.optionsApi.getCommentOptions()?.showCommentAction ?? 'hover';
const shouldOpenForNote = !!cellNote && openNoteMode === 'hover';
const shouldOpenForComment = !!cellComments && showCommentMode === 'hover';
if (!shouldOpenForNote && !shouldOpenForComment) {
return;
}
this.showPopup(cellPosition, false);
}
handleCellSelected(cellAddress) {
const openCellAddress = this.getOpenCellAddress();
if (AnnotationsService.isSameAddress(openCellAddress, cellAddress)) {
return;
}
const cellNote = this.api.noteApi.getNoteForCell(cellAddress);
const cellComments = this.api.commentApi.getCommentThreadForCell(cellAddress);
const hasNotesOrComments = cellNote || cellComments;
if (openCellAddress && !hasNotesOrComments) {
if (this.getEditMode()) {
this.hidePopup();
}
return;
}
if (openCellAddress && hasNotesOrComments) {
this.showPopup(cellAddress, true);
return;
}
}
getOpenCellAddress() {
return InternalRedux.CommentsAndNotesSelector(this.getAdaptableState().Internal);
}
getEditMode() {
return InternalRedux.CommentsAndNotesEditModeSelector(this.getAdaptableState().Internal);
}
hidePopup() {
this.dispatchAction(InternalRedux.CellPopupHide());
}
showPopup(cellAddress, editMode) {
this.dispatchAction(InternalRedux.CellPopupShow(cellAddress, editMode));
}
editFocusedEntity(entity) {
this.dispatchAction(InternalRedux.CellPopupEditFocusedEntity(entity));
}
getNotePopupEditMode() {
return InternalRedux.CommentsAndNotesEditModeSelector(this.getAdaptableState().Internal);
}
}