UNPKG

d3plus-common

Version:

Common functions and methods used across D3plus modules.

51 lines (39 loc) 1.99 kB
import {select} from "d3-selection"; import {transition} from "d3-transition"; import {default as attrize} from "./attrize"; /** @function elem @desc Manages the enter/update/exit pattern for a single DOM element. @param {String} selector A D3 selector, which must include the tagname and a class and/or ID. @param {Object} params Additional parameters. @param {Boolean} [params.condition = true] Whether or not the element should be rendered (or removed). @param {Object} [params.enter = {}] A collection of key/value pairs that map to attributes to be given on enter. @param {Object} [params.exit = {}] A collection of key/value pairs that map to attributes to be given on exit. @param {D3Selection} [params.parent = d3.select("body")] The parent element for this new element to be appended to. @param {D3Transition} [params.transition = d3.transition().duration(0)] The transition to use when animated the different life cycle stages. @param {Object} [params.update = {}] A collection of key/value pairs that map to attributes to be given on update. */ export default function(selector, p) { // overrides default params p = Object.assign({}, { condition: true, enter: {}, exit: {}, parent: select("body"), transition: transition().duration(0), update: {} }, p); var className = (/\.([^#]+)/g).exec(selector), id = (/#([^\.]+)/g).exec(selector), tag = (/^([^.^#]+)/g).exec(selector)[1]; var elem = p.parent.selectAll(selector.includes(":") ? selector.split(":")[1] : selector) .data(p.condition ? [null] : []); var enter = elem.enter().append(tag).call(attrize, p.enter); if (id) { enter.attr("id", id[1]); } if (className) { enter.attr("class", className[1]); } elem.exit().transition(p.transition).call(attrize, p.exit).remove(); var update = enter.merge(elem); update.transition(p.transition).call(attrize, p.update); return update; } //# sourceMappingURL=elem.js.map