UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

168 lines (167 loc) 7.93 kB
import { AdaptableModuleBase } from './AdaptableModuleBase'; import * as ModuleConstants from '../Utilities/Constants/ModuleConstants'; import { TEAMSHARING_GET, TEAMSHARING_IMPORT_ITEM, TEAMSHARING_LINK_ITEM, TEAMSHARING_PROCESS_IMPORT, TEAMSHARING_REMOVE_ITEM, TEAMSHARING_SET, TEAMSHARING_SHARE, TEAMSHARING_UPDATE_ITEM, TeamSharingUpdateItem, } from '../Redux/ActionsReducers/TeamSharingRedux'; import * as TeamSharingRedux from '../Redux/ActionsReducers/TeamSharingRedux'; import { isEqual } from '../Utilities/Extensions/ObjectExtensions'; import { LAYOUT_SAVE } from '../Redux/ActionsReducers/LayoutRedux'; import { getSharedEntityStaleDepsItemView, SharedEntityTypeItemView, getSharedEntityActiveStatusObjectView, } from '../View/TeamSharing/SharedEntityObjectView'; import { TeamSharingApplyButton } from '../View/TeamSharing/TeamSharingApplyButton'; import { SharedEntityDependencies } from '../View/TeamSharing/SharedEntityDependencies'; import ArrayExtensions from '../Utilities/Extensions/ArrayExtensions'; import { flatten } from '../Utilities/Extensions/ArrayExtensions'; import AdaptableHelper from '../Utilities/Helpers/AdaptableHelper'; export class TeamSharingModule extends AdaptableModuleBase { SKIP_TEAMSHARING_UPDATE_ACTIONS = [ TEAMSHARING_GET, TEAMSHARING_SET, TEAMSHARING_SHARE, TEAMSHARING_IMPORT_ITEM, TEAMSHARING_PROCESS_IMPORT, TEAMSHARING_REMOVE_ITEM, TEAMSHARING_LINK_ITEM, TEAMSHARING_UPDATE_ITEM, ]; constructor(api) { super(ModuleConstants.TeamSharingModuleId, ModuleConstants.TeamSharingFriendlyName, 'folder-shared', 'TeamSharingPopup', 'Team Sharing allows users to share - at run-time - Adaptable Objects between colleagues.', api); } onAdaptableReady() { if (this.api.teamSharingApi.isTeamSharingAvailable()) { this.api.internalApi.dispatchReduxAction(TeamSharingRedux.TeamSharingCommitImport()); this.api.teamSharingApi.refreshTeamSharing(); } this.api.eventApi.on('AdaptableStateChanged', (adaptableStateChangedInfo) => this.handleStateChanged(adaptableStateChangedInfo)); } isModuleEnabled() { const teamSharingOptions = this.api.optionsApi.getTeamSharingOptions(); return (teamSharingOptions?.enableTeamSharing && typeof teamSharingOptions?.loadSharedEntities === 'function' && typeof teamSharingOptions?.persistSharedEntities === 'function'); } isModuleObjectsShareable() { return false; } getPopupMaxWidth() { return 1000; } handleStateChanged(adaptableStateChangedInfo) { if (!this.api.teamSharingApi.isTeamSharingAvailable()) { return; } if (this.SKIP_TEAMSHARING_UPDATE_ACTIONS.includes(adaptableStateChangedInfo.action.type)) { return; } const changedAdaptableObject = this.extractAdaptableObjectFromAction(adaptableStateChangedInfo.action); if (!changedAdaptableObject) { return; } const activeSharedEntity = this.api.internalApi.getState().TeamSharing.ActiveSharedEntityMap[changedAdaptableObject.Uuid]; if (!activeSharedEntity) { return; } const isLayoutStateUnchanged = adaptableStateChangedInfo.action.type === LAYOUT_SAVE && isEqual(adaptableStateChangedInfo.oldState.Layout, adaptableStateChangedInfo.newState.Layout); if (isLayoutStateUnchanged) { return; } this.api.internalApi.dispatchReduxAction(TeamSharingUpdateItem(changedAdaptableObject, adaptableStateChangedInfo.userName)); } extractAdaptableObjectFromAction(action) { if (!action) { return; } return Object.values(action).find((actionPropertyValue) => AdaptableHelper.isAdaptableObject(actionPropertyValue)); } isAdaptableObjectPresentInLocalState(sharedEntity) { return !!this.api.internalApi .getModuleService() .getModuleById(sharedEntity.Module) ?.getModuleAdaptableObjects() .find((adaptableObject) => adaptableObject.Uuid === sharedEntity.Entity.Uuid); } isSharedEntityADependency(sharedEntity) { const allSharedEntries = this.api.teamSharingApi.getLoadedAdaptableSharedEntities(); return allSharedEntries.some((sharedEntryCandidate) => { return sharedEntryCandidate?.EntityDependencyIds?.includes(sharedEntity.Uuid); }); } isStaleAndActive(sharedEntity) { const staleActiveEntities = this.api.internalApi .getTeamSharingService() .getStaleActiveSharedEntities(); return (this.isAdaptableObjectPresentInLocalState(sharedEntity) && !!staleActiveEntities[sharedEntity.Uuid]); } getDependencies(sharedEntity) { if (!Array.isArray(sharedEntity.EntityDependencyIds) || sharedEntity.EntityDependencyIds.length === 0) { return [sharedEntity]; } const allSharedEntities = this.api.teamSharingApi.getLoadedAdaptableSharedEntities(); const dependencies = sharedEntity.EntityDependencyIds.map((dependencyUuid) => allSharedEntities.find((entity) => entity.Uuid === dependencyUuid)); return flatten(dependencies.map((dependency) => this.getDependencies(dependency))); } getStaleDependencies(sharedEntity) { return this.getDependencies(sharedEntity).filter((dependency) => dependency.Uuid !== sharedEntity.Uuid && this.isStaleAndActive(dependency)); } toView(sharedEntity) { const isDependency = this.isSharedEntityADependency(sharedEntity); const staleDependencies = this.getStaleDependencies(sharedEntity); const staleDependenciesViewItems = []; if (staleDependencies?.length) { staleDependenciesViewItems.push({ name: 'Stale Deps', view: getSharedEntityStaleDepsItemView(staleDependencies), }); } const sharedViewItems = []; const isStaleAndActive = this.isStaleAndActive(sharedEntity); if (!isDependency || isStaleAndActive) { sharedViewItems.push({ name: 'Shared', view: getSharedEntityActiveStatusObjectView(isDependency), }); } return { items: [ !isDependency && sharedEntity.Description && { name: 'Name', values: [sharedEntity.Description], }, !isDependency && { name: 'Share Mode', values: [sharedEntity.Type], }, ...sharedViewItems, { name: 'Type', view: SharedEntityTypeItemView, }, ...staleDependenciesViewItems, ArrayExtensions.IsNotNullOrEmpty(sharedEntity.EntityDependencyIds) && { name: 'Dependencies', isLabelInline: true, view: SharedEntityDependencies, }, ].filter(Boolean), abObject: sharedEntity, }; } toViewAll() { return ((this.api.teamSharingApi.getLoadedAdaptableSharedEntities() ?? []) .filter((sharedEntity) => !this.isSharedEntityADependency(sharedEntity)) .map((item) => this.toView(item))); } getViewProperties() { return { actions: [TeamSharingApplyButton], onMount: () => { this.api.teamSharingApi.refreshTeamSharing(); }, getDeleteAction: (sharedEntity) => { return TeamSharingRedux.TeamSharingRemoveItem(sharedEntity.Uuid); }, emptyView: 'Shared Items will appear here when available.', }; } }