Repository navigation
Expand file tree
/
Copy pathbirdify.js
More file actions
139 lines (112 loc) · 3.33 KB
/
Copy pathbirdify.js
File metadata and controls
139 lines (112 loc) · 3.33 KB
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
function Birdify(ctx, mode){
if (this.constructor === Window){
return new Birdify(ctx, mode);
}
if (ctx.constructor.name !== Function){
ctx = $(ctx);
}
this.ctx = ctx;
this.mode = mode;
// wrap everything
Birdify._wrapBirdTextRecursive(ctx, mode);
this.birds = ctx.find(".bird-candidate");
}
Birdify.modes = {
"parentheses":1,
"text":2
};
Birdify.prototype.fly = function(span, delay){
Birdify._activateBirds(this.birds, span, delay);
}
Birdify.prototype.reset = function(){
Birdify._deactivateBirds(this.birds);
}
Birdify._roundRobin=1;
Birdify._numPaths=6;
Birdify._selectFlightPath=function(){
var selected = Birdify._roundRobin;
Birdify._roundRobin++;
if (Birdify._roundRobin > Birdify._numPaths){
Birdify._roundRobin = 1;
}
return "flightpath-" + selected;
}
Birdify._wrapParenReplacer = '<span class="bird-candidate bird-candidate-paren"><span class="left wing">$1</span>$2<span class="right wing">$3</span></span>';
Birdify._wrapTextReplacer = '<span class="bird-candidate bird-candidate-text"><span class="left wing">$1</span>$2<span class="right wing">$3</span></span>';
Birdify._wrapBirdTextRecursive = function(ctx, mode){
if (mode === undefined){
mode = 1;
}
ctx.contents().filter(function() { return this.nodeType != Node.TEXT_NODE; })
.each(function(){ Birdify._wrapBirdTextRecursive($(this), mode);});
ctx.contents().filter(function() { return this.nodeType == Node.TEXT_NODE; })
.each(function(){
var text = $(this).text();
if (mode & 1){
text = text.replace(/([{\[\(])([^\{^\[^\(^\}^\]^\)]?)([}\]\)])/g, Birdify._wrapParenReplacer);
}
if (mode & 2){
text = text.replace(/([A-Za-z0-9]{5,8})([\.+@&\*=])([A-Za-z0-9]{5,8})/g, Birdify._wrapTextReplacer)
.replace(/([A-Za-z0-9]{3,4})([\.+@&\*=])([A-Za-z0-9]{3,4})/g, Birdify._wrapTextReplacer);
}
$(this).replaceWith(text);
});
return ctx;
}
Birdify._transitionToActive = function(bird){
bird.removeClass("bird-candidate")
bird.addClass("birdified");
if (bird.hasClass("bird-candidate-paren")){
bird.removeClass("bird-candidate-paren");
bird.addClass("with-paren");
} else if (bird.hasClass("bird-candidate-text")){
bird.removeClass("bird-candidate-text");
bird.addClass("with-text");
}
if (bird.attr('hasPath') === undefined){
bird.attr('hasPath', true);
bird.addClass(Birdify._selectFlightPath());
}
return;
}
Birdify._transitionToInactive = function(bird){
bird.addClass("bird-candidate")
bird.removeClass("birdified");
if (bird.hasClass("with-paren")){
bird.addClass("bird-candidate-paren");
bird.removeClass("with-paren");
} else if (bird.hasClass("with-text")){
bird.addClass("bird-candidate-text");
ctx.removeClass("with-text");
}
if (bird.attr('hasPath') === undefined){
bird.attr('hasPath', true);
bird.addClass(Birdify._selectFlightPath());
}
return;
}
Birdify._activateBirds = function(ctx, span, delay){
if (delay === undefined){
delay = 0;
}
if (span === undefined){
span = 0;
}
ctx.each(function(){
var bird = $(this);
if (span + delay == 0){
Birdify._transitionToActive(bird);
} else {
var bird_delay = Math.floor(Math.random() * span) + delay;
setTimeout(function(){
Birdify._transitionToActive(bird);
}, bird_delay);
}
});
}
Birdify._deactivateBirds = function(ctx){
ctx.each(function(){
var bird = $(this);
Birdify._transitionToInactive(bird);
});
}