add scheme

This commit is contained in:
Harvey Zack committed 2018-10-26 10:29:27 +08:00
1 parent 4da0ac7650
commit 5d48d3a437
8 files changed
+332 -160

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
import './style';
import 'balloon-css/balloon.min.css';
import { deepMerge } from './utils';
import validOption from './verification/player';
import validOption from './verification';
import config from './config';
import Emitter from 'tiny-emitter';
import Template from './template';
@@ -27,7 +27,7 @@ class Artplayer extends Emitter {
super();
this.emit('init:start');
this.option = deepMerge({}, Artplayer.DEFAULTS, option);
validOption(Artplayer.DEFAULTS, this.option);
validOption(this.option);
this.init();
this.emit('init:end');
}
+3 -3
View File
@@ -20,9 +20,9 @@ export default class Player {
$video.volume = clamp(option.volume, 0, 1);
$video.poster = option.poster;
$video.autoplay = option.autoplay;
sleep().then(() => {
$video.src = option.url;
});
// TODO
$video.src = option.url;
}
eventBind() {
+30
View File
@@ -0,0 +1,30 @@
import { errorHandle, getType } from '../utils';
import scheme from './scheme';
export default function validOption(option, param = scheme, path = ['option']) {
Object.keys(option).some(key => {
if (!param[key]) {
return true;
}
const value = option[key];
const type = getType(value);
const requiredType = param[key].type;
const isRequired = param[key].required;
const requiredChild = param[key].child;
if (type === 'object' && requiredChild) {
validOption(value, requiredChild, path.concat(key));
}
if (type === 'array' && requiredChild) {
value.forEach((item, index) => {
validOption(item, requiredChild, path.concat(`${key}[${index}]`));
});
}
errorHandle(!isRequired || value, `'${path.join('.')}.${key}' is required`);
errorHandle(requiredType === type, `'${path.join('.')}.${key}' require '${requiredType}' type, but got '${type}'`);
return false;
});
}
-87
View File
@@ -1,87 +0,0 @@
import { errorHandle, getType } from '../utils';
export default function validOption(DEFAULTS, option, path = ['option']) {
Object.keys(DEFAULTS).forEach(key => {
const defaultValue = DEFAULTS[key];
const value = option[key];
const defaultType = getType(defaultValue);
const type = getType(value);
if (key === 'container' || key === 'html') {
errorHandle(
type === 'string' || value instanceof Element,
`'${path.join('.')}.${key}' require 'string' or 'Element' type, but got '${type}'`
);
} else {
errorHandle(
defaultType === type,
`'${path.join('.')}.${key}' require '${defaultType}' type, but got '${type}'`
);
}
if (type === 'object') {
validOption(defaultValue, value, path.concat(key));
}
if (path.length === 1) {
if (key === 'layers') {
value.forEach((item, index) => {
validOption(
{
name: '',
index: 0,
html: '',
style: {}
},
item,
path.concat(`${key}[${index}]`)
);
});
}
if (key === 'contextmenu') {
value.forEach((item, index) => {
validOption(
{
name: '',
click: new Function(),
html: '',
style: {}
},
item,
path.concat(`${key}[${index}]`)
);
});
}
if (key === 'highlight') {
value.forEach((item, index) => {
validOption(
{
time: 0,
text: ''
},
item,
path.concat(`${key}[${index}]`)
);
});
}
if (key === 'controls') {
value.forEach((item, index) => {
if (item.option) {
validOption(
{
disable: false,
position: '',
index: 0
},
item.option,
path.concat(`${key}[${index}]`)
);
}
});
}
}
});
}
+132
View File
@@ -0,0 +1,132 @@
export default {
container: {
type: 'string',
required: true
},
url: {
type: 'string',
required: true
},
poster: {
type: 'string'
},
volume: {
type: 'number'
},
thumbnails: {
type: 'object',
child: {
url: {
type: 'string'
},
number: {
type: 'number'
},
width: {
type: 'number'
},
height: {
type: 'number'
},
column: {
type: 'number'
}
}
},
autoplay: {
type: 'boolean'
},
loop: {
type: 'boolean'
},
type: {
type: 'string'
},
mimeCodec: {
type: 'string'
},
layers: {
type: 'array',
child: {
name: {
type: 'string'
},
index: {
type: 'number'
},
html: {
type: 'string'
},
style: {
type: 'object'
}
}
},
contextmenu: {
type: 'array',
child: {
name: {
type: 'string'
},
html: {
type: 'string'
},
click: {
type: 'function'
}
}
},
loading: {
type: 'string'
},
theme: {
type: 'string'
},
hotkey: {
type: 'boolean'
},
subtitle: {
type: 'object',
child: {
url: {
type: 'string'
},
style: {
type: 'object'
}
}
},
controls: {
type: 'array',
child: {
option: {
disable: {
type: 'boolean'
},
position: {
type: 'string'
},
index: {
type: 'number'
}
}
}
},
highlight: {
type: 'array',
child: {
time: {
type: 'number'
},
text: {
type: 'string'
}
}
},
moreVideoAttr: {
type: 'object'
},
lang: {
type: 'string'
}
};