2012-08-29 20:30:17 +02:00
|
|
|
function resize_main_div() {
|
|
|
|
// Resize main_div to exactly take up remaining vertical space.
|
|
|
|
var div = $('#main_div');
|
2012-08-29 22:31:01 +02:00
|
|
|
div.height(Math.max(200, div.height() + $(window).height() - $('body').height()));
|
2012-08-29 20:30:17 +02:00
|
|
|
}
|
|
|
|
$(function () {
|
|
|
|
resize_main_div();
|
|
|
|
$(window).resize(resize_main_div);
|
|
|
|
$('#zephyr-type-tabs a').on('shown', function (e) { resize_main_div(); });
|
|
|
|
});
|
|
|
|
|
2012-08-29 23:22:27 +02:00
|
|
|
$(function() {
|
|
|
|
$('#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-08-29 17:12:21 +02:00
|
|
|
$.ajaxSetup({
|
|
|
|
beforeSend: function(xhr, settings) {
|
|
|
|
function getCookie(name) {
|
|
|
|
var cookieValue = null;
|
|
|
|
if (document.cookie && document.cookie != '') {
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
var cookie = jQuery.trim(cookies[i]);
|
|
|
|
// Does this cookie string begin with the name we want?
|
|
|
|
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
|
|
|
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-30 17:28:15 +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 = {
|
|
|
|
beforeSubmit: function (form, _options) {
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-info')
|
|
|
|
.text('Sending')
|
|
|
|
.stop(true).fadeTo(0,1);
|
|
|
|
buttons.attr('disabled', 'disabled');
|
|
|
|
},
|
|
|
|
success: function (resp, statusText, xhr, form) {
|
|
|
|
form.find('textarea').val('');
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-success')
|
|
|
|
.text('Sent message')
|
|
|
|
.stop(true).fadeTo(0,1).delay(1000).fadeOut(1000);
|
|
|
|
buttons.removeAttr('disabled');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
send_status.removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
2012-08-30 21:35:56 +02:00
|
|
|
.html('Error sending message ')
|
|
|
|
.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-08-29 21:14:55 +02:00
|
|
|
selected_tag = '<p id="selected">▶</p>'
|
2012-08-29 20:57:31 +02:00
|
|
|
|
2012-08-30 22:19:34 +02:00
|
|
|
var allow_hotkeys = true;
|
|
|
|
|
|
|
|
// NB: This just binds to current elements, and won't bind to elements
|
|
|
|
// created after ready() is called.
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
$('input, textarea, button').focus(function() {
|
|
|
|
allow_hotkeys = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('input, textarea, button').blur(function() {
|
|
|
|
allow_hotkeys = true;
|
|
|
|
});
|
|
|
|
});
|
2012-08-29 23:08:16 +02:00
|
|
|
|
2012-08-29 17:12:21 +02:00
|
|
|
$(document).keydown(function(event) {
|
2012-08-30 22:19:34 +02:00
|
|
|
if (allow_hotkeys && (event.keyCode == 38 || event.keyCode == 40)) { // down or up arrow
|
2012-08-29 23:08:16 +02:00
|
|
|
// Remove focus from zephyr creation.
|
|
|
|
$("#new_zephyr").blur();
|
|
|
|
$("#new_personal_zephyr").blur();
|
|
|
|
|
2012-08-29 17:12:21 +02:00
|
|
|
p = $("#selected");
|
|
|
|
tr = $(p).closest("tr");
|
|
|
|
td = $(p).closest("td");
|
|
|
|
|
|
|
|
if (event.keyCode == 40) { // down arrow
|
|
|
|
// There are probably more verbose but more efficient ways to do this.
|
|
|
|
next_zephyr = tr.nextAll(":not(:hidden):first");
|
|
|
|
} else { // up arrow
|
|
|
|
next_zephyr = tr.prevAll(":not(:hidden):first");
|
|
|
|
}
|
|
|
|
if (next_zephyr.length != 0) { // We are not at the bottom or top of the zephyrs.
|
2012-08-29 20:57:31 +02:00
|
|
|
next_zephyr.children("td:first").html(selected_tag);
|
2012-08-29 17:12:21 +02:00
|
|
|
td.empty(); // Clear the previous arrow.
|
|
|
|
$.post("update", {pointer: next_zephyr.attr("id")});
|
|
|
|
|
|
|
|
if ($(next_zephyr).offset().top < $("#main_div").offset().top) {
|
|
|
|
$("#main_div").scrollTop($("#main_div").scrollTop() - 75);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($(next_zephyr).offset().top + $(next_zephyr).height() > $("#main_div").offset().top + $("#main_div").height()) {
|
|
|
|
$("#main_div").scrollTop($("#main_div").scrollTop() + 75);
|
|
|
|
}
|
|
|
|
}
|
2012-08-30 22:19:34 +02:00
|
|
|
} else if ((event.keyCode == 82) && allow_hotkeys) { // 'r' keypress, for responding to a zephyr
|
2012-08-29 17:12:21 +02:00
|
|
|
var parent = $("#selected").parents("tr");
|
|
|
|
var zephyr_class = parent.find("span.zephyr_class").text();
|
|
|
|
var instance = parent.find("span.zephyr_instance").text();
|
|
|
|
$("#class").val(zephyr_class);
|
|
|
|
$("#instance").val(instance);
|
|
|
|
$("#new_zephyr").focus();
|
2012-08-29 23:09:33 +02:00
|
|
|
event.preventDefault();
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function scroll_to_zephyr(target_zephyr, old_offset) {
|
|
|
|
// target_zephyr is an id.
|
|
|
|
// old_offset is how far from the top of the scroll area the
|
|
|
|
// zephyr was before any narrowing or unnarrowing happened.
|
|
|
|
var height_above_zephyr = 0;
|
|
|
|
$("#table tr:lt(" + $("#" + target_zephyr).index() + ")").each(function() {
|
|
|
|
if (!$(this).is(":hidden")) {
|
|
|
|
height_above_zephyr += $(this).height();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#main_div").scrollTop(height_above_zephyr + old_offset);
|
|
|
|
}
|
|
|
|
|
2012-08-29 22:02:26 +02:00
|
|
|
function hide_personals() {
|
|
|
|
$("span.zephyr_personal_recipient").each(
|
|
|
|
function() {
|
|
|
|
$(this).parents("tr").hide();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2012-08-29 17:12:21 +02:00
|
|
|
|
2012-08-29 22:23:56 +02:00
|
|
|
function narrow_personals(target_zephyr) {
|
|
|
|
var old_top = $("#main_div").offset().top - $("#" + target_zephyr).offset().top;
|
|
|
|
|
|
|
|
$("span.zephyr_personal_recipient").each(
|
|
|
|
function() {
|
|
|
|
$(this).parents("tr").show();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$("span.zephyr_class").each(
|
|
|
|
function() {
|
|
|
|
$(this).parents("tr").hide();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$("#selected").closest("td").empty();
|
|
|
|
$("#" + target_zephyr).children("td:first").html(selected_tag);
|
|
|
|
$.post("update", {pointer: target_zephyr});
|
|
|
|
scroll_to_zephyr(target_zephyr, old_top);
|
|
|
|
|
|
|
|
$("#unhide").removeAttr("disabled");
|
|
|
|
$("#narrow_indicator").html("Showing personals");
|
|
|
|
}
|
|
|
|
|
2012-08-29 17:12:21 +02:00
|
|
|
function narrow(class_name, target_zephyr) {
|
|
|
|
// We want the zephyr on which the narrow happened to stay in the same place if possible.
|
|
|
|
var old_top = $("#main_div").offset().top - $("#" + target_zephyr).offset().top;
|
|
|
|
$("span.zephyr_class").each(
|
|
|
|
function() {
|
|
|
|
if ($(this).text() != class_name) {
|
|
|
|
$(this).parents("tr").hide();
|
|
|
|
} else {
|
|
|
|
// If you've narrowed on an instance and then click on the class, that should unhide the other instances on that class.
|
|
|
|
$(this).parents("tr").show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2012-08-29 22:02:26 +02:00
|
|
|
hide_personals();
|
2012-08-29 17:12:21 +02:00
|
|
|
$("#selected").closest("td").empty();
|
2012-08-29 20:57:31 +02:00
|
|
|
$("#" + target_zephyr).children("td:first").html(selected_tag);
|
2012-08-29 17:12:21 +02:00
|
|
|
$.post("update", {pointer: target_zephyr});
|
|
|
|
|
|
|
|
// Try to keep the zephyr in the same place on the screen after narrowing.
|
|
|
|
scroll_to_zephyr(target_zephyr, old_top);
|
2012-08-29 18:54:58 +02:00
|
|
|
|
|
|
|
$("#unhide").removeAttr("disabled");
|
|
|
|
$("#narrow_indicator").html("Showing <span class='label zephyr_class'>" + class_name + "</span>");
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function narrow_instance(class_name, instance, target_zephyr) {
|
|
|
|
var old_top = $("#main_div").offset().top - $("#" + target_zephyr).offset().top;
|
|
|
|
$("tr").each(
|
|
|
|
function() {
|
|
|
|
if (($(this).find("span.zephyr_class").text() != class_name) ||
|
|
|
|
($(this).find("span.zephyr_instance").text() != instance)) {
|
|
|
|
$(this).hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2012-08-29 22:02:26 +02:00
|
|
|
hide_personals();
|
2012-08-29 17:12:21 +02:00
|
|
|
$("#selected").closest("td").empty();
|
2012-08-29 20:57:31 +02:00
|
|
|
$("#" + target_zephyr).children("td:first").html(selected_tag);
|
2012-08-29 17:12:21 +02:00
|
|
|
$.post("update", {pointer: target_zephyr});
|
|
|
|
|
|
|
|
// Try to keep the zephyr in the same place on the screen after narrowing.
|
|
|
|
scroll_to_zephyr(target_zephyr, old_top);
|
2012-08-29 18:54:58 +02:00
|
|
|
|
|
|
|
$("#unhide").removeAttr("disabled");
|
|
|
|
$("#narrow_indicator").html("Showing <span class='label zephyr_class'>" + class_name
|
|
|
|
+ "</span> <span class='label zephyr_instance'>" + instance + "</span>");
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepare_personal(username) {
|
2012-08-29 20:07:21 +02:00
|
|
|
$('#zephyr-type-tabs a[href="#personal-message"]').tab('show');
|
2012-08-29 17:12:21 +02:00
|
|
|
$("#recipient").val(username);
|
|
|
|
$("#new_personal_zephyr").focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function unhide() {
|
|
|
|
$("tr").show();
|
|
|
|
|
|
|
|
p = $("#selected");
|
|
|
|
tr = $(p).closest("tr");
|
|
|
|
scroll_to_zephyr(tr.attr("id"), 0);
|
2012-08-29 18:54:58 +02:00
|
|
|
|
|
|
|
$("#unhide").attr("disabled", "disabled");
|
|
|
|
$("#narrow_indicator").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-08-29 17:12:21 +02:00
|
|
|
function add_message(index, zephyr) {
|
2012-08-30 20:24:29 +02:00
|
|
|
var zephyr_para = $('<p />');
|
|
|
|
var new_label = function (text, classes, on_click) {
|
|
|
|
zephyr_para.append($('<span />')
|
|
|
|
.text(text)
|
|
|
|
.addClass('label zephyr_label_clickable ' + classes)
|
|
|
|
.click(on_click));
|
|
|
|
zephyr_para.append(' ');
|
|
|
|
};
|
|
|
|
|
|
|
|
if (zephyr.type == 'class') {
|
|
|
|
new_label(zephyr.display_recipient, 'zephyr_class',
|
|
|
|
function (e) { narrow(zephyr.display_recipient, zephyr.id); });
|
|
|
|
new_label(zephyr.instance, 'zephyr_instance',
|
|
|
|
function (e) { narrow_instance(zephyr.display_recipient,
|
|
|
|
zephyr.instance, zephyr.id); });
|
2012-08-29 18:53:03 +02:00
|
|
|
} else {
|
2012-08-30 20:24:29 +02:00
|
|
|
new_label(zephyr.display_recipient, 'zephyr_personal_recipient',
|
|
|
|
function (e) { narrow_personals(zephyr.id); });
|
|
|
|
zephyr_para.append('← ');
|
2012-08-29 18:53:03 +02:00
|
|
|
}
|
2012-08-30 20:24:29 +02:00
|
|
|
|
|
|
|
new_label(zephyr.sender, 'zephyr_sender',
|
|
|
|
function (e) { prepare_personal(zephyr.sender); });
|
|
|
|
|
|
|
|
zephyr_para.append('<br />' + newline2br(zephyr.content));
|
|
|
|
|
|
|
|
$('#table tr:last').after($('<tr />')
|
|
|
|
.attr('id', zephyr.id)
|
|
|
|
.append('<td class="pointer"><p></p></td>')
|
|
|
|
.append($('<td />').append(zephyr_para)));
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
|
|
|
|
2012-08-30 19:56:15 +02:00
|
|
|
$(function() {
|
|
|
|
$(initial_zephyr_json).each(add_message);
|
|
|
|
});
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
function get_updates_longpoll(data) {
|
|
|
|
if (data && data.zephyrs) {
|
|
|
|
$.each(data.zephyrs, add_message);
|
|
|
|
}
|
2012-08-29 17:12:21 +02:00
|
|
|
var last_received = $("tr:last").attr("id");
|
2012-08-28 22:56:21 +02:00
|
|
|
$.post("get_updates_longpoll",
|
|
|
|
{last_received: last_received},
|
|
|
|
function(data) {
|
|
|
|
get_updates_longpoll(data);
|
|
|
|
}, "json");
|
2012-08-29 17:12:21 +02:00
|
|
|
}
|
2012-08-28 22:56:21 +02:00
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
get_updates_longpoll()
|
|
|
|
});
|
|
|
|
|