UNPKG

@cycle/dom

Version:

The standard DOM Driver for Cycle.js, based on Snabbdom

281 lines 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var thunk_1 = require("./thunk"); exports.thunk = thunk_1.thunk; var MainDOMSource_1 = require("./MainDOMSource"); exports.MainDOMSource = MainDOMSource_1.MainDOMSource; /** * A factory for the DOM driver function. * * Takes a `container` to define the target on the existing DOM which this * driver will operate on, and an `options` object as the second argument. The * input to this driver is a stream of virtual DOM objects, or in other words, * Snabbdom "VNode" objects. The output of this driver is a "DOMSource": a * collection of Observables queried with the methods `select()` and `events()`. * * **`DOMSource.select(selector)`** returns a new DOMSource with scope * restricted to the element(s) that matches the CSS `selector` given. To select * the page's `document`, use `.select('document')`. To select the container * element for this app, use `.select(':root')`. * * **`DOMSource.events(eventType, options)`** returns a stream of events of * `eventType` happening on the elements that match the current DOMSource. The * event object contains the `ownerTarget` property that behaves exactly like * `currentTarget`. The reason for this is that some browsers doesn't allow * `currentTarget` property to be mutated, hence a new property is created. The * returned stream is an *xstream* Stream if you use `@cycle/xstream-run` to run * your app with this driver, or it is an RxJS Observable if you use * `@cycle/rxjs-run`, and so forth. * * **options for DOMSource.events** * * The `options` parameter on `DOMSource.events(eventType, options)` is an * (optional) object with two optional fields: `useCapture` and * `preventDefault`. * * `useCapture` is by default `false`, except it is `true` for event types that * do not bubble. Read more here * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener * about the `useCapture` and its purpose. * * `preventDefault` is by default `false`, and indicates to the driver whether * `event.preventDefault()` should be invoked. This option can be configured in * three ways: * * - `{preventDefault: boolean}` to invoke preventDefault if `true`, and not * invoke otherwise. * - `{preventDefault: (ev: Event) => boolean}` for conditional invocation. * - `{preventDefault: NestedObject}` uses an object to be recursively compared * to the `Event` object. `preventDefault` is invoked when all properties on the * nested object match with the properties on the event object. * * Here are some examples: * ```typescript * // always prevent default * DOMSource.select('input').events('keydown', { * preventDefault: true * }) * * // prevent default only when `ENTER` is pressed * DOMSource.select('input').events('keydown', { * preventDefault: e => e.keyCode === 13 * }) * * // prevent defualt when `ENTER` is pressed AND target.value is 'HELLO' * DOMSource.select('input').events('keydown', { * preventDefault: { keyCode: 13, ownerTarget: { value: 'HELLO' } } * }); * ``` * * **`DOMSource.elements()`** returns a stream of arrays containing the DOM * elements that match the selectors in the DOMSource (e.g. from previous * `select(x)` calls). * * **`DOMSource.element()`** returns a stream of DOM elements. Notice that this * is the singular version of `.elements()`, so the stream will emit an element, * not an array. If there is no element that matches the selected DOMSource, * then the returned stream will not emit anything. * * @param {(String|HTMLElement)} container the DOM selector for the element * (or the element itself) to contain the rendering of the VTrees. * @param {DOMDriverOptions} options an object with two optional properties: * * - `modules: array` overrides `@cycle/dom`'s default Snabbdom modules as * as defined in [`src/modules.ts`](./src/modules.ts). * - `reportSnabbdomError: (err: any) => void` overrides the default error reporter function. * @return {Function} the DOM driver function. The function expects a stream of * VNode as input, and outputs the DOMSource object. * @function makeDOMDriver */ var makeDOMDriver_1 = require("./makeDOMDriver"); exports.makeDOMDriver = makeDOMDriver_1.makeDOMDriver; /** * A factory function to create mocked DOMSource objects, for testing purposes. * * Takes a `mockConfig` object as argument, and returns * a DOMSource that can be given to any Cycle.js app that expects a DOMSource in * the sources, for testing. * * The `mockConfig` parameter is an object specifying selectors, eventTypes and * their streams. Example: * * ```js * const domSource = mockDOMSource({ * '.foo': { * 'click': xs.of({target: {}}), * 'mouseover': xs.of({target: {}}), * }, * '.bar': { * 'scroll': xs.of({target: {}}), * elements: xs.of({tagName: 'div'}), * } * }); * * // Usage * const click$ = domSource.select('.foo').events('click'); * const element$ = domSource.select('.bar').elements(); * ``` * * The mocked DOM Source supports isolation. It has the functions `isolateSink` * and `isolateSource` attached to it, and performs simple isolation using * classNames. *isolateSink* with scope `foo` will append the class `___foo` to * the stream of virtual DOM nodes, and *isolateSource* with scope `foo` will * perform a conventional `mockedDOMSource.select('.__foo')` call. * * @param {Object} mockConfig an object where keys are selector strings * and values are objects. Those nested objects have `eventType` strings as keys * and values are streams you created. * @return {Object} fake DOM source object, with an API containing `select()` * and `events()` and `elements()` which can be used just like the DOM Driver's * DOMSource. * * @function mockDOMSource */ var mockDOMSource_1 = require("./mockDOMSource"); exports.mockDOMSource = mockDOMSource_1.mockDOMSource; exports.MockedDOMSource = mockDOMSource_1.MockedDOMSource; /** * The hyperscript function `h()` is a function to create virtual DOM objects, * also known as VNodes. Call * * ```js * h('div.myClass', {style: {color: 'red'}}, []) * ``` * * to create a VNode that represents a `DIV` element with className `myClass`, * styled with red color, and no children because the `[]` array was passed. The * API is `h(tagOrSelector, optionalData, optionalChildrenOrText)`. * * However, usually you should use "hyperscript helpers", which are shortcut * functions based on hyperscript. There is one hyperscript helper function for * each DOM tagName, such as `h1()`, `h2()`, `div()`, `span()`, `label()`, * `input()`. For instance, the previous example could have been written * as: * * ```js * div('.myClass', {style: {color: 'red'}}, []) * ``` * * There are also SVG helper functions, which apply the appropriate SVG * namespace to the resulting elements. `svg()` function creates the top-most * SVG element, and `svg.g`, `svg.polygon`, `svg.circle`, `svg.path` are for * SVG-specific child elements. Example: * * ```js * svg({attrs: {width: 150, height: 150}}, [ * svg.polygon({ * attrs: { * class: 'triangle', * points: '20 0 20 150 150 20' * } * }) * ]) * ``` * * @function h */ var snabbdom_1 = require("snabbdom"); exports.h = snabbdom_1.h; var hyperscript_helpers_1 = require("./hyperscript-helpers"); exports.svg = hyperscript_helpers_1.default.svg; exports.a = hyperscript_helpers_1.default.a; exports.abbr = hyperscript_helpers_1.default.abbr; exports.address = hyperscript_helpers_1.default.address; exports.area = hyperscript_helpers_1.default.area; exports.article = hyperscript_helpers_1.default.article; exports.aside = hyperscript_helpers_1.default.aside; exports.audio = hyperscript_helpers_1.default.audio; exports.b = hyperscript_helpers_1.default.b; exports.base = hyperscript_helpers_1.default.base; exports.bdi = hyperscript_helpers_1.default.bdi; exports.bdo = hyperscript_helpers_1.default.bdo; exports.blockquote = hyperscript_helpers_1.default.blockquote; exports.body = hyperscript_helpers_1.default.body; exports.br = hyperscript_helpers_1.default.br; exports.button = hyperscript_helpers_1.default.button; exports.canvas = hyperscript_helpers_1.default.canvas; exports.caption = hyperscript_helpers_1.default.caption; exports.cite = hyperscript_helpers_1.default.cite; exports.code = hyperscript_helpers_1.default.code; exports.col = hyperscript_helpers_1.default.col; exports.colgroup = hyperscript_helpers_1.default.colgroup; exports.dd = hyperscript_helpers_1.default.dd; exports.del = hyperscript_helpers_1.default.del; exports.dfn = hyperscript_helpers_1.default.dfn; exports.dir = hyperscript_helpers_1.default.dir; exports.div = hyperscript_helpers_1.default.div; exports.dl = hyperscript_helpers_1.default.dl; exports.dt = hyperscript_helpers_1.default.dt; exports.em = hyperscript_helpers_1.default.em; exports.embed = hyperscript_helpers_1.default.embed; exports.fieldset = hyperscript_helpers_1.default.fieldset; exports.figcaption = hyperscript_helpers_1.default.figcaption; exports.figure = hyperscript_helpers_1.default.figure; exports.footer = hyperscript_helpers_1.default.footer; exports.form = hyperscript_helpers_1.default.form; exports.h1 = hyperscript_helpers_1.default.h1; exports.h2 = hyperscript_helpers_1.default.h2; exports.h3 = hyperscript_helpers_1.default.h3; exports.h4 = hyperscript_helpers_1.default.h4; exports.h5 = hyperscript_helpers_1.default.h5; exports.h6 = hyperscript_helpers_1.default.h6; exports.head = hyperscript_helpers_1.default.head; exports.header = hyperscript_helpers_1.default.header; exports.hgroup = hyperscript_helpers_1.default.hgroup; exports.hr = hyperscript_helpers_1.default.hr; exports.html = hyperscript_helpers_1.default.html; exports.i = hyperscript_helpers_1.default.i; exports.iframe = hyperscript_helpers_1.default.iframe; exports.img = hyperscript_helpers_1.default.img; exports.input = hyperscript_helpers_1.default.input; exports.ins = hyperscript_helpers_1.default.ins; exports.kbd = hyperscript_helpers_1.default.kbd; exports.keygen = hyperscript_helpers_1.default.keygen; exports.label = hyperscript_helpers_1.default.label; exports.legend = hyperscript_helpers_1.default.legend; exports.li = hyperscript_helpers_1.default.li; exports.link = hyperscript_helpers_1.default.link; exports.main = hyperscript_helpers_1.default.main; exports.map = hyperscript_helpers_1.default.map; exports.mark = hyperscript_helpers_1.default.mark; exports.menu = hyperscript_helpers_1.default.menu; exports.meta = hyperscript_helpers_1.default.meta; exports.nav = hyperscript_helpers_1.default.nav; exports.noscript = hyperscript_helpers_1.default.noscript; exports.object = hyperscript_helpers_1.default.object; exports.ol = hyperscript_helpers_1.default.ol; exports.optgroup = hyperscript_helpers_1.default.optgroup; exports.option = hyperscript_helpers_1.default.option; exports.p = hyperscript_helpers_1.default.p; exports.param = hyperscript_helpers_1.default.param; exports.pre = hyperscript_helpers_1.default.pre; exports.progress = hyperscript_helpers_1.default.progress; exports.q = hyperscript_helpers_1.default.q; exports.rp = hyperscript_helpers_1.default.rp; exports.rt = hyperscript_helpers_1.default.rt; exports.ruby = hyperscript_helpers_1.default.ruby; exports.s = hyperscript_helpers_1.default.s; exports.samp = hyperscript_helpers_1.default.samp; exports.script = hyperscript_helpers_1.default.script; exports.section = hyperscript_helpers_1.default.section; exports.select = hyperscript_helpers_1.default.select; exports.small = hyperscript_helpers_1.default.small; exports.source = hyperscript_helpers_1.default.source; exports.span = hyperscript_helpers_1.default.span; exports.strong = hyperscript_helpers_1.default.strong; exports.style = hyperscript_helpers_1.default.style; exports.sub = hyperscript_helpers_1.default.sub; exports.sup = hyperscript_helpers_1.default.sup; exports.table = hyperscript_helpers_1.default.table; exports.tbody = hyperscript_helpers_1.default.tbody; exports.td = hyperscript_helpers_1.default.td; exports.textarea = hyperscript_helpers_1.default.textarea; exports.tfoot = hyperscript_helpers_1.default.tfoot; exports.th = hyperscript_helpers_1.default.th; exports.thead = hyperscript_helpers_1.default.thead; exports.title = hyperscript_helpers_1.default.title; exports.tr = hyperscript_helpers_1.default.tr; exports.u = hyperscript_helpers_1.default.u; exports.ul = hyperscript_helpers_1.default.ul; exports.video = hyperscript_helpers_1.default.video; //# sourceMappingURL=index.js.map