aboutsummaryrefslogtreecommitdiffstats
path: root/src/arrange.js
blob: a90814301b94ce25ac5af8d38225252d36637578 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ### This module adds backward / forward functionality to elements.

//
SVG.extend(SVG.Element, {
  // Get all siblings, including myself
  siblings: function () {
    return this.parent().children()
  },

  // Get the curent position siblings
  position: function () {
    return this.parent().index(this)
  },

  // Get the next element (will return null if there is none)
  next: function () {
    return this.siblings()[this.position() + 1]
  },

  // Get the next element (will return null if there is none)
  prev: function () {
    return this.siblings()[this.position() - 1]
  },

  // Send given element one step forward
  forward: function () {
    var i = this.position() + 1
    var p = this.parent()

    // move node one step forward
    p.removeElement(this).add(this, i)

    // make sure defs node is always at the top
    if (p instanceof SVG.Doc) {
      p.node.appendChild(p.defs().node)
    }

    return this
  },

  // Send given element one step backward
  backward: function () {
    var i = this.position()

    if (i > 0) {
      this.parent().removeElement(this).add(this, i - 1)
    }

    return this
  },

  // Send given element all the way to the front
  front: function () {
    var p = this.parent()

    // Move node forward
    p.node.appendChild(this.node)

    // Make sure defs node is always at the top
    if (p instanceof SVG.Doc) {
      p.node.appendChild(p.defs().node)
    }

    return this
  },

  // Send given element all the way to the back
  back: function () {
    if (this.position() > 0) {
      this.parent().removeElement(this).add(this, 0)
    }

    return this
  },

  // Inserts a given element before the targeted element
  before: function (element) {
    element.remove()

    var i = this.position()

    this.parent().add(element, i)

    return this
  },

  // Insters a given element after the targeted element
  after: function (element) {
    element.remove()

    var i = this.position()

    this.parent().add(element, i + 1)

    return this
  }
})