Skip to content

Commit

Permalink
Merge branch 'Akkuma-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
heff committed May 6, 2014
2 parents d96e008 + e7617bf commit a5bc688
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 46 deletions.
73 changes: 52 additions & 21 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,31 +439,62 @@ vjs.Component.prototype.removeChild = function(component){
* myChildOption: true
* }
* }
*
* // Or when creating the component
* var myComp = new MyComponent(player, {
* children: {
* myChildComponent: {
* myChildOption: true
* }
* }
* });
*
* The children option can also be an Array of child names or
* child options objects (that also include a 'name' key).
*
* var myComp = new MyComponent(player, {
* children: [
* 'button',
* {
* name: 'button',
* someOtherOption: true
* }
* ]
* });
*
*/
vjs.Component.prototype.initChildren = function(){
var options = this.options_;

if (options && options['children']) {
var self = this;

// Loop through components and add them to the player
vjs.obj.each(options['children'], function(name, opts){
// Allow for disabling default components
// e.g. vjs.options['children']['posterImage'] = false
if (opts === false) return;
var parent, children, child, name, opts;

parent = this;
children = this.options()['children'];

if (children) {
// Allow for an array of children details to passed in the options
if (children instanceof Array) {
for (var i = 0; i < children.length; i++) {
child = children[i];

if (typeof child == 'string') {
name = child;
opts = {};
} else {
name = child.name;
opts = child;
}

parent[name] = parent.addChild(name, opts);
}
} else {
vjs.obj.each(children, function(name, opts){
// Allow for disabling default components
// e.g. vjs.options['children']['posterImage'] = false
if (opts === false) return;

// Allow waiting to add components until a specific event is called
var tempAdd = function(){
// Set property name on player. Could cause conflicts with other prop names, but it's worth making refs easy.
self[name] = self.addChild(name, opts);
};

if (opts['loadEvent']) {
// this.one(opts.loadEvent, tempAdd)
} else {
tempAdd();
}
});
parent[name] = parent.addChild(name, opts);
});
}
}
};

Expand Down
48 changes: 24 additions & 24 deletions src/js/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ vjs.obj = {};
* @param {Object} obj Object to use as prototype
* @private
*/
vjs.obj.create = Object.create || function(obj){
vjs.obj.create = Object.create || function(obj){
//Create a new function called 'F' which is just an empty object.
function F() {}

Expand Down Expand Up @@ -669,33 +669,33 @@ vjs.log = function(){
// Offset Left
// getBoundingClientRect technique from John Resig http://ejohn.org/blog/getboundingclientrect-is-awesome/
vjs.findPosition = function(el) {
var box, docEl, body, clientLeft, scrollLeft, left, clientTop, scrollTop, top;
var box, docEl, body, clientLeft, scrollLeft, left, clientTop, scrollTop, top;

if (el.getBoundingClientRect && el.parentNode) {
box = el.getBoundingClientRect();
}
if (el.getBoundingClientRect && el.parentNode) {
box = el.getBoundingClientRect();
}

if (!box) {
return {
left: 0,
top: 0
};
}
if (!box) {
return {
left: 0,
top: 0
};
}

docEl = document.documentElement;
body = document.body;
docEl = document.documentElement;
body = document.body;

clientLeft = docEl.clientLeft || body.clientLeft || 0;
scrollLeft = window.pageXOffset || body.scrollLeft;
left = box.left + scrollLeft - clientLeft;
clientLeft = docEl.clientLeft || body.clientLeft || 0;
scrollLeft = window.pageXOffset || body.scrollLeft;
left = box.left + scrollLeft - clientLeft;

clientTop = docEl.clientTop || body.clientTop || 0;
scrollTop = window.pageYOffset || body.scrollTop;
top = box.top + scrollTop - clientTop;
clientTop = docEl.clientTop || body.clientTop || 0;
scrollTop = window.pageYOffset || body.scrollTop;
top = box.top + scrollTop - clientTop;

// Android sometimes returns slightly off decimal values, so need to round
return {
left: vjs.round(left),
top: vjs.round(top)
};
// Android sometimes returns slightly off decimal values, so need to round
return {
left: vjs.round(left),
top: vjs.round(top)
};
};
28 changes: 27 additions & 1 deletion test/unit/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: [
{ 'name': 'component' },
{ 'name': 'component' },
{ 'name': '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_ = {
Expand Down

0 comments on commit a5bc688

Please sign in to comment.