2012-09-07 21:06:41 +02:00
|
|
|
/*jslint browser: true, devel: true, sloppy: true,
|
|
|
|
plusplus: true, nomen: true, regexp: true */
|
|
|
|
/*global $: false, jQuery: false, ich: false,
|
2012-09-12 22:55:37 +02:00
|
|
|
zephyr_json: false, initial_pointer: false, username: false,
|
2012-09-07 21:06:41 +02:00
|
|
|
class_list: false, instance_list: false, people_list: false */
|
|
|
|
|
2012-09-06 19:54:29 +02:00
|
|
|
$(function () {
|
2012-08-29 23:22:27 +02:00
|
|
|
$('#zephyr-type-tabs a[href="#class-message"]').on('shown', function (e) {
|
|
|
|
$('#class-message input:not(:hidden):first').focus().select();
|
|
|
|
});
|
|
|
|
$('#zephyr-type-tabs a[href="#personal-message"]').on('shown', function (e) {
|
|
|
|
$('#personal-message input:not(:hidden):first').focus().select();
|
|
|
|
});
|
2012-09-14 21:21:49 +02:00
|
|
|
|
|
|
|
// Prepare the click handler for subbing to a new class to which
|
|
|
|
// you have composed a zephyr.
|
|
|
|
$('#create-it').click(function () {
|
|
|
|
sub(compose_class_name());
|
|
|
|
$("#class-message form").ajaxSubmit();
|
|
|
|
$('#class-dne').stop(true).fadeOut(500);
|
|
|
|
});
|
2012-09-14 21:25:00 +02:00
|
|
|
|
|
|
|
// Prepare the click handler for subbing to an existing class.
|
|
|
|
$('#sub-it').click(function () {
|
|
|
|
sub(compose_class_name());
|
|
|
|
$("#class-message form").ajaxSubmit();
|
|
|
|
$('#class-nosub').stop(true).fadeOut(500);
|
|
|
|
});
|
2012-08-29 23:22:27 +02:00
|
|
|
});
|
|
|
|
|
2012-08-29 17:12:21 +02:00
|
|
|
$.ajaxSetup({
|
2012-09-07 20:18:34 +02:00
|
|
|
beforeSend: function (xhr, settings) {
|
|
|
|
function getCookie(name) {
|
2012-09-07 20:39:58 +02:00
|
|
|
var i, cookies, cookieValue = null;
|
2012-09-07 20:35:15 +02:00
|
|
|
if (document.cookie && document.cookie !== '') {
|
2012-09-07 20:39:58 +02:00
|
|
|
cookies = document.cookie.split(';');
|
|
|
|
for (i = 0; i < cookies.length; i++) {
|
2012-09-07 20:18:34 +02:00
|
|
|
var cookie = jQuery.trim(cookies[i]);
|
|
|
|
// Does this cookie string begin with the name we want?
|
2012-09-07 20:35:15 +02:00
|
|
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
2012-09-07 20:18:34 +02:00
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cookieValue;
|
|
|
|
}
|
|
|
|
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
|
|
|
|
// Only send the token to relative URLs i.e. locally.
|
|
|
|
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 17:12:21 +02:00
|
|
|
});
|
|
|
|
|
2012-09-05 23:38:20 +02:00
|
|
|
function sub(zephyr_class) {
|
|
|
|
// Supports multiple classes, separate by commas.
|
|
|
|
// TODO: check the return value and handle an error condition
|
|
|
|
$.post('/subscriptions/add/', {new_subscriptions: zephyr_class});
|
|
|
|
}
|
|
|
|
|
2012-09-13 00:01:39 +02:00
|
|
|
function hide_compose() {
|
|
|
|
$('input, textarea, button').blur();
|
|
|
|
$('.zephyr_compose').slideUp('fast');
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_compose(tabname) {
|
|
|
|
$('.zephyr_compose').slideDown('fast');
|
|
|
|
if (tabname) {
|
|
|
|
$('#zephyr-type-tabs a[href="#' + tabname + '-message"]').tab('show');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 23:48:20 +02:00
|
|
|
function compose_class_name() {
|
|
|
|
return $.trim($("#class").val());
|
|
|
|
}
|
|
|
|
|
2012-09-06 19:54:29 +02:00
|
|
|
$(function () {
|
2012-08-30 18:24:16 +02:00
|
|
|
var status_classes = 'alert-error alert-success alert-info';
|
|
|
|
var send_status = $('#send-status');
|
|
|
|
var buttons = $('#class-message, #personal-message').find('input[type="submit"]');
|
2012-08-30 21:35:56 +02:00
|
|
|
|
2012-08-30 18:24:16 +02:00
|
|
|
var options = {
|
2012-09-05 19:36:58 +02:00
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
2012-08-30 18:24:16 +02:00
|
|
|
beforeSubmit: function (form, _options) {
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-info')
|
|
|
|
.text('Sending')
|
|
|
|
.stop(true).fadeTo(0,1);
|
|
|
|
buttons.attr('disabled', 'disabled');
|
2012-09-07 20:41:34 +02:00
|
|
|
buttons.blur();
|
2012-09-06 21:19:56 +02:00
|
|
|
|
2012-09-07 20:35:15 +02:00
|
|
|
if ($("#class-message:visible")[0] === undefined) {// we're not dealing with classes
|
2012-09-06 21:19:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-09-13 23:48:20 +02:00
|
|
|
|
|
|
|
var zephyr_class = compose_class_name();
|
|
|
|
if (zephyr_class === "") {
|
|
|
|
// You can't try to send to an empty class.
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.text('Please specify a class')
|
|
|
|
.stop(true).fadeTo(0,1);
|
|
|
|
buttons.removeAttr('disabled');
|
|
|
|
$('#class-message input:not(:hidden):first').focus().select();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-05 23:38:20 +02:00
|
|
|
var okay = true;
|
2012-09-06 21:30:02 +02:00
|
|
|
$.ajax({
|
2012-09-13 23:48:20 +02:00
|
|
|
url: "subscriptions/exists/" + zephyr_class,
|
2012-09-06 21:30:02 +02:00
|
|
|
async: false,
|
|
|
|
success: function (data) {
|
2012-09-07 20:35:15 +02:00
|
|
|
if (data === "False") {
|
2012-09-06 21:30:02 +02:00
|
|
|
// The class doesn't exist
|
|
|
|
okay = false;
|
2012-09-07 20:41:34 +02:00
|
|
|
send_status.removeClass(status_classes);
|
2012-09-06 21:30:02 +02:00
|
|
|
send_status.toggle();
|
2012-09-13 23:48:20 +02:00
|
|
|
$('#class-dne-name').text(zephyr_class);
|
2012-09-06 21:30:02 +02:00
|
|
|
$('#class-dne').show();
|
2012-09-14 21:21:49 +02:00
|
|
|
$('#create-it').focus();
|
2012-09-06 21:30:02 +02:00
|
|
|
buttons.removeAttr('disabled');
|
2012-09-13 21:42:06 +02:00
|
|
|
hide_compose();
|
2012-09-06 21:30:02 +02:00
|
|
|
}
|
2012-09-05 23:38:20 +02:00
|
|
|
}
|
|
|
|
});
|
2012-09-13 23:48:20 +02:00
|
|
|
if (okay && class_list.indexOf(zephyr_class) === -1) {
|
2012-09-05 23:38:20 +02:00
|
|
|
// You're not subbed to the class
|
|
|
|
okay = false;
|
|
|
|
send_status.removeClass(status_classes);
|
|
|
|
send_status.toggle();
|
2012-09-13 23:48:20 +02:00
|
|
|
$('#class-nosub-name').text(zephyr_class);
|
2012-09-05 23:38:20 +02:00
|
|
|
$('#class-nosub').show();
|
2012-09-14 21:25:00 +02:00
|
|
|
$('#sub-it').focus();
|
2012-09-05 23:38:20 +02:00
|
|
|
buttons.removeAttr('disabled');
|
2012-09-13 21:42:06 +02:00
|
|
|
hide_compose();
|
2012-09-05 23:38:20 +02:00
|
|
|
}
|
|
|
|
return okay;
|
2012-08-30 18:24:16 +02:00
|
|
|
},
|
|
|
|
success: function (resp, statusText, xhr, form) {
|
|
|
|
form.find('textarea').val('');
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-success')
|
|
|
|
.text('Sent message')
|
2012-09-13 21:42:06 +02:00
|
|
|
.stop(true).fadeTo(0,1).delay(1000).fadeOut(1000, hide_compose);
|
2012-08-30 18:24:16 +02:00
|
|
|
buttons.removeAttr('disabled');
|
2012-09-13 17:47:07 +02:00
|
|
|
clear_compose_box();
|
2012-08-30 18:24:16 +02:00
|
|
|
},
|
2012-09-06 19:54:29 +02:00
|
|
|
error: function (xhr) {
|
2012-09-05 19:36:58 +02:00
|
|
|
var response = "Error sending message";
|
2012-09-07 20:35:15 +02:00
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
2012-09-05 19:36:58 +02:00
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
|
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
|
|
}
|
2012-08-30 18:24:16 +02:00
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
2012-09-05 19:36:58 +02:00
|
|
|
.text(response)
|
2012-08-30 21:35:56 +02:00
|
|
|
.append($('<span />')
|
|
|
|
.addClass('send-status-close').html('×')
|
|
|
|
.click(function () { send_status.stop(true).fadeOut(500); }))
|
2012-08-30 18:24:16 +02:00
|
|
|
.stop(true).fadeTo(0,1);
|
2012-08-30 21:35:56 +02:00
|
|
|
|
2012-08-30 18:24:16 +02:00
|
|
|
buttons.removeAttr('disabled');
|
|
|
|
}
|
|
|
|
};
|
2012-08-30 21:35:56 +02:00
|
|
|
|
2012-08-30 18:24:16 +02:00
|
|
|
send_status.hide();
|
|
|
|
$("#class-message form").ajaxForm(options);
|
|
|
|
$("#personal-message form").ajaxForm(options);
|
2012-08-30 17:28:15 +02:00
|
|
|
});
|
|
|
|
|
2012-09-06 19:09:43 +02:00
|
|
|
var selected_zephyr_id = 0; /* to be filled in on document.ready */
|
2012-09-06 20:16:19 +02:00
|
|
|
var last_received = -1;
|
|
|
|
|
|
|
|
function get_all_zephyr_rows() {
|
2012-09-12 17:47:56 +02:00
|
|
|
return $('tr.zephyr_row');
|
2012-09-06 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_next_visible(zephyr_row) {
|
|
|
|
return zephyr_row.nextAll(':visible:first');
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_prev_visible(zephyr_row) {
|
|
|
|
return zephyr_row.prevAll(':visible:first');
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_id(zephyr_row) {
|
|
|
|
return zephyr_row.attr('id');
|
|
|
|
}
|
2012-08-30 22:19:34 +02:00
|
|
|
|
2012-09-13 16:58:29 +02:00
|
|
|
function get_zephyr(zephyr_id) {
|
|
|
|
return $("#" + zephyr_id);
|
|
|
|
}
|
|
|
|
|
2012-09-07 20:44:55 +02:00
|
|
|
function scroll_to_selected() {
|
|
|
|
var main_div = $('#main_div');
|
|
|
|
main_div.scrollTop(0);
|
2012-09-13 17:44:51 +02:00
|
|
|
main_div.scrollTop(get_zephyr(selected_zephyr_id).offset().top - main_div.height()/1.5);
|
2012-09-07 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 16:23:55 +02:00
|
|
|
function respond_to_zephyr() {
|
2012-09-13 20:30:41 +02:00
|
|
|
var parent, zephyr;
|
2012-09-12 16:23:55 +02:00
|
|
|
var recipient, recipients;
|
2012-09-13 17:44:51 +02:00
|
|
|
parent = get_zephyr(selected_zephyr_id);
|
2012-09-13 20:30:41 +02:00
|
|
|
zephyr = zephyr_dict[parent.attr('id')];
|
|
|
|
|
|
|
|
$('.zephyr_compose').slideToggle('fast');
|
|
|
|
|
|
|
|
if (zephyr.type === 'class') {
|
2012-09-12 16:23:55 +02:00
|
|
|
$('#zephyr-type-tabs a[href="#class-message"]').tab('show');
|
2012-09-13 20:30:41 +02:00
|
|
|
$("#class").val(zephyr.display_recipient);
|
|
|
|
$("#instance").val(zephyr.instance);
|
2012-09-12 16:23:55 +02:00
|
|
|
$("#new_zephyr").focus();
|
|
|
|
$("#new_zephyr").select();
|
2012-09-13 20:30:41 +02:00
|
|
|
} else if (zephyr.type === 'huddle') {
|
|
|
|
$('#zephyr-type-tabs a[href="#personal-message"]').tab('show');
|
|
|
|
recipient = '';
|
|
|
|
for (i in zephyr.display_recipient) {
|
|
|
|
recipient += zephyr.display_recipient[i].name + ', ';
|
|
|
|
}
|
|
|
|
$("#recipient").val(recipient);
|
|
|
|
$("#new_personal_zephyr").focus();
|
|
|
|
$("#new_personal_zephyr").select();
|
|
|
|
} else if (zephyr.type === 'personal') {
|
2012-09-12 16:23:55 +02:00
|
|
|
// Until we allow sending zephyrs based on multiple meaningful
|
|
|
|
// representations of a user (name, username, email, etc.), just
|
|
|
|
// deal with usernames.
|
2012-09-13 20:30:41 +02:00
|
|
|
recipient = zephyr.display_recipient;
|
|
|
|
if ( recipient === username) { // that is, we sent the original message
|
|
|
|
recipient = zephyr.sender;
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
|
|
|
prepare_huddle(recipient);
|
|
|
|
}
|
2012-09-13 20:30:41 +02:00
|
|
|
|
|
|
|
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 16:58:29 +02:00
|
|
|
function update_pointer(zephyr) {
|
2012-09-13 21:47:17 +02:00
|
|
|
var new_selected = get_id(zephyr);
|
|
|
|
if (new_selected == selected_zephyr_id)
|
|
|
|
return;
|
|
|
|
selected_zephyr_id = new_selected;
|
2012-09-13 16:58:29 +02:00
|
|
|
|
2012-09-13 17:53:13 +02:00
|
|
|
$('.selected_zephyr').removeClass('selected_zephyr');
|
|
|
|
zephyr.addClass('selected_zephyr');
|
2012-09-13 16:58:29 +02:00
|
|
|
|
|
|
|
$.post("update", { pointer: selected_zephyr_id });
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_pointer_by_id(zephyr_id) {
|
|
|
|
update_pointer(get_zephyr(zephyr_id));
|
|
|
|
}
|
|
|
|
|
2012-09-06 19:09:43 +02:00
|
|
|
function select_zephyr(zephyr_id) {
|
2012-09-13 17:44:51 +02:00
|
|
|
var next_zephyr = get_zephyr(zephyr_id);
|
2012-09-12 16:23:55 +02:00
|
|
|
var main_div = $("#main_div");
|
2012-09-06 19:32:02 +02:00
|
|
|
|
|
|
|
/* If the zephyr exists but is hidden, try to find the next visible one. */
|
2012-09-07 20:35:15 +02:00
|
|
|
if (next_zephyr.length !== 0 && next_zephyr.is(':hidden')) {
|
2012-09-06 20:16:19 +02:00
|
|
|
next_zephyr = get_next_visible(next_zephyr);
|
2012-09-06 19:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall back to the first visible zephyr. */
|
2012-09-07 20:35:15 +02:00
|
|
|
if (next_zephyr.length === 0) {
|
2012-09-06 19:32:02 +02:00
|
|
|
next_zephyr = $('tr:not(:hidden):first');
|
2012-09-05 16:56:10 +02:00
|
|
|
}
|
2012-09-06 19:32:02 +02:00
|
|
|
|
2012-09-13 16:58:29 +02:00
|
|
|
update_pointer(next_zephyr);
|
2012-09-05 16:56:10 +02:00
|
|
|
|
2012-09-06 19:09:43 +02:00
|
|
|
if ((next_zephyr.offset().top < main_div.offset().top) ||
|
|
|
|
(next_zephyr.offset().top + next_zephyr.height() >
|
|
|
|
main_div.offset().top + main_div.height())) {
|
2012-09-05 16:56:10 +02:00
|
|
|
scroll_to_selected();
|
2012-08-31 17:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-13 21:18:00 +02:00
|
|
|
|
|
|
|
/* We use 'visibility' rather than 'display' and jQuery's show() / hide(),
|
|
|
|
because we want to reserve space for the email address. This avoids
|
|
|
|
things jumping around slightly when the email address is shown. */
|
|
|
|
|
|
|
|
function show_email(zephyr_id) {
|
|
|
|
get_zephyr(zephyr_id).find('.zephyr_sender_email').removeClass('invisible');
|
|
|
|
}
|
|
|
|
|
|
|
|
function hide_email(zephyr_id) {
|
|
|
|
get_zephyr(zephyr_id).find('.zephyr_sender_email').addClass('invisible');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-07 21:42:11 +02:00
|
|
|
function process_hotkey(code) {
|
2012-09-12 16:23:55 +02:00
|
|
|
var next_zephyr;
|
|
|
|
|
2012-09-07 23:11:21 +02:00
|
|
|
switch (code) {
|
|
|
|
case 40: // down arrow
|
|
|
|
case 38: // up arrow
|
|
|
|
if (code === 40) {
|
2012-09-13 17:44:51 +02:00
|
|
|
next_zephyr = get_next_visible(get_zephyr(selected_zephyr_id));
|
2012-09-07 23:11:21 +02:00
|
|
|
} else {
|
2012-09-13 17:44:51 +02:00
|
|
|
next_zephyr = get_prev_visible(get_zephyr(selected_zephyr_id));
|
2012-09-07 21:42:11 +02:00
|
|
|
}
|
|
|
|
if (next_zephyr.length !== 0) {
|
|
|
|
select_zephyr(get_id(next_zephyr));
|
|
|
|
}
|
2012-09-11 20:12:39 +02:00
|
|
|
if ((next_zephyr.length === 0) && (code === 40)) {
|
|
|
|
// At the last zephyr, scroll to the bottom so we have
|
|
|
|
// lots of nice whitespace for new zephyrs coming in.
|
|
|
|
$("#main_div").scrollTop($("#main_div").prop("scrollHeight"));
|
|
|
|
}
|
2012-09-08 00:51:59 +02:00
|
|
|
return process_hotkey;
|
2012-09-07 23:11:21 +02:00
|
|
|
|
2012-09-13 00:05:49 +02:00
|
|
|
case 27: // Esc: hide compose pane
|
|
|
|
hide_compose();
|
|
|
|
return process_hotkey;
|
|
|
|
|
2012-09-07 23:11:21 +02:00
|
|
|
case 82: // 'r': respond to zephyr
|
2012-09-12 16:23:55 +02:00
|
|
|
respond_to_zephyr();
|
2012-09-08 00:51:59 +02:00
|
|
|
return process_key_in_input;
|
2012-09-07 23:11:21 +02:00
|
|
|
|
|
|
|
case 71: // 'g': start of "go to" command
|
2012-09-08 00:51:59 +02:00
|
|
|
return process_goto_hotkey;
|
2012-09-07 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_goto_hotkey(code) {
|
2012-09-07 23:11:21 +02:00
|
|
|
switch (code) {
|
|
|
|
case 67: // 'c': narrow by recipient
|
2012-09-13 17:44:51 +02:00
|
|
|
parent = get_zephyr(selected_zephyr_id);
|
2012-09-13 22:20:07 +02:00
|
|
|
zephyr_class = parent.find(".zephyr_class").text();
|
|
|
|
zephyr_huddle = parent.find(".zephyr_huddle_recipient").text();
|
2012-09-11 18:12:02 +02:00
|
|
|
if (zephyr_class == '' && zephyr_huddle == '') {
|
|
|
|
narrow_personals();
|
|
|
|
} else if (zephyr_class == '') {
|
|
|
|
narrow_huddle();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
narrow_class();
|
|
|
|
}
|
2012-09-08 00:51:59 +02:00
|
|
|
break;
|
2012-09-07 23:11:21 +02:00
|
|
|
|
|
|
|
case 73: // 'i': narrow by instance
|
2012-09-10 20:40:03 +02:00
|
|
|
narrow_instance();
|
2012-09-08 00:51:59 +02:00
|
|
|
break;
|
2012-09-07 23:11:21 +02:00
|
|
|
|
|
|
|
case 80: // 'p': narrow to personals
|
2012-09-07 21:42:11 +02:00
|
|
|
narrow_all_personals();
|
2012-09-08 00:51:59 +02:00
|
|
|
break;
|
2012-09-07 23:11:21 +02:00
|
|
|
|
|
|
|
case 65: // 'a': un-narrow
|
2012-09-12 19:18:49 +02:00
|
|
|
show_all_messages();
|
2012-09-08 00:51:59 +02:00
|
|
|
break;
|
2012-09-13 00:05:49 +02:00
|
|
|
|
|
|
|
case 27: // Esc: hide compose pane
|
|
|
|
hide_compose();
|
|
|
|
break;
|
2012-09-07 21:42:11 +02:00
|
|
|
}
|
|
|
|
|
2012-09-08 00:51:59 +02:00
|
|
|
/* Always return to the initial hotkey mode, even
|
|
|
|
with an unrecognized "go to" command. */
|
|
|
|
return process_hotkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function process_key_in_input(code) {
|
|
|
|
if (code === 27) {
|
|
|
|
// User hit Escape key
|
2012-09-13 00:01:39 +02:00
|
|
|
hide_compose();
|
2012-09-08 00:51:59 +02:00
|
|
|
return process_hotkey;
|
|
|
|
}
|
2012-09-07 21:42:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-08 00:51:59 +02:00
|
|
|
/* The current handler function for keydown events.
|
|
|
|
It should return a new handler, or 'false' to
|
|
|
|
decline to handle the event. */
|
|
|
|
var keydown_handler = process_hotkey;
|
|
|
|
|
2012-09-06 19:54:29 +02:00
|
|
|
$(document).keydown(function (event) {
|
2012-09-08 00:51:59 +02:00
|
|
|
var result = keydown_handler(event.keyCode);
|
|
|
|
if (typeof result === 'function') {
|
|
|
|
keydown_handler = result;
|
2012-08-30 23:13:05 +02:00
|
|
|
event.preventDefault();
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
2012-09-08 00:51:59 +02:00
|
|
|
});
|
2012-09-07 21:42:11 +02:00
|
|
|
|
2012-09-08 00:51:59 +02:00
|
|
|
// NB: This just binds to current elements, and won't bind to elements
|
|
|
|
// created after ready() is called.
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
$('input, textarea, button').focus(function () {
|
|
|
|
keydown_handler = process_key_in_input;
|
|
|
|
});
|
|
|
|
$('input, textarea, button').blur(function () {
|
|
|
|
keydown_handler = process_hotkey;
|
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
});
|
|
|
|
|
2012-09-04 16:56:48 +02:00
|
|
|
function home_view(element) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_view_predicate = home_view;
|
|
|
|
|
2012-09-06 19:52:53 +02:00
|
|
|
function apply_view(element) {
|
2012-09-04 16:56:48 +02:00
|
|
|
if (current_view_predicate(element)) {
|
2012-09-04 18:45:51 +02:00
|
|
|
element.show();
|
2012-09-04 16:56:48 +02:00
|
|
|
} else {
|
2012-09-04 18:45:51 +02:00
|
|
|
element.hide();
|
2012-09-04 16:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 20:39:33 +02:00
|
|
|
function prepare_huddle(recipients) {
|
|
|
|
// Used for both personals and huddles.
|
2012-09-13 00:01:39 +02:00
|
|
|
show_compose('personal');
|
2012-09-11 20:39:33 +02:00
|
|
|
$("#recipient").val(recipients);
|
|
|
|
$("#new_personal_zephyr").focus();
|
|
|
|
$("#new_personal_zephyr").select();
|
|
|
|
}
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
function do_narrow(description, filter_function) {
|
2012-09-11 21:37:09 +02:00
|
|
|
// Hide the messages temporarily, so the browser doesn't waste time
|
|
|
|
// incrementally recalculating the layout.
|
|
|
|
$("#main_div").hide();
|
|
|
|
|
2012-08-31 23:52:11 +02:00
|
|
|
// We want the zephyr on which the narrow happened to stay in the same place if possible.
|
2012-09-13 17:44:51 +02:00
|
|
|
var old_top = $("#main_div").offset().top - get_zephyr(selected_zephyr_id).offset().top;
|
2012-09-04 16:56:48 +02:00
|
|
|
current_view_predicate = filter_function;
|
2012-09-06 20:16:19 +02:00
|
|
|
get_all_zephyr_rows().each(function () {
|
2012-09-06 19:52:53 +02:00
|
|
|
apply_view($(this));
|
2012-09-04 16:56:48 +02:00
|
|
|
});
|
2012-08-29 22:23:56 +02:00
|
|
|
|
2012-09-11 21:37:09 +02:00
|
|
|
// Show the new set of messages.
|
|
|
|
$("#main_div").show();
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
select_zephyr(selected_zephyr_id);
|
2012-09-04 17:38:10 +02:00
|
|
|
scroll_to_selected();
|
2012-08-29 22:23:56 +02:00
|
|
|
|
2012-09-12 19:18:49 +02:00
|
|
|
$("#show_all_messages").removeAttr("disabled");
|
2012-09-13 22:20:07 +02:00
|
|
|
$("#narrowbox").show();
|
2012-09-12 19:18:49 +02:00
|
|
|
$("#currently_narrowed_to").html(description);
|
2012-08-29 22:23:56 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
function narrow_huddle() {
|
2012-09-13 22:20:07 +02:00
|
|
|
var recipients = get_zephyr(selected_zephyr_id).find(".zephyr_huddle_recipients_list").text();
|
2012-09-11 21:33:24 +02:00
|
|
|
var message = "Group chats with " + recipients;
|
2012-09-06 19:47:31 +02:00
|
|
|
do_narrow(message, function (element) {
|
2012-09-13 22:20:07 +02:00
|
|
|
return (element.find(".zephyr_huddle_recipient").length > 0 &&
|
|
|
|
element.find(".zephyr_huddle_recipients_list").text() === recipients);
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-09-05 01:27:58 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
function narrow_all_personals() {
|
2012-09-05 17:00:47 +02:00
|
|
|
// Narrow to all personals
|
2012-09-11 21:33:24 +02:00
|
|
|
var message = "All huddles with you";
|
2012-09-06 19:47:31 +02:00
|
|
|
do_narrow(message, function (element) {
|
2012-09-13 22:20:07 +02:00
|
|
|
return (element.find(".zephyr_personal_recipient").length > 0);
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-09-05 17:00:47 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
function narrow_personals() {
|
2012-09-05 17:00:47 +02:00
|
|
|
// Narrow to personals with a specific user
|
2012-09-13 17:44:51 +02:00
|
|
|
var target_zephyr = get_zephyr(selected_zephyr_id);
|
2012-09-13 20:30:41 +02:00
|
|
|
var zephyr_obj = zephyr_dict[target_zephyr.attr('id')];
|
2012-09-05 17:48:27 +02:00
|
|
|
var other_party;
|
2012-09-13 20:30:41 +02:00
|
|
|
if (zephyr_obj.display_recipient === username) {
|
|
|
|
other_party = zephyr_obj.sender;
|
2012-09-05 17:48:27 +02:00
|
|
|
} else {
|
2012-09-13 20:30:41 +02:00
|
|
|
other_party = zephyr_obj.display_recipient;
|
2012-09-05 17:48:27 +02:00
|
|
|
}
|
2012-09-11 21:33:24 +02:00
|
|
|
var message = "Huddles with " + other_party;
|
2012-09-06 19:47:31 +02:00
|
|
|
do_narrow(message, function (element) {
|
2012-09-13 20:30:41 +02:00
|
|
|
var other_zephyr_obj = zephyr_dict[target_zephyr.attr('id')];
|
2012-09-13 22:20:07 +02:00
|
|
|
var recipient = element.find(".zephyr_personal_recipient");
|
|
|
|
var sender = element.find(".zephyr_sender");
|
2012-09-06 19:47:31 +02:00
|
|
|
|
2012-09-07 21:04:21 +02:00
|
|
|
return (recipient.length > 0) &&
|
2012-09-13 20:30:41 +02:00
|
|
|
(((other_zephyr_obj.display_recipient === zephyr_obj.display_recipient) && (other_zephyr_obj.sender === zephyr_obj.sender)) ||
|
|
|
|
((other_zephyr_obj.display_recipient === zephyr_obj.sender) && (other_zephyr_obj.sender === zephyr_obj.display_recipient)));
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-08-31 23:52:11 +02:00
|
|
|
}
|
2012-08-29 18:54:58 +02:00
|
|
|
|
2012-09-10 20:40:03 +02:00
|
|
|
function narrow_class() {
|
2012-09-13 17:44:51 +02:00
|
|
|
var parent = get_zephyr(selected_zephyr_id);
|
2012-09-13 22:20:07 +02:00
|
|
|
var zephyr_class = parent.find(".zephyr_class").text();
|
2012-09-11 21:33:24 +02:00
|
|
|
var message = "<span class='zephyr_class'>" + zephyr_class + "</span>";
|
2012-09-06 19:47:31 +02:00
|
|
|
do_narrow(message, function (element) {
|
2012-09-13 22:20:07 +02:00
|
|
|
return (element.find(".zephyr_class").length > 0 &&
|
|
|
|
element.find(".zephyr_class").text() === zephyr_class);
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 20:40:03 +02:00
|
|
|
function narrow_instance() {
|
2012-09-13 17:44:51 +02:00
|
|
|
var parent = get_zephyr(selected_zephyr_id);
|
2012-09-13 22:20:07 +02:00
|
|
|
var zephyr_class = parent.find(".zephyr_class").text();
|
|
|
|
var zephyr_instance = parent.find(".zephyr_instance").text();
|
2012-09-11 21:33:24 +02:00
|
|
|
var message = "<span class='zephyr_class'>" + zephyr_class
|
|
|
|
+ "</span> | <span class='zephyr_instance'>" + zephyr_instance + "</span>";
|
2012-09-06 19:47:31 +02:00
|
|
|
do_narrow(message, function (element) {
|
2012-09-13 22:20:07 +02:00
|
|
|
return (element.find(".zephyr_class").length > 0 &&
|
|
|
|
element.find(".zephyr_class").text() === zephyr_class &&
|
|
|
|
element.find(".zephyr_instance").text() === zephyr_instance);
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 19:18:49 +02:00
|
|
|
function show_all_messages() {
|
2012-09-04 16:56:48 +02:00
|
|
|
current_view_predicate = home_view;
|
2012-09-06 20:16:19 +02:00
|
|
|
get_all_zephyr_rows().show();
|
2012-08-29 17:12:21 +02:00
|
|
|
|
2012-08-31 22:20:20 +02:00
|
|
|
scroll_to_selected();
|
2012-08-29 18:54:58 +02:00
|
|
|
|
2012-09-13 22:20:07 +02:00
|
|
|
$("#narrowbox").hide();
|
2012-09-12 19:18:49 +02:00
|
|
|
$("#show_all_messages").attr("disabled", "disabled");
|
|
|
|
$("#currently_narrowed_to").html("");
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-08-29 22:47:53 +02:00
|
|
|
function newline2br(content) {
|
|
|
|
return content.replace(/\n/g, '<br />');
|
|
|
|
}
|
|
|
|
|
2012-09-04 20:31:23 +02:00
|
|
|
function update_autocomplete() {
|
|
|
|
class_list.sort();
|
|
|
|
instance_list.sort();
|
|
|
|
people_list.sort();
|
|
|
|
|
|
|
|
$( "#class" ).autocomplete({
|
|
|
|
source: class_list
|
|
|
|
});
|
|
|
|
$( "#instance" ).autocomplete({
|
|
|
|
source: instance_list
|
|
|
|
});
|
|
|
|
$( "#recipient" ).autocomplete({
|
|
|
|
source: people_list
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-08-29 17:12:21 +02:00
|
|
|
function add_message(index, zephyr) {
|
2012-09-06 20:16:19 +02:00
|
|
|
last_received = Math.max(last_received, zephyr.id);
|
|
|
|
|
2012-09-07 20:35:15 +02:00
|
|
|
if (zephyr.type === 'class') {
|
2012-09-04 23:20:21 +02:00
|
|
|
zephyr.is_class = true;
|
2012-09-07 20:35:15 +02:00
|
|
|
if ($.inArray(zephyr.display_recipient, class_list) === -1) {
|
2012-09-04 20:31:23 +02:00
|
|
|
class_list.push(zephyr.display_recipient);
|
|
|
|
update_autocomplete();
|
|
|
|
}
|
2012-09-07 20:35:15 +02:00
|
|
|
if ($.inArray(zephyr.instance, instance_list) === -1) {
|
2012-09-04 20:31:23 +02:00
|
|
|
instance_list.push(zephyr.instance);
|
|
|
|
update_autocomplete();
|
|
|
|
}
|
2012-09-07 20:35:15 +02:00
|
|
|
} else if (zephyr.type === "huddle") {
|
2012-09-04 23:20:21 +02:00
|
|
|
zephyr.is_huddle = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
} else {
|
2012-09-04 18:20:07 +02:00
|
|
|
zephyr.is_personal = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2012-09-07 20:35:15 +02:00
|
|
|
if (zephyr.display_recipient !== username &&
|
|
|
|
$.inArray(zephyr.display_recipient, people_list) === -1) {
|
2012-09-04 20:31:23 +02:00
|
|
|
people_list.push(zephyr.display_recipient);
|
|
|
|
update_autocomplete();
|
|
|
|
}
|
2012-09-07 20:35:15 +02:00
|
|
|
if (zephyr.sender !== username &&
|
|
|
|
$.inArray(zephyr.sender, people_list) === -1) {
|
2012-09-04 20:31:23 +02:00
|
|
|
people_list.push(zephyr.sender);
|
|
|
|
update_autocomplete();
|
|
|
|
}
|
2012-08-29 18:53:03 +02:00
|
|
|
}
|
2012-09-04 18:20:07 +02:00
|
|
|
zephyr.html_content = newline2br(zephyr.content);
|
2012-08-30 20:24:29 +02:00
|
|
|
|
2012-09-12 22:35:06 +02:00
|
|
|
var time = new Date(zephyr.timestamp * 1000);
|
2012-09-12 23:07:13 +02:00
|
|
|
var two_digits = function (x) { return ('0' + x).slice(-2); }
|
|
|
|
zephyr.timestr = two_digits(time.getHours())
|
|
|
|
+ ':' + two_digits(time.getMinutes());
|
2012-09-13 07:00:28 +02:00
|
|
|
zephyr.full_date_str = time.toLocaleString();
|
2012-09-12 22:35:06 +02:00
|
|
|
|
2012-09-12 17:47:56 +02:00
|
|
|
var new_tr = $('<tr />').attr('id', zephyr.id).addClass('zephyr_row');
|
2012-09-04 18:20:07 +02:00
|
|
|
$('#table').append(new_tr);
|
|
|
|
new_tr.append(ich.zephyr(zephyr));
|
2012-09-06 19:52:53 +02:00
|
|
|
apply_view(new_tr);
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:20:07 +02:00
|
|
|
$(function () {
|
2012-09-12 22:55:37 +02:00
|
|
|
$(initial_zephyr_array).each(add_message);
|
2012-09-06 21:13:15 +02:00
|
|
|
select_zephyr(initial_pointer);
|
|
|
|
get_updates_longpoll();
|
2012-08-30 19:56:15 +02:00
|
|
|
});
|
|
|
|
|
2012-09-13 17:47:07 +02:00
|
|
|
function clear_compose_box() {
|
|
|
|
$("#zephyr_compose").find('input[type=text], textarea').val('');
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:33:04 +02:00
|
|
|
var longpoll_failures = 0;
|
2012-09-05 20:06:08 +02:00
|
|
|
function get_updates_longpoll() {
|
2012-09-05 20:00:56 +02:00
|
|
|
console.log(new Date() + ': longpoll started');
|
2012-08-31 21:33:04 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: 'get_updates_longpoll',
|
|
|
|
data: { last_received: last_received },
|
|
|
|
dataType: 'json',
|
2012-09-05 22:17:14 +02:00
|
|
|
timeout: 10*60*1000, // 10 minutes in ms
|
2012-08-31 21:33:04 +02:00
|
|
|
success: function (data) {
|
2012-09-05 20:00:56 +02:00
|
|
|
console.log(new Date() + ': longpoll success');
|
2012-08-31 21:33:04 +02:00
|
|
|
longpoll_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
|
|
|
|
2012-09-05 20:06:08 +02:00
|
|
|
if (data && data.zephyrs) {
|
|
|
|
$.each(data.zephyrs, add_message);
|
2012-09-12 22:55:37 +02:00
|
|
|
for (i in data.zephyrs) {
|
|
|
|
console.log(data.zephyrs[i]);
|
|
|
|
console.log(data.zephyrs[i].id);
|
|
|
|
zephyr_dict[data.zephyrs[i].id] = data.zephyrs[i];
|
|
|
|
}
|
2012-09-05 20:06:08 +02:00
|
|
|
}
|
2012-09-05 20:06:19 +02:00
|
|
|
setTimeout(get_updates_longpoll, 0);
|
2012-08-31 21:33:04 +02:00
|
|
|
},
|
2012-09-05 22:17:14 +02:00
|
|
|
error: function (xhr, error_type, exn) {
|
2012-09-07 20:35:15 +02:00
|
|
|
if (error_type === 'timeout') {
|
2012-09-05 22:17:14 +02:00
|
|
|
// Retry indefinitely on timeout.
|
|
|
|
console.log(new Date() + ': longpoll timed out');
|
|
|
|
longpoll_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-09-05 22:17:14 +02:00
|
|
|
} else {
|
|
|
|
console.log(new Date() + ': longpoll failed with ' + error_type +
|
|
|
|
' (' + longpoll_failures + ' failures)');
|
|
|
|
longpoll_failures += 1;
|
|
|
|
}
|
|
|
|
|
2012-09-05 22:33:04 +02:00
|
|
|
if (longpoll_failures >= 5) {
|
2012-08-31 21:33:04 +02:00
|
|
|
$('#connection-error').show();
|
|
|
|
} else {
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
2012-09-05 22:33:04 +02:00
|
|
|
|
|
|
|
var retry_sec = Math.min(90, Math.exp(longpoll_failures/2));
|
|
|
|
console.log(new Date() + ': longpoll retrying in ' + retry_sec + ' seconds');
|
|
|
|
setTimeout(get_updates_longpoll, retry_sec*1000);
|
2012-08-31 21:33:04 +02:00
|
|
|
}
|
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2012-09-06 19:54:29 +02:00
|
|
|
$(function () {
|
2012-09-04 20:31:23 +02:00
|
|
|
update_autocomplete();
|
2012-09-13 00:01:39 +02:00
|
|
|
$('.button-slide').click(show_compose);
|
2012-09-10 20:31:53 +02:00
|
|
|
});
|