Skip to content

Commit

Permalink
Merge pull request embedly#4 from eskimoblood/master
Browse files Browse the repository at this point in the history
Add a function to create the `play`, `pause`, `mute`,  `unmute` and  get/setter functions  automatically
  • Loading branch information
screeley committed Jan 22, 2014
2 parents de864df + 2a4ffa0 commit 6b38d08
Showing 1 changed file with 28 additions and 76 deletions.
104 changes: 28 additions & 76 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,86 +195,38 @@ playerjs.Player.prototype.off = function(event, callback){
return false;
};

playerjs.Player.prototype.play = function(){
this.send({
method: 'play'
});
};

playerjs.Player.prototype.pause = function(){
this.send({
method: 'pause'
});
};

playerjs.Player.prototype.getPaused = function(callback, ctx){
this.send({
method: 'getPaused'
}, callback, ctx);
};

playerjs.Player.prototype.mute = function(){
this.send({
method: 'mute'
});
};

playerjs.Player.prototype.unmute = function(){
this.send({
method: 'unmute'
});
};

playerjs.Player.prototype.getMuted = function(callback, ctx){
this.send({
method: 'getMuted'
}, callback, ctx);
};

playerjs.Player.prototype.getVolume = function(callback, ctx){
this.send({
method: 'getVolume'
}, callback, ctx);
};

playerjs.Player.prototype.setVolume = function(value){
this.send({
method: 'setVolume',
value: value
});
};

playerjs.Player.prototype.getDuration = function(callback, ctx){
this.send({
method: 'getDuration'
}, callback, ctx);
};
for (var i = 0, l = playerjs.METHODS.length - 2; i < l; i++) {
var methodName = playerjs.METHODS[i];
playerjs.Player.prototype[methodName] = createPrototypeFunction(methodName)
}

//create function to add to the Player prototype
function createPrototypeFunction(name) {
return function() {
var data = {
method: name
};
var args = Array.prototype.slice.call(arguments);
var argsToCall;

playerjs.Player.prototype.setCurrentTime = function(value){
this.send({
method: 'setCurrentTime',
value: value
});
};
//for setter add the first arg to the value field
if (/^set/.test(name)) {
data.value = args[0]
}

playerjs.Player.prototype.getCurrentTime = function(callback, ctx){
this.send({
method: 'getCurrentTime'
}, callback, ctx);
};
//for getters add the passed parameters to the arguments for the send call
if (/^get/.test(name)) {
args.unshift(data);
argsToCall = args;
//otherwise only use the data object
} else {
argsToCall = [data];
}

playerjs.Player.prototype.setLoop = function(value){
this.send({
method: 'getLoop',
value: value
});
};
this.send.apply(this, argsToCall)
}
}

playerjs.Player.prototype.getLoop = function(callback, ctx){
this.send({
method: 'getLoop'
}, callback, ctx);
};

window.playerjs = playerjs;

Expand Down

0 comments on commit 6b38d08

Please sign in to comment.