Permalink
Browse files

added an API

  • Loading branch information...
1 parent 7036503 commit b1aeee608be33f466363781d72d36413fc14689a ojii committed Jun 18, 2010
View
@@ -1 +1 @@
-__version__ = '0.1.7'
+__version__ = '0.1.8'
@@ -1,134 +1,165 @@
-function dbg(msg){window.console && console.log && console.log(msg);}
+(function($) {
+ function dbg(msg){window.console && console.log && console.log(msg);}
-function cmsplayer_ready (element, options)
-{
- var playerid = options.playerid;
- var autoplay = options.autoplay;
- var autonext = options.autonext;
- var playlist = options.playlist;
- var playItem = 0;
- var playtime = $("#play_time_"+playerid);
- var totaltime = $("#total_time_"+playerid);
- element.jPlayer("cssId", "play", "player_play_"+playerid)
- .jPlayer("cssId", "pause", "player_pause_"+playerid)
- .jPlayer("cssId", "stop", "player_stop_"+playerid)
- .jPlayer("cssId", "loadBar", "player_progress_load_bar_"+playerid)
- .jPlayer("cssId", "playBar", "player_progress_play_bar_"+playerid)
- .jPlayer("cssId", "volumeMin", "player_volume_min_"+playerid)
- .jPlayer("cssId", "volumeMax", "player_volume_max_"+playerid)
- .jPlayer("cssId", "volumeBar", "player_volume_bar_"+playerid)
- .jPlayer("cssId", "volumeBarValue", "player_volume_bar_value_"+playerid)
- .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
- var myPlayedTime = new Date(playedTime);
- var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
- var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
- playtime.text(ptMin+":"+ptSec);
-
- var myTotalTime = new Date(totalTime);
- var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
- var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
- totaltime.text(ttMin+":"+ttSec);
- }).jPlayer("onSoundComplete", function() {
- element.trigger('cmsplayer_songcomplete', [playlist[playItem]]);
- if(autonext){
- playListNext();
+ function cmsplayer_ready (element, options)
+ {
+ element.data('cmsplayer.playlist', options.playlist);
+ function get_playlist(){
+ return element.data('cmsplayer.playlist');
}
- });
-
- $("#ctrl_prev_"+playerid).click( function() {
- playListPrev();
- return false;
- });
-
- $("#ctrl_next_"+playerid).click( function() {
- playListNext();
- return false;
- });
-
- function displayPlayList() {
- for (i=0; i < playlist.length; i++) {
- song = playlist[i];
- $("#playlist_item_"+playerid+"_"+i).data( "index", i ).hover(
- function() {
- if (playItem != $(this).data("index")) {
- $(this).addClass("playlist_hover");
+ var playerid = options.playerid;
+ var autoplay = options.autoplay;
+ var autonext = options.autonext;
+ var playItem = 0;
+ var playtime = $("#play_time_"+playerid);
+ var totaltime = $("#total_time_"+playerid);
+ element.jPlayer("cssId", "play", "player_play_"+playerid)
+ .jPlayer("cssId", "pause", "player_pause_"+playerid)
+ .jPlayer("cssId", "stop", "player_stop_"+playerid)
+ .jPlayer("cssId", "loadBar", "player_progress_load_bar_"+playerid)
+ .jPlayer("cssId", "playBar", "player_progress_play_bar_"+playerid)
+ .jPlayer("cssId", "volumeMin", "player_volume_min_"+playerid)
+ .jPlayer("cssId", "volumeMax", "player_volume_max_"+playerid)
+ .jPlayer("cssId", "volumeBar", "player_volume_bar_"+playerid)
+ .jPlayer("cssId", "volumeBarValue", "player_volume_bar_value_"+playerid)
+ .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
+ var myPlayedTime = new Date(playedTime);
+ var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
+ var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
+ playtime.text(ptMin+":"+ptSec);
+
+ var myTotalTime = new Date(totalTime);
+ var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
+ var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
+ totaltime.text(ttMin+":"+ttSec);
+ }).jPlayer("onSoundComplete", function() {
+ element.trigger('cmsplayer_songcomplete', [get_playlist()[playItem]]);
+ if(autonext){
+ playListNext();
+ }
+ });
+
+ $("#ctrl_prev_"+playerid).click( function() {
+ playListPrev();
+ return false;
+ });
+
+ $("#ctrl_next_"+playerid).click( function() {
+ playListNext();
+ return false;
+ });
+
+ function displayPlayList() {
+ var playlist = get_playlist();
+ for (i=0; i < playlist.length; i++) {
+ song = playlist[i];
+ $("#playlist_item_"+playerid+"_"+i).data("cmsplayer.index", i).hover(
+ function() {
+ if (playItem != $(this).data("cmsplayer.index")) {
+ $(this).addClass("playlist_hover");
+ }
+ },
+ function() {
+ $(this).removeClass("playlist_hover");
}
- },
- function() {
- $(this).removeClass("playlist_hover");
- }
- ).click( function() {
- var index = $(this).data("index");
- playListChange( index );
- });
+ ).click( function() {
+ var index = $(this).data("cmsplayer.index");
+ playListChange(index);
+ });
+ }
}
- }
-
- function playListInit(autoplay) {
- element.trigger('cmsplayer_init', [playlist[playItem]]);
- if(autoplay) {
- playListChange( playItem );
- } else {
- playListConfig( playItem );
+ element.data('cmsplayer.displayPlayList', displayPlayList);
+
+ function playListInit(autoplay) {
+ element.trigger('cmsplayer_init', [get_playlist()[playItem]]);
+ if(autoplay) {
+ playListChange(playItem);
+ } else {
+ playListConfig(playItem);
+ }
}
- }
-
- function playListConfig( index ) {
- $("#playlist_item_"+playerid+"_"+playItem).removeClass("playlist_current");
- $("#playlist_item_"+playerid+"_"+index).addClass("playlist_current");
- playItem = index;
- if (playlist[playItem].ogg)
- {
- element.jPlayer("setFile", playlist[playItem].mp3, playlist[playItem].ogg);
+ element.data('cmsplayer.playListInit', playListInit);
+
+ function playListConfig(index) {
+ $("#playlist_item_"+playerid+"_"+playItem).removeClass("playlist_current");
+ $("#playlist_item_"+playerid+"_"+index).addClass("playlist_current");
+ playItem = index;
+ var playlist = get_playlist();
+ if (playlist[playItem].ogg)
+ {
+ element.jPlayer("setFile", playlist[playItem].mp3, playlist[playItem].ogg);
+ }
+ else
+ {
+ element.jPlayer("setFile", playlist[playItem].mp3);
+ }
+ element.trigger('cmsplayer_config', [playlist[playItem]]);
}
- else
- {
- element.jPlayer("setFile", playlist[playItem].mp3);
+ element.data('cmsplayer.playListConfig', playListConfig);
+
+ function playListChange( index ) {
+ playListConfig( index );
+ element.jPlayer("play");
+ element.trigger('cmsplayer_change', [get_playlist()[playItem]]);
}
- element.trigger('cmsplayer_config', [playlist[playItem]]);
- }
-
- function playListChange( index ) {
- playListConfig( index );
- element.jPlayer("play");
- element.trigger('cmsplayer_change', [playlist[playItem]]);
- }
-
- function playListNext() {
- var index = (playItem+1 < playlist.length) ? playItem+1 : 0;
- playListChange( index );
- }
-
- function playListPrev() {
- var index = (playItem-1 >= 0) ? playItem-1 : playlist.length-1;
- playListChange( index );
- }
-
- displayPlayList();
- playListInit(autoplay);
-}
+
+ function playListNext() {
+ var index = (playItem+1 < get_playlist().length) ? playItem+1 : 0;
+ playListChange( index );
+ }
+ element.data('cmsplayer.playListNext', playListNext);
+
+ function playListPrev() {
+ var index = (playItem-1 >= 0) ? playItem-1 : get_playlist().length-1;
+ playListChange( index );
+ }
+ element.data('cmsplayer.playListPrev', playListPrev);
+
+ function init(){
+ playItem = 0;
+ displayPlayList();
+ playListInit(autoplay);
+ }
+ element.data('cmsplayer.init', init);
+
+ function replacePlaylist(new_playlist){
+ element.data('cmsplayer.playlist', new_playlist);
+ }
+ element.data('cmsplayer.replacePlaylist');
-(function($) {
+ init();
+ }
$.fn.cmsPlayer = function(options) {
var das = $(this);
- if (!options){
- var options = {};
- }
- $.extend(options, {
- ready: function(){cmsplayer_ready(das, options)}
- });
- var fulloptions = {
- autoplay: true,
- autonext: true
- };
- $.extend(fulloptions, options);
- if (!fulloptions.playerid || !fulloptions.playlist)
+ if (typeof options == 'string')
{
- dbg("[CMSPLAYER] ERROR: playerid and playlist *must* be defined!");
- return this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ var func = das.data('cmsplayer.' + options);
+ if (func){
+ func(args);
+ } else {
+ das.jPlayer(options, args);
+ }
+ } else {
+ if (!options){
+ var options = {};
+ }
+ $.extend(options, {
+ ready: function(){cmsplayer_ready(das, options)}
+ });
+ var fulloptions = {
+ autoplay: true,
+ autonext: true
+ };
+ $.extend(fulloptions, options);
+ if (!fulloptions.playerid || !fulloptions.playlist)
+ {
+ dbg("[CMSPLAYER] ERROR: playerid and playlist *must* be defined!");
+ return das;
+ }
+ $(das).jPlayer(fulloptions);
}
- $(das).jPlayer(fulloptions);
- return this;
+ return das;
};
})(jQuery);
View
@@ -14,9 +14,10 @@ class JPlayer(models.Model):
def __unicode__(self):
return self.name
- @safe_json
def get_json_playlist(self):
- return self.playlist()
+ if not hasattr(self, '_cached_playlist'):
+ self._cached_playlist = safe_json(self.playlist())
+ return self._cached_playlist
def playlist(self):
playlist = []
@@ -39,17 +40,14 @@ def playlist(self):
def ogg_support(self):
return not self.songs.filter(ogg_file__exact='').count()
- @safe_json
def get_json_ogg_support(self):
- return self.ogg_support()
+ return safe_json(self.ogg_support())
- @safe_json
def get_base_path(self):
- return settings.JPLAYER_BASE_PATH
+ return safe_json(settings.JPLAYER_BASE_PATH)
- @safe_json
def get_json_autoplay(self):
- return self.autoplay
+ return safe_json(self.autoplay)
class Artist(models.Model):
@@ -1,51 +1 @@
-<div class="jplayer" id="jquery_jplayer_{{ player.id }}"></div>
-
-<div id="player_container_{{ player.id }}" class="cmsplayer">
- <ul id="player_controls_{{ player.id }}" class="controls">
- <li id="player_play_{{ player.id }}" class="play">play</li>
- <li id="player_pause_{{ player.id }}" class="pause">pause</li>
- <li id="player_stop_{{ player.id }}" class="stop">stop</li>
- <li id="player_volume_min_{{ player.id }}" class="volume-min">min volume</li>
- <li id="player_volume_max_{{ player.id }}" class="volume-max">max volume</li>
- <li id="ctrl_prev_{{ player.id }}" class="prev">previous</li>
- <li id="ctrl_next_{{ player.id }}" class="next">next</li>
- </ul>
- <div id="play_time_{{ player.id }}" class="playtime"></div>
- <div id="total_time_{{ player.id }}" class="totaltime"></div>
- <div id="player_progress_{{ player.id }}" class="progress">
- <div id="player_progress_load_bar_{{ player.id }}" class="loadbar">
- <div id="player_progress_play_bar_{{ player.id }}" class="playbar"></div>
- </div>
- </div>
- <div id="player_volume_bar_{{ player.id }}" class="volumebar">
- <div id="player_volume_bar_value_{{ player.id }}" class="volumebarvalue"></div>
- </div>
-</div>
-
-<div id="playlist_list_{{ player.id }}" class="cmsplaylist">
- <ul>
- {% for song in player.playlist %}
- {% include "jplayer/song.html" %}
- {% endfor %}
- </ul>
-</div>
-<script type="text/javascript">
-$(document).ready(function(){
- if (!document.cmsPlayerLoaded){
- $("head").append('<scr' + 'ipt type="text/javascript" src="{{ MEDIA_URL }}jplayer/js/cmsplayer.js"></scr' + 'ipt>');
- }
- var player = $('#jquery_jplayer_{{ player.id }}');
- player.cmsPlayer(
- {
- playlist: {{ player.get_json_playlist }},
- autoplay: {{ player.get_json_autoplay }},
- playerid: "{{ player.id }}",
- autonext: true,
- cssPrefix: 'jqjp_{{ player.id }}',
- swfPath: 'http://' + window.location.host + {{ player.get_base_path }},
- oggSupport: {{ player.get_json_ogg_support }},
- customCssIds: true
- }
- );
-});
-</script>
+{% extends "jplayer/player_base.html %}
Oops, something went wrong.

0 comments on commit b1aeee6

Please sign in to comment.