Skip to content

Commit a551afe

Browse files
committed
Add selection.clone.
Fixes #149.
1 parent f6d44d7 commit a551afe

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ The specified *name* may have a namespace prefix, such as `svg:text` to specify
386386

387387
Removes the selected elements from the document. Returns this selection (the removed elements) which are now detached from the DOM. There is not currently a dedicated API to add removed elements back to the document; however, you can pass a function to [*selection*.append](#selection_append) or [*selection*.insert](#selection_insert) to re-add elements.
388388

389+
<a name="selection_clone" href="#selection_clone">#</a> <i>selection</i>.<b>clone</b>([<i>deep</i>]) [<>](https://github.com/d3/d3-selection/blob/master/src/selection/clone.js "Source")
390+
391+
Inserts clones the selected elements immediately following the selected elements. Equivalent to:
392+
393+
```js
394+
selection.select(function() {
395+
return this.parentNode.insertBefore(this.cloneNode(deep), this.nextSibling);
396+
});
397+
```
398+
389399
<a name="selection_sort" href="#selection_sort">#</a> <i>selection</i>.<b>sort</b>(<i>compare</i>) [<>](https://github.com/d3/d3-selection/blob/master/src/selection/sort.js "Source")
390400

391401
Returns a new selection that contains a copy of each group in this selection sorted according to the *compare* function. After sorting, re-inserts elements to match the resulting order (per [*selection*.order](#selection_order)).

src/selection/clone.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function selection_cloneShallow() {
2+
return this.parentNode.insertBefore(this.cloneNode(false), this.nextSibling);
3+
}
4+
5+
function selection_cloneDeep() {
6+
return this.parentNode.insertBefore(this.cloneNode(true), this.nextSibling);
7+
}
8+
9+
export default function(deep) {
10+
return this.select(deep ? selection_cloneDeep : selection_cloneShallow);
11+
}

src/selection/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import selection_lower from "./lower";
2424
import selection_append from "./append";
2525
import selection_insert from "./insert";
2626
import selection_remove from "./remove";
27+
import selection_clone from "./clone";
2728
import selection_datum from "./datum";
2829
import selection_on from "./on";
2930
import selection_dispatch from "./dispatch";
@@ -67,6 +68,7 @@ Selection.prototype = selection.prototype = {
6768
append: selection_append,
6869
insert: selection_insert,
6970
remove: selection_remove,
71+
clone: selection_clone,
7072
datum: selection_datum,
7173
on: selection_on,
7274
dispatch: selection_dispatch

0 commit comments

Comments
 (0)