Fix for className being wiped out

This commit is contained in:
Sam Potts 2019-04-25 12:01:52 +10:00
parent ad68d9484f
commit f0d3e8c3b9

164
src/js/controls.js vendored
View File

@ -172,7 +172,7 @@ const controls = {
// Create a <button>
createButton(buttonType, attr) {
const attributes = Object.assign({}, attr);
const attributes = extend({}, attr);
let type = toCamelCase(buttonType);
const props = {
@ -198,8 +198,10 @@ const controls = {
// Set class name
if (Object.keys(attributes).includes('class')) {
if (!attributes.class.includes(this.config.classNames.control)) {
attributes.class += ` ${this.config.classNames.control}`;
if (!attributes.class.split(' ').some(c => c === this.config.classNames.control)) {
extend(attributes, {
class: `${attributes.class} ${this.config.classNames.control}`,
});
}
} else {
attributes.class = this.config.classNames.control;
@ -377,13 +379,13 @@ const controls = {
},
// Create time display
createTime(type) {
const attributes = getAttributesFromSelector(this.config.selectors.display[type]);
createTime(type, attrs) {
const attributes = getAttributesFromSelector(this.config.selectors.display[type], attrs);
const container = createElement(
'div',
extend(attributes, {
class: `${this.config.classNames.display.time} ${attributes.class ? attributes.class : ''}`.trim(),
class: `${attributes.class ? attributes.class : ''} ${this.config.classNames.display.time} `.trim(),
'aria-label': i18n.get(type, this.config),
}),
'00:00',
@ -1258,44 +1260,70 @@ const controls = {
},
// Build the default HTML
// TODO: Set order based on order in the config.controls array?
create(data) {
const {
bindMenuItemShortcuts,
createButton,
createProgress,
createRange,
createTime,
setQualityMenu,
setSpeedMenu,
showMenuPanel,
} = controls;
this.elements.controls = null;
// Larger overlaid play button
if (this.config.controls.includes('play-large')) {
this.elements.container.appendChild(createButton.call(this, 'play-large'));
}
// Create the container
const container = createElement('div', getAttributesFromSelector(this.config.selectors.controls.wrapper));
this.elements.controls = container;
// Default item attributes
const defaultAttributes = { class: 'plyr__controls__item' };
// Loop through controls in order
dedupe(this.config.controls).forEach(control => {
// Restart button
if (this.config.controls.includes('restart')) {
container.appendChild(controls.createButton.call(this, 'restart'));
if (control === 'restart') {
container.appendChild(createButton.call(this, 'restart', defaultAttributes));
}
// Rewind button
if (this.config.controls.includes('rewind')) {
container.appendChild(controls.createButton.call(this, 'rewind'));
if (control === 'rewind') {
container.appendChild(createButton.call(this, 'rewind', defaultAttributes));
}
// Play/Pause button
if (this.config.controls.includes('play')) {
container.appendChild(controls.createButton.call(this, 'play'));
if (control === 'play') {
container.appendChild(createButton.call(this, 'play', defaultAttributes));
}
// Fast forward button
if (this.config.controls.includes('fast-forward')) {
container.appendChild(controls.createButton.call(this, 'fast-forward'));
if (control === 'fast-forward') {
container.appendChild(createButton.call(this, 'fast-forward', defaultAttributes));
}
// Progress
if (this.config.controls.includes('progress')) {
if (control === 'progress') {
const progressContainer = createElement('div', {
class: `${defaultAttributes.class} plyr__progress__container`,
});
const progress = createElement('div', getAttributesFromSelector(this.config.selectors.progress));
// Seek range slider
progress.appendChild(
controls.createRange.call(this, 'seek', {
createRange.call(this, 'seek', {
id: `plyr-seek-${data.id}`,
}),
);
// Buffer progress
progress.appendChild(controls.createProgress.call(this, 'buffer'));
progress.appendChild(createProgress.call(this, 'buffer'));
// TODO: Add loop display indicator
@ -1314,32 +1342,45 @@ const controls = {
}
this.elements.progress = progress;
container.appendChild(this.elements.progress);
progressContainer.appendChild(this.elements.progress);
container.appendChild(progressContainer);
}
// Media current time display
if (this.config.controls.includes('current-time')) {
container.appendChild(controls.createTime.call(this, 'currentTime'));
if (control === 'current-time') {
container.appendChild(createTime.call(this, 'currentTime', defaultAttributes));
}
// Media duration display
if (this.config.controls.includes('duration')) {
container.appendChild(controls.createTime.call(this, 'duration'));
if (control === 'duration') {
container.appendChild(createTime.call(this, 'duration', defaultAttributes));
}
// Volume controls
if (this.config.controls.includes('mute') || this.config.controls.includes('volume')) {
const volume = createElement('div', {
class: 'plyr__volume',
});
if (control === 'mute' || control === 'volume') {
let { volume } = this.elements;
// Create the volume container if needed
if (!is.element(volume) || !container.contains(volume)) {
volume = createElement(
'div',
extend({}, defaultAttributes, {
class: `${defaultAttributes.class} plyr__volume`.trim(),
}),
);
this.elements.volume = volume;
container.appendChild(volume);
}
// Toggle mute button
if (this.config.controls.includes('mute')) {
volume.appendChild(controls.createButton.call(this, 'mute'));
if (control === 'mute') {
volume.appendChild(createButton.call(this, 'mute'));
}
// Volume range control
if (this.config.controls.includes('volume')) {
if (control === 'volume') {
// Set the attributes
const attributes = {
max: 1,
@ -1349,7 +1390,7 @@ const controls = {
// Create the volume range slider
volume.appendChild(
controls.createRange.call(
createRange.call(
this,
'volume',
extend(attributes, {
@ -1357,27 +1398,26 @@ const controls = {
}),
),
);
this.elements.volume = volume;
}
container.appendChild(volume);
}
// Toggle captions button
if (this.config.controls.includes('captions')) {
container.appendChild(controls.createButton.call(this, 'captions'));
if (control === 'captions') {
container.appendChild(createButton.call(this, 'captions', defaultAttributes));
}
// Settings button / menu
if (this.config.controls.includes('settings') && !is.empty(this.config.settings)) {
const control = createElement('div', {
class: 'plyr__menu',
if (control === 'settings' && !is.empty(this.config.settings)) {
const control = createElement(
'div',
extend({}, defaultAttributes, {
class: `${defaultAttributes.class} plyr__menu`.trim(),
hidden: '',
});
}),
);
control.appendChild(
controls.createButton.call(this, 'settings', {
createButton.call(this, 'settings', {
'aria-haspopup': true,
'aria-controls': `plyr-settings-${data.id}`,
'aria-expanded': false,
@ -1420,11 +1460,11 @@ const controls = {
);
// Bind menu shortcuts for keyboard users
controls.bindMenuItemShortcuts.call(this, menuItem, type);
bindMenuItemShortcuts.call(this, menuItem, type);
// Show menu on click
on(menuItem, 'click', () => {
controls.showMenuPanel.call(this, type, false);
showMenuPanel.call(this, type, false);
});
const flex = createElement('span', null, i18n.get(type, this.config));
@ -1489,14 +1529,14 @@ const controls = {
event.stopPropagation();
// Show the respective menu
controls.showMenuPanel.call(this, 'home', true);
showMenuPanel.call(this, 'home', true);
},
false,
);
// Go back via button click
on(backButton, 'click', () => {
controls.showMenuPanel.call(this, 'home', false);
showMenuPanel.call(this, 'home', false);
});
// Add to pane
@ -1524,22 +1564,22 @@ const controls = {
}
// Picture in picture button
if (this.config.controls.includes('pip') && support.pip) {
container.appendChild(controls.createButton.call(this, 'pip'));
if (control === 'pip' && support.pip) {
container.appendChild(createButton.call(this, 'pip', defaultAttributes));
}
// Airplay button
if (this.config.controls.includes('airplay') && support.airplay) {
container.appendChild(controls.createButton.call(this, 'airplay'));
if (control === 'airplay' && support.airplay) {
container.appendChild(createButton.call(this, 'airplay', defaultAttributes));
}
// Download button
if (this.config.controls.includes('download')) {
const attributes = {
if (control === 'download') {
const attributes = extend({}, defaultAttributes, {
element: 'a',
href: this.download,
target: '_blank',
};
});
const { download } = this.config.urls;
@ -1550,27 +1590,21 @@ const controls = {
});
}
container.appendChild(controls.createButton.call(this, 'download', attributes));
container.appendChild(createButton.call(this, 'download', attributes));
}
// Toggle fullscreen button
if (this.config.controls.includes('fullscreen')) {
container.appendChild(controls.createButton.call(this, 'fullscreen'));
if (control === 'fullscreen') {
container.appendChild(createButton.call(this, 'fullscreen', defaultAttributes));
}
// Larger overlaid play button
if (this.config.controls.includes('play-large')) {
this.elements.container.appendChild(controls.createButton.call(this, 'play-large'));
}
this.elements.controls = container;
});
// Set available quality levels
if (this.isHTML5) {
controls.setQualityMenu.call(this, html5.getQualityOptions.call(this));
setQualityMenu.call(this, html5.getQualityOptions.call(this));
}
controls.setSpeedMenu.call(this);
setSpeedMenu.call(this);
return container;
},