-
Notifications
You must be signed in to change notification settings - Fork 0
/
raphael.backward-forward.js
52 lines (48 loc) · 1.25 KB
/
raphael.backward-forward.js
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
/*
* raphael.backward-forward 0.0.3
*
* Copyright (c) 2010 Wout Fierens
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
// get all elements in the paper
Raphael.fn.elements = function() {
var b = this.bottom,
r = [];
while (b) {
r.push(b);
b = b.next;
}
return r;
};
// move an element in the stack
Raphael.fn.arrange = function(shape, steps, scope) {
if (!parseInt(steps)) return;
var elements = scope || this.elements(),
pos = elements.indexOf(shape),
lastPos = elements.length - 1,
newPos = pos + steps;
if (newPos > lastPos)
newPos = lastPos;
if (newPos <= 0)
newPos = 0;
if (steps > 0)
shape.insertAfter(elements[newPos]);
else if (steps < 0)
shape.insertBefore(elements[newPos]);
if (scope) {
scope.splice(pos, 1);
scope.splice(newPos, 0, shape);
}
};
// move an element one step backward in the stack
Raphael.el.backward = function(steps, scope) {
steps = parseInt(steps) || 1;
this.paper.arrange(this, -steps, scope);
return this;
};
// move an element one step forward in the stack
Raphael.el.forward = function(steps, scope) {
steps = parseInt(steps) || 1;
this.paper.arrange(this, steps, scope);
return this;
};