-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathgl-effects-manager.js
63 lines (52 loc) · 1.93 KB
/
gl-effects-manager.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
/*
* GLEffectsManager is a part of HTML GL library for applying effects based on tag attributes
* Copyright (c) 2015 pixelscommander.com
* Distributed under MIT license
* http://htmlgl.com
* */
(function (w) {
var GLEffectsManager = function (element) {
this.element = element;
this.filters = {};
this.initObserver();
this.updateFilters();
}
var p = GLEffectsManager.prototype;
p.initObserver = function () {
var self = this,
config = {
attributes: true,
attributeFilter: ['effects']
};
this.observer = this.observer || new MutationObserver(self.updateFilters.bind(this));
this.observer.observe(this.element, config);
}
p.updateFilters = function () {
var attributeValue = this.element.getAttribute('effects') || '',
effectsNamesArray = attributeValue.split(' ');
this.addFiltersFromAttributes(effectsNamesArray);
this.cleanFiltersFromAttributes(effectsNamesArray);
}
p.addFiltersFromAttributes = function (effectsNamesArray) {
var self = this;
effectsNamesArray.forEach(function (effectName) {
if (HTMLGL.effects[effectName]) {
self.filters[effectName] = self.filters[effectName] || new HTMLGL.effects[effectName](self.element);
}
});
}
p.cleanFiltersFromAttributes = function (effectsNamesArray) {
var self = this;
Object.keys(this.filters).forEach(function (effectName) {
if (effectsNamesArray.indexOf(effectName) === -1 && self.filters[effectName]) {
self.filters[effectName].destroy();
self.filters[effectName] = null;
}
});
}
w.HTMLGL.GLEffectsManager = GLEffectsManager;
//Reinitialize effects on elements
w.HTMLGL.elements.forEach(function (element) {
element.initEffects();
});
})(window);