Skip to content

Commit

Permalink
Adds version, a test and updates the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
screeley committed May 7, 2014
1 parent bcf1900 commit 5f3654a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 15 deletions.
2 changes: 0 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,4 @@ module.exports = function(grunt) {
grunt.registerTask("test", ["connect", "qunit"]);
grunt.registerTask("default", ["concat:local", "connect:parent", "connect:child", "watch"]);
grunt.registerTask("release", ["jshint", "test", "concat:release", "uglify:release", "s3:release"]);


};
50 changes: 44 additions & 6 deletions SPEC.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ These are ``play``, ``pause`` and ``getDuration`` are all examples of methods.
They have a common format::

{
context: 'player.js',
version: 'version'
method: 'methodName',
value: 'methodValue',
listener: 'listenerName'
listener: 'listenerName',
}

``context``
The context of the postMessage data to avoid conflicts. Will always be
``player.js``.

``version``
The version of the library that you are using. Currently this is ``0.0.10``.

``method``
The method name that we wish to invoke.

Expand All @@ -79,6 +88,8 @@ following message to the parent.
::

{
context: 'player.js',
version: '0.0.10',
event: 'methodName',
listener: 'listenerName',
value: 'returnValue'
Expand All @@ -94,6 +105,8 @@ No events should be passed back unless the parent explicitly asks for them. To
add a listener we send the following message::

{
context: 'player.js',
version: 'version',
method: 'addEventListener',
value: 'event',
listener: 'listenerName'
Expand All @@ -102,6 +115,8 @@ add a listener we send the following message::
When the event is fired, the parent will receive the following event data::

{
context: 'player.js',
version: 'version',
event: 'event',
listener: 'listenerName',
value: {}
Expand All @@ -116,6 +131,8 @@ It's helpful to have a quick example of the JavaScript before moving forward.
// Play the video
document.getElementById('#iframe').contentWindow.postMessage(
JSON.stringify({
context: 'player.js',
version: 'version',
method: 'play'
})
);
Expand All @@ -125,23 +142,26 @@ It's helpful to have a quick example of the JavaScript before moving forward.
var iframe = document.getElementById('#iframe'),
origin = iframe.src.split('/', 3).join('/');

var play = function(){
console.log('play);
var played = function(){
console.log('played');
};

window.addEventListener('message', function(){
if (e.origin === origin){
if (e.event === play){
var data = JSON.parse(e.data);
if (data.context === 'player.js' && data.event === play){
played();
}
}
});

iframe.contentWindow.postMessage(
JSON.stringify({
context: 'player.js',
version: 'version',
method: 'addEventListener',
value: 'event'
})
value: 'play'
});
);


Expand Down Expand Up @@ -248,6 +268,8 @@ Methods
that listener, otherwise remove all listeners::

{
context: 'player.js',
version: 'version',
method: 'removeEventListener',
value: 'event',
listener: 'listenerName'
Expand All @@ -259,6 +281,8 @@ Methods
Add an event listener::

{
context: 'player.js',
version: 'version',
method: 'addEventListener',
value: 'event',
listener: 'listenerName'
Expand All @@ -274,6 +298,8 @@ Events that can be listened to.
of listening to the event.::

{
context: 'player.js',
version: 'version',
event: 'ready',
value: {
src: 'srcOfIframe',
Expand Down Expand Up @@ -314,6 +340,8 @@ Events that can be listened to.
Fires when the media is loading additional media for playback::

{
context: 'player.js',
version: 'version',
event: 'progress',
value: {
seconds: 10,
Expand All @@ -325,6 +353,8 @@ Events that can be listened to.
Fires during playback::

{
context: 'player.js',
version: 'version',
event: 'timeupdate',
value: {
seconds: 10,
Expand All @@ -336,27 +366,35 @@ Events that can be listened to.
Fires when the video starts to play::

{
context: 'player.js',
version: 'version',
event: 'play',
}

``pause``
Fires when the video is paused::

{
context: 'player.js',
version: 'version',
event: 'pause',
}

``ended``
Fires when the video has ended::

{
context: 'player.js',
version: 'version',
event: 'ended',
}

``error``
Fires when something goes wrong::

{
context: 'player.js',
version: 'version',
event: 'error',
value: {
code: -1
Expand Down
8 changes: 7 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
var playerjs = {};

playerjs.DEBUG = false;
playerjs.VERSION = '0.0.10';
playerjs.CONTEXT = 'player.js';
playerjs.POST_MESSAGE = !!window.postMessage;
playerjs.POST_MESSAGE_CONTEXT = 'player.js';

// Currently, CONTEXT will render a number of providers useless until they
// upgrade their receivers. It's a bit of a chicken in the egg problem, so
// the 0.0.10 release will include this flag.
playerjs.ENABLE_CONTEXT = false;

/*
* Utils.
Expand Down
14 changes: 10 additions & 4 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ playerjs.Player.prototype.init = function(elem, options){
};

playerjs.Player.prototype.send = function(data, callback, ctx){
data.context = playerjs.POST_MESSAGE_CONTEXT;
// Add the context and version to the data.
data.context = playerjs.CONTEXT;
data.version = playerjs.VERSION;

// We are expecting a response.
if (callback) {
Expand Down Expand Up @@ -152,7 +154,7 @@ playerjs.Player.prototype.receive = function(e){
}

// abort if this message wasn't a player.js message
if (data.context !== playerjs.POST_MESSAGE_CONTEXT) {
if (playerjs.ENABLE_CONTEXT && data.context !== playerjs.CONTEXT) {
return false;
}

Expand Down Expand Up @@ -308,9 +310,13 @@ playerjs.addEvent(window, 'message', function(e){
return false;
}

// abort if this message wasn't a player.js message
if (playerjs.ENABLE_CONTEXT && data.context !== playerjs.CONTEXT) {
return false;
}

// We need to determine if we are ready.
if (data.context === playerjs.POST_MESSAGE_CONTEXT && data.event === 'ready' &&
data.value && data.value.src){
if (data.event === 'ready' && data.value && data.value.src){
playerjs.READIED.push(data.value.src);
}
});
11 changes: 9 additions & 2 deletions src/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ playerjs.Receiver.prototype.receive = function(e){
playerjs.log('Receiver.receive', e, data);

// Nothing for us to do.
if (data.context !== playerjs.POST_MESSAGE_CONTEXT || !data.method){
if (!data.method){
return false;
}

// make sure the context is correct.
if (playerjs.ENABLE_CONTEXT && data.context !== playerjs.CONTEXT){
return false;
}

Expand Down Expand Up @@ -154,8 +159,10 @@ playerjs.Receiver.prototype.send = function(event, value, listener){
playerjs.log('Receiver.send.reject', event, value, listener);
return false;
}

var data = {
context: playerjs.POST_MESSAGE_CONTEXT,
context: playerjs.CONTEXT,
version: playerjs.VERSION,
event: event
};

Expand Down
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ var isNumber= function(obj){
return Object.prototype.toString.call(obj) === "[object Number]";
};

var removeEvent = function(elem, type, eventHandle) {
if (!elem) { return; }
if ( elem.removeEventListener ) {
elem.removeEventListener( type, eventHandle, false );
} else if ( elem.detachEvent ) {
elem.detachEvent( "on" + type, eventHandle );
} else {
elem["on"+type]=null;
}
};

function testCases(){
var player = this;

Expand Down Expand Up @@ -59,6 +70,24 @@ function testCases(){
player.play();
});

// Make sure we are receiving context.
asyncTest("context", 2, function(){

var onMessage = function(e){
var data = JSON.parse(e.data);

ok(data.context === playerjs.CONTEXT);
ok(data.version === playerjs.VERSION);
removeEvent(window, 'message', onMessage);
start();
};

playerjs.addEvent(window, 'message', onMessage);

// This will force the receiver to echo.
player.on('ready', function(){});
});

// Test to make sure we can attach multiple listeners to the same event.
asyncTest("multi-listeners", 4, function() {

Expand Down

0 comments on commit 5f3654a

Please sign in to comment.