Skip to content

Commit

Permalink
Adds a Receiver and fixes a few things to bring it closer to the Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
screeley committed Dec 11, 2013
1 parent 50ee41d commit 2bc7906
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 151 deletions.
7 changes: 5 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ module.exports = function(grunt) {
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n'
},
local: {
src: ['src/core.js', 'src/keeper.js', 'src/player.js'],
src: ['src/core.js', 'src/keeper.js', 'src/player.js', 'src/receiver.js'],
dest: 'dist/player.js'
},
release: {
src: ['src/intro.js', 'src/core.js', 'src/keeper.js', 'src/player.js', 'src/outro.js'],
src: [
'src/intro.js', 'src/core.js', 'src/keeper.js',
'src/player.js', 'src/receiver.js', 'src/outro.js'
],
dest: 'dist/player-<%= pkg.version %>.js'
}
},
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ player object. However, the player will internally queue messages until
ready is called.
::

var player = Player('iframe');
var player = new playerjs.Player('iframe');

player.on(Player.Events.PLAY, function(
player.on(playerjs.Events.PLAY, function(
console.log('play');
));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Player.js",
"name": "Player.js",
"version": "0.0.2",
"version": "0.0.3",
"homepage": "http://github.com/embedly/player.js",
"author": {
"name": "Embedly"
Expand Down
27 changes: 16 additions & 11 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var playjs = {};
var playerjs = {};

playjs.DEBUG = false;
playjs.POST_MESSAGE = !!window.postMessage;
playerjs.DEBUG = false;
playerjs.POST_MESSAGE = !!window.postMessage;

/*
* Utils.
*/
playjs.origin = function(url){
playerjs.origin = function(url){
// Grab the origin of a URL
if (url.substr(0, 2) === '//'){
url = window.location.protocol + url;
Expand All @@ -15,7 +15,7 @@ playjs.origin = function(url){
return url.split('/').slice(0,3).join('/');
};

playjs.addEvent = function(elem, type, eventHandle) {
playerjs.addEvent = function(elem, type, eventHandle) {
if (!elem) { return; }
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
Expand All @@ -28,19 +28,24 @@ playjs.addEvent = function(elem, type, eventHandle) {

// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
playjs.log = function(){
playjs.log.history = playjs.log.history || []; // store logs to an array for reference
playjs.log.history.push(arguments);
if(window.console && playjs.DEBUG){
playerjs.log = function(){
playerjs.log.history = playerjs.log.history || []; // store logs to an array for reference
playerjs.log.history.push(arguments);
if(window.console && playerjs.DEBUG){
window.console.log( Array.prototype.slice.call(arguments) );
}
};

// isFunctions
playjs.isString = function (obj) {
playerjs.isString = function (obj) {
return Object.prototype.toString.call(obj) === '[object String]';
};

playjs.isNone = function(obj){
playerjs.isObject = function(obj){
return Object.prototype.toString.call(obj) === "[object Object]";
};


playerjs.isNone = function(obj){
return (obj === null || obj === undefined);
};
28 changes: 14 additions & 14 deletions src/keeper.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/*globals playjs:true*/
/*globals playerjs:true*/
/*
* Keeper is just a method for keeping track of all the callbacks.
*/

playjs.Keeper = function(){
playerjs.Keeper = function(){
this.init();
};

playjs.Keeper.prototype.init = function(){
playerjs.Keeper.prototype.init = function(){
this.data = {};
};

playjs.Keeper.prototype.getUUID = function(){
playerjs.Keeper.prototype.getUUID = function(){
// Create a random id. #http://stackoverflow.com/a/2117523/564191
return 'listener-xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};

playjs.Keeper.prototype.has = function(event, id){
playerjs.Keeper.prototype.has = function(event, id){
if (!this.data.hasOwnProperty(event)){
return false;
}

if (playjs.isNone(id)){
if (playerjs.isNone(id)){
return true;
}

Expand All @@ -40,7 +40,7 @@ playjs.Keeper.prototype.has = function(event, id){
return false;
};

playjs.Keeper.prototype.add = function(id, event, cb, ctx, one){
playerjs.Keeper.prototype.add = function(id, event, cb, ctx, one){
var d = {
id: id,
event: event,
Expand All @@ -56,7 +56,7 @@ playjs.Keeper.prototype.add = function(id, event, cb, ctx, one){
}
};

playjs.Keeper.prototype.execute = function(event, id, data){
playerjs.Keeper.prototype.execute = function(event, id, data){
if (!this.has(event, id)){
return false;
}
Expand All @@ -67,7 +67,7 @@ playjs.Keeper.prototype.execute = function(event, id, data){
for (var i=0; i< this.data[event].length; i++){
var d = this.data[event][i];

if (!playjs.isNone(id) && d.id !== id){
if (!playerjs.isNone(id) && d.id !== id){
continue;
}

Expand Down Expand Up @@ -96,15 +96,15 @@ playjs.Keeper.prototype.execute = function(event, id, data){
}
};

playjs.Keeper.prototype.on = function(id, event, cb, ctx){
playerjs.Keeper.prototype.on = function(id, event, cb, ctx){
this.add(id, event, cb, ctx, false);
};

playjs.Keeper.prototype.one = function(id, event, cb, ctx){
playerjs.Keeper.prototype.one = function(id, event, cb, ctx){
this.add(id, event, cb, ctx, true);
};

playjs.Keeper.prototype.off = function(event, cb){
playerjs.Keeper.prototype.off = function(event, cb){
// We should probably restructure so this is a bit less of a pain.
var listeners = [];

Expand All @@ -118,9 +118,9 @@ playjs.Keeper.prototype.off = function(event, cb){
for (var i=0; i< this.data[event].length; i++){
var data = this.data[event][i];
// If we only keep if there was a CB and the CB is there.
if (!playjs.isNone(cb) && data.cb !== cb) {
if (!playerjs.isNone(cb) && data.cb !== cb) {
keep.push(data);
} else if (!playjs.isNone(data.id)) {
} else if (!playerjs.isNone(data.id)) {
listeners.push(data.id);
}
}
Expand Down
Loading

0 comments on commit 2bc7906

Please sign in to comment.