summaryrefslogtreecommitdiffstats
path: root/src/doc.js
blob: 20977471086e300f223b393429dc8cd10e912019 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
SVG.Doc = SVG.invent({
  // Initialize node
  create: function (node) {
    this.constructor(node || SVG.create('svg'))

    // set svg element attributes and ensure defs node
    this.namespace().defs()
  },

  // Inherit from
  inherit: SVG.Container,

  // Add class methods
  extend: {
    // Add namespaces
    namespace: function () {
      return this
        .attr({ xmlns: SVG.ns, version: '1.1' })
        .attr('xmlns:xlink', SVG.xlink, SVG.xmlns)
        .attr('xmlns:svgjs', SVG.svgjs, SVG.xmlns)
    },
    // Creates and returns defs element
    defs: function () {
      return SVG.adopt(this.node.getElementsByTagName('defs')[0]) || this.put(new SVG.Defs())
    },
    // custom parent method
    parent: function () {
      return this.node.parentNode.nodeName === '#document' ? null : this.node.parentNode
    },
      // Removes the doc from the DOM
    remove: function () {
      if (this.parent()) {
        this.parent().removeChild(this.node)
      }

      return this
    },
    clear: function () {
      // remove children
      while (this.node.hasChildNodes()) {
        this.node.removeChild(this.node.lastChild)
      }
      return this
    },
    toNested: function () {
      var el = SVG.create('svg')
      this.node.instance = null
      el.appendChild(this.node)

      return SVG.adopt(this.node)
    }
  }

})