summaryrefslogtreecommitdiffstats
path: root/src/set.js
blob: 3808a91291f4ca67ab1516e997ffc7a54389b939 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
SVG.Set = function() {
  /* set initial state */
  this.clear()
}

// Set FX class
SVG.SetFX = function(set) {
  /* store reference to set */
  this.set = set
}

//
SVG.extend(SVG.Set, {
  // Add element to set
  add: function() {
    var i, il, elements = [].slice.call(arguments)

    for (i = 0, il = elements.length; i < il; i++)
      this.members.push(elements[i])
    
    return this
  }
  // Remove element from set
, remove: function(element) {
    var i = this.members.indexOf(element)
    
    /* remove given child */
    if (i > -1)
      this.members.splice(i, 1)

    return this
  }
  // Iterate over all members
, each: function(block) {
    for (var i = 0, il = this.members.length; i < il; i++)
      block.apply(this.members[i], [i, this.members])

    return this
  }
  // Restore to defaults
, clear: function() {
    /* initialize store */
    this.members = []

    return this
  }
  // Checks if a given element is present in set
, has: function(element) {
    return this.members.indexOf(element) >= 0
  }
  // Get member at given index
, get: function(i) {
    return this.members[i]
  }
  // Default value
, valueOf: function() {
    return this.members
  }
  // Get the bounding box of all members included or empty box if set has no items
, bbox: function(){
    var box = new SVG.BBox()

    /* return an empty box of there are no members */
    if (this.members.length == 0)
      return box

    /* get the first rbox and update the target bbox */
    var rbox = this.members[0].rbox()
    box.x      = rbox.x
    box.y      = rbox.y
    box.width  = rbox.width
    box.height = rbox.height

    this.each(function() {
      /* user rbox for correct position and visual representation */
      box = box.merge(this.rbox())
    })

    return box
  }

})



// Alias methods
SVG.Set.inherit = function() {
  var m
    , methods = []
  
  /* gather shape methods */
  for(var m in SVG.Shape.prototype)
    if (typeof SVG.Shape.prototype[m] == 'function' && typeof SVG.Set.prototype[m] != 'function')
      methods.push(m)

  /* apply shape aliasses */
  methods.forEach(function(method) {
    SVG.Set.prototype[method] = function() {
      for (var i = 0, il = this.members.length; i < il; i++)
        if (this.members[i] && typeof this.members[i][method] == 'function')
          this.members[i][method].apply(this.members[i], arguments)

      return method == 'animate' ? (this.fx || (this.fx = new SVG.SetFX(this))) : this
    }
  })

  /* clear methods for the next round */
  methods = []

  /* gather fx methods */
  for(var m in SVG.FX.prototype)
    if (typeof SVG.FX.prototype[m] == 'function' && typeof SVG.SetFX.prototype[m] != 'function')
      methods.push(m)

  /* apply fx aliasses */
  methods.forEach(function(method) {
    SVG.SetFX.prototype[method] = function() {
      for (var i = 0, il = this.set.members.length; i < il; i++)
        this.set.members[i].fx[method].apply(this.set.members[i].fx, arguments)

      return this
    }
  })
}

//
SVG.extend(SVG.Container, {
  // Create a new set
  set: function() {
    return new SVG.Set
  }

})