1
0
mirror of https://github.com/videojs/video.js.git synced 2025-01-27 11:22:06 +02:00

Array traversing for children opts - closes #1070

This commit is contained in:
Gregory Waxman 2014-03-18 15:49:59 -04:00
parent 80d7b5014a
commit 204bb84ecb
4 changed files with 116 additions and 38 deletions

View File

@ -447,7 +447,13 @@ vjs.Component.prototype.initChildren = function(){
var self = this;
// Loop through components and add them to the player
vjs.obj.each(options['children'], function(name, opts){
vjs.each(options['children'], function(name, opts){
//Support for a simpler setup, children with no options
if (typeof name == 'number') {
name = opts;
opts = {};
}
// Allow for disabling default components
// e.g. vjs.options['children']['posterImage'] = false
if (opts === false) return;

View File

@ -44,6 +44,18 @@ vjs.capitalize = function(string){
return string.charAt(0).toUpperCase() + string.slice(1);
};
vjs.each = function (arrLike, fn, context) {
if (vjs.obj.isPlain(arrLike)) vjs.obj.each(arrLike, fn, context);
else {
for (var i = 0, len = arrLike.length; i < len; ++i) {
var val = arrLike[i];
if (vjs.obj.isPlain(val)) vjs.obj.each(val, fn, context);
else fn.call(context || this, i, val);
}
}
};
/**
* Object functions container
* @type {Object}

View File

@ -26,7 +26,7 @@ test('should add a child component', function(){
ok(comp.getChildById(child.id()) === child);
});
test('should init child coponents from options', function(){
test('should init child components from options', function(){
var comp = new vjs.Component(getFakePlayer(), {
children: {
'component': true
@ -37,6 +37,32 @@ test('should init child coponents from options', function(){
ok(comp.el().childNodes.length === 1);
});
test('should init child components from simple children array', function(){
var comp = new vjs.Component(getFakePlayer(), {
children: [
'component',
'component',
'component'
]
});
ok(comp.children().length === 3);
ok(comp.el().childNodes.length === 3);
});
test('should init child components from children array of objects', function(){
var comp = new vjs.Component(getFakePlayer(), {
children: [
{'component':{}},
{'component':{}},
{'component':{}}
]
});
ok(comp.children().length === 3);
ok(comp.el().childNodes.length === 3);
});
test('should do a deep merge of child options', function(){
// Create a default option for component
vjs.Component.prototype.options_ = {

View File

@ -22,13 +22,47 @@ test('should loop through each property on an object', function(){
};
// Add 3 to each value
vjs.obj.each(asdf, function(key, value){
vjs.each(asdf, function(key, value){
asdf[key] = value + 3;
});
deepEqual(asdf,{a:4,b:5,'c':6});
});
test('should loop through simple array', function(){
var asdf = [
'a',
'b',
'c'
];
var newArr = [];
vjs.each(asdf, function(key, value){
newArr[key] = value;
ok(typeof key == 'number', 'Key is not a number, the array index');
ok(typeof value == 'string', 'Value is not a string');
});
deepEqual(asdf,newArr);
});
test('should loop through an array of objects', function(){
var asdf = [
{ a: {} },
{ b: {} },
{ 'c': {} }
];
var newObj = {};
vjs.each(asdf, function(key, value){
newObj[key] = value;
ok(typeof key == 'string', 'Key is not a string');
ok(vjs.obj.isPlain(value), 'Value is not an object');
});
deepEqual(newObj, { a: {}, b: {}, 'c': {}});
});
test('should copy an object', function(){
var asdf = {
a: 1,