d3-selection.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. // https://d3js.org/d3-selection/ v1.4.2 Copyright 2020 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, function (exports) { 'use strict';
  7. var xhtml = "http://www.w3.org/1999/xhtml";
  8. var namespaces = {
  9. svg: "http://www.w3.org/2000/svg",
  10. xhtml: xhtml,
  11. xlink: "http://www.w3.org/1999/xlink",
  12. xml: "http://www.w3.org/XML/1998/namespace",
  13. xmlns: "http://www.w3.org/2000/xmlns/"
  14. };
  15. function namespace(name) {
  16. var prefix = name += "", i = prefix.indexOf(":");
  17. if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1);
  18. return namespaces.hasOwnProperty(prefix) ? {space: namespaces[prefix], local: name} : name;
  19. }
  20. function creatorInherit(name) {
  21. return function() {
  22. var document = this.ownerDocument,
  23. uri = this.namespaceURI;
  24. return uri === xhtml && document.documentElement.namespaceURI === xhtml
  25. ? document.createElement(name)
  26. : document.createElementNS(uri, name);
  27. };
  28. }
  29. function creatorFixed(fullname) {
  30. return function() {
  31. return this.ownerDocument.createElementNS(fullname.space, fullname.local);
  32. };
  33. }
  34. function creator(name) {
  35. var fullname = namespace(name);
  36. return (fullname.local
  37. ? creatorFixed
  38. : creatorInherit)(fullname);
  39. }
  40. function none() {}
  41. function selector(selector) {
  42. return selector == null ? none : function() {
  43. return this.querySelector(selector);
  44. };
  45. }
  46. function selection_select(select) {
  47. if (typeof select !== "function") select = selector(select);
  48. for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
  49. for (var group = groups[j], n = group.length, subgroup = subgroups[j] = new Array(n), node, subnode, i = 0; i < n; ++i) {
  50. if ((node = group[i]) && (subnode = select.call(node, node.__data__, i, group))) {
  51. if ("__data__" in node) subnode.__data__ = node.__data__;
  52. subgroup[i] = subnode;
  53. }
  54. }
  55. }
  56. return new Selection(subgroups, this._parents);
  57. }
  58. function empty() {
  59. return [];
  60. }
  61. function selectorAll(selector) {
  62. return selector == null ? empty : function() {
  63. return this.querySelectorAll(selector);
  64. };
  65. }
  66. function selection_selectAll(select) {
  67. if (typeof select !== "function") select = selectorAll(select);
  68. for (var groups = this._groups, m = groups.length, subgroups = [], parents = [], j = 0; j < m; ++j) {
  69. for (var group = groups[j], n = group.length, node, i = 0; i < n; ++i) {
  70. if (node = group[i]) {
  71. subgroups.push(select.call(node, node.__data__, i, group));
  72. parents.push(node);
  73. }
  74. }
  75. }
  76. return new Selection(subgroups, parents);
  77. }
  78. function matcher(selector) {
  79. return function() {
  80. return this.matches(selector);
  81. };
  82. }
  83. function selection_filter(match) {
  84. if (typeof match !== "function") match = matcher(match);
  85. for (var groups = this._groups, m = groups.length, subgroups = new Array(m), j = 0; j < m; ++j) {
  86. for (var group = groups[j], n = group.length, subgroup = subgroups[j] = [], node, i = 0; i < n; ++i) {
  87. if ((node = group[i]) && match.call(node, node.__data__, i, group)) {
  88. subgroup.push(node);
  89. }
  90. }
  91. }
  92. return new Selection(subgroups, this._parents);
  93. }
  94. function sparse(update) {
  95. return new Array(update.length);
  96. }
  97. function selection_enter() {
  98. return new Selection(this._enter || this._groups.map(sparse), this._parents);
  99. }
  100. function EnterNode(parent, datum) {
  101. this.ownerDocument = parent.ownerDocument;
  102. this.namespaceURI = parent.namespaceURI;
  103. this._next = null;
  104. this._parent = parent;
  105. this.__data__ = datum;
  106. }
  107. EnterNode.prototype = {
  108. constructor: EnterNode,
  109. appendChild: function(child) { return this._parent.insertBefore(child, this._next); },
  110. insertBefore: function(child, next) { return this._parent.insertBefore(child, next); },
  111. querySelector: function(selector) { return this._parent.querySelector(selector); },
  112. querySelectorAll: function(selector) { return this._parent.querySelectorAll(selector); }
  113. };
  114. function constant(x) {
  115. return function() {
  116. return x;
  117. };
  118. }
  119. var keyPrefix = "$"; // Protect against keys like “__proto__”.
  120. function bindIndex(parent, group, enter, update, exit, data) {
  121. var i = 0,
  122. node,
  123. groupLength = group.length,
  124. dataLength = data.length;
  125. // Put any non-null nodes that fit into update.
  126. // Put any null nodes into enter.
  127. // Put any remaining data into enter.
  128. for (; i < dataLength; ++i) {
  129. if (node = group[i]) {
  130. node.__data__ = data[i];
  131. update[i] = node;
  132. } else {
  133. enter[i] = new EnterNode(parent, data[i]);
  134. }
  135. }
  136. // Put any non-null nodes that don’t fit into exit.
  137. for (; i < groupLength; ++i) {
  138. if (node = group[i]) {
  139. exit[i] = node;
  140. }
  141. }
  142. }
  143. function bindKey(parent, group, enter, update, exit, data, key) {
  144. var i,
  145. node,
  146. nodeByKeyValue = {},
  147. groupLength = group.length,
  148. dataLength = data.length,
  149. keyValues = new Array(groupLength),
  150. keyValue;
  151. // Compute the key for each node.
  152. // If multiple nodes have the same key, the duplicates are added to exit.
  153. for (i = 0; i < groupLength; ++i) {
  154. if (node = group[i]) {
  155. keyValues[i] = keyValue = keyPrefix + key.call(node, node.__data__, i, group);
  156. if (keyValue in nodeByKeyValue) {
  157. exit[i] = node;
  158. } else {
  159. nodeByKeyValue[keyValue] = node;
  160. }
  161. }
  162. }
  163. // Compute the key for each datum.
  164. // If there a node associated with this key, join and add it to update.
  165. // If there is not (or the key is a duplicate), add it to enter.
  166. for (i = 0; i < dataLength; ++i) {
  167. keyValue = keyPrefix + key.call(parent, data[i], i, data);
  168. if (node = nodeByKeyValue[keyValue]) {
  169. update[i] = node;
  170. node.__data__ = data[i];
  171. nodeByKeyValue[keyValue] = null;
  172. } else {
  173. enter[i] = new EnterNode(parent, data[i]);
  174. }
  175. }
  176. // Add any remaining nodes that were not bound to data to exit.
  177. for (i = 0; i < groupLength; ++i) {
  178. if ((node = group[i]) && (nodeByKeyValue[keyValues[i]] === node)) {
  179. exit[i] = node;
  180. }
  181. }
  182. }
  183. function selection_data(value, key) {
  184. if (!value) {
  185. data = new Array(this.size()), j = -1;
  186. this.each(function(d) { data[++j] = d; });
  187. return data;
  188. }
  189. var bind = key ? bindKey : bindIndex,
  190. parents = this._parents,
  191. groups = this._groups;
  192. if (typeof value !== "function") value = constant(value);
  193. for (var m = groups.length, update = new Array(m), enter = new Array(m), exit = new Array(m), j = 0; j < m; ++j) {
  194. var parent = parents[j],
  195. group = groups[j],
  196. groupLength = group.length,
  197. data = value.call(parent, parent && parent.__data__, j, parents),
  198. dataLength = data.length,
  199. enterGroup = enter[j] = new Array(dataLength),
  200. updateGroup = update[j] = new Array(dataLength),
  201. exitGroup = exit[j] = new Array(groupLength);
  202. bind(parent, group, enterGroup, updateGroup, exitGroup, data, key);
  203. // Now connect the enter nodes to their following update node, such that
  204. // appendChild can insert the materialized enter node before this node,
  205. // rather than at the end of the parent node.
  206. for (var i0 = 0, i1 = 0, previous, next; i0 < dataLength; ++i0) {
  207. if (previous = enterGroup[i0]) {
  208. if (i0 >= i1) i1 = i0 + 1;
  209. while (!(next = updateGroup[i1]) && ++i1 < dataLength);
  210. previous._next = next || null;
  211. }
  212. }
  213. }
  214. update = new Selection(update, parents);
  215. update._enter = enter;
  216. update._exit = exit;
  217. return update;
  218. }
  219. function selection_exit() {
  220. return new Selection(this._exit || this._groups.map(sparse), this._parents);
  221. }
  222. function selection_join(onenter, onupdate, onexit) {
  223. var enter = this.enter(), update = this, exit = this.exit();
  224. enter = typeof onenter === "function" ? onenter(enter) : enter.append(onenter + "");
  225. if (onupdate != null) update = onupdate(update);
  226. if (onexit == null) exit.remove(); else onexit(exit);
  227. return enter && update ? enter.merge(update).order() : update;
  228. }
  229. function selection_merge(selection) {
  230. for (var groups0 = this._groups, groups1 = selection._groups, m0 = groups0.length, m1 = groups1.length, m = Math.min(m0, m1), merges = new Array(m0), j = 0; j < m; ++j) {
  231. for (var group0 = groups0[j], group1 = groups1[j], n = group0.length, merge = merges[j] = new Array(n), node, i = 0; i < n; ++i) {
  232. if (node = group0[i] || group1[i]) {
  233. merge[i] = node;
  234. }
  235. }
  236. }
  237. for (; j < m0; ++j) {
  238. merges[j] = groups0[j];
  239. }
  240. return new Selection(merges, this._parents);
  241. }
  242. function selection_order() {
  243. for (var groups = this._groups, j = -1, m = groups.length; ++j < m;) {
  244. for (var group = groups[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
  245. if (node = group[i]) {
  246. if (next && node.compareDocumentPosition(next) ^ 4) next.parentNode.insertBefore(node, next);
  247. next = node;
  248. }
  249. }
  250. }
  251. return this;
  252. }
  253. function selection_sort(compare) {
  254. if (!compare) compare = ascending;
  255. function compareNode(a, b) {
  256. return a && b ? compare(a.__data__, b.__data__) : !a - !b;
  257. }
  258. for (var groups = this._groups, m = groups.length, sortgroups = new Array(m), j = 0; j < m; ++j) {
  259. for (var group = groups[j], n = group.length, sortgroup = sortgroups[j] = new Array(n), node, i = 0; i < n; ++i) {
  260. if (node = group[i]) {
  261. sortgroup[i] = node;
  262. }
  263. }
  264. sortgroup.sort(compareNode);
  265. }
  266. return new Selection(sortgroups, this._parents).order();
  267. }
  268. function ascending(a, b) {
  269. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  270. }
  271. function selection_call() {
  272. var callback = arguments[0];
  273. arguments[0] = this;
  274. callback.apply(null, arguments);
  275. return this;
  276. }
  277. function selection_nodes() {
  278. var nodes = new Array(this.size()), i = -1;
  279. this.each(function() { nodes[++i] = this; });
  280. return nodes;
  281. }
  282. function selection_node() {
  283. for (var groups = this._groups, j = 0, m = groups.length; j < m; ++j) {
  284. for (var group = groups[j], i = 0, n = group.length; i < n; ++i) {
  285. var node = group[i];
  286. if (node) return node;
  287. }
  288. }
  289. return null;
  290. }
  291. function selection_size() {
  292. var size = 0;
  293. this.each(function() { ++size; });
  294. return size;
  295. }
  296. function selection_empty() {
  297. return !this.node();
  298. }
  299. function selection_each(callback) {
  300. for (var groups = this._groups, j = 0, m = groups.length; j < m; ++j) {
  301. for (var group = groups[j], i = 0, n = group.length, node; i < n; ++i) {
  302. if (node = group[i]) callback.call(node, node.__data__, i, group);
  303. }
  304. }
  305. return this;
  306. }
  307. function attrRemove(name) {
  308. return function() {
  309. this.removeAttribute(name);
  310. };
  311. }
  312. function attrRemoveNS(fullname) {
  313. return function() {
  314. this.removeAttributeNS(fullname.space, fullname.local);
  315. };
  316. }
  317. function attrConstant(name, value) {
  318. return function() {
  319. this.setAttribute(name, value);
  320. };
  321. }
  322. function attrConstantNS(fullname, value) {
  323. return function() {
  324. this.setAttributeNS(fullname.space, fullname.local, value);
  325. };
  326. }
  327. function attrFunction(name, value) {
  328. return function() {
  329. var v = value.apply(this, arguments);
  330. if (v == null) this.removeAttribute(name);
  331. else this.setAttribute(name, v);
  332. };
  333. }
  334. function attrFunctionNS(fullname, value) {
  335. return function() {
  336. var v = value.apply(this, arguments);
  337. if (v == null) this.removeAttributeNS(fullname.space, fullname.local);
  338. else this.setAttributeNS(fullname.space, fullname.local, v);
  339. };
  340. }
  341. function selection_attr(name, value) {
  342. var fullname = namespace(name);
  343. if (arguments.length < 2) {
  344. var node = this.node();
  345. return fullname.local
  346. ? node.getAttributeNS(fullname.space, fullname.local)
  347. : node.getAttribute(fullname);
  348. }
  349. return this.each((value == null
  350. ? (fullname.local ? attrRemoveNS : attrRemove) : (typeof value === "function"
  351. ? (fullname.local ? attrFunctionNS : attrFunction)
  352. : (fullname.local ? attrConstantNS : attrConstant)))(fullname, value));
  353. }
  354. function defaultView(node) {
  355. return (node.ownerDocument && node.ownerDocument.defaultView) // node is a Node
  356. || (node.document && node) // node is a Window
  357. || node.defaultView; // node is a Document
  358. }
  359. function styleRemove(name) {
  360. return function() {
  361. this.style.removeProperty(name);
  362. };
  363. }
  364. function styleConstant(name, value, priority) {
  365. return function() {
  366. this.style.setProperty(name, value, priority);
  367. };
  368. }
  369. function styleFunction(name, value, priority) {
  370. return function() {
  371. var v = value.apply(this, arguments);
  372. if (v == null) this.style.removeProperty(name);
  373. else this.style.setProperty(name, v, priority);
  374. };
  375. }
  376. function selection_style(name, value, priority) {
  377. return arguments.length > 1
  378. ? this.each((value == null
  379. ? styleRemove : typeof value === "function"
  380. ? styleFunction
  381. : styleConstant)(name, value, priority == null ? "" : priority))
  382. : styleValue(this.node(), name);
  383. }
  384. function styleValue(node, name) {
  385. return node.style.getPropertyValue(name)
  386. || defaultView(node).getComputedStyle(node, null).getPropertyValue(name);
  387. }
  388. function propertyRemove(name) {
  389. return function() {
  390. delete this[name];
  391. };
  392. }
  393. function propertyConstant(name, value) {
  394. return function() {
  395. this[name] = value;
  396. };
  397. }
  398. function propertyFunction(name, value) {
  399. return function() {
  400. var v = value.apply(this, arguments);
  401. if (v == null) delete this[name];
  402. else this[name] = v;
  403. };
  404. }
  405. function selection_property(name, value) {
  406. return arguments.length > 1
  407. ? this.each((value == null
  408. ? propertyRemove : typeof value === "function"
  409. ? propertyFunction
  410. : propertyConstant)(name, value))
  411. : this.node()[name];
  412. }
  413. function classArray(string) {
  414. return string.trim().split(/^|\s+/);
  415. }
  416. function classList(node) {
  417. return node.classList || new ClassList(node);
  418. }
  419. function ClassList(node) {
  420. this._node = node;
  421. this._names = classArray(node.getAttribute("class") || "");
  422. }
  423. ClassList.prototype = {
  424. add: function(name) {
  425. var i = this._names.indexOf(name);
  426. if (i < 0) {
  427. this._names.push(name);
  428. this._node.setAttribute("class", this._names.join(" "));
  429. }
  430. },
  431. remove: function(name) {
  432. var i = this._names.indexOf(name);
  433. if (i >= 0) {
  434. this._names.splice(i, 1);
  435. this._node.setAttribute("class", this._names.join(" "));
  436. }
  437. },
  438. contains: function(name) {
  439. return this._names.indexOf(name) >= 0;
  440. }
  441. };
  442. function classedAdd(node, names) {
  443. var list = classList(node), i = -1, n = names.length;
  444. while (++i < n) list.add(names[i]);
  445. }
  446. function classedRemove(node, names) {
  447. var list = classList(node), i = -1, n = names.length;
  448. while (++i < n) list.remove(names[i]);
  449. }
  450. function classedTrue(names) {
  451. return function() {
  452. classedAdd(this, names);
  453. };
  454. }
  455. function classedFalse(names) {
  456. return function() {
  457. classedRemove(this, names);
  458. };
  459. }
  460. function classedFunction(names, value) {
  461. return function() {
  462. (value.apply(this, arguments) ? classedAdd : classedRemove)(this, names);
  463. };
  464. }
  465. function selection_classed(name, value) {
  466. var names = classArray(name + "");
  467. if (arguments.length < 2) {
  468. var list = classList(this.node()), i = -1, n = names.length;
  469. while (++i < n) if (!list.contains(names[i])) return false;
  470. return true;
  471. }
  472. return this.each((typeof value === "function"
  473. ? classedFunction : value
  474. ? classedTrue
  475. : classedFalse)(names, value));
  476. }
  477. function textRemove() {
  478. this.textContent = "";
  479. }
  480. function textConstant(value) {
  481. return function() {
  482. this.textContent = value;
  483. };
  484. }
  485. function textFunction(value) {
  486. return function() {
  487. var v = value.apply(this, arguments);
  488. this.textContent = v == null ? "" : v;
  489. };
  490. }
  491. function selection_text(value) {
  492. return arguments.length
  493. ? this.each(value == null
  494. ? textRemove : (typeof value === "function"
  495. ? textFunction
  496. : textConstant)(value))
  497. : this.node().textContent;
  498. }
  499. function htmlRemove() {
  500. this.innerHTML = "";
  501. }
  502. function htmlConstant(value) {
  503. return function() {
  504. this.innerHTML = value;
  505. };
  506. }
  507. function htmlFunction(value) {
  508. return function() {
  509. var v = value.apply(this, arguments);
  510. this.innerHTML = v == null ? "" : v;
  511. };
  512. }
  513. function selection_html(value) {
  514. return arguments.length
  515. ? this.each(value == null
  516. ? htmlRemove : (typeof value === "function"
  517. ? htmlFunction
  518. : htmlConstant)(value))
  519. : this.node().innerHTML;
  520. }
  521. function raise() {
  522. if (this.nextSibling) this.parentNode.appendChild(this);
  523. }
  524. function selection_raise() {
  525. return this.each(raise);
  526. }
  527. function lower() {
  528. if (this.previousSibling) this.parentNode.insertBefore(this, this.parentNode.firstChild);
  529. }
  530. function selection_lower() {
  531. return this.each(lower);
  532. }
  533. function selection_append(name) {
  534. var create = typeof name === "function" ? name : creator(name);
  535. return this.select(function() {
  536. return this.appendChild(create.apply(this, arguments));
  537. });
  538. }
  539. function constantNull() {
  540. return null;
  541. }
  542. function selection_insert(name, before) {
  543. var create = typeof name === "function" ? name : creator(name),
  544. select = before == null ? constantNull : typeof before === "function" ? before : selector(before);
  545. return this.select(function() {
  546. return this.insertBefore(create.apply(this, arguments), select.apply(this, arguments) || null);
  547. });
  548. }
  549. function remove() {
  550. var parent = this.parentNode;
  551. if (parent) parent.removeChild(this);
  552. }
  553. function selection_remove() {
  554. return this.each(remove);
  555. }
  556. function selection_cloneShallow() {
  557. var clone = this.cloneNode(false), parent = this.parentNode;
  558. return parent ? parent.insertBefore(clone, this.nextSibling) : clone;
  559. }
  560. function selection_cloneDeep() {
  561. var clone = this.cloneNode(true), parent = this.parentNode;
  562. return parent ? parent.insertBefore(clone, this.nextSibling) : clone;
  563. }
  564. function selection_clone(deep) {
  565. return this.select(deep ? selection_cloneDeep : selection_cloneShallow);
  566. }
  567. function selection_datum(value) {
  568. return arguments.length
  569. ? this.property("__data__", value)
  570. : this.node().__data__;
  571. }
  572. var filterEvents = {};
  573. exports.event = null;
  574. if (typeof document !== "undefined") {
  575. var element = document.documentElement;
  576. if (!("onmouseenter" in element)) {
  577. filterEvents = {mouseenter: "mouseover", mouseleave: "mouseout"};
  578. }
  579. }
  580. function filterContextListener(listener, index, group) {
  581. listener = contextListener(listener, index, group);
  582. return function(event) {
  583. var related = event.relatedTarget;
  584. if (!related || (related !== this && !(related.compareDocumentPosition(this) & 8))) {
  585. listener.call(this, event);
  586. }
  587. };
  588. }
  589. function contextListener(listener, index, group) {
  590. return function(event1) {
  591. var event0 = exports.event; // Events can be reentrant (e.g., focus).
  592. exports.event = event1;
  593. try {
  594. listener.call(this, this.__data__, index, group);
  595. } finally {
  596. exports.event = event0;
  597. }
  598. };
  599. }
  600. function parseTypenames(typenames) {
  601. return typenames.trim().split(/^|\s+/).map(function(t) {
  602. var name = "", i = t.indexOf(".");
  603. if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i);
  604. return {type: t, name: name};
  605. });
  606. }
  607. function onRemove(typename) {
  608. return function() {
  609. var on = this.__on;
  610. if (!on) return;
  611. for (var j = 0, i = -1, m = on.length, o; j < m; ++j) {
  612. if (o = on[j], (!typename.type || o.type === typename.type) && o.name === typename.name) {
  613. this.removeEventListener(o.type, o.listener, o.capture);
  614. } else {
  615. on[++i] = o;
  616. }
  617. }
  618. if (++i) on.length = i;
  619. else delete this.__on;
  620. };
  621. }
  622. function onAdd(typename, value, capture) {
  623. var wrap = filterEvents.hasOwnProperty(typename.type) ? filterContextListener : contextListener;
  624. return function(d, i, group) {
  625. var on = this.__on, o, listener = wrap(value, i, group);
  626. if (on) for (var j = 0, m = on.length; j < m; ++j) {
  627. if ((o = on[j]).type === typename.type && o.name === typename.name) {
  628. this.removeEventListener(o.type, o.listener, o.capture);
  629. this.addEventListener(o.type, o.listener = listener, o.capture = capture);
  630. o.value = value;
  631. return;
  632. }
  633. }
  634. this.addEventListener(typename.type, listener, capture);
  635. o = {type: typename.type, name: typename.name, value: value, listener: listener, capture: capture};
  636. if (!on) this.__on = [o];
  637. else on.push(o);
  638. };
  639. }
  640. function selection_on(typename, value, capture) {
  641. var typenames = parseTypenames(typename + ""), i, n = typenames.length, t;
  642. if (arguments.length < 2) {
  643. var on = this.node().__on;
  644. if (on) for (var j = 0, m = on.length, o; j < m; ++j) {
  645. for (i = 0, o = on[j]; i < n; ++i) {
  646. if ((t = typenames[i]).type === o.type && t.name === o.name) {
  647. return o.value;
  648. }
  649. }
  650. }
  651. return;
  652. }
  653. on = value ? onAdd : onRemove;
  654. if (capture == null) capture = false;
  655. for (i = 0; i < n; ++i) this.each(on(typenames[i], value, capture));
  656. return this;
  657. }
  658. function customEvent(event1, listener, that, args) {
  659. var event0 = exports.event;
  660. event1.sourceEvent = exports.event;
  661. exports.event = event1;
  662. try {
  663. return listener.apply(that, args);
  664. } finally {
  665. exports.event = event0;
  666. }
  667. }
  668. function dispatchEvent(node, type, params) {
  669. var window = defaultView(node),
  670. event = window.CustomEvent;
  671. if (typeof event === "function") {
  672. event = new event(type, params);
  673. } else {
  674. event = window.document.createEvent("Event");
  675. if (params) event.initEvent(type, params.bubbles, params.cancelable), event.detail = params.detail;
  676. else event.initEvent(type, false, false);
  677. }
  678. node.dispatchEvent(event);
  679. }
  680. function dispatchConstant(type, params) {
  681. return function() {
  682. return dispatchEvent(this, type, params);
  683. };
  684. }
  685. function dispatchFunction(type, params) {
  686. return function() {
  687. return dispatchEvent(this, type, params.apply(this, arguments));
  688. };
  689. }
  690. function selection_dispatch(type, params) {
  691. return this.each((typeof params === "function"
  692. ? dispatchFunction
  693. : dispatchConstant)(type, params));
  694. }
  695. var root = [null];
  696. function Selection(groups, parents) {
  697. this._groups = groups;
  698. this._parents = parents;
  699. }
  700. function selection() {
  701. return new Selection([[document.documentElement]], root);
  702. }
  703. Selection.prototype = selection.prototype = {
  704. constructor: Selection,
  705. select: selection_select,
  706. selectAll: selection_selectAll,
  707. filter: selection_filter,
  708. data: selection_data,
  709. enter: selection_enter,
  710. exit: selection_exit,
  711. join: selection_join,
  712. merge: selection_merge,
  713. order: selection_order,
  714. sort: selection_sort,
  715. call: selection_call,
  716. nodes: selection_nodes,
  717. node: selection_node,
  718. size: selection_size,
  719. empty: selection_empty,
  720. each: selection_each,
  721. attr: selection_attr,
  722. style: selection_style,
  723. property: selection_property,
  724. classed: selection_classed,
  725. text: selection_text,
  726. html: selection_html,
  727. raise: selection_raise,
  728. lower: selection_lower,
  729. append: selection_append,
  730. insert: selection_insert,
  731. remove: selection_remove,
  732. clone: selection_clone,
  733. datum: selection_datum,
  734. on: selection_on,
  735. dispatch: selection_dispatch
  736. };
  737. function select(selector) {
  738. return typeof selector === "string"
  739. ? new Selection([[document.querySelector(selector)]], [document.documentElement])
  740. : new Selection([[selector]], root);
  741. }
  742. function create(name) {
  743. return select(creator(name).call(document.documentElement));
  744. }
  745. var nextId = 0;
  746. function local() {
  747. return new Local;
  748. }
  749. function Local() {
  750. this._ = "@" + (++nextId).toString(36);
  751. }
  752. Local.prototype = local.prototype = {
  753. constructor: Local,
  754. get: function(node) {
  755. var id = this._;
  756. while (!(id in node)) if (!(node = node.parentNode)) return;
  757. return node[id];
  758. },
  759. set: function(node, value) {
  760. return node[this._] = value;
  761. },
  762. remove: function(node) {
  763. return this._ in node && delete node[this._];
  764. },
  765. toString: function() {
  766. return this._;
  767. }
  768. };
  769. function sourceEvent() {
  770. var current = exports.event, source;
  771. while (source = current.sourceEvent) current = source;
  772. return current;
  773. }
  774. function point(node, event) {
  775. var svg = node.ownerSVGElement || node;
  776. if (svg.createSVGPoint) {
  777. var point = svg.createSVGPoint();
  778. point.x = event.clientX, point.y = event.clientY;
  779. point = point.matrixTransform(node.getScreenCTM().inverse());
  780. return [point.x, point.y];
  781. }
  782. var rect = node.getBoundingClientRect();
  783. return [event.clientX - rect.left - node.clientLeft, event.clientY - rect.top - node.clientTop];
  784. }
  785. function mouse(node) {
  786. var event = sourceEvent();
  787. if (event.changedTouches) event = event.changedTouches[0];
  788. return point(node, event);
  789. }
  790. function selectAll(selector) {
  791. return typeof selector === "string"
  792. ? new Selection([document.querySelectorAll(selector)], [document.documentElement])
  793. : new Selection([selector == null ? [] : selector], root);
  794. }
  795. function touch(node, touches, identifier) {
  796. if (arguments.length < 3) identifier = touches, touches = sourceEvent().changedTouches;
  797. for (var i = 0, n = touches ? touches.length : 0, touch; i < n; ++i) {
  798. if ((touch = touches[i]).identifier === identifier) {
  799. return point(node, touch);
  800. }
  801. }
  802. return null;
  803. }
  804. function touches(node, touches) {
  805. if (touches == null) touches = sourceEvent().touches;
  806. for (var i = 0, n = touches ? touches.length : 0, points = new Array(n); i < n; ++i) {
  807. points[i] = point(node, touches[i]);
  808. }
  809. return points;
  810. }
  811. exports.clientPoint = point;
  812. exports.create = create;
  813. exports.creator = creator;
  814. exports.customEvent = customEvent;
  815. exports.local = local;
  816. exports.matcher = matcher;
  817. exports.mouse = mouse;
  818. exports.namespace = namespace;
  819. exports.namespaces = namespaces;
  820. exports.select = select;
  821. exports.selectAll = selectAll;
  822. exports.selection = selection;
  823. exports.selector = selector;
  824. exports.selectorAll = selectorAll;
  825. exports.style = styleValue;
  826. exports.touch = touch;
  827. exports.touches = touches;
  828. exports.window = defaultView;
  829. Object.defineProperty(exports, '__esModule', { value: true });
  830. }));