selection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Selection = exports.select = void 0;
  4. const g_1 = require("@antv/g");
  5. const d3_array_1 = require("d3-array");
  6. const helper_1 = require("./helper");
  7. function select(node) {
  8. return new Selection([node], null, node, node.ownerDocument);
  9. }
  10. exports.select = select;
  11. /**
  12. * A simple implementation of d3-selection for @antv/g.
  13. * It has the core features of d3-selection and extended ability.
  14. * Every methods of selection returns new selection if elements
  15. * are mutated(e.g. append, remove), otherwise return the selection itself(e.g. attr, style).
  16. * @see https://github.com/d3/d3-selection
  17. * @see https://github.com/antvis/g
  18. * @todo Nested selections.
  19. * @todo More useful functor.
  20. */
  21. class Selection {
  22. constructor(elements = null, data = null, parent = null, document = null, selections = [
  23. null,
  24. null,
  25. null,
  26. null,
  27. null,
  28. ], transitions = [], updateElements = []) {
  29. this._elements = Array.from(elements);
  30. this._data = data;
  31. this._parent = parent;
  32. this._document = document;
  33. this._enter = selections[0];
  34. this._update = selections[1];
  35. this._exit = selections[2];
  36. this._merge = selections[3];
  37. this._split = selections[4];
  38. this._transitions = transitions;
  39. this._facetElements = updateElements;
  40. }
  41. selectAll(selector) {
  42. const elements = typeof selector === 'string'
  43. ? this._parent.querySelectorAll(selector)
  44. : selector;
  45. return new Selection(elements, null, this._elements[0], this._document);
  46. }
  47. selectFacetAll(selector) {
  48. const elements = typeof selector === 'string'
  49. ? this._parent.querySelectorAll(selector)
  50. : selector;
  51. return new Selection(this._elements, null, this._parent, this._document, undefined, undefined, elements);
  52. }
  53. /**
  54. * @todo Replace with querySelector which has bug now.
  55. */
  56. select(selector) {
  57. const element = typeof selector === 'string'
  58. ? this._parent.querySelectorAll(selector)[0] || null
  59. : selector;
  60. return new Selection([element], null, element, this._document);
  61. }
  62. append(node) {
  63. const callback = typeof node === 'function' ? node : () => this.createElement(node);
  64. const elements = [];
  65. if (this._data !== null) {
  66. // For empty selection, append new element to parent.
  67. // Each element is bind with datum.
  68. for (let i = 0; i < this._data.length; i++) {
  69. const d = this._data[i];
  70. const [datum, from] = Array.isArray(d) ? d : [d, null];
  71. const newElement = callback(datum, i);
  72. newElement.__data__ = datum;
  73. if (from !== null)
  74. newElement.__fromElements__ = from;
  75. this._parent.appendChild(newElement);
  76. elements.push(newElement);
  77. }
  78. return new Selection(elements, null, this._parent, this._document);
  79. }
  80. else {
  81. // For non-empty selection, append new element to
  82. // selected element and return new selection.
  83. for (let i = 0; i < this._elements.length; i++) {
  84. const element = this._elements[i];
  85. const datum = element.__data__;
  86. const newElement = callback(datum, i);
  87. element.appendChild(newElement);
  88. elements.push(newElement);
  89. }
  90. return new Selection(elements, null, elements[0], this._document);
  91. }
  92. }
  93. maybeAppend(id, node) {
  94. const element = this._elements[0];
  95. const child = element.getElementById(id);
  96. if (child) {
  97. return new Selection([child], null, this._parent, this._document);
  98. }
  99. const newChild = typeof node === 'string' ? this.createElement(node) : node();
  100. newChild.id = id;
  101. element.appendChild(newChild);
  102. return new Selection([newChild], null, this._parent, this._document);
  103. }
  104. /**
  105. * Bind data to elements, and produce three selection:
  106. * Enter: Selection with empty elements and data to be bind to elements.
  107. * Update: Selection with elements to be updated.
  108. * Exit: Selection with elements to be removed.
  109. */
  110. data(data, id = (d) => d, groupId = () => null) {
  111. // An Array of new data.
  112. const enter = [];
  113. // An Array of elements to be updated.
  114. const update = [];
  115. // A Set of elements to be removed.
  116. const exit = new Set(this._elements);
  117. // An Array of data to be merged into one element.
  118. const merge = [];
  119. // A Set of elements to be split into multiple datum.
  120. const split = new Set();
  121. // A Map from key to each element.
  122. const keyElement = new Map(this._elements.map((d, i) => [id(d.__data__, i), d]));
  123. // A Map from key to exist element. The Update Selection
  124. // can get element from this map, this is for diff among
  125. // facets.
  126. const keyUpdateElement = new Map(this._facetElements.map((d, i) => [id(d.__data__, i), d]));
  127. // A Map from groupKey to a group of elements.
  128. const groupKeyElements = (0, d3_array_1.group)(this._elements, (d) => groupId(d.__data__));
  129. // Diff data with selection(elements with data).
  130. // !!! Note
  131. // The switch is strictly ordered, not not change the order of them.
  132. for (let i = 0; i < data.length; i++) {
  133. const datum = data[i];
  134. const key = id(datum, i);
  135. const groupKey = groupId(datum, i);
  136. // Append element to update selection if incoming data has
  137. // exactly the same key with elements.
  138. if (keyElement.has(key)) {
  139. const element = keyElement.get(key);
  140. element.__data__ = datum;
  141. element.__facet__ = false;
  142. update.push(element);
  143. exit.delete(element);
  144. keyElement.delete(key);
  145. // Append element to update selection if incoming data has
  146. // exactly the same key with updateElements.
  147. }
  148. else if (keyUpdateElement.has(key)) {
  149. const element = keyUpdateElement.get(key);
  150. element.__data__ = datum;
  151. // Flag this element should update its parentNode.
  152. element.__facet__ = true;
  153. update.push(element);
  154. keyUpdateElement.delete(key);
  155. // Append datum to merge selection if existed elements has
  156. // its key as groupKey.
  157. }
  158. else if (groupKeyElements.has(key)) {
  159. const group = groupKeyElements.get(key);
  160. merge.push([datum, group]);
  161. for (const element of group)
  162. exit.delete(element);
  163. groupKeyElements.delete(key);
  164. // Append element to split selection if incoming data has
  165. // groupKey as its key, and bind to datum for it.
  166. }
  167. else if (keyElement.has(groupKey)) {
  168. const element = keyElement.get(groupKey);
  169. if (element.__toData__)
  170. element.__toData__.push(datum);
  171. else
  172. element.__toData__ = [datum];
  173. split.add(element);
  174. exit.delete(element);
  175. }
  176. else {
  177. // @todo Data with non-unique key.
  178. enter.push(datum);
  179. }
  180. }
  181. // Create new selection with enter, update and exit.
  182. const S = [
  183. new Selection([], enter, this._parent, this._document),
  184. new Selection(update, null, this._parent, this._document),
  185. new Selection(exit, null, this._parent, this._document),
  186. new Selection([], merge, this._parent, this._document),
  187. new Selection(split, null, this._parent, this._document),
  188. ];
  189. return new Selection(this._elements, null, this._parent, this._document, S);
  190. }
  191. merge(other) {
  192. const elements = [...this._elements, ...other._elements];
  193. const transitions = [...this._transitions, ...other._transitions];
  194. return new Selection(elements, null, this._parent, this._document, undefined, transitions);
  195. }
  196. createElement(type) {
  197. if (this._document) {
  198. return this._document.createElement(type, {});
  199. }
  200. const Ctor = Selection.registry[type];
  201. if (Ctor)
  202. return new Ctor();
  203. return (0, helper_1.error)(`Unknown node type: ${type}`);
  204. }
  205. /**
  206. * Apply callback for each selection(enter, update, exit)
  207. * and merge them into one selection.
  208. */
  209. join(enter = (d) => d, update = (d) => d, exit = (d) => d.remove(), merge = (d) => d, split = (d) => d.remove()) {
  210. const newEnter = enter(this._enter);
  211. const newUpdate = update(this._update);
  212. const newExit = exit(this._exit);
  213. const newMerge = merge(this._merge);
  214. const newSplit = split(this._split);
  215. return newUpdate
  216. .merge(newEnter)
  217. .merge(newExit)
  218. .merge(newMerge)
  219. .merge(newSplit);
  220. }
  221. remove() {
  222. // Remove node immediately if there is no transition,
  223. // otherwise wait until transition finished.
  224. for (let i = 0; i < this._elements.length; i++) {
  225. const transition = this._transitions[i];
  226. if (transition) {
  227. const T = Array.isArray(transition) ? transition : [transition];
  228. Promise.all(T.map((d) => d.finished)).then(() => {
  229. const element = this._elements[i];
  230. element.remove();
  231. });
  232. }
  233. else {
  234. const element = this._elements[i];
  235. element.remove();
  236. }
  237. }
  238. return new Selection([], null, this._parent, this._document, undefined, this._transitions);
  239. }
  240. each(callback) {
  241. for (let i = 0; i < this._elements.length; i++) {
  242. const element = this._elements[i];
  243. const datum = element.__data__;
  244. callback.call(element, datum, i);
  245. }
  246. return this;
  247. }
  248. attr(key, value) {
  249. const callback = typeof value !== 'function' ? () => value : value;
  250. return this.each(function (d, i) {
  251. if (value !== undefined)
  252. this[key] = callback.call(this, d, i);
  253. });
  254. }
  255. style(key, value) {
  256. const callback = typeof value !== 'function' ? () => value : value;
  257. return this.each(function (d, i) {
  258. if (value !== undefined)
  259. this.style[key] = callback.call(this, d, i);
  260. });
  261. }
  262. transition(value) {
  263. const callback = typeof value !== 'function' ? () => value : value;
  264. const { _transitions: T } = this;
  265. return this.each(function (d, i) {
  266. T[i] = callback.call(this, d, i);
  267. });
  268. }
  269. on(event, handler) {
  270. this.each(function () {
  271. this.addEventListener(event, handler);
  272. });
  273. return this;
  274. }
  275. call(callback, ...args) {
  276. callback.call(this._parent, this, ...args);
  277. return this;
  278. }
  279. node() {
  280. return this._elements[0];
  281. }
  282. nodes() {
  283. return this._elements;
  284. }
  285. transitions() {
  286. return this._transitions;
  287. }
  288. parent() {
  289. return this._parent;
  290. }
  291. }
  292. exports.Selection = Selection;
  293. Selection.registry = {
  294. g: g_1.Group,
  295. rect: g_1.Rect,
  296. circle: g_1.Circle,
  297. path: g_1.Path,
  298. text: g_1.Text,
  299. ellipse: g_1.Ellipse,
  300. image: g_1.Image,
  301. line: g_1.Line,
  302. polygon: g_1.Polygon,
  303. polyline: g_1.Polyline,
  304. html: g_1.HTML,
  305. };
  306. //# sourceMappingURL=selection.js.map