1 | import xs, {Stream, MemoryStream} from 'xstream';
|
2 | import {adapt} from '@cycle/run/lib/adapt';
|
3 | import {DevToolEnabledSource} from '@cycle/run';
|
4 | import {EventsFnOptions, DOMSource} from './DOMSource';
|
5 | import {fromEvent} from './fromEvent';
|
6 |
|
7 | export class BodyDOMSource {
|
8 | constructor(private _name: string) {}
|
9 |
|
10 | public select(selector: string): BodyDOMSource {
|
11 |
|
12 | return this;
|
13 | }
|
14 |
|
15 | public elements(): MemoryStream<Array<HTMLBodyElement>> {
|
16 | const out: DevToolEnabledSource &
|
17 | MemoryStream<Array<HTMLBodyElement>> = adapt(xs.of([document.body]));
|
18 | out._isCycleSource = this._name;
|
19 | return out;
|
20 | }
|
21 |
|
22 | public element(): MemoryStream<HTMLBodyElement> {
|
23 | const out: DevToolEnabledSource & MemoryStream<HTMLBodyElement> = adapt(
|
24 | xs.of(document.body)
|
25 | );
|
26 | out._isCycleSource = this._name;
|
27 | return out;
|
28 | }
|
29 |
|
30 | public events<K extends keyof HTMLBodyElementEventMap>(
|
31 | eventType: K,
|
32 | options?: EventsFnOptions,
|
33 | bubbles?: boolean
|
34 | ): Stream<HTMLBodyElementEventMap[K]>;
|
35 | public events(
|
36 | eventType: string,
|
37 | options: EventsFnOptions = {},
|
38 | bubbles?: boolean
|
39 | ): Stream<Event> {
|
40 | let stream: Stream<Event>;
|
41 |
|
42 | stream = fromEvent(
|
43 | document.body,
|
44 | eventType,
|
45 | options.useCapture,
|
46 | options.preventDefault
|
47 | );
|
48 |
|
49 | const out: DevToolEnabledSource & Stream<Event> = adapt(stream);
|
50 | out._isCycleSource = this._name;
|
51 | return out;
|
52 | }
|
53 | }
|