Permalink
Browse files

Presentation logic

  • Loading branch information...
1 parent 452ee2f commit 4d37b638eef8f5e0cd310c93e8f0670bd15a3ec0 @jwheare committed Dec 14, 2009
Showing with 122 additions and 88 deletions.
  1. +14 −0 src/js/controllers.js
  2. +92 −45 src/js/controllers/playlist.controller.js
  3. +0 −23 src/js/main/models.js
  4. +16 −20 src/js/models/playlist.model.js
View
@@ -14,6 +14,20 @@ var CONTROLLERS = {
MODELS.onCouchUp = function () {
Playlist.couchUpHandler.apply(Playlist, arguments);
};
+ MODELS.Playlist.DefaultOptions = {
+ onSave: function () {
+ Playlist.onSave(this);
+ },
+ onCreate: function () {
+ Playlist.onCreate(this);
+ },
+ onUnload: function () {
+ Playlist.onUnload(this);
+ },
+ onDelete: function () {
+ Playlist.onDelete(this);
+ }
+ };
// Expose
CONTROLLERS.Playlist = Playlist;
})();
@@ -76,13 +76,13 @@ Playlist.prototype = {
}
// Set a new current playlist
this.setCurrent();
- // Show add track details
- this.addTrackTable.show();
- // Show title
- this.showCreateTitle();
- // Hide playlist actions and footer
- this.footerElem.hide();
- this.actionsElem.hide();
+ // Metadata
+ this.hideMetadata();
+ },
+
+ onCreate: function (playlist) {
+ // Add to sidebar
+ this.playlistSidebarElem.append(playlist.element);
},
/* RETRIEVE */
@@ -145,26 +145,50 @@ Playlist.prototype = {
}
// Update the current playlist object
this.setCurrent(playlist);
+ // Load metadata
+ this.loadMetadata();
+ // Load tracks
+ this.loadTracks();
+ },
+ loadFooter: function () {
+ this.loadCopyright();
+ this.loadSource();
+ this.footerElem.show();
+ this.loadAppleScript();
+ this.actionsElem.show();
+ },
+ hideFooter: function () {
+ this.footerElem.hide();
+ this.actionsElem.hide();
+ },
+ loadMetadata: function () {
+ // Header
+ this.loadTitle();
+ // Footer
+ this.loadFooter();
// Hide add track details
this.addTrackTable.hide();
- // Update the title
- this.updateTitle(playlist);
- // Update the copyright
- this.updateCopyright(playlist);
- // Update the source
- this.updateSource(playlist);
+ },
+ hideMetadata: function () {
+ // Header
+ this.showCreateTitle();
+ // Footer
+ this.hideFooter();
+ // Show add track details
+ this.addTrackTable.show();
+ },
+ loadTracks: function () {
// Show loading message
this.tracksLoadingElem.show();
// Hide error message
this.tracksErrorElem.hide();
// Load tracks
var elements = this.current.load();
- // Update the AppleScript link
- this.updateAppleScript();
this.tracksLoadingElem.hide();
if (elements) {
// Add to the DOM
this.trackListElem.append(elements);
+ // Set loading/playback progress bars correctly
setTimeout(function () {
var nowPlayingSound = Playdar.player.getNowPlaying();
if (nowPlayingSound) {
@@ -174,17 +198,34 @@ Playlist.prototype = {
});
// Resolve tracks with Playdar
PLAYDAR.resolve_current_playlist();
+ // Show SM2 if the current playing track is in this playlist
+ if (this.playingTrack && this.playingTrack.playlist === this.current) {
+ PLAYDAR.showSM2Container();
+ }
} else {
this.tracksErrorElem.show();
}
- // Show playlist actions and footer
- this.footerElem.show();
- this.actionsElem.show();
- // Show SM2
- if (this.playingTrack && this.playingTrack.playlist === this.current) {
- PLAYDAR.showSM2Container();
+ },
+ loadTitle: function () {
+ this.titleElem.html(this.current.titleHTML());
+ },
+ loadCopyright: function () {
+ this.copyrightElem.text(this.current.copyright || '');
+ },
+ loadSource: function () {
+ if (this.current.source) {
+ this.sourceLink
+ .attr('href', this.current.source)
+ .text(Playdar.Util.location_from_url(this.current.source).host);
+ this.sourceElem.show();
+ } else {
+ this.sourceElem.hide();
}
},
+ // Update the playlist iTunes export AppleScript (when loading a playlist or saving)
+ loadAppleScript: function () {
+ this.applescriptLink.attr('href', this.current.toApplescript());
+ },
/* UPDATE */
// Show/hide edit mode for playlist in sidebar
@@ -211,11 +252,14 @@ Playlist.prototype = {
if (title) {
playlist.set_name(title);
}
+ // Update sidebar title
+ this.updateSidebarTitle(playlist);
// Update current title
if (this.current == playlist) {
- this.titleElem.html(playlist.titleHTML());
+ this.loadTitle();
}
- // Update sidebar title
+ },
+ updateSidebarTitle: function (playlist) {
playlist.element.find('a.playlist').text(UTIL.truncateString(playlist.toString()));
},
updateCopyright: function (playlist, copyright) {
@@ -226,25 +270,8 @@ Playlist.prototype = {
}
// Update current copyright
if (this.current == playlist) {
- this.copyrightElem.text(playlist.copyright || '');
+ this.loadCopyright();
}
-
- },
- updateSource: function (playlist) {
- if (this.current == playlist) {
- if (playlist.source) {
- this.sourceLink
- .attr('href', playlist.source)
- .text(Playdar.Util.location_from_url(playlist.source).host);
- this.sourceElem.show();
- } else {
- this.sourceElem.hide();
- }
- }
- },
- // Update the playlist iTunes export AppleScript (when loading a playlist or saving)
- updateAppleScript: function () {
- this.applescriptLink.attr('href', this.current.toApplescript());
},
addTrack: function (artistName, trackName, albumName, url) {
@@ -265,23 +292,43 @@ Playlist.prototype = {
this.updateTitle(this.current, playlistName);
} else {
// Update title display
- this.updateTitle(this.current);
+ this.loadTitle();
// Save
this.current.save();
}
// Add the track to the playlist in the DOM
this.trackListElem.append(playlist_track.element);
// Show playlist actions and footer
- this.footerElem.show();
- this.actionsElem.show();
+ this.loadFooter();
// Resolve
PLAYDAR.resolve_track(playlist_track);
},
+ onSave: function (playlist) {
+ if (playlist == this.current) {
+ this.loadAppleScript();
+ PLAYDAR.showSM2Container();
+ }
+ },
+
+ /* UNLOAD */
+ onUnload : function (playlist) {
+ PLAYDAR.hideSM2Container();
+ },
+
/* DELETE */
remove: function (playlist) {
if (confirm('Are you sure you want to delete this playlist:\n\n' + playlist.name)) {
playlist.remove();
}
+ },
+
+ onDelete: function (playlist) {
+ if (playlist == this.current) {
+ this.create();
+ }
+ if (this.playingTrack && playlist == this.playingTrack.playlist) {
+ PLAYDAR.stopPlaySession();
+ }
}
-};
+};
View
@@ -92,27 +92,4 @@
MODELS.Track.prototype.toHTML = trackToHtml;
MODELS.Playlist.prototype.toHTML = playlistToHtml;
MODELS.Playlist.prototype.titleHTML = playlistTitleHtml;
- MODELS.Playlist.DefaultOptions = {
- onSave: function () {
- if (this == CONTROLLERS.Playlist.current) {
- CONTROLLERS.Playlist.updateAppleScript();
- PLAYDAR.showSM2Container();
- }
- },
- onCreate: function () {
- // Add to sidebar
- CONTROLLERS.Playlist.playlistSidebarElem.append(this.element);
- },
- onUnload: function () {
- PLAYDAR.hideSM2Container();
- },
- onDelete: function () {
- if (this == CONTROLLERS.Playlist.current) {
- CONTROLLERS.Playlist.create();
- }
- if (CONTROLLERS.Playlist.playingTrack && this === CONTROLLERS.Playlist.playingTrack.playlist) {
- PLAYDAR.stopPlaySession();
- }
- }
- };
})();
@@ -53,27 +53,26 @@ Playlist.prototype = {
this.tracks.push(playlist_track);
return playlist_track;
},
- remove_track: function (playlist_track, onSave) {
+ remove_track: function (playlist_track) {
var i = $.inArray(playlist_track, this.tracks);
this.tracks.splice(i, 1);
+ // Refactor view
+ playlist_track.element.remove();
// AUTOSAVE
- this.save(function () {
- // Remove from the DOM
- playlist_track.element.remove();
- });
+ this.save();
},
- reset_tracks: function (playlist_tracks, onSave) {
+ reset_tracks: function (playlist_tracks) {
this.tracks = playlist_tracks;
// AUTOSAVE
- this.save(onSave);
+ this.save();
},
/**
* Playlist management
**/
- set_name: function (name, onSave) {
+ set_name: function (name) {
this.name = name;
// AUTOSAVE
- this.save(onSave);
+ this.save();
},
update_duration: function () {
var duration = 0;
@@ -217,27 +216,24 @@ Playlist.prototype = {
/**
* Persistance
**/
- onSave: function (callback) {
+ onSave: function () {
if (!this.persisted && this.options.onCreate) {
this.options.onCreate.call(this);
}
- if (callback) {
- callback.call(this);
- }
if (this.options.onSave) {
this.options.onSave.call(this);
}
this.saved = true;
},
- save: function (callback) {
+ save: function () {
// Persist in CouchDB
if (MODELS.couch_up) {
try {
var result = MODELS.couch.save(this.get_doc());
// console.dir(result);
if (result.ok) {
this.set_doc_ref(result.id, result.rev);
- this.onSave(callback);
+ this.onSave();
this.persisted = true;
// console.info('[saved] ' + result.id + ' [' + result.rev + ']');
}
@@ -246,7 +242,7 @@ Playlist.prototype = {
}
}
if (!MODELS.couch_up && !this.persisted) {
- this.onSave(callback);
+ this.onSave();
}
},
onRemove: function () {
@@ -275,15 +271,15 @@ Playlist.prototype = {
this.onRemove();
}
},
- publish: function (onSave) {
+ publish: function () {
this.published = true;
// AUTOSAVE
- this.save(onSave);
+ this.save();
},
- make_private: function (onSave) {
+ make_private: function () {
this.published = false;
// AUTOSAVE
- this.save(onSave);
+ this.save();
},
share: function (person) {
// TODO

0 comments on commit 4d37b63

Please sign in to comment.