2012-09-07 21:06:41 +02:00
|
|
|
/*jslint browser: true, devel: true, sloppy: true,
|
2012-09-24 19:36:17 +02:00
|
|
|
plusplus: true, nomen: true, regexp: true,
|
|
|
|
white: true, undef: true */
|
2012-09-24 17:10:12 +02:00
|
|
|
/*global $: false, jQuery: false, Handlebars: false,
|
2012-09-21 00:26:59 +02:00
|
|
|
zephyr_json: false, initial_pointer: false, email: false,
|
2012-09-27 22:12:57 +02:00
|
|
|
class_list: false, instance_list: false, people_list: false,
|
|
|
|
have_initial_messages: false */
|
2012-09-07 21:06:41 +02:00
|
|
|
|
2012-09-26 22:44:38 +02:00
|
|
|
var loading_spinner;
|
2012-09-24 17:10:12 +02:00
|
|
|
var templates = {};
|
|
|
|
$(function () {
|
2012-09-26 22:44:38 +02:00
|
|
|
// Display loading indicator. This disappears after the first
|
2012-09-27 21:44:54 +02:00
|
|
|
// get_updates completes.
|
2012-09-27 22:12:57 +02:00
|
|
|
if (have_initial_messages)
|
|
|
|
loading_spinner = new Spinner().spin($('#loading_spinner')[0]);
|
|
|
|
else
|
|
|
|
$('#loading_indicator').hide();
|
2012-09-26 22:44:38 +02:00
|
|
|
|
|
|
|
// Compile Handlebars templates.
|
2012-09-24 17:10:12 +02:00
|
|
|
templates.zephyr = Handlebars.compile($("#template_zephyr").html());
|
|
|
|
templates.subscription = Handlebars.compile($("#template_subscription").html());
|
|
|
|
});
|
|
|
|
|
2012-09-17 17:16:53 +02:00
|
|
|
function register_huddle_onclick(zephyr_row, sender) {
|
|
|
|
zephyr_row.find(".zephyr_sender").click(function (e) {
|
|
|
|
prepare_huddle(sender);
|
|
|
|
// The sender span is inside the messagebox, which also has an
|
|
|
|
// onclick handler. We don't want to trigger the messagebox
|
|
|
|
// handler.
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:37:21 +02:00
|
|
|
function register_onclick(zephyr_row, zephyr_id) {
|
|
|
|
zephyr_row.find(".messagebox").click(function (e) {
|
|
|
|
if (!(clicking && mouse_moved)) {
|
|
|
|
// Was a click (not a click-and-drag).
|
|
|
|
select_zephyr_by_id(zephyr_id);
|
|
|
|
respond_to_zephyr();
|
|
|
|
}
|
|
|
|
mouse_moved = false;
|
|
|
|
clicking = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-26 19:44:21 +02:00
|
|
|
var zephyr_array = [];
|
2012-09-14 21:50:58 +02:00
|
|
|
var zephyr_dict = {};
|
2012-09-26 19:53:14 +02:00
|
|
|
var instance_list = [];
|
2012-09-22 01:11:54 +02:00
|
|
|
var status_classes = 'alert-error alert-success alert-info';
|
|
|
|
|
|
|
|
function report_error(response, xhr, status_box) {
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
|
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_box.removeClass(status_classes).addClass('alert-error')
|
|
|
|
.text(response).stop(true).fadeTo(0, 1);
|
|
|
|
}
|
2012-09-14 21:50:58 +02:00
|
|
|
|
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-09-18 16:30:25 +02:00
|
|
|
|
|
|
|
$('#sidebar a[href="#subscriptions"]').click(function () {
|
|
|
|
$.ajax({
|
|
|
|
type: 'GET',
|
2012-09-21 23:22:15 +02:00
|
|
|
url: 'json/subscriptions/list',
|
2012-09-18 16:30:25 +02:00
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: function (data) {
|
2012-09-21 20:34:00 +02:00
|
|
|
$('#subscriptions_table tr').remove();
|
2012-09-18 16:30:25 +02:00
|
|
|
if (data) {
|
|
|
|
$.each(data.subscriptions, function (index, name) {
|
2012-09-24 17:10:12 +02:00
|
|
|
$('#subscriptions_table').append(templates.subscription({subscription: name}));
|
2012-09-18 16:30:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
$('#new_subscriptions').focus().select();
|
2012-09-22 01:11:54 +02:00
|
|
|
$("#subscriptions-status").fadeOut(0);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error listing subscriptions", xhr, $("#subscriptions-status"));
|
2012-09-18 16:30:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2012-09-26 20:44:41 +02:00
|
|
|
|
|
|
|
var last_mousewheel = 0;
|
|
|
|
$("#main_div").mousewheel(function () {
|
|
|
|
var time = $.now();
|
|
|
|
if (time - last_mousewheel > 50) {
|
|
|
|
keep_pointer_in_view();
|
|
|
|
last_mousewheel = time;
|
|
|
|
}
|
|
|
|
});
|
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) {
|
|
|
|
// TODO: check the return value and handle an error condition
|
2012-09-21 23:22:15 +02:00
|
|
|
$.post('/json/subscriptions/add', {new_subscription: zephyr_class});
|
2012-09-05 23:38:20 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 19:07:59 +02:00
|
|
|
function compose_button() {
|
2012-09-25 17:55:30 +02:00
|
|
|
clear_compose_box();
|
2012-09-17 19:07:59 +02:00
|
|
|
$('#sidebar a[href="#home"]').tab('show');
|
2012-09-17 20:59:26 +02:00
|
|
|
show_compose('class', $("#class"));
|
2012-09-17 19:07:59 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 00:01:39 +02:00
|
|
|
function hide_compose() {
|
|
|
|
$('input, textarea, button').blur();
|
2012-09-28 22:17:29 +02:00
|
|
|
$('.zephyr_compose').slideUp(100);
|
2012-09-13 00:01:39 +02:00
|
|
|
}
|
|
|
|
|
2012-09-17 20:35:21 +02:00
|
|
|
function show_compose(tabname, focus_area) {
|
2012-09-28 22:17:29 +02:00
|
|
|
$('.zephyr_compose').slideDown(100);
|
2012-09-17 20:35:21 +02:00
|
|
|
$('#zephyr-type-tabs a[href="#' + tabname + '-message"]').tab('show');
|
|
|
|
focus_area.focus();
|
|
|
|
focus_area.select();
|
2012-09-13 00:01:39 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 16:04:27 +02:00
|
|
|
function toggle_compose() {
|
|
|
|
if ($("#zephyr-type-tabs li.active").find("a[href=#class-message]").length !== 0) {
|
|
|
|
// In class tab, switch to personals.
|
|
|
|
show_compose('personal', $("#recipient"));
|
|
|
|
} else {
|
|
|
|
show_compose('class', $("#class"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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-17 17:21:08 +02:00
|
|
|
.stop(true).fadeTo(0,1).delay(250).fadeOut(250, hide_compose);
|
2012-08-30 18:24:16 +02:00
|
|
|
buttons.removeAttr('disabled');
|
|
|
|
},
|
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-18 17:45:03 +02:00
|
|
|
$(function () {
|
|
|
|
var options = {
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
|
|
success: function (resp, statusText, xhr, form) {
|
2012-09-21 22:30:29 +02:00
|
|
|
var name = $.parseJSON(xhr.responseText).data;
|
2012-09-21 22:53:55 +02:00
|
|
|
$('#subscriptions_table').find('button[value="' + name + '"]').parents('tr').remove();
|
2012-09-28 23:12:53 +02:00
|
|
|
var removal_index = class_list.indexOf(name);
|
|
|
|
if (removal_index !== -1) {
|
|
|
|
class_list.splice(removal_index, 1);
|
|
|
|
}
|
|
|
|
update_autocomplete();
|
2012-09-22 01:11:54 +02:00
|
|
|
$("#subscriptions-status").fadeOut(0);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error removing subscription", xhr, $("#subscriptions-status"));
|
2012-09-18 17:45:03 +02:00
|
|
|
},
|
|
|
|
};
|
2012-09-21 20:34:00 +02:00
|
|
|
$("#current_subscriptions").ajaxForm(options);
|
2012-09-18 17:45:03 +02:00
|
|
|
});
|
|
|
|
|
2012-09-18 16:30:25 +02:00
|
|
|
$(function () {
|
|
|
|
var options = {
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
|
|
success: function (resp, statusText, xhr, form) {
|
2012-09-21 22:30:29 +02:00
|
|
|
$("#new_subscription").val("");
|
|
|
|
var name = $.parseJSON(xhr.responseText).data;
|
2012-09-24 17:10:12 +02:00
|
|
|
$('#subscriptions_table').prepend(templates.subscription({subscription: name}));
|
2012-09-21 22:30:29 +02:00
|
|
|
class_list.push(name);
|
2012-09-22 01:11:54 +02:00
|
|
|
$("#subscriptions-status").fadeOut(0);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error adding subscription", xhr, $("#subscriptions-status"));
|
2012-09-18 16:30:25 +02:00
|
|
|
},
|
|
|
|
};
|
2012-09-21 22:30:29 +02:00
|
|
|
$("#add_new_subscription").ajaxForm(options);
|
2012-09-18 16:30:25 +02:00
|
|
|
});
|
|
|
|
|
2012-09-21 19:32:01 +02:00
|
|
|
$(function () {
|
|
|
|
var settings_status = $('#settings-status');
|
|
|
|
settings_status.hide();
|
|
|
|
var options = {
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
|
|
success: function (resp, statusText, xhr, form) {
|
|
|
|
var message = "Updated settings!";
|
|
|
|
var result = $.parseJSON(xhr.responseText);
|
|
|
|
if ((result.full_name !== undefined) || (result.short_name !== undefined)) {
|
|
|
|
message = "Updated settings! You will need to reload the page for your changes to take effect.";
|
|
|
|
}
|
|
|
|
settings_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-success')
|
|
|
|
.text(message).stop(true).fadeTo(0,1);
|
|
|
|
// TODO: In theory we should auto-reload or something if
|
|
|
|
// you changed the email address or other fields that show
|
|
|
|
// up on all screens
|
|
|
|
},
|
|
|
|
error: function (xhr, error_type, xhn) {
|
|
|
|
var response = "Error changing settings";
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
// Only display the error response for 4XX, where we've crafted
|
|
|
|
// a nice response.
|
|
|
|
response += ": " + $.parseJSON(xhr.responseText).msg;
|
|
|
|
}
|
|
|
|
settings_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.text(response).stop(true).fadeTo(0,1);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
$("#current_settings form").ajaxForm(options);
|
|
|
|
});
|
|
|
|
|
2012-09-26 23:52:17 +02:00
|
|
|
var selected_zephyr_id = -1; /* to be filled in on document.ready */
|
2012-09-24 17:13:03 +02:00
|
|
|
var selected_zephyr; // = get_zephyr_row(selected_zephyr_id)
|
2012-09-28 18:05:49 +02:00
|
|
|
var received = {
|
|
|
|
first: -1,
|
|
|
|
last: -1
|
|
|
|
};
|
2012-09-25 00:42:04 +02:00
|
|
|
|
|
|
|
// Narrowing predicate, or 'false' for the home view.
|
2012-09-17 19:04:52 +02:00
|
|
|
var narrowed = false;
|
2012-09-25 00:42:04 +02:00
|
|
|
|
2012-09-17 19:04:52 +02:00
|
|
|
// For tracking where you were before you narrowed.
|
|
|
|
var persistent_zephyr_id = 0;
|
2012-09-25 15:47:22 +02:00
|
|
|
var high_water_mark = 0;
|
2012-09-06 20:16:19 +02:00
|
|
|
|
|
|
|
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) {
|
2012-09-26 19:39:37 +02:00
|
|
|
if (zephyr_row === undefined)
|
|
|
|
return [];
|
2012-09-21 22:44:30 +02:00
|
|
|
return zephyr_row.nextAll('.zephyr_row:first');
|
2012-09-06 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 23:55:08 +02:00
|
|
|
function get_prev_visible(zephyr_row) {
|
2012-09-26 19:39:37 +02:00
|
|
|
if (zephyr_row === undefined)
|
|
|
|
return [];
|
2012-09-21 23:55:08 +02:00
|
|
|
return zephyr_row.prevAll('.zephyr_row:first');
|
2012-09-21 23:13:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_first_visible() {
|
2012-09-21 22:44:30 +02:00
|
|
|
return $('.focused_table .zephyr_row:first');
|
2012-09-21 23:13:01 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 23:55:08 +02:00
|
|
|
function get_last_visible() {
|
|
|
|
return $('.focused_table .zephyr_row:last');
|
2012-09-06 20:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_id(zephyr_row) {
|
2012-09-13 22:00:11 +02:00
|
|
|
return zephyr_row.attr('zid');
|
2012-09-06 20:16:19 +02:00
|
|
|
}
|
2012-08-30 22:19:34 +02:00
|
|
|
|
2012-09-24 17:13:03 +02:00
|
|
|
function get_zephyr_row(zephyr_id) {
|
2012-09-20 20:36:59 +02:00
|
|
|
return $('#' + (narrowed ? 'zfilt' : 'zhome') + zephyr_id);
|
2012-09-13 16:58:29 +02:00
|
|
|
}
|
|
|
|
|
2012-09-07 20:44:55 +02:00
|
|
|
function scroll_to_selected() {
|
|
|
|
var main_div = $('#main_div');
|
|
|
|
main_div.scrollTop(0);
|
2012-09-19 23:12:59 +02:00
|
|
|
main_div.scrollTop(selected_zephyr.offset().top - main_div.height()/1.5);
|
2012-09-07 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 22:00:11 +02:00
|
|
|
function get_huddle_recipient(zephyr) {
|
|
|
|
var recipient, i;
|
|
|
|
|
2012-09-26 19:19:25 +02:00
|
|
|
recipient = zephyr.display_recipient[0].email;
|
2012-09-20 21:33:45 +02:00
|
|
|
for (i = 1; i < zephyr.display_recipient.length; i++) {
|
2012-09-26 19:19:25 +02:00
|
|
|
recipient += ', ' + zephyr.display_recipient[i].email;
|
2012-09-13 22:00:11 +02:00
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2012-09-26 21:25:49 +02:00
|
|
|
function get_huddle_recipient_names(zephyr) {
|
|
|
|
var recipient, i;
|
|
|
|
|
|
|
|
recipient = zephyr.display_recipient[0].name;
|
|
|
|
for (i = 1; i < zephyr.display_recipient.length; i++) {
|
|
|
|
recipient += ', ' + zephyr.display_recipient[i].name;
|
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
}
|
|
|
|
|
2012-09-12 16:23:55 +02:00
|
|
|
function respond_to_zephyr() {
|
2012-09-19 22:40:42 +02:00
|
|
|
var zephyr, recipient, recipients;
|
|
|
|
zephyr = zephyr_dict[selected_zephyr_id];
|
2012-09-13 20:30:41 +02:00
|
|
|
|
2012-09-26 19:59:25 +02:00
|
|
|
switch (zephyr.type) {
|
|
|
|
case '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-17 20:35:21 +02:00
|
|
|
show_compose('class', $("#new_zephyr"));
|
2012-09-26 19:59:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'huddle':
|
2012-09-13 20:30:41 +02:00
|
|
|
$('#zephyr-type-tabs a[href="#personal-message"]').tab('show');
|
2012-09-20 21:33:45 +02:00
|
|
|
prepare_huddle(zephyr.reply_to);
|
2012-09-26 19:59:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '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
|
2012-09-21 00:26:59 +02:00
|
|
|
// deal with emails.
|
2012-09-13 20:30:41 +02:00
|
|
|
recipient = zephyr.display_recipient;
|
2012-09-21 00:26:59 +02:00
|
|
|
if (recipient === email) { // that is, we sent the original message
|
2012-09-21 17:02:27 +02:00
|
|
|
recipient = zephyr.sender_email;
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
|
|
|
prepare_huddle(recipient);
|
2012-09-26 19:59:25 +02:00
|
|
|
break;
|
2012-09-12 16:23:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 23:29:30 +02:00
|
|
|
// Called by mouseover etc.
|
|
|
|
function select_zephyr_by_id(zephyr_id) {
|
2012-09-20 19:44:31 +02:00
|
|
|
if (zephyr_id === selected_zephyr_id) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-24 17:13:03 +02:00
|
|
|
select_zephyr(get_zephyr_row(zephyr_id), false);
|
2012-09-21 23:29:30 +02:00
|
|
|
}
|
|
|
|
|
2012-09-26 23:37:21 +02:00
|
|
|
var clicking = false;
|
|
|
|
var mouse_moved = false;
|
|
|
|
|
|
|
|
function zephyr_mousedown() {
|
|
|
|
mouse_moved = false;
|
|
|
|
clicking = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function zephyr_mousemove() {
|
|
|
|
if (clicking) {
|
|
|
|
mouse_moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 23:29:30 +02:00
|
|
|
// Called on page load and when we [un]narrow.
|
|
|
|
// Forces a call to select_zephyr even if the id has not changed,
|
|
|
|
// because the visible table might have.
|
|
|
|
function select_and_show_by_id(zephyr_id) {
|
2012-09-24 17:13:03 +02:00
|
|
|
select_zephyr(get_zephyr_row(zephyr_id), true);
|
2012-09-13 16:58:29 +02:00
|
|
|
}
|
|
|
|
|
2012-09-24 19:50:09 +02:00
|
|
|
function update_selected_zephyr(zephyr) {
|
|
|
|
$('.selected_zephyr').removeClass('selected_zephyr');
|
|
|
|
zephyr.addClass('selected_zephyr');
|
|
|
|
|
|
|
|
var new_selected_id = get_id(zephyr);
|
|
|
|
if (!narrowed && new_selected_id !== selected_zephyr_id) {
|
|
|
|
// Narrowing is a temporary view on top of the home view and
|
|
|
|
// doesn't permanently affect where you are.
|
|
|
|
//
|
|
|
|
// We also don't want to post if there's no effective change.
|
|
|
|
$.post("update", {pointer: new_selected_id});
|
|
|
|
}
|
|
|
|
selected_zephyr_id = new_selected_id;
|
|
|
|
selected_zephyr = zephyr;
|
2012-09-25 15:47:22 +02:00
|
|
|
|
|
|
|
if (parseInt(selected_zephyr_id, 10) > high_water_mark) {
|
|
|
|
high_water_mark = parseInt(selected_zephyr_id, 10);
|
|
|
|
}
|
|
|
|
|
2012-09-24 19:50:09 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 23:38:27 +02:00
|
|
|
function select_zephyr(next_zephyr, scroll_to) {
|
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-17 16:42:45 +02:00
|
|
|
if (next_zephyr.length === 0) {
|
|
|
|
// There are no zephyrs!
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-06 19:32:02 +02:00
|
|
|
|
2012-09-24 19:50:09 +02:00
|
|
|
update_selected_zephyr(next_zephyr);
|
2012-09-05 16:56:10 +02:00
|
|
|
|
2012-09-19 23:38:27 +02:00
|
|
|
if (scroll_to &&
|
|
|
|
((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-25 15:47:22 +02:00
|
|
|
function go_to_high_water_mark() {
|
|
|
|
select_and_show_by_id(high_water_mark);
|
|
|
|
}
|
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. */
|
|
|
|
|
2012-09-20 19:28:53 +02:00
|
|
|
function hide_email() {
|
|
|
|
$('.zephyr_sender_email').addClass('invisible');
|
2012-09-13 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2012-09-26 21:10:35 +02:00
|
|
|
function show_email(zephyr_id) {
|
2012-09-20 19:28:53 +02:00
|
|
|
hide_email();
|
2012-09-26 21:10:35 +02:00
|
|
|
get_zephyr_row(zephyr_id).find('.zephyr_sender_email').removeClass('invisible');
|
2012-09-13 21:18:00 +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-11 20:39:33 +02:00
|
|
|
function prepare_huddle(recipients) {
|
|
|
|
// Used for both personals and huddles.
|
2012-09-17 20:35:21 +02:00
|
|
|
show_compose('personal', $("#new_personal_zephyr"));
|
2012-09-11 20:39:33 +02:00
|
|
|
$("#recipient").val(recipients);
|
|
|
|
}
|
|
|
|
|
2012-09-24 19:38:07 +02:00
|
|
|
function do_narrow(description, filter_function) {
|
2012-09-25 00:42:04 +02:00
|
|
|
narrowed = filter_function;
|
|
|
|
|
2012-09-17 19:04:52 +02:00
|
|
|
// Your pointer isn't changed when narrowed.
|
|
|
|
persistent_zephyr_id = selected_zephyr_id;
|
|
|
|
|
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-19 23:12:59 +02:00
|
|
|
var old_top = $("#main_div").offset().top - selected_zephyr.offset().top;
|
2012-09-13 22:00:11 +02:00
|
|
|
var parent;
|
|
|
|
|
|
|
|
// Empty the filtered table right before we fill it again
|
2012-09-20 20:27:43 +02:00
|
|
|
$("#zfilt").empty();
|
2012-09-28 19:48:04 +02:00
|
|
|
add_to_table(zephyr_array, 'zfilt', filter_function, 'bottom');
|
2012-08-29 22:23:56 +02:00
|
|
|
|
2012-09-11 21:37:09 +02:00
|
|
|
// Show the new set of messages.
|
2012-09-20 20:27:43 +02:00
|
|
|
$("#zfilt").addClass("focused_table");
|
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-13 22:00:11 +02:00
|
|
|
$("#main_div").addClass("narrowed_view");
|
2012-09-12 19:18:49 +02:00
|
|
|
$("#currently_narrowed_to").html(description);
|
2012-09-20 20:27:43 +02:00
|
|
|
$("#zhome").removeClass("focused_table");
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-09-21 23:29:30 +02:00
|
|
|
select_and_show_by_id(selected_zephyr_id);
|
2012-09-13 22:00:11 +02:00
|
|
|
scroll_to_selected();
|
2012-08-29 22:23:56 +02:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:45:23 +02:00
|
|
|
function narrow_huddle() {
|
2012-09-24 19:38:07 +02:00
|
|
|
var original = zephyr_dict[selected_zephyr_id];
|
2012-09-24 19:55:30 +02:00
|
|
|
do_narrow("Group chats with " + original.reply_to, function (other) {
|
2012-09-20 21:33:45 +02:00
|
|
|
return other.reply_to === original.reply_to;
|
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-24 19:55:30 +02:00
|
|
|
do_narrow("All huddles with you", function (other) {
|
2012-09-13 22:00:11 +02:00
|
|
|
return other.type === "personal" || other.type === "huddle";
|
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-24 19:38:07 +02:00
|
|
|
var original = zephyr_dict[selected_zephyr_id];
|
2012-09-05 17:48:27 +02:00
|
|
|
var other_party;
|
2012-09-24 19:38:07 +02:00
|
|
|
if (original.display_recipient === email) {
|
|
|
|
other_party = original.sender_email;
|
2012-09-05 17:48:27 +02:00
|
|
|
} else {
|
2012-09-24 19:38:07 +02:00
|
|
|
other_party = original.display_recipient;
|
2012-09-05 17:48:27 +02:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-09-24 19:55:30 +02:00
|
|
|
do_narrow("Huddles with " + other_party, function (other) {
|
2012-09-13 22:00:11 +02:00
|
|
|
return (other.type === 'personal') &&
|
2012-09-21 17:02:27 +02:00
|
|
|
(((other.display_recipient === original.display_recipient) && (other.sender_email === original.sender_email)) ||
|
|
|
|
((other.display_recipient === original.sender_email) && (other.sender_email === original.display_recipient)));
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-09-13 22:00:11 +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-24 19:38:07 +02:00
|
|
|
var original = zephyr_dict[selected_zephyr_id];
|
2012-09-28 18:11:27 +02:00
|
|
|
var message = "<span class='narrowed_name'>" + original.display_recipient + "</span>";
|
2012-09-24 19:38:07 +02:00
|
|
|
do_narrow(message, function (other) {
|
2012-09-13 22:00:11 +02:00
|
|
|
return (other.type === 'class' &&
|
2012-09-21 22:57:04 +02:00
|
|
|
original.recipient_id === other.recipient_id);
|
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-24 19:38:07 +02:00
|
|
|
var original = zephyr_dict[selected_zephyr_id];
|
|
|
|
if (original.type !== 'class')
|
2012-09-21 22:55:03 +02:00
|
|
|
return;
|
|
|
|
|
2012-09-28 18:11:27 +02:00
|
|
|
var message = "<span class='narrowed_name'>" + original.display_recipient
|
|
|
|
+ " | " + original.instance + "</span>";
|
2012-09-24 19:38:07 +02:00
|
|
|
do_narrow(message, function (other) {
|
2012-09-13 22:00:11 +02:00
|
|
|
return (other.type === 'class' &&
|
2012-09-21 22:57:04 +02:00
|
|
|
original.recipient_id === other.recipient_id &&
|
2012-09-13 22:00:11 +02:00
|
|
|
original.instance === other.instance);
|
2012-09-06 19:47:31 +02:00
|
|
|
});
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:55:03 +02:00
|
|
|
// Called for the 'narrow by class' hotkey.
|
|
|
|
function narrow_by_recipient() {
|
|
|
|
switch (zephyr_dict[selected_zephyr_id].type) {
|
|
|
|
case 'personal': narrow_personals(); break;
|
|
|
|
case 'huddle': narrow_huddle(); break;
|
|
|
|
case 'class': narrow_class(); break;
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 19:18:49 +02:00
|
|
|
function show_all_messages() {
|
2012-09-13 22:00:11 +02:00
|
|
|
if (!narrowed) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-17 19:04:52 +02:00
|
|
|
narrowed = false;
|
|
|
|
|
2012-09-20 20:27:43 +02:00
|
|
|
$("#zfilt").removeClass('focused_table');
|
|
|
|
$("#zhome").addClass('focused_table');
|
2012-09-13 22:20:07 +02:00
|
|
|
$("#narrowbox").hide();
|
2012-09-17 22:58:05 +02:00
|
|
|
$("#main_div").removeClass('narrowed_view');
|
2012-09-12 19:18:49 +02:00
|
|
|
$("#show_all_messages").attr("disabled", "disabled");
|
|
|
|
$("#currently_narrowed_to").html("");
|
2012-09-13 22:00:11 +02:00
|
|
|
|
|
|
|
// Includes scrolling.
|
2012-09-21 23:29:30 +02:00
|
|
|
select_and_show_by_id(persistent_zephyr_id);
|
2012-09-13 22:00:11 +02:00
|
|
|
|
|
|
|
scroll_to_selected();
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-26 20:25:02 +02:00
|
|
|
var autocomplete_needs_update = false;
|
|
|
|
|
2012-09-04 20:31:23 +02:00
|
|
|
function update_autocomplete() {
|
|
|
|
class_list.sort();
|
|
|
|
instance_list.sort();
|
|
|
|
people_list.sort();
|
|
|
|
|
2012-09-19 19:10:09 +02:00
|
|
|
// limit number of items so the list doesn't fall off the screen
|
2012-09-19 17:51:53 +02:00
|
|
|
$( "#class" ).typeahead({
|
2012-09-19 19:10:09 +02:00
|
|
|
source: class_list,
|
|
|
|
items: 3,
|
2012-09-04 20:31:23 +02:00
|
|
|
});
|
2012-09-19 17:51:53 +02:00
|
|
|
$( "#instance" ).typeahead({
|
2012-09-19 19:10:09 +02:00
|
|
|
source: instance_list,
|
|
|
|
items: 2,
|
2012-09-04 20:31:23 +02:00
|
|
|
});
|
2012-09-19 17:51:53 +02:00
|
|
|
$( "#recipient" ).typeahead({
|
2012-09-19 19:10:09 +02:00
|
|
|
source: people_list,
|
|
|
|
items: 4,
|
2012-09-04 20:31:23 +02:00
|
|
|
});
|
2012-09-26 20:25:02 +02:00
|
|
|
|
|
|
|
autocomplete_needs_update = false;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:29:35 +02:00
|
|
|
function same_recipient(a, b) {
|
|
|
|
if ((a === undefined) || (b === undefined))
|
|
|
|
return false;
|
|
|
|
if (a.type !== b.type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (a.type) {
|
|
|
|
case 'huddle':
|
|
|
|
return a.recipient_id === b.recipient_id;
|
|
|
|
case 'personal':
|
|
|
|
return a.reply_to === b.reply_to;
|
|
|
|
case 'class':
|
|
|
|
return (a.recipient_id === b.recipient_id) &&
|
|
|
|
(a.instance === b.instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// should never get here
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:26:50 +02:00
|
|
|
function same_sender(a, b) {
|
|
|
|
return ((a !== undefined) && (b !== undefined) &&
|
|
|
|
(a.sender_email === b.sender_email));
|
|
|
|
}
|
|
|
|
|
2012-09-28 19:48:04 +02:00
|
|
|
function add_to_table(zephyrs, table_name, filter_function, where) {
|
2012-09-20 20:33:57 +02:00
|
|
|
var table = $('#' + table_name);
|
2012-09-24 22:36:09 +02:00
|
|
|
var zephyrs_to_render = [];
|
|
|
|
var ids_where_next_is_same_sender = [];
|
2012-09-28 19:48:04 +02:00
|
|
|
var prev;
|
|
|
|
|
|
|
|
if (where !== 'top')
|
|
|
|
prev = zephyr_dict[table.find('tr:last-child').attr('zid')];
|
2012-09-20 20:33:57 +02:00
|
|
|
|
2012-09-24 21:21:23 +02:00
|
|
|
$.each(zephyrs, function (index, zephyr) {
|
|
|
|
if (! filter_function(zephyr))
|
|
|
|
return;
|
|
|
|
|
2012-09-25 23:17:27 +02:00
|
|
|
zephyr.content = linkify(zephyr.content, {
|
|
|
|
callback: function (text, href) {
|
|
|
|
return href ? '<a href="' + href + '" target="_blank" title="' +
|
|
|
|
href + '" onclick="event.cancelBubble = true;"">' + text + '</a>' : text;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-09-24 22:36:09 +02:00
|
|
|
zephyr.include_recipient = false;
|
|
|
|
zephyr.include_bookend = false;
|
|
|
|
if (! same_recipient(prev, zephyr)) {
|
|
|
|
// Add a space to the table, but not for the first element.
|
2012-09-24 21:21:23 +02:00
|
|
|
zephyr.include_recipient = true;
|
2012-09-24 22:36:09 +02:00
|
|
|
zephyr.include_bookend = (prev !== undefined);
|
2012-09-19 19:15:12 +02:00
|
|
|
}
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-09-24 22:36:09 +02:00
|
|
|
zephyr.include_sender = true;
|
2012-09-24 21:21:23 +02:00
|
|
|
if (same_sender(prev, zephyr) && !zephyr.include_recipient) {
|
|
|
|
zephyr.include_sender = false;
|
2012-09-24 22:36:09 +02:00
|
|
|
ids_where_next_is_same_sender.push(prev.id);
|
2012-09-24 21:21:23 +02:00
|
|
|
}
|
2012-09-19 19:15:12 +02:00
|
|
|
|
2012-09-24 21:21:23 +02:00
|
|
|
zephyr.dom_id = table_name + zephyr.id;
|
2012-09-13 22:00:11 +02:00
|
|
|
|
2012-09-24 22:36:09 +02:00
|
|
|
zephyrs_to_render.push(zephyr);
|
|
|
|
prev = zephyr;
|
|
|
|
});
|
|
|
|
|
2012-09-28 19:48:04 +02:00
|
|
|
var rendered = templates.zephyr({zephyrs: zephyrs_to_render});
|
|
|
|
|
|
|
|
if (where === 'top')
|
|
|
|
table.prepend(rendered);
|
|
|
|
else
|
|
|
|
table.append(rendered);
|
2012-09-20 20:33:57 +02:00
|
|
|
|
2012-09-24 22:36:09 +02:00
|
|
|
$.each(zephyrs_to_render, function (index, zephyr) {
|
2012-09-26 23:37:21 +02:00
|
|
|
var row = get_zephyr_row(zephyr.id);
|
|
|
|
register_huddle_onclick(row, zephyr.sender_email);
|
|
|
|
register_onclick(row, zephyr.id);
|
2012-09-24 22:36:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$.each(ids_where_next_is_same_sender, function (index, id) {
|
|
|
|
get_zephyr_row(id).find('.messagebox').addClass("next_is_same_sender");
|
2012-09-24 21:21:23 +02:00
|
|
|
});
|
2012-09-13 22:00:11 +02:00
|
|
|
}
|
|
|
|
|
2012-09-24 21:21:23 +02:00
|
|
|
function add_zephyr_metadata(dummy, zephyr) {
|
2012-09-28 18:05:49 +02:00
|
|
|
if (received.first === -1)
|
|
|
|
received.first = zephyr.id;
|
|
|
|
else
|
|
|
|
received.first = Math.min(received.first, zephyr.id);
|
|
|
|
|
|
|
|
received.last = Math.max(received.last, zephyr.id);
|
2012-09-06 20:16:19 +02:00
|
|
|
|
2012-09-26 19:57:19 +02:00
|
|
|
switch (zephyr.type) {
|
|
|
|
case '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);
|
2012-09-26 20:25:02 +02:00
|
|
|
autocomplete_needs_update = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
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);
|
2012-09-26 20:25:02 +02:00
|
|
|
autocomplete_needs_update = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'huddle':
|
2012-09-04 23:20:21 +02:00
|
|
|
zephyr.is_huddle = true;
|
2012-09-20 21:33:45 +02:00
|
|
|
zephyr.reply_to = get_huddle_recipient(zephyr);
|
2012-09-26 21:25:49 +02:00
|
|
|
zephyr.display_reply_to = get_huddle_recipient_names(zephyr);
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'personal':
|
2012-09-04 18:20:07 +02:00
|
|
|
zephyr.is_personal = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
if (zephyr.display_recipient === email) { // that is, we sent the original message
|
2012-09-21 17:02:27 +02:00
|
|
|
zephyr.reply_to = zephyr.sender_email;
|
2012-09-20 21:33:45 +02:00
|
|
|
} else {
|
|
|
|
zephyr.reply_to = zephyr.display_recipient;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-09-26 21:25:49 +02:00
|
|
|
zephyr.display_reply_to = zephyr.reply_to;
|
2012-09-20 21:33:45 +02:00
|
|
|
|
2012-09-21 00:26:59 +02:00
|
|
|
if (zephyr.reply_to !== email &&
|
2012-09-20 21:33:45 +02:00
|
|
|
$.inArray(zephyr.reply_to, people_list) === -1) {
|
|
|
|
people_list.push(zephyr.reply_to);
|
2012-09-26 20:25:02 +02:00
|
|
|
autocomplete_needs_update = true;
|
2012-09-04 20:31:23 +02:00
|
|
|
}
|
2012-09-26 19:57:19 +02:00
|
|
|
break;
|
2012-08-29 18:53:03 +02:00
|
|
|
}
|
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-13 22:00:11 +02:00
|
|
|
var two_digits = function (x) { return ('0' + x).slice(-2); };
|
2012-09-12 23:07:13 +02:00
|
|
|
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-14 21:50:58 +02:00
|
|
|
zephyr_dict[zephyr.id] = zephyr;
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 19:27:52 +02:00
|
|
|
function add_messages(data) {
|
|
|
|
if (!data || !data.zephyrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$.each(data.zephyrs, add_zephyr_metadata);
|
2012-09-25 00:42:04 +02:00
|
|
|
|
2012-09-26 22:44:38 +02:00
|
|
|
if (loading_spinner) {
|
|
|
|
loading_spinner.stop();
|
|
|
|
$('#loading_indicator').hide();
|
|
|
|
loading_spinner = undefined;
|
|
|
|
}
|
|
|
|
|
2012-09-25 00:42:04 +02:00
|
|
|
if (narrowed)
|
2012-09-28 19:48:04 +02:00
|
|
|
add_to_table(data.zephyrs, 'zfilt', narrowed, data.where);
|
2012-09-25 00:42:04 +02:00
|
|
|
|
|
|
|
// Even when narrowed, add messages to the home view so they exist when we un-narrow.
|
2012-09-28 19:48:04 +02:00
|
|
|
add_to_table(data.zephyrs, 'zhome', function () { return true; }, data.where);
|
2012-09-26 20:25:02 +02:00
|
|
|
|
2012-09-28 19:27:52 +02:00
|
|
|
$.each(data.zephyrs, function () {
|
2012-09-26 20:28:22 +02:00
|
|
|
zephyr_array.push(this);
|
2012-09-26 23:52:17 +02:00
|
|
|
|
|
|
|
// If we received the initially selected message, select it on the client side,
|
|
|
|
// but not if the user has already selected another one during load.
|
|
|
|
if ((this.id === initial_pointer) && (selected_zephyr_id === -1)) {
|
2012-09-26 20:28:22 +02:00
|
|
|
select_and_show_by_id(initial_pointer);
|
2012-09-26 23:52:17 +02:00
|
|
|
}
|
2012-09-26 20:28:22 +02:00
|
|
|
});
|
|
|
|
|
2012-09-26 20:25:02 +02:00
|
|
|
if (autocomplete_needs_update)
|
|
|
|
update_autocomplete();
|
2012-09-24 21:21:23 +02:00
|
|
|
}
|
|
|
|
|
2012-09-13 17:47:07 +02:00
|
|
|
function clear_compose_box() {
|
|
|
|
$("#zephyr_compose").find('input[type=text], textarea').val('');
|
|
|
|
}
|
|
|
|
|
2012-09-27 21:44:54 +02:00
|
|
|
var get_updates_failures = 0;
|
|
|
|
function get_updates() {
|
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',
|
2012-09-27 21:44:54 +02:00
|
|
|
url: 'get_updates',
|
2012-09-28 18:05:49 +02:00
|
|
|
data: received,
|
2012-08-31 21:33:04 +02:00
|
|
|
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-09-27 21:44:54 +02:00
|
|
|
get_updates_failures = 0;
|
2012-09-05 22:33:04 +02:00
|
|
|
$('#connection-error').hide();
|
|
|
|
|
2012-09-28 19:27:52 +02:00
|
|
|
add_messages(data);
|
2012-09-27 21:44:54 +02:00
|
|
|
setTimeout(get_updates, 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');
|
2012-09-27 21:44:54 +02:00
|
|
|
get_updates_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 +
|
2012-09-27 21:44:54 +02:00
|
|
|
' (' + get_updates_failures + ' failures)');
|
|
|
|
get_updates_failures += 1;
|
2012-09-05 22:17:14 +02:00
|
|
|
}
|
|
|
|
|
2012-09-27 21:44:54 +02:00
|
|
|
if (get_updates_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
|
|
|
|
2012-09-27 21:44:54 +02:00
|
|
|
var retry_sec = Math.min(90, Math.exp(get_updates_failures/2));
|
2012-09-05 22:33:04 +02:00
|
|
|
console.log(new Date() + ': longpoll retrying in ' + retry_sec + ' seconds');
|
2012-09-27 21:44:54 +02:00
|
|
|
setTimeout(get_updates, 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-27 21:44:54 +02:00
|
|
|
get_updates();
|
2012-09-17 20:35:21 +02:00
|
|
|
$('.button-slide').click(function () {
|
|
|
|
show_compose('class', $("#class"));
|
|
|
|
});
|
2012-09-10 20:31:53 +02:00
|
|
|
});
|
2012-09-26 20:44:41 +02:00
|
|
|
|
|
|
|
function above_view(zephyr) {
|
|
|
|
return zephyr.offset().top < $("#main_div").offset().top;
|
|
|
|
}
|
|
|
|
|
|
|
|
function below_view(zephyr) {
|
|
|
|
var main_div = $("#main_div");
|
|
|
|
return zephyr.offset().top + zephyr.height() > main_div.offset().top + main_div.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
function keep_pointer_in_view() {
|
|
|
|
var main_div = $("#main_div");
|
|
|
|
var next_zephyr = get_zephyr_row(selected_zephyr_id);
|
|
|
|
|
|
|
|
if (above_view(next_zephyr)) {
|
|
|
|
while (above_view(next_zephyr)) {
|
|
|
|
next_zephyr = get_next_visible(next_zephyr);
|
|
|
|
}
|
|
|
|
} else if (below_view(next_zephyr)) {
|
|
|
|
while (below_view(next_zephyr)) {
|
|
|
|
next_zephyr = get_prev_visible(next_zephyr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((main_div.scrollTop() === 0) && (next_zephyr.attr("zid") > get_first_visible().attr("zid"))) {
|
|
|
|
// If we've scrolled to the top, keep inching the selected
|
|
|
|
// zephyr up to the top instead of just the latest one that is
|
|
|
|
// still on the screen.
|
|
|
|
next_zephyr = get_prev_visible(next_zephyr);
|
|
|
|
} else if ((main_div.scrollTop() + main_div.innerHeight() >= main_div[0].scrollHeight) &&
|
|
|
|
(next_zephyr.attr("zid") < get_last_visible().attr("zid"))) {
|
|
|
|
next_zephyr = get_next_visible(next_zephyr);
|
|
|
|
}
|
|
|
|
update_selected_zephyr(next_zephyr);
|
|
|
|
}
|