-
Notifications
You must be signed in to change notification settings - Fork 7
/
cssanimationstore.js
136 lines (108 loc) · 2.77 KB
/
cssanimationstore.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
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
/**
* CSS Animation Store v0.1
* JSON style interface to CSS Animations
*
* Copyright 2011, Joe Lambert (http://www.joelambert.co.uk).
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function(){
var KeyframeRule = function(r) {
this.original = r;
this.keyText = r.keyText;
this.css = r.css();
};
var KeyframeAnimation = function(kf) {
var _this = this;
this.original = kf;
this.name = kf.name;
this.keyframes = [];
var keytexts = [],
keyframeHash = {},
/**
* Makes the rule indexable
* @param {WebKitKeyframeRule} r The CSSOM keyframe rule
* @returns undefined
*/
indexRule = function(r) {
var rule = new KeyframeRule(r);
_this.keyframes.push(rule);
keytexts.push(rule.keyText);
keyframeHash[rule.keyText] = rule;
},
/**
* Initialises the object
* @returns undefined
*/
init = function() {
_this.keyframes = [];
keytexts = [];
keyframeHash = {};
for(var i=0; i<kf.cssRules.length; i++) {
indexRule(kf.cssRules[i]);
}
};
init();
this.getKeyframeTexts = function() {
return keytexts;
};
this.getKeyframe = function(text) {
return keyframeHash[text];
};
this.setKeyframe = function(text, css) {
var cssRule = text+" {";
for(var k in css) {
cssRule += k+':'+css[k]+';';
}
cssRule += "}";
_this.original.insertRule(cssRule);
init();
};
};
var trim = function(str) {
str = str || "";
return str.replace(/^\s+|\s+$/g,"");
};
var prefix = "",
prefixes = ['WebKit', 'Moz'];
for(var i=0; i<prefixes.length; i++) {
if(window[prefixes[i]+'CSSKeyframeRule'])
prefix = prefixes[i];
}
window[prefix+'CSSKeyframeRule'].prototype.css = function() {
var css = {};
var rules = this.style.cssText.split(';');
for(var i=0; i<rules.length; i++) {
var parts = rules[i].split(':'),
key = trim(parts[0]),
value = trim(parts[1]);
if(key !== '' && value !== '')
css[key] = value;
}
return css;
};
var CSSAnimationStore = function(){
// Find all the animations
var animations = (function() {
var ss = document.styleSheets,
anims = {};
for (var i = ss.length - 1; i >= 0; i--) {
try {
var s = ss[i],
rs = s.cssRules ? s.cssRules :
s.rules ? s.rules :
[];
for (var j = rs.length - 1; j >= 0; j--) {
if ((rs[j].type === window.CSSRule.WEBKIT_KEYFRAMES_RULE || rs[j].type === window.CSSRule.MOZ_KEYFRAMES_RULE)){
anims[rs[j].name] = new KeyframeAnimation(rs[j]);
}
}
}
catch(e) { /* Trying to interrogate a stylesheet from another domain will throw a security error */ }
}
return anims;
})();
return animations;
};
window.CSSAnimations = new CSSAnimationStore();
})();