Skip to content

Commit

Permalink
user module; unlimited danmaku setting
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 10, 2017
1 parent 0bad7b7 commit 6dd8e6f
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dist/DPlayer.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/DPlayer.min.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/DPlayer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/DPlayer.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dplayer",
"version": "1.9.1",
"version": "1.10.0",
"description": "Wow, such a lovely HTML5 danmaku video player",
"main": "dist/DPlayer.min.js",
"style": "dist/DPlayer.min.css",
Expand Down
46 changes: 38 additions & 8 deletions src/DPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Danmaku from './danmaku';
import Thumbnails from './thumbnails';
import Events from './events';
import FullScreen from './fullscreen';
import User from './user';

let index = 0;

Expand All @@ -34,6 +35,8 @@ class DPlayer {

this.events = new Events();

this.user = new User(this);

this.element = this.option.element;
if (!this.option.danmaku) {
this.element.classList.add('dplayer-no-danmaku');
Expand Down Expand Up @@ -76,7 +79,7 @@ class DPlayer {
if (this.option.danmaku) {
this.danmaku = new Danmaku({
container: this.element.getElementsByClassName('dplayer-danmaku')[0],
opacity: localStorage.getItem('danmaku-opacity') || 0.7,
opacity: this.user.get('opacity'),
callback: () => {
this.element.getElementsByClassName('dplayer-danloading')[0].style.display = 'none';

Expand All @@ -95,7 +98,7 @@ class DPlayer {
borderColor: this.option.theme,
height: this.arrow ? 24 : 30,
time: () => this.video.currentTime,
unlimited: this.option.danmaku.unlimited,
unlimited: this.user.get('unlimited'),
api: {
id: this.option.danmaku.id,
address: this.option.danmaku.api,
Expand Down Expand Up @@ -384,7 +387,11 @@ class DPlayer {
});

this.loop = this.option.loop;
let showdan = true;
let showdan = this.user.get('danmaku');
if (!showdan) {
this.danmaku && this.danmaku.hide();
}
let unlimitDan = this.user.get('unlimited');
const settingEvent = () => {
// loop control
const loopEle = this.element.getElementsByClassName('dplayer-setting-loop')[0];
Expand Down Expand Up @@ -421,6 +428,27 @@ class DPlayer {
showdan = false;
this.danmaku.hide();
}
this.user.set('danmaku', showdan ? 1 : 0);
closeSetting();
});

// unlimited danmaku control
const unlimitDanEle = this.element.getElementsByClassName('dplayer-setting-danunlimit')[0];
const unlimitDanToggle = unlimitDanEle.getElementsByClassName('dplayer-danunlimit-setting-input')[0];

unlimitDanToggle.checked = unlimitDan;

unlimitDanEle.addEventListener('click', () => {
unlimitDanToggle.checked = !unlimitDanToggle.checked;
if (unlimitDanToggle.checked) {
unlimitDan = true;
this.danmaku.unlimit(true);
}
else {
unlimitDan = false;
this.danmaku.unlimit(false);
}
this.user.set('unlimited', unlimitDan ? 1 : 0);
closeSetting();
});

Expand All @@ -446,14 +474,17 @@ class DPlayer {
const danmakuBarWrap = this.element.getElementsByClassName('dplayer-danmaku-bar')[0];
const danmakuSettingBox = this.element.getElementsByClassName('dplayer-setting-danmaku')[0];
const dWidth = 130;
this.updateBar('danmaku', this.danmaku.opacity(), 'width');
this.on('danmaku_opacity', (percentage) => {
this.updateBar('danmaku', percentage, 'width');
this.user.set('opacity', percentage);
});
this.danmaku.opacity(this.user.get('opacity'));

const danmakuMove = (event) => {
const e = event || window.event;
let percentage = (e.clientX - utils.getElementViewLeft(danmakuBarWrap)) / dWidth;
percentage = percentage > 0 ? percentage : 0;
percentage = percentage < 1 ? percentage : 1;
this.updateBar('danmaku', percentage, 'width');
this.danmaku.opacity(percentage);
};
const danmakuUp = () => {
Expand All @@ -467,7 +498,6 @@ class DPlayer {
let percentage = (e.clientX - utils.getElementViewLeft(danmakuBarWrap)) / dWidth;
percentage = percentage > 0 ? percentage : 0;
percentage = percentage < 1 ? percentage : 1;
this.updateBar('danmaku', percentage, 'width');
this.danmaku.opacity(percentage);
});
danmakuBarWrapWrap.addEventListener('mousedown', () => {
Expand Down Expand Up @@ -819,7 +849,7 @@ class DPlayer {
percentage = percentage < 1 ? percentage : 1;
this.updateBar('volume', percentage, 'width');
if (!nostorage) {
localStorage.setItem('dplayer-volume', percentage);
this.user.set('volume', percentage);
}
if (!nonotice) {
this.notice(`${this.tran('Volume')} ${(percentage * 100).toFixed(0)}%`);
Expand Down Expand Up @@ -975,7 +1005,7 @@ class DPlayer {
});
}

this.volume(localStorage.getItem('dplayer-volume') || this.option.volume, true, true);
this.volume(this.user.get('volume'), true, true);
}

switchQuality (index) {
Expand Down
3 changes: 2 additions & 1 deletion src/DPlayer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
&.dplayer-no-danmaku {
.dplayer-controller .dplayer-icons .dplayer-setting .dplayer-setting-box {
.dplayer-setting-showdan,
.dplayer-setting-danmaku {
.dplayer-setting-danmaku,
.dplayer-setting-danunlimit {
display: none;
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/danmaku.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Danmaku {
this.showing = true;
this._opacity = this.options.opacity;
this.events = this.options.events;
this.unlimited = this.options.unlimited;
this._measure('');

this.load();
}
Expand Down Expand Up @@ -124,7 +126,6 @@ class Danmaku {
items[i].style.opacity = percentage;
}
this._opacity = percentage;
localStorage.setItem('danmaku-opacity', this._opacity);

this.events && this.events.trigger('danmaku_opacity', this._opacity);
}
Expand Down Expand Up @@ -156,7 +157,7 @@ class Danmaku {
const getTunnel = (ele, type, width) => {
const tmp = danWidth / danSpeed(width);

for (let i = 0; this.options.unlimited || i < itemY; i++) {
for (let i = 0; this.unlimited || i < itemY; i++) {
const item = this.danTunnel[type][i + ''];
if (item && item.length) {
if (type !== 'right') {
Expand Down Expand Up @@ -330,6 +331,10 @@ class Danmaku {

this.events && this.events.trigger('danmaku_show');
}

unlimit (boolean) {
this.unlimited = boolean;
}
}

module.exports = Danmaku;
11 changes: 9 additions & 2 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,24 @@ const html = {
</div>
</div>
<div class="dplayer-setting-item dplayer-setting-showdan">
<span class="dplayer-label">${tran('Danmaku')}</span>
<span class="dplayer-label">${tran('Show danmaku')}</span>
<div class="dplayer-toggle">
<input class="dplayer-showdan-setting-input" type="checkbox" name="dplayer-toggle-dan">
<label for="dplayer-toggle-dan"></label>
</div>
</div>
<div class="dplayer-setting-item dplayer-setting-danunlimit">
<span class="dplayer-label">${tran('Unlimited danmaku')}</span>
<div class="dplayer-toggle">
<input class="dplayer-danunlimit-setting-input" type="checkbox" name="dplayer-toggle-danunlimit">
<label for="dplayer-toggle-danunlimit"></label>
</div>
</div>
<div class="dplayer-setting-item dplayer-setting-danmaku">
<span class="dplayer-label">${tran('Opacity for danmaku')}</span>
<div class="dplayer-danmaku-bar-wrap">
<div class="dplayer-danmaku-bar">
<div class="dplayer-danmaku-bar-inner" style="width: ${(localStorage.getItem('DPlayer-opacity') || 0.7) * 100}%">
<div class="dplayer-danmaku-bar-inner">
<span class="dplayer-thumb"></span>
</div>
</div>
Expand Down
10 changes: 6 additions & 4 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ const tranTxt = {
'Please input danmaku content!': '要输入弹幕内容啊喂!',
'Set danmaku color': '设置弹幕颜色',
'Set danmaku type': '设置弹幕类型',
'Danmaku': '弹幕',
'Show danmaku': '显示弹幕',
'This video fails to load': '视频加载失败',
'Switching to': '正在切换至',
'Switched to': '已经切换至',
'quality': '画质',
'FF to': '快进至',
'REW to': '快退至'
'REW to': '快退至',
'Unlimited danmaku': '海量弹幕'
},
"zh-tw" : {
'Danmaku is loading': '彈幕加載中',
Expand All @@ -61,12 +62,13 @@ const tranTxt = {
'Please input danmaku content!': '請輸入彈幕内容啊!',
'Set danmaku color': '設置彈幕顏色',
'Set danmaku type': '設置彈幕類型',
'Danmaku': '彈幕',
'Show danmaku': '顯示彈幕',
'This video fails to load': '視頻加載失敗',
'Switching to': '正在切換至',
'Switched to': '已經切換至',
'quality': '畫質',
'FF to': '快進至',
'REW to': '快退至'
'REW to': '快退至',
'Unlimited danmaku': '海量彈幕'
}
};
39 changes: 39 additions & 0 deletions src/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import utils from './utils';

class User {
constructor (player) {
this.storageName = {
opacity: 'dplayer-danmaku-opacity',
volume: 'dplayer-volume',
unlimited: 'dplayer-danmaku-unlimited',
danmaku: 'dplayer-danmaku-show'
};
this.default = {
opacity: 0.7,
volume: player.option.volume || 0.7,
unlimited: (player.option.danmaku && player.option.danmaku.unlimited ? 1 : 0) || 0,
danmaku: 1
};
this.data = {};

this.init();
}

init () {
for (const item in this.storageName) {
const name = this.storageName[item];
this.data[item] = parseFloat(utils.storage.get(name) || this.default[item]);
}
}

get (key) {
return this.data[key];
}

set (key, value) {
this.data[key] = value;
utils.storage.set(this.storageName[key], value);
}
}

module.exports = User;
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ module.exports = {
/**
* check if user is using mobile or not
*/
isMobile: /mobile/i.test(window.navigator.userAgent)
isMobile: /mobile/i.test(window.navigator.userAgent),

storage: {
set: (key, value) => {
localStorage.setItem(key, value);
},

get: (key) => localStorage.getItem(key)
}

};

0 comments on commit 6dd8e6f

Please sign in to comment.