Skip to content

Commit 6b38d08

Browse files
committed
Merge pull request embedly#4 from eskimoblood/master
Add a function to create the `play`, `pause`, `mute`, `unmute` and get/setter functions automatically
2 parents de864df + 2a4ffa0 commit 6b38d08

File tree

1 file changed

+28
-76
lines changed

1 file changed

+28
-76
lines changed

src/player.js

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -195,86 +195,38 @@ playerjs.Player.prototype.off = function(event, callback){
195195
return false;
196196
};
197197

198-
playerjs.Player.prototype.play = function(){
199-
this.send({
200-
method: 'play'
201-
});
202-
};
203-
204-
playerjs.Player.prototype.pause = function(){
205-
this.send({
206-
method: 'pause'
207-
});
208-
};
209-
210-
playerjs.Player.prototype.getPaused = function(callback, ctx){
211-
this.send({
212-
method: 'getPaused'
213-
}, callback, ctx);
214-
};
215-
216-
playerjs.Player.prototype.mute = function(){
217-
this.send({
218-
method: 'mute'
219-
});
220-
};
221-
222-
playerjs.Player.prototype.unmute = function(){
223-
this.send({
224-
method: 'unmute'
225-
});
226-
};
227-
228-
playerjs.Player.prototype.getMuted = function(callback, ctx){
229-
this.send({
230-
method: 'getMuted'
231-
}, callback, ctx);
232-
};
233-
234-
playerjs.Player.prototype.getVolume = function(callback, ctx){
235-
this.send({
236-
method: 'getVolume'
237-
}, callback, ctx);
238-
};
239-
240-
playerjs.Player.prototype.setVolume = function(value){
241-
this.send({
242-
method: 'setVolume',
243-
value: value
244-
});
245-
};
246-
247-
playerjs.Player.prototype.getDuration = function(callback, ctx){
248-
this.send({
249-
method: 'getDuration'
250-
}, callback, ctx);
251-
};
198+
for (var i = 0, l = playerjs.METHODS.length - 2; i < l; i++) {
199+
var methodName = playerjs.METHODS[i];
200+
playerjs.Player.prototype[methodName] = createPrototypeFunction(methodName)
201+
}
202+
203+
//create function to add to the Player prototype
204+
function createPrototypeFunction(name) {
205+
return function() {
206+
var data = {
207+
method: name
208+
};
209+
var args = Array.prototype.slice.call(arguments);
210+
var argsToCall;
252211

253-
playerjs.Player.prototype.setCurrentTime = function(value){
254-
this.send({
255-
method: 'setCurrentTime',
256-
value: value
257-
});
258-
};
212+
//for setter add the first arg to the value field
213+
if (/^set/.test(name)) {
214+
data.value = args[0]
215+
}
259216

260-
playerjs.Player.prototype.getCurrentTime = function(callback, ctx){
261-
this.send({
262-
method: 'getCurrentTime'
263-
}, callback, ctx);
264-
};
217+
//for getters add the passed parameters to the arguments for the send call
218+
if (/^get/.test(name)) {
219+
args.unshift(data);
220+
argsToCall = args;
221+
//otherwise only use the data object
222+
} else {
223+
argsToCall = [data];
224+
}
265225

266-
playerjs.Player.prototype.setLoop = function(value){
267-
this.send({
268-
method: 'getLoop',
269-
value: value
270-
});
271-
};
226+
this.send.apply(this, argsToCall)
227+
}
228+
}
272229

273-
playerjs.Player.prototype.getLoop = function(callback, ctx){
274-
this.send({
275-
method: 'getLoop'
276-
}, callback, ctx);
277-
};
278230

279231
window.playerjs = playerjs;
280232

0 commit comments

Comments
 (0)