2011-04-15 13:26:55 -05:00
|
|
|
// TimeStamper namespace
|
|
|
|
var TS = {};
|
2011-03-16 07:39:09 -05:00
|
|
|
|
2011-03-01 18:00:51 -06:00
|
|
|
/* 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-04-15 13:26:55 -05:00
|
|
|
// ======== DEFINE MODELS ========//
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
/* Entry model.
|
|
|
|
* Attributes
|
|
|
|
* - id
|
|
|
|
* - mark
|
|
|
|
* - notes
|
|
|
|
* - start
|
|
|
|
*/
|
|
|
|
TS.Entry = Backbone.Model.extend({
|
2011-03-08 18:02:33 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
});
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
/* Timeline model.
|
|
|
|
* Attributes:
|
|
|
|
* - id
|
|
|
|
* - desc
|
|
|
|
* - created
|
|
|
|
*/
|
|
|
|
TS.Timeline = Backbone.Model.extend({
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
});
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
/* User model.
|
|
|
|
* Attributes:
|
|
|
|
* - username
|
|
|
|
* - fullname
|
|
|
|
* - email
|
|
|
|
* - join_date
|
|
|
|
*/
|
|
|
|
TS.User = Backbone.Model.extend({
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
});
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.EntryList = Backbone.Collection.extend({
|
|
|
|
model: TS.Entry,
|
|
|
|
|
|
|
|
comparator: function(entry) { return entry.get('timestamp'); },
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll(this, "ur");
|
2011-03-01 08:23:51 -06:00
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
url: function() {
|
|
|
|
return "/entries/" + TS.currentUser.get('username') + "/" + this.timeline.get('id');
|
|
|
|
}
|
|
|
|
});
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.TimelineList = Backbone.Collection.extend({
|
|
|
|
model: TS.Timeline,
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
url: function() {
|
|
|
|
return "/timelines/" + TS.currentUser.get('username');
|
|
|
|
}
|
|
|
|
});
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
// ======== DEFINE VIEWS ========//
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
/* Entry view
|
|
|
|
*/
|
|
|
|
TS.EntryView = Backbone.View.extend({
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
model: TS.Entry,
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
events: {
|
|
|
|
"dblclick div.mark" : "editMark",
|
|
|
|
"dblclick div.timestamp" : "editTimestamp",
|
|
|
|
"keypress .mark-input" : "updateOnEnter"
|
|
|
|
"keypress .timestamp-input" : "updateOnEnter"
|
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll(this, 'render', 'close');
|
|
|
|
this.model.bind('change', this.render);
|
|
|
|
this.model.view = this;
|
2011-03-01 08:23:51 -06:00
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
render: function() {
|
|
|
|
$(this.el).html(ich.entry(this.model.toJSON()));
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-01 08:23:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
editMark: function() {
|
|
|
|
$(this.el).addClass('edit-mark');
|
|
|
|
this.$('.mark-input').focus();
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
editTimestamp: function() {
|
|
|
|
$(this.el).addClass('edit-timestamp');
|
|
|
|
this.$('timestamp-input').focus();
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-01 18:00:51 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
close: function() {
|
|
|
|
this.model.save({
|
|
|
|
mark: this.$('.mark-input').val(),
|
|
|
|
timestamp: this.$('.timestamp-input').val()});
|
|
|
|
$(this.el).removeClass('edit-mark edit-timestamp');
|
2011-03-03 17:05:30 -06:00
|
|
|
},
|
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
updateOnEnter: function(e) {
|
|
|
|
if(e.keyCode == 13) this.close();
|
2011-03-01 08:23:51 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.TimelineView = Backbone.View.extend({
|
2011-03-03 17:05:30 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
el: $("#timeline"),
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
model: TS.Timeline,
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
events: {
|
|
|
|
"dblclick .timeline-id" : "editId",
|
|
|
|
"dblclick .timeline-desc" : "editDesc",
|
|
|
|
"keypress .timeline-id-input" : "updateOnEnter",
|
|
|
|
"keypress .timeline-desc-input" : "updateOnEnter"
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll(this, 'render', 'close');
|
|
|
|
this.model.bind('change', this.render);
|
|
|
|
this.model.view = this;
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
render: function() {
|
|
|
|
this.$('.timeline-id').html('( ' +
|
|
|
|
this.model.get('id') + ' )');
|
|
|
|
this.$('.timeline-desc').text(this.model.get('desc'));
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
editId: function() {
|
|
|
|
$(this.el).addClass('edit-id');
|
|
|
|
this.$('.timeline-id-input').focus();
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
editDesc: function() {
|
|
|
|
$(this.el).addClass('edit-desc');
|
|
|
|
this.$('.timeline-desc-input').focus();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
this.model.save({
|
|
|
|
id: this.$('timeline-id-input').val(),
|
|
|
|
desc: this.$('.timeline-desc-input').val()});
|
|
|
|
$(this.el).removeClass('.edit-id .edit-desc');
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
updateOnEnter: function(e) {
|
|
|
|
if (e.keyCode == 13) this.close();
|
|
|
|
}
|
2011-03-03 17:05:30 -06:00
|
|
|
});
|
2011-02-13 11:38:50 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.UserView = Backbone.View.extend({
|
|
|
|
|
|
|
|
el: $("user"),
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
model: TS.User,
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
events: {
|
|
|
|
'dblclick .fullname': 'editFullname',
|
|
|
|
'keypress .fullname-input': 'updateOnEnter'
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll(this, 'render', 'close');
|
|
|
|
this.model.bind('change', this.render);
|
|
|
|
this.model.view = this;
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
render: function() {
|
|
|
|
this.$('.fullname').text(this.model.get('name'));
|
|
|
|
this.$('.username').text(this.model.get('id'));
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
editFullname: function() {
|
|
|
|
$(this.el).addClass('.edit-fullname');
|
|
|
|
this.$('.fullname-input').focus();
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
close: function() {
|
|
|
|
this.model.set({name: this.$('.fullname-input').val()});
|
|
|
|
this.model.save();
|
|
|
|
$(this.el).removeClass('.edit-fullname');
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
updateOnEnter: function(e) {
|
|
|
|
if (keyCode == 13) this.close();
|
|
|
|
}
|
|
|
|
});
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.currentUser = {};
|
|
|
|
TS.currentUser.model = new TS.User({
|
|
|
|
id: $("#user .username").text(),
|
|
|
|
name: $("#user .fullname").text() });
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.currentUser.view = new TS.UserView(TS.currentUser.model);
|
2011-03-08 18:02:33 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
})
|
2011-03-08 18:02:33 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
function login() {
|
|
|
|
// lookup the login dialog elements
|
|
|
|
var name = $("#login-name");
|
|
|
|
var pwd = $("#login-password");
|
2011-03-08 18:02:33 -06:00
|
|
|
|
2011-04-15 13:26:55 -05:00
|
|
|
// call the API via AJAX
|
|
|
|
$.ajax({
|
|
|
|
url: "/ts_api/login",
|
|
|
|
processData: false,
|
|
|
|
data: JSON.stringify({username: name.val(), password: pwd.val()}),
|
2011-03-08 18:02:33 -06:00
|
|
|
type: "POST",
|
|
|
|
|
|
|
|
error: function(jqXHR, textStatus, error) {
|
2011-04-15 13:26:55 -05:00
|
|
|
// assuming bad credentials (possible server error or bad request,
|
|
|
|
// we should check that, FIXME
|
|
|
|
var tips = $(".validate-tips");
|
|
|
|
tips.text("Incorrect username/password combination.");
|
|
|
|
tips.addClass("ui-state-error");
|
|
|
|
tips.slideDown();
|
|
|
|
},
|
2011-03-07 16:43:40 -06:00
|
|
|
|
2011-03-08 18:02:33 -06:00
|
|
|
success: function(data, textStatus, jqXHR) {
|
2011-04-15 13:26:55 -05:00
|
|
|
TS.currentUser = new TS.User(); // TODO
|
2011-03-08 18:02:33 -06:00
|
|
|
}});
|
2011-03-07 16:43:40 -06:00
|
|
|
}
|