'use strict'; (function($) { $.fn.padmavideo = function(options) { //get options, giving preference, in order, to data- attributes defined on the html element, options passed when instantiatiing $(element).padmavideo(options), defaults. var options = options || {}, namespace = options.namespace || "padma", $this = this; var optionTypes = { 'strings': ['api', 'base', 'resolution', 'action', 'id'], 'integers': ['in', 'out', 'width', 'interval'], 'arrays': ['layers', 'keys'], 'booleans': [] //'functions': ['callback'] FIXME: lets not. }; var dataOptions = $.extend(options, $this.getDataOptions(optionTypes, namespace)); var opts = $.extend({ 'id': 'ABC', //FIXME: throw an error if id is undefined at this point 'layers': ['transcripts'], 'keys': ['layers'], //FIXME: add more apt keys 'api': "http://pad.ma/api/", 'in': 0, 'out': 0, 'padma_base': 'http://pad.ma/', 'resolution': '480p', 'width': '640', 'interval': 400, 'action': 'get', //action POST param to send to url 'callback': function() { $.noop(); } //function to call after done instantiating PadmaVideo object, called with the object. }, dataOptions); //get the padma id and instantiate a PadmaVideo object with the current element and render it and execute optional callback var id = opts.id, $loading = $('
').addClass("padmaLoading").text("Loading video...").appendTo($this), sendData = JSON.stringify({'id': id, 'keys': opts.keys}); var deferred = $.post(opts.api, {'action': opts.action, 'data': sendData}, function(response) { $loading.hide().remove(); var padma = new PadmaVideo(id, response.data, $this, opts); padma.render(); opts.callback(padma); }, "json"); deferred.error(function(data) { alert("failed to load video data"); }); }; /* PadmaVideo class Parameters: id: pandora video id data: data for the video object $el: opts: options object */ var PadmaVideo = function(id, data, $el, opts) { this.id = id; this.data = data; this.$el = $el; this.o = opts; this.getVideoURL = function() { return opts.padma_base + id + "/" + opts.resolution + ".webm"; }; //empties element, appends video widget this.render = function() { var that = this; this.$el.empty(); this.$el.append(that.getWidget()); }; /* Use this to set options on the player from the outside. Example: var padmaVideoObject = $('#video').data("padmavideo"); padmaVideoObject.setOption("width", 250); */ this.setOption = function(key, val) { var that = this; this.o[key] = val; if ($.inArray(key, ['resolution']) != -1) { this.destroy(); this.render(); return this; } if ($.inArray(key, ['in', 'out', 'layers']) != -1) { this.updatePlayer(); return this; } if ($.inArray(key, ['width']) != -1) { this.$video.animate({'width': that.o.width + "px"}); return this; } console.log("attempt to set invalid option or option which will make no difference to player state"); }; /* Returns currently defined option for key specified Parameters: key: eg. 'width' */ this.getOption = function(key) { return this.o[key] || "invalid option"; }; //returns widget for this PadmaVideo object this.getWidget = function() { var that = this; var $container = this.$container = $('
').addClass("padmaContainer"); var $video = this.$video = $('