Skip to content

Commit a268bf5

Browse files
committed
refactor: Reorganized some controllers and utility functions
1 parent afeef23 commit a268bf5

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

src/components/tile-manager/controllers/fullscreen.ts renamed to src/components/common/controllers/fullscreen.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import type { ReactiveController, ReactiveControllerHost } from 'lit';
99
type FullscreenControllerCallback = (state: boolean) => boolean;
1010

1111
/** Configuration object for the fullscreen controller. */
12-
type FullscreenControllerConfig = {
12+
type FullscreenControllerConfiguration = {
1313
/**
1414
* Invoked when the host element is entering fullscreen mode.
1515
* See the {@link FullscreenControllerCallback} for details.
1616
*/
17-
onEnterFullscreen?: FullscreenControllerCallback;
17+
enter?: FullscreenControllerCallback;
1818
/**
1919
* Invoked when the host element is leaving fullscreen mode.
2020
* See the {@link FullscreenControllerCallback} for details.
2121
*/
22-
onExitFullscreen?: FullscreenControllerCallback;
22+
exit?: FullscreenControllerCallback;
2323
};
2424

2525
class FullscreenController implements ReactiveController {
2626
private _host: ReactiveControllerHost & HTMLElement;
27-
private _config: FullscreenControllerConfig = {};
27+
private _options: FullscreenControllerConfiguration = {};
2828

2929
private _fullscreen = false;
3030

@@ -34,21 +34,19 @@ class FullscreenController implements ReactiveController {
3434

3535
constructor(
3636
host: ReactiveControllerHost & HTMLElement,
37-
config?: FullscreenControllerConfig
37+
options?: FullscreenControllerConfiguration
3838
) {
3939
this._host = host;
40-
Object.assign(this._config, config);
40+
Object.assign(this._options, options);
4141
host.addController(this);
4242
}
4343

4444
/**
4545
* Transitions the host element to/from fullscreen mode.
46-
* This method **will invoke** onEnter/onExitFullscreen callbacks if present.
46+
* This method **will invoke** enter/exit callbacks if present.
4747
*/
4848
public setState(fullscreen: boolean): void {
49-
const callback = fullscreen
50-
? this._config.onEnterFullscreen
51-
: this._config.onExitFullscreen;
49+
const callback = fullscreen ? this._options.enter : this._options.exit;
5250

5351
if (callback && !callback.call(this._host, fullscreen)) {
5452
return;
@@ -70,18 +68,20 @@ class FullscreenController implements ReactiveController {
7068
}
7169
}
7270

71+
/** @internal */
7372
public hostConnected(): void {
7473
this._host.addEventListener('fullscreenchange', this);
7574
}
7675

76+
/** @internal */
7777
public hostDisconnected(): void {
7878
this._host.removeEventListener('fullscreenchange', this);
7979
}
8080
}
8181

8282
export function addFullscreenController(
8383
host: ReactiveControllerHost & HTMLElement,
84-
config?: FullscreenControllerConfig
85-
) {
86-
return new FullscreenController(host, config);
84+
options?: FullscreenControllerConfiguration
85+
): FullscreenController {
86+
return new FullscreenController(host, options);
8787
}

src/components/common/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,15 @@ export function partition<T>(
312312

313313
return [truthy, falsy];
314314
}
315+
316+
export function pick<T extends object>(entry: T, ...props: Array<keyof T>) {
317+
return Object.fromEntries(
318+
Object.entries(entry).filter(([key, _]) => props.includes(key as keyof T))
319+
);
320+
}
321+
322+
export function omit<T extends object>(entry: T, ...props: Array<keyof T>) {
323+
return Object.fromEntries(
324+
Object.entries(entry).filter(([key, _]) => !props.includes(key as keyof T))
325+
);
326+
}

src/components/tile-manager/serializer.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { omit, pick } from '../common/util.js';
12
import type IgcTileManagerComponent from './tile-manager.js';
23

34
export interface SerializedTile {
@@ -68,15 +69,3 @@ class TileManagerSerializer {
6869
export function createSerializer(host: IgcTileManagerComponent) {
6970
return new TileManagerSerializer(host);
7071
}
71-
72-
function pick<T extends object>(entry: T, ...props: Array<keyof T>) {
73-
return Object.fromEntries(
74-
Object.entries(entry).filter(([key, _]) => props.includes(key as keyof T))
75-
);
76-
}
77-
78-
function omit<T extends object>(entry: T, ...props: Array<keyof T>) {
79-
return Object.fromEntries(
80-
Object.entries(entry).filter(([key, _]) => !props.includes(key as keyof T))
81-
);
82-
}

src/components/tile-manager/tile-dnd.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ describe('Tile drag and drop', () => {
128128
});
129129
});
130130

131-
// REVIEW when the logic is implemented
132131
it('should cancel dragging with Escape', async () => {
133132
const draggedTile = getTile(0);
134133
const dropTarget = getTile(4);

src/components/tile-manager/tile-resize.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ describe('Tile resize', () => {
146146
expect(eventSpy).calledWith('igcResize');
147147
expect(state.ghost).to.equal(DOM.ghostElement);
148148
expect(state.initial).to.eql(tileRect);
149-
// FIXME: Investigate why the the delta is so great
150149
assertRectsAreEqual(
151150
state.current,
152151
DOM.ghostElement.getBoundingClientRect()

src/components/tile-manager/tile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../common/context.js';
1212
import { createAsyncContext } from '../common/controllers/async-consumer.js';
1313
import { addDragController } from '../common/controllers/drag.js';
14+
import { addFullscreenController } from '../common/controllers/fullscreen.js';
1415
import { registerComponent } from '../common/definitions/register.js';
1516
import type { Constructor } from '../common/mixins/constructor.js';
1617
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
@@ -23,7 +24,6 @@ import {
2324
import IgcDividerComponent from '../divider/divider.js';
2425
import IgcResizeContainerComponent from '../resize-container/resize-container.js';
2526
import type { ResizeCallbackParams } from '../resize-container/types.js';
26-
import { addFullscreenController } from './controllers/fullscreen.js';
2727
import { createTileDragStack, swapTiles } from './position.js';
2828
import { styles as shared } from './themes/shared/tile/tile.common.css.js';
2929
import { styles } from './themes/tile.base.css.js';
@@ -107,8 +107,8 @@ export default class IgcTileComponent extends EventEmitterMixin<
107107
});
108108

109109
private _fullscreenController = addFullscreenController(this, {
110-
onEnterFullscreen: this.emitFullScreenEvent,
111-
onExitFullscreen: this.emitFullScreenEvent,
110+
enter: this.emitFullScreenEvent,
111+
exit: this.emitFullScreenEvent,
112112
});
113113

114114
private _colSpan = 1;

0 commit comments

Comments
 (0)