summaryrefslogtreecommitdiffstats
path: root/src/flatten.js
blob: 19eebd7a9be8e28b9a6f67e94c950cef85d20102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
SVG.extend(SVG.Parent, {
  flatten: function (parent) {
    // flattens is only possible for nested svgs and groups
    if (!(this instanceof SVG.G || this instanceof SVG.Doc)) {
      return this
    }

    parent = parent || (this instanceof SVG.Doc && this.isRoot() ? this : this.parent(SVG.Parent))

    this.each(function () {
      if (this instanceof SVG.Defs) return this
      if (this instanceof SVG.Parent) return this.flatten(parent)
      return this.toParent(parent)
    })

    // we need this so that SVG.Doc does not get removed
    this.node.firstElementChild || this.remove()

    return this
  },
  ungroup: function (parent) {
    // ungroup is only possible for nested svgs and groups
    if (!(this instanceof SVG.G || (this instanceof SVG.Doc && !this.isRoot()))) {
      return this
    }

    parent = parent || this.parent(SVG.Parent)

    this.each(function () {
      return this.toParent(parent)
    })

    // we need this so that SVG.Doc does not get removed
    this.remove()

    return this
  }
})