2012-10-05 20:25:27 +02:00
|
|
|
// We want to remember how far we were scrolled on each 'tab'.
|
|
|
|
// To do so, we need to save away the old position of the
|
|
|
|
// scrollbar when we switch to a new tab (and restore it
|
|
|
|
// when we switch back.)
|
|
|
|
var scroll_positions = {};
|
|
|
|
|
2012-10-10 16:20:48 +02:00
|
|
|
function register_onclick(message_row, message_id) {
|
|
|
|
message_row.find(".messagebox").click(function (e) {
|
2012-10-03 21:44:07 +02:00
|
|
|
if (!(clicking && mouse_moved)) {
|
|
|
|
// Was a click (not a click-and-drag).
|
2012-10-10 16:20:48 +02:00
|
|
|
select_message_by_id(message_id);
|
2012-10-10 16:18:51 +02:00
|
|
|
respond_to_message();
|
2012-10-03 21:44:07 +02:00
|
|
|
}
|
|
|
|
mouse_moved = false;
|
|
|
|
clicking = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-10-09 17:41:34 +02:00
|
|
|
function focus_on(field_id) {
|
|
|
|
// Call after autocompleting on a field, to advance the focus to
|
|
|
|
// the next input field.
|
|
|
|
|
|
|
|
// Bootstrap's typeahead does not expose a callback for when an
|
|
|
|
// autocomplete selection has been made, so we have to do this
|
|
|
|
// manually.
|
|
|
|
$("#" + field_id).focus();
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:44:07 +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 hide_email() {
|
2012-10-09 23:58:24 +02:00
|
|
|
$('.sender_email').addClass('invisible');
|
2012-10-03 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 16:20:48 +02:00
|
|
|
function show_email(message_id) {
|
2012-10-03 21:44:07 +02:00
|
|
|
hide_email();
|
2012-10-10 16:20:48 +02:00
|
|
|
get_message_row(message_id).find('.sender_email').removeClass('invisible');
|
2012-10-03 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
status_box.show();
|
|
|
|
}
|
|
|
|
|
2012-10-09 20:56:11 +02:00
|
|
|
function report_success(response, status_box) {
|
|
|
|
status_box.removeClass(status_classes).addClass('alert-success')
|
|
|
|
.text(response).stop(true).fadeTo(0, 1);
|
|
|
|
status_box.show();
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:44:07 +02:00
|
|
|
var clicking = false;
|
|
|
|
var mouse_moved = false;
|
|
|
|
|
2012-10-10 16:21:40 +02:00
|
|
|
function mousedown() {
|
2012-10-03 21:44:07 +02:00
|
|
|
mouse_moved = false;
|
|
|
|
clicking = true;
|
|
|
|
}
|
|
|
|
|
2012-10-10 16:21:40 +02:00
|
|
|
function mousemove() {
|
2012-10-03 21:44:07 +02:00
|
|
|
if (clicking) {
|
|
|
|
mouse_moved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:34:57 +02:00
|
|
|
function resizehandler(e) {
|
|
|
|
var viewport = $(window);
|
|
|
|
var sidebar = $("#sidebar");
|
|
|
|
var narrowcontent = $(".narrowcontent");
|
|
|
|
var sidebar_nav = $(".sidebar-nav");
|
|
|
|
var composebox = $("#compose");
|
|
|
|
var narrowbox = $("#narrowbox");
|
|
|
|
if (viewport.width() <= 767) {
|
|
|
|
sidebar.removeClass('nav-stacked');
|
|
|
|
|
|
|
|
var space_taken_up_by_navbar = sidebar_nav.outerHeight(true);
|
|
|
|
$("#nav_whitespace").height(space_taken_up_by_navbar); // .visible-phone only, so doesn't need undoing
|
|
|
|
narrowbox.css('top', space_taken_up_by_navbar);
|
|
|
|
|
|
|
|
var message_list_width = $("#main_div").outerWidth();
|
|
|
|
composebox.width(message_list_width);
|
|
|
|
// narrowcontent has padding and a border, so we need to
|
|
|
|
// subtract those out when we set the width
|
|
|
|
var border_and_padding = narrowcontent.outerWidth() - narrowcontent.width();
|
|
|
|
narrowcontent.width(message_list_width - border_and_padding);
|
|
|
|
sidebar_nav.width(message_list_width);
|
|
|
|
} else {
|
|
|
|
sidebar.addClass('nav-stacked');
|
|
|
|
narrowbox.css('top', 0);
|
|
|
|
compose.width('');
|
|
|
|
narrowcontent.width('');
|
|
|
|
siebar_nav.width('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:44:07 +02:00
|
|
|
var autocomplete_needs_update = false;
|
|
|
|
|
|
|
|
function update_autocomplete() {
|
2012-10-10 23:26:01 +02:00
|
|
|
stream_list.sort();
|
2012-10-11 00:01:39 +02:00
|
|
|
subject_list.sort();
|
2012-10-03 21:44:07 +02:00
|
|
|
people_list.sort();
|
|
|
|
|
|
|
|
// limit number of items so the list doesn't fall off the screen
|
2012-10-10 23:33:38 +02:00
|
|
|
$( "#stream" ).typeahead({
|
2012-10-10 23:26:01 +02:00
|
|
|
source: stream_list,
|
2012-10-03 23:22:51 +02:00
|
|
|
items: 3
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
2012-10-11 00:01:39 +02:00
|
|
|
$( "#subject" ).typeahead({
|
|
|
|
source: subject_list,
|
2012-10-03 23:22:51 +02:00
|
|
|
items: 2
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
2012-10-09 15:36:24 +02:00
|
|
|
$( "#huddle_recipient" ).typeahead({
|
2012-10-03 21:44:07 +02:00
|
|
|
source: people_list,
|
2012-10-05 23:39:13 +02:00
|
|
|
items: 4,
|
|
|
|
matcher: function (item) {
|
|
|
|
// Assumes we are matching on email addresses, not
|
|
|
|
// e.g. full names which would have spaces.
|
|
|
|
var current_recipient = $(this.query.split(" ")).last()[0];
|
|
|
|
// Case-insensitive (from Bootstrap's default matcher).
|
|
|
|
return (item.toLowerCase().indexOf(current_recipient.toLowerCase()) !== -1);
|
|
|
|
},
|
|
|
|
updater: function (item) {
|
|
|
|
var previous_recipients = this.query.split(" ");
|
|
|
|
previous_recipients.pop();
|
|
|
|
previous_recipients = previous_recipients.join(" ");
|
|
|
|
if (previous_recipients.length !== 0) {
|
|
|
|
previous_recipients += " ";
|
|
|
|
}
|
|
|
|
return previous_recipients + item;
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
autocomplete_needs_update = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
// NB: This just binds to current elements, and won't bind to elements
|
|
|
|
// created after ready() is called.
|
2012-10-09 15:51:12 +02:00
|
|
|
|
2012-10-10 23:29:02 +02:00
|
|
|
$('#message-type-tabs a[href="#stream-message"]').on('shown', function (e) {
|
2012-10-05 23:42:58 +02:00
|
|
|
$('#personal-message').hide();
|
2012-10-10 23:29:02 +02:00
|
|
|
$('#stream-message').show();
|
2012-10-10 23:09:16 +02:00
|
|
|
$('#new_message_type').val('stream');
|
2012-10-10 22:57:02 +02:00
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
2012-10-10 23:09:16 +02:00
|
|
|
focus_on("stream");
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
2012-10-09 23:46:41 +02:00
|
|
|
$('#message-type-tabs a[href="#personal-message"]').on('shown', function (e) {
|
2012-10-05 23:42:58 +02:00
|
|
|
$('#personal-message').show();
|
2012-10-10 23:29:02 +02:00
|
|
|
$('#stream-message').hide();
|
2012-10-05 23:42:58 +02:00
|
|
|
$('#new_message_type').val('personal');
|
2012-10-10 22:57:02 +02:00
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
|
|
|
focus_on("huddle_recipient");
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
// Prepare the click handler for subbing to a new stream to which
|
2012-10-10 16:20:48 +02:00
|
|
|
// you have composed a message.
|
2012-10-03 21:44:07 +02:00
|
|
|
$('#create-it').click(function () {
|
2012-10-10 23:33:38 +02:00
|
|
|
sub_from_home(compose_stream_name(), $('#stream-dne'));
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
// Prepare the click handler for subbing to an existing stream.
|
2012-10-03 21:44:07 +02:00
|
|
|
$('#sub-it').click(function () {
|
2012-10-10 23:33:38 +02:00
|
|
|
sub_from_home(compose_stream_name(), $('#stream-nosub'));
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
|
|
|
|
2012-10-10 20:20:31 +02:00
|
|
|
var throttled_scrollhandler = $.throttle(50, function(e) {
|
2012-10-05 19:22:43 +02:00
|
|
|
if ($('#home').hasClass('active')) {
|
2012-10-10 20:20:31 +02:00
|
|
|
keep_pointer_in_view();
|
|
|
|
if (e.type === 'mousewheel') {
|
|
|
|
// If we mousewheel (or trackpad-scroll) when
|
|
|
|
// we're at the top and bottom of the page, the
|
|
|
|
// pointer may still want to move.
|
|
|
|
move_pointer_at_page_top_and_bottom();
|
|
|
|
}
|
2012-10-03 21:44:07 +02:00
|
|
|
}
|
2012-10-05 21:35:36 +02:00
|
|
|
});
|
|
|
|
$(window).mousewheel(throttled_scrollhandler);
|
|
|
|
$(window).scroll(throttled_scrollhandler);
|
2012-10-03 21:44:07 +02:00
|
|
|
|
2012-10-11 22:34:57 +02:00
|
|
|
var throttled_resizehandler = $.throttle(50, resizehandler);
|
|
|
|
$(window).resize(throttled_resizehandler);
|
|
|
|
|
2012-10-05 20:25:27 +02:00
|
|
|
$('#sidebar a[data-toggle="pill"]').on('show', function (e) {
|
|
|
|
// Save the position of our old tab away, before we switch
|
|
|
|
var viewport = $(window);
|
|
|
|
var old_tab = $(e.relatedTarget).attr('href');
|
|
|
|
scroll_positions[old_tab] = viewport.scrollTop();
|
|
|
|
});
|
|
|
|
$('#sidebar a[data-toggle="pill"]').on('shown', function (e) {
|
|
|
|
// Right after we show the new tab, restore its old scroll position
|
|
|
|
var viewport = $(window);
|
|
|
|
var target_tab = $(e.target).attr('href');
|
|
|
|
if (scroll_positions.hasOwnProperty(target_tab)) {
|
|
|
|
viewport.scrollTop(scroll_positions[target_tab]);
|
|
|
|
} else {
|
|
|
|
viewport.scrollTop(0);
|
|
|
|
}
|
2012-10-09 21:44:29 +02:00
|
|
|
|
|
|
|
// Hide all our error messages when switching tabs
|
|
|
|
$('.alert-error').hide();
|
|
|
|
$('.alert-success').hide();
|
|
|
|
$('.alert-info').hide();
|
|
|
|
$('.alert').hide();
|
2012-10-10 20:06:59 +02:00
|
|
|
|
|
|
|
// Set the URL bar title to show the sub-page you're currently on.
|
|
|
|
var browser_url = $(e.target).attr('href');
|
|
|
|
if (browser_url === "#home") {
|
|
|
|
browser_url = "#";
|
|
|
|
}
|
|
|
|
window.history.pushState("object or string", "Title", browser_url);
|
2012-10-05 20:25:27 +02:00
|
|
|
});
|
|
|
|
|
2012-10-03 21:44:07 +02:00
|
|
|
$('.button-slide').click(function () {
|
2012-10-10 23:33:38 +02:00
|
|
|
show_compose('stream', $("#stream"));
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|
2012-10-03 22:24:17 +02:00
|
|
|
|
|
|
|
$('#sidebar a[href="#subscriptions"]').click(fetch_subs);
|
2012-10-03 22:35:35 +02:00
|
|
|
|
|
|
|
var settings_status = $('#settings-status');
|
|
|
|
$("#current_settings form").ajaxForm({
|
|
|
|
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);
|
2012-10-03 23:22:51 +02:00
|
|
|
}
|
2012-10-03 22:35:35 +02:00
|
|
|
});
|
2012-10-03 21:44:07 +02:00
|
|
|
});
|