forked from davatron5000/FitVids.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyui3.responsivevids.js
113 lines (96 loc) · 2.86 KB
/
yui3.responsivevids.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
YUI.add('responsive-vids', function (Y) {
Y.Plugin.ResponsiveVids = Y.Base.create('responsivevids', Y.Plugin.Base, [], {
initializer: function () {
var customSelector = this.get('customSelector');
var customIgnore = this.get('ignore');
this.selectors = [
"iframe[src*='player.vimeo.com']",
"iframe[src*='youtube.com']",
"iframe[src*='youtube-nocookie.com']",
"iframe[src*='kickstarter.com'][src*='video.html']",
"object",
"embed"
];
this.ignoreList = '.responsivevidsignore';
if (customSelector) {
this.selectors.push(customSelector);
}
if (customIgnore) {
this.ignoreList = this.ignoreList + ',' + customIgnore;
}
Y.all(this.selectors.join(',')).each(function (video) {
if (this._ignore(video) == true) {
return false;
}
var height = video.get('clientHeight');
var width = video.get('clientWidth');
var aspect = !height || !width ? (9 / 16 * 100) : (height / width * 100) + '%';
var widthAttr = video.getAttribute('width');
var heightAttr = video.getAttribute('height');
var wrapper = Y.Node.create('<div class="fluid-width-video-wrapper"></div>');
if (widthAttr) {
video.setAttribute('data-width', widthAttr);
video.removeAttribute('width');
}
if (heightAttr) {
video.setAttribute('data-height', heightAttr);
video.removeAttribute('height');
}
wrapper.append(video.replace(wrapper)).setStyles({
height: 0,
paddingTop: aspect,
position: 'relative'
});
video.setStyles({
width: '100%',
height: '100%',
position: 'absolute',
top: 0,
left: 0
});
}, this);
},
destructor: function () {
Y.all(this.selectors.join(',')).each(function (video) {
if (this._ignore(video) == true) {
return false;
}
var wrapper = video.ancestor('.fluid-width-video-wrapper');
video.setAttribute('width', video.getData('width').setData('width', ''));
video.setAttribute('height', video.getData('height').setData('height', ''));
video.removeAttribute('style')
wrapper.replace(wrapper.getHTML());
}, this);
},
_ignore: function (video) {
if (
video.test(this.ignoreList) ||
video.test('object object') ||
video.ancestor(this.ignoreList)
) {
return true;
}
return false;
}
}, {
NS: 'responsivevids',
ATTRS: {
customSelector: {
value: null // Selector string.
},
ignore: {
value: null // Selector string.
}
}
});
},
'1.0',
{
requires: [
'base',
'node',
'plugin',
'event'
]
}
);