2011-03-01 18:00:51 -06:00
|
|
|
var user = [];
|
|
|
|
var timelines = [];
|
|
|
|
var activeTimeline = [];
|
|
|
|
var entries = [];
|
|
|
|
|
|
|
|
var lastEntryBar;
|
|
|
|
|
|
|
|
/* Setup after the document is ready for manipulation. */
|
2011-03-01 08:23:51 -06:00
|
|
|
$(document).ready(function(){
|
2011-02-24 07:29:30 -06:00
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
lastEntryBar = $("#last-entry");
|
|
|
|
|
|
|
|
// wire the login dialog using jQuery UI
|
2011-03-01 08:23:51 -06:00
|
|
|
$("#login-dialog").dialog({
|
|
|
|
autoOpen: false,
|
|
|
|
height: 300,
|
|
|
|
width: 300,
|
|
|
|
modal: true,
|
2011-03-01 18:00:51 -06:00
|
|
|
buttons: { Login: function(){login()} }
|
2011-03-01 08:23:51 -06:00
|
|
|
});
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// try to load user information for an authenticated user
|
2011-03-01 08:23:51 -06:00
|
|
|
$.ajax({
|
|
|
|
url: "/ts_api/users/",
|
|
|
|
type: "GET",
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
error: function(jqXHR, textStatus, error) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// assume there is no authenticated user, show login dialog
|
2011-03-01 08:23:51 -06:00
|
|
|
$("#login-dialog").dialog("open"); },
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
success: function(data, textStatus, jqXHR) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// load the user information
|
2011-03-01 08:23:51 -06:00
|
|
|
loadUser(data.user.username); }});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Read the user's credentials from the login form and perform
|
|
|
|
* an AJAX request to the API to set the session cookie. */
|
2011-03-01 08:23:51 -06:00
|
|
|
function login() {
|
2011-03-01 18:00:51 -06:00
|
|
|
// lookup the login dialog elements
|
2011-03-01 08:23:51 -06:00
|
|
|
var name = $("#login-name");
|
|
|
|
var pwd = $("#login-password");
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// call the API via AJAX
|
2011-03-01 08:23:51 -06:00
|
|
|
$.ajax({
|
|
|
|
url: "/ts_api/login",
|
|
|
|
processData: false,
|
|
|
|
data: JSON.stringify({username: name.val(), password: pwd.val()}),
|
|
|
|
type: "POST",
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
error: function(jqXHR, textStatus, error) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// assuming bad credentials (possible server error or bad request,
|
|
|
|
// we should check that, FIXME
|
2011-03-01 08:23:51 -06:00
|
|
|
var tips = $(".validate-tips");
|
|
|
|
tips.text("Incorrect username/password combination.");
|
|
|
|
tips.addClass("ui-state-error");
|
|
|
|
tips.slideDown();
|
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
success: function(data, textStatus, jqXHR) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// load the user information and hide the login dialog
|
2011-03-01 08:23:51 -06:00
|
|
|
loadUser(name.val());
|
|
|
|
$("#login-dialog").dialog("close");
|
|
|
|
}});
|
2011-02-24 07:29:30 -06:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* End the current user session and expire any session credentials we
|
|
|
|
* have aquired. */
|
2011-02-24 07:29:30 -06:00
|
|
|
function logout(event) {
|
|
|
|
alert("TODO: log user out via AJAX.");
|
2011-03-01 18:00:51 -06:00
|
|
|
// TODO: wipe username, timeline, entry variables and displays
|
2011-02-24 07:29:30 -06:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Load and display the user's information and timelines. */
|
2011-03-01 08:23:51 -06:00
|
|
|
function loadUser(username) {
|
2011-03-01 18:00:51 -06:00
|
|
|
|
|
|
|
// call the user_summary API function
|
2011-03-01 08:23:51 -06:00
|
|
|
$.ajax({
|
|
|
|
url: "/ts_api/app/user_summary/" + username,
|
|
|
|
type: "GET",
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
success: function(data, textStatus, jqXHR) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// set the user variable
|
2011-03-01 08:23:51 -06:00
|
|
|
user = data.user;
|
2011-03-01 18:00:51 -06:00
|
|
|
|
|
|
|
// set the timelines variable
|
2011-03-01 08:23:51 -06:00
|
|
|
timelines = data.timelines;
|
2011-03-01 18:00:51 -06:00
|
|
|
|
|
|
|
// update the user id display
|
2011-03-01 08:23:51 -06:00
|
|
|
$("#fullname").text(user.name);
|
|
|
|
$("#username").text("- " + user.username);
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// pre-populate the editable user-info fields
|
2011-03-01 08:23:51 -06:00
|
|
|
// TODO: not working
|
|
|
|
$("#fullname-input").text(user.name);
|
|
|
|
$("#email-input").text(user.email);
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// set the active timeline to the first in the list
|
|
|
|
// TODO: implement a mechanism to remember the last used timeline
|
|
|
|
// on the server side and respond to that here.
|
2011-03-01 08:23:51 -06:00
|
|
|
activeTimeline = timelines[0];
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// update the timeline display
|
2011-03-01 08:23:51 -06:00
|
|
|
$("#timeline-name").text(activeTimeline.timeline_id + " |");
|
|
|
|
$("#timeline-desc").text(activeTimeline.description);
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// TODO: populate the drop-down list for the available timeline
|
|
|
|
// choices
|
|
|
|
|
|
|
|
// load the entries for this timeline
|
|
|
|
loadEntries(user, activeTimeline)
|
2011-03-01 08:23:51 -06:00
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
error: function(jqXHR, textStatus, error) {
|
2011-03-01 18:00:51 -06:00
|
|
|
// TODO
|
2011-03-01 08:23:51 -06:00
|
|
|
alert("TODO: handle error for user load.")
|
2011-03-01 18:00:51 -06:00
|
|
|
alert(jqXHR.responseText)
|
2011-03-01 08:23:51 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Read the first 50 entries for a timeline. */
|
|
|
|
function loadEntries(user, timeline) {
|
|
|
|
|
|
|
|
// call the API list_entries function via AJAX
|
2011-03-01 08:23:51 -06:00
|
|
|
$.ajax({
|
|
|
|
url: "/ts_api/entries/" + user.username + "/" + timeline.timeline_id,
|
|
|
|
type: "GET",
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
success: function(data, textStatus, jqXHR) {
|
2011-03-01 18:00:51 -06:00
|
|
|
entries = data.entries;
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
// push the entries onto the page
|
|
|
|
displayEntries(entries)
|
2011-03-01 08:23:51 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Show/hide the editable user-info panel. */
|
2011-02-12 07:48:19 -06:00
|
|
|
function toggleUserInfo(event) {
|
|
|
|
$("#user-info").slideToggle("slow");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Show/hide the password change controls. */
|
2011-02-24 07:29:30 -06:00
|
|
|
function showChangePwd(event) { $("#change-pwd").slideToggle("slow"); }
|
2011-02-12 07:48:19 -06:00
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Update the user information based on the editable user-info panel. */
|
2011-02-12 07:48:19 -06:00
|
|
|
function updateUser(event) {
|
|
|
|
alert("TODO: update user via AJAX.");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Show/hide the editable timeline-info panel. */
|
2011-02-12 07:48:19 -06:00
|
|
|
function toggleTimelineInfo(event) {
|
|
|
|
$("#timeline-info").slideToggle("slow");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Show the change timeline menu. */
|
2011-02-12 14:57:29 -06:00
|
|
|
function showTimelineMenu(event) {
|
|
|
|
alert("TODO: show other timelines via a popup menu");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Update the timeline details based on the editable timeline-info panel. */
|
2011-02-12 14:57:29 -06:00
|
|
|
function updateTimeline(event) {
|
|
|
|
alert("TODO: update timeline via AJAX.");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Show/hide the add notes panel. */
|
2011-02-24 07:29:30 -06:00
|
|
|
function showNewNotes(event) { $("#add-notes").slideToggle("slow"); }
|
2011-02-13 11:38:50 -06:00
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* Create a new entry based on the user's input in the new-entry panel. */
|
2011-02-14 09:01:32 -06:00
|
|
|
function newEntry(event) {
|
|
|
|
alert("TODO: create entry vi AJAX");
|
|
|
|
event.preventDefault();
|
|
|
|
}
|