Skip to content

Commit

Permalink
Presentation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jwheare committed Dec 14, 2009
1 parent 452ee2f commit 4d37b63
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 88 deletions.
14 changes: 14 additions & 0 deletions src/js/controllers.js
Expand Up @@ -14,6 +14,20 @@ var CONTROLLERS = {
MODELS.onCouchUp = function () { MODELS.onCouchUp = function () {
Playlist.couchUpHandler.apply(Playlist, arguments); 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 // Expose
CONTROLLERS.Playlist = Playlist; CONTROLLERS.Playlist = Playlist;
})(); })();
137 changes: 92 additions & 45 deletions src/js/controllers/playlist.controller.js
Expand Up @@ -76,13 +76,13 @@ Playlist.prototype = {
} }
// Set a new current playlist // Set a new current playlist
this.setCurrent(); this.setCurrent();
// Show add track details // Metadata
this.addTrackTable.show(); this.hideMetadata();
// Show title },
this.showCreateTitle();
// Hide playlist actions and footer onCreate: function (playlist) {
this.footerElem.hide(); // Add to sidebar
this.actionsElem.hide(); this.playlistSidebarElem.append(playlist.element);
}, },


/* RETRIEVE */ /* RETRIEVE */
Expand Down Expand Up @@ -145,26 +145,50 @@ Playlist.prototype = {
} }
// Update the current playlist object // Update the current playlist object
this.setCurrent(playlist); 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 // Hide add track details
this.addTrackTable.hide(); this.addTrackTable.hide();
// Update the title },
this.updateTitle(playlist); hideMetadata: function () {
// Update the copyright // Header
this.updateCopyright(playlist); this.showCreateTitle();
// Update the source // Footer
this.updateSource(playlist); this.hideFooter();
// Show add track details
this.addTrackTable.show();
},
loadTracks: function () {
// Show loading message // Show loading message
this.tracksLoadingElem.show(); this.tracksLoadingElem.show();
// Hide error message // Hide error message
this.tracksErrorElem.hide(); this.tracksErrorElem.hide();
// Load tracks // Load tracks
var elements = this.current.load(); var elements = this.current.load();
// Update the AppleScript link
this.updateAppleScript();
this.tracksLoadingElem.hide(); this.tracksLoadingElem.hide();
if (elements) { if (elements) {
// Add to the DOM // Add to the DOM
this.trackListElem.append(elements); this.trackListElem.append(elements);
// Set loading/playback progress bars correctly
setTimeout(function () { setTimeout(function () {
var nowPlayingSound = Playdar.player.getNowPlaying(); var nowPlayingSound = Playdar.player.getNowPlaying();
if (nowPlayingSound) { if (nowPlayingSound) {
Expand All @@ -174,17 +198,34 @@ Playlist.prototype = {
}); });
// Resolve tracks with Playdar // Resolve tracks with Playdar
PLAYDAR.resolve_current_playlist(); 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 { } else {
this.tracksErrorElem.show(); this.tracksErrorElem.show();
} }
// Show playlist actions and footer },
this.footerElem.show(); loadTitle: function () {
this.actionsElem.show(); this.titleElem.html(this.current.titleHTML());
// Show SM2 },
if (this.playingTrack && this.playingTrack.playlist === this.current) { loadCopyright: function () {
PLAYDAR.showSM2Container(); 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 */ /* UPDATE */
// Show/hide edit mode for playlist in sidebar // Show/hide edit mode for playlist in sidebar
Expand All @@ -211,11 +252,14 @@ Playlist.prototype = {
if (title) { if (title) {
playlist.set_name(title); playlist.set_name(title);
} }
// Update sidebar title
this.updateSidebarTitle(playlist);
// Update current title // Update current title
if (this.current == playlist) { 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())); playlist.element.find('a.playlist').text(UTIL.truncateString(playlist.toString()));
}, },
updateCopyright: function (playlist, copyright) { updateCopyright: function (playlist, copyright) {
Expand All @@ -226,25 +270,8 @@ Playlist.prototype = {
} }
// Update current copyright // Update current copyright
if (this.current == playlist) { 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) { addTrack: function (artistName, trackName, albumName, url) {
Expand All @@ -265,23 +292,43 @@ Playlist.prototype = {
this.updateTitle(this.current, playlistName); this.updateTitle(this.current, playlistName);
} else { } else {
// Update title display // Update title display
this.updateTitle(this.current); this.loadTitle();
// Save // Save
this.current.save(); this.current.save();
} }
// Add the track to the playlist in the DOM // Add the track to the playlist in the DOM
this.trackListElem.append(playlist_track.element); this.trackListElem.append(playlist_track.element);
// Show playlist actions and footer // Show playlist actions and footer
this.footerElem.show(); this.loadFooter();
this.actionsElem.show();
// Resolve // Resolve
PLAYDAR.resolve_track(playlist_track); PLAYDAR.resolve_track(playlist_track);
}, },


onSave: function (playlist) {
if (playlist == this.current) {
this.loadAppleScript();
PLAYDAR.showSM2Container();
}
},

/* UNLOAD */
onUnload : function (playlist) {
PLAYDAR.hideSM2Container();
},

/* DELETE */ /* DELETE */
remove: function (playlist) { remove: function (playlist) {
if (confirm('Are you sure you want to delete this playlist:\n\n' + playlist.name)) { if (confirm('Are you sure you want to delete this playlist:\n\n' + playlist.name)) {
playlist.remove(); playlist.remove();
} }
},

onDelete: function (playlist) {
if (playlist == this.current) {
this.create();
}
if (this.playingTrack && playlist == this.playingTrack.playlist) {
PLAYDAR.stopPlaySession();
}
} }
}; };
23 changes: 0 additions & 23 deletions src/js/main/models.js
Expand Up @@ -92,27 +92,4 @@
MODELS.Track.prototype.toHTML = trackToHtml; MODELS.Track.prototype.toHTML = trackToHtml;
MODELS.Playlist.prototype.toHTML = playlistToHtml; MODELS.Playlist.prototype.toHTML = playlistToHtml;
MODELS.Playlist.prototype.titleHTML = playlistTitleHtml; 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();
}
}
};
})(); })();
36 changes: 16 additions & 20 deletions src/js/models/playlist.model.js
Expand Up @@ -53,27 +53,26 @@ Playlist.prototype = {
this.tracks.push(playlist_track); this.tracks.push(playlist_track);
return playlist_track; return playlist_track;
}, },
remove_track: function (playlist_track, onSave) { remove_track: function (playlist_track) {
var i = $.inArray(playlist_track, this.tracks); var i = $.inArray(playlist_track, this.tracks);
this.tracks.splice(i, 1); this.tracks.splice(i, 1);
// Refactor view
playlist_track.element.remove();
// AUTOSAVE // AUTOSAVE
this.save(function () { this.save();
// Remove from the DOM
playlist_track.element.remove();
});
}, },
reset_tracks: function (playlist_tracks, onSave) { reset_tracks: function (playlist_tracks) {
this.tracks = playlist_tracks; this.tracks = playlist_tracks;
// AUTOSAVE // AUTOSAVE
this.save(onSave); this.save();
}, },
/** /**
* Playlist management * Playlist management
**/ **/
set_name: function (name, onSave) { set_name: function (name) {
this.name = name; this.name = name;
// AUTOSAVE // AUTOSAVE
this.save(onSave); this.save();
}, },
update_duration: function () { update_duration: function () {
var duration = 0; var duration = 0;
Expand Down Expand Up @@ -217,27 +216,24 @@ Playlist.prototype = {
/** /**
* Persistance * Persistance
**/ **/
onSave: function (callback) { onSave: function () {
if (!this.persisted && this.options.onCreate) { if (!this.persisted && this.options.onCreate) {
this.options.onCreate.call(this); this.options.onCreate.call(this);
} }
if (callback) {
callback.call(this);
}
if (this.options.onSave) { if (this.options.onSave) {
this.options.onSave.call(this); this.options.onSave.call(this);
} }
this.saved = true; this.saved = true;
}, },
save: function (callback) { save: function () {
// Persist in CouchDB // Persist in CouchDB
if (MODELS.couch_up) { if (MODELS.couch_up) {
try { try {
var result = MODELS.couch.save(this.get_doc()); var result = MODELS.couch.save(this.get_doc());
// console.dir(result); // console.dir(result);
if (result.ok) { if (result.ok) {
this.set_doc_ref(result.id, result.rev); this.set_doc_ref(result.id, result.rev);
this.onSave(callback); this.onSave();
this.persisted = true; this.persisted = true;
// console.info('[saved] ' + result.id + ' [' + result.rev + ']'); // console.info('[saved] ' + result.id + ' [' + result.rev + ']');
} }
Expand All @@ -246,7 +242,7 @@ Playlist.prototype = {
} }
} }
if (!MODELS.couch_up && !this.persisted) { if (!MODELS.couch_up && !this.persisted) {
this.onSave(callback); this.onSave();
} }
}, },
onRemove: function () { onRemove: function () {
Expand Down Expand Up @@ -275,15 +271,15 @@ Playlist.prototype = {
this.onRemove(); this.onRemove();
} }
}, },
publish: function (onSave) { publish: function () {
this.published = true; this.published = true;
// AUTOSAVE // AUTOSAVE
this.save(onSave); this.save();
}, },
make_private: function (onSave) { make_private: function () {
this.published = false; this.published = false;
// AUTOSAVE // AUTOSAVE
this.save(onSave); this.save();
}, },
share: function (person) { share: function (person) {
// TODO // TODO
Expand Down

0 comments on commit 4d37b63

Please sign in to comment.