forked from yui/yui3-gallery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-fitvids.js
159 lines (133 loc) · 4.26 KB
/
node-fitvids.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* <p>The FitVids Node Plugin transforms video embeds into fluid width video embeds.
*
* <p>
* <code>
* <script type="text/javascript"> <br>
* <br>
* // Call the "use" method, passing in "gallery-node-fitvids". This will <br>
* // load the script and CSS for the FitVids Node Plugin and all of <br>
* // the required dependencies. <br>
* <br>
* YUI().use('gallery-node-fitvids', 'event-base', function(Y) { <br>
* <br>
* // Use the 'contentready' event to initialize fitvids when <br>
* // the element that contains the video embed<br>
* // is ready to be scripted. <br>
* <br>
* Y.on('contentready', function () { <br>
* <br>
* // The scope of the callback will be a Node instance <br>
* // representing the container element (<div id="container">). <br>
* // Therefore, since "this" represents a Node instance, it <br>
* // is possible to just call "this.plug" passing in a <br>
* // reference to the FitVids Node Plugin. <br>
* <br>
* this.plug(Y.Plugin.NodeFitVids); <br>
* <br>
* }, '#container'); <br>
* <br>
* }); <br>
* <br>
* </script> <br>
* </code>
* </p>
*
* Based on FitVids - https://github.com/davatron5000/FitVids.js
*
* @module gallery-node-fitvids
*/
var CLASS_NAME = Y.ClassNameManager.getClassName('fluid-width-video-wrapper'),
ANCESTOR_SELECTOR = '.' + CLASS_NAME,
DATA_KEY = 'fitvids:originalAttributes',
SELECTORS = [
'iframe[src^="http://player.vimeo.com"]',
'iframe[src^="http://www.youtube.com"]',
'iframe[src^="https://www.youtube.com"]',
'iframe[src^="http://www.kickstarter.com"]',
'object',
'embed'
];
function getSelectors(customSelector) {
var selectors = [].concat(SELECTORS);
if (customSelector) {
selectors.push(customSelector);
}
return selectors.join(',');
}
/**
* FitVids Node Plugin.
*
* @namespace Y.Plugin
* @class NodeFitVids
* @extends Plugin.Base
*/
Y.namespace('Plugin').NodeFitVids = Y.Base.create('NodeFitVids', Y.Plugin.Base, [], {
initializer: function (config) {
var host = this.get('host'),
query = getSelectors(this.get('customSelector'));
if (!Y.instanceOf(host, Y.Node)) {
return;
}
host.all(query).each(function () {
var tagName = this.get('tagName'),
parentNode = this.get('parentNode'),
heightAttr = this.get('height'),
widthAttr = this.get('width'),
data = {},
height,
width,
aspectRatio;
if ((tagName === 'EMBED' && parentNode.get('tagName') === 'OBJECT') || parentNode.hasClass(CLASS_NAME)) {
return;
}
height = tagName === 'OBJECT' ? heightAttr : this.getComputedStyle('height');
width = this.getComputedStyle('width');
aspectRatio = parseInt(height, 10) / parseInt(width, 10);
this.wrap('<div class="' + CLASS_NAME + '"></div>');
this.ancestor(ANCESTOR_SELECTOR).setStyle('paddingTop', (aspectRatio * 100) + '%');
if (heightAttr) {
data.height = heightAttr;
this.removeAttribute('height');
}
if (widthAttr) {
data.width = widthAttr;
this.removeAttribute('width');
}
// Save the original values of the height and width so we can restore them on unplug()
this.setData(DATA_KEY, data);
});
},
destructor: function () {
var host = this.get('host'),
query = getSelectors(this.get('customSelector'));
if (!Y.instanceOf(host, Y.Node)) {
return;
}
host.all(query).each(function () {
var originalDimensions;
if (this.ancestor(ANCESTOR_SELECTOR)) {
this.unwrap();
originalDimensions = this.getData(DATA_KEY) || {};
if (originalDimensions.height) {
this.set('height', originalDimensions.height);
}
if (originalDimensions.width) {
this.set('width', originalDimensions.width);
}
this.clearData(DATA_KEY);
}
});
}
}, {
NS: 'fitvids',
ATTRS: {
/**
* @attribute customSelector
* @description Video vendor selector if none of the default selectors match the the player you wish to target.
* @type String
* @writeOnce
*/
customSelector: null
}
});