Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/js/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class ContextMenu {
}
});

this.maskHideHandler = () => {
this.hide();
};
this.player.template.mask.addEventListener('click', this.maskHideHandler);

this.contextmenuHandler = (e) => {
if (this.shown) {
this.hide();
Expand All @@ -23,10 +28,6 @@ class ContextMenu {

const clientRect = this.player.container.getBoundingClientRect();
this.show(event.clientX - clientRect.left, event.clientY - clientRect.top);

this.player.template.mask.addEventListener('click', () => {
this.hide();
});
};
this.player.container.addEventListener('contextmenu', this.contextmenuHandler);
}
Expand Down Expand Up @@ -66,6 +67,7 @@ class ContextMenu {

destroy() {
this.player.container.removeEventListener('contextmenu', this.contextmenuHandler);
this.player.template.mask.removeEventListener('click', this.maskHideHandler);
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ class Controller {
}
} else {
this.player.template.videoWrap.addEventListener('click', () => {
this.toggle();
this.player.toggle();
});
this.player.template.controllerMask.addEventListener('click', () => {
this.toggle();
this.player.toggle();
});
}
}
Expand All @@ -82,7 +82,10 @@ class Controller {
const p = document.createElement('div');
p.classList.add('dplayer-highlight');
p.style.left = (this.player.options.highlight[i].time / this.player.video.duration) * 100 + '%';
p.innerHTML = '<span class="dplayer-highlight-text">' + this.player.options.highlight[i].text + '</span>';
const span = document.createElement('span');
span.className = 'dplayer-highlight-text';
span.textContent = this.player.options.highlight[i].text;
p.appendChild(span);
this.player.template.playedBarWrap.insertBefore(p, this.player.template.playedBarTime);
}
}
Expand Down Expand Up @@ -111,7 +114,7 @@ class Controller {
percentage = Math.max(percentage, 0);
percentage = Math.min(percentage, 1);
this.player.bar.set('played', percentage, 'width');
this.player.template.ptime.innerHTML = utils.secondToTime(percentage * this.player.video.duration);
this.player.template.ptime.textContent = utils.secondToTime(percentage * this.player.video.duration);
};

const thumbUp = (e) => {
Expand Down Expand Up @@ -144,7 +147,7 @@ class Controller {
}
this.thumbnails && this.thumbnails.move(tx);
this.player.template.playedBarTime.style.left = `${tx - (time >= 3600 ? 25 : 20)}px`;
this.player.template.playedBarTime.innerText = utils.secondToTime(time);
this.player.template.playedBarTime.textContent = utils.secondToTime(time);
this.player.template.playedBarTime.classList.remove('hidden');
}
});
Expand Down Expand Up @@ -257,24 +260,22 @@ class Controller {
initAirplayButton() {
if (this.player.options.airplay) {
if (window.WebKitPlaybackTargetAvailabilityEvent) {
this.airplayClickHandler = function () {
this.video.webkitShowPlaybackTargetPicker();
}.bind(this.player);
this.player.template.airplayButton.addEventListener('click', this.airplayClickHandler);

this.player.video.addEventListener(
'webkitplaybacktargetavailabilitychanged',
function (event) {
switch (event.availability) {
case 'available':
this.template.airplayButton.disable = false;
this.template.airplayButton.disabled = false;
break;

default:
this.template.airplayButton.disable = true;
this.template.airplayButton.disabled = true;
}

this.template.airplayButton.addEventListener(
'click',
function () {
this.video.webkitShowPlaybackTargetPicker();
}.bind(this)
);
}.bind(this.player)
);
} else {
Expand Down Expand Up @@ -417,6 +418,9 @@ class Controller {
this.player.container.removeEventListener('mousemove', this.setAutoHideHandler);
this.player.container.removeEventListener('click', this.setAutoHideHandler);
}
if (this.airplayClickHandler) {
this.player.template.airplayButton.removeEventListener('click', this.airplayClickHandler);
}
clearTimeout(this.autoHideTimer);
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/js/danmaku.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Danmaku {
this._opacity = this.options.opacity;
this.events = this.options.events;
this.unlimited = this.options.unlimited;
this._rafId = null;
this._measure('');

this.load();
Expand Down Expand Up @@ -124,7 +125,7 @@ class Danmaku {
}
this.draw(dan);
}
window.requestAnimationFrame(() => {
this._rafId = window.requestAnimationFrame(() => {
this.frame();
});
}
Expand Down Expand Up @@ -213,9 +214,9 @@ class Danmaku {
item.classList.add('dplayer-danmaku-item');
item.classList.add(`dplayer-danmaku-${dan[i].type}`);
if (dan[i].border) {
item.innerHTML = `<span style="border:${dan[i].border}">${dan[i].text}</span>`;
item.innerHTML = `<span style="border:${dan[i].border}">${this.htmlEncode(dan[i].text)}</span>`;
} else {
item.innerHTML = dan[i].text;
item.textContent = dan[i].text;
}
item.style.opacity = this._opacity;
item.style.color = utils.number2Color(dan[i].color);
Expand Down Expand Up @@ -278,9 +279,10 @@ class Danmaku {

_measure(text) {
if (!this.context) {
const measureStyle = getComputedStyle(this.container.getElementsByClassName('dplayer-danmaku-item')[0], null);
const existingItem = this.container.getElementsByClassName('dplayer-danmaku-item')[0];
const font = existingItem ? getComputedStyle(existingItem, null).getPropertyValue('font') : '14px sans-serif';
this.context = document.createElement('canvas').getContext('2d');
this.context.font = measureStyle.getPropertyValue('font');
this.context.font = font;
}
return this.context.measureText(text).width;
}
Expand All @@ -303,7 +305,7 @@ class Danmaku {
bottom: {},
};
this.danIndex = 0;
this.options.container.innerHTML = '';
this.container.innerHTML = '';

this.events && this.events.trigger('danmaku_clear');
}
Expand Down Expand Up @@ -354,6 +356,14 @@ class Danmaku {
};
return animations[position];
}

destroy() {
if (this._rafId) {
window.cancelAnimationFrame(this._rafId);
this._rafId = null;
}
this.clear();
}
}

export default Danmaku;
8 changes: 8 additions & 0 deletions src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Events {
'danmaku_show',
'danmaku_hide',
'danmaku_clear',
'danmaku_load_start',
'danmaku_load_end',
'danmaku_loaded',
'danmaku_send',
'danmaku_opacity',
Expand Down Expand Up @@ -64,6 +66,12 @@ class Events {
}
}

off(name, callback) {
if (this.events[name]) {
this.events[name] = this.events[name].filter((fn) => fn !== callback);
}
}

trigger(name, info) {
if (this.events[name] && this.events[name].length) {
for (let i = 0; i < this.events[name].length; i++) {
Expand Down
Loading