Pseudo-module pattern for rows.js

It's actually simpler than a full module, due to lack of local state or functions.

(imported from commit b9180f1cdc26aed3f2e1f35dfae9ed4240b64446)
This commit is contained in:
Keegan McAllister 2012-10-18 14:55:41 -04:00
parent 01061e95e0
commit eb9140a0a2
5 changed files with 69 additions and 65 deletions

View File

@ -15,8 +15,7 @@ var globals =
+ ' compose'
// rows.js
+ ' get_first_visible get_last_visible get_next_visible get_prev_visible'
+ ' get_id get_message_row'
+ ' rows'
// hotkey.js
+ ' hotkeys'

View File

@ -14,12 +14,12 @@ function num_pressed_keys() {
}
var directional_hotkeys = {
40: get_next_visible, // down arrow
106: get_next_visible, // 'j'
38: get_prev_visible, // up arrow
107: get_prev_visible, // 'k'
36: get_first_visible, // Home
35: get_last_visible // End
40: rows.next_visible, // down arrow
106: rows.next_visible, // 'j'
38: rows.prev_visible, // up arrow
107: rows.prev_visible, // 'k'
36: rows.first_visible, // Home
35: rows.last_visible // End
};
// These are not exported, but we declare them here to make JSLint happy.
@ -51,7 +51,7 @@ function process_hotkey(code) {
// At the last message, scroll to the bottom so we have
// lots of nice whitespace for new messages coming in.
//
// FIXME: this doesn't work for End because get_last_visible()
// FIXME: this doesn't work for End because rows.last_visible()
// always returns a message.
viewport.scrollTop($("#main_div").outerHeight(true));
}
@ -70,13 +70,13 @@ function process_hotkey(code) {
switch (code) {
case 33: // Page Up
if (at_top_of_viewport()) {
select_message(get_first_visible(), false);
select_message(rows.first_visible(), false);
}
return false; // We want the browser to actually page up and down
case 32: // Spacebar
case 34: // Page Down
if (at_bottom_of_viewport()) {
select_message(get_last_visible(), false);
select_message(rows.last_visible(), false);
}
return false;
case 27: // Esc: hide compose pane or un-narrow

View File

@ -1,7 +1,11 @@
// We don't need an andSelf() here because we already know
// that our next element is *not* a message_row, so this
// isn't going to end up empty unless we're at the bottom or top.
function get_next_visible(message_row) {
// This file doesn't have any local state or helper functions, so it has
// a simpler structure than the proper modules.
var rows = {
// We don't need an andSelf() here because we already know
// that our next element is *not* a message_row, so this
// isn't going to end up empty unless we're at the bottom or top.
next_visible: function (message_row) {
if (message_row === undefined)
return [];
var row = message_row.next('.message_row');
@ -9,9 +13,9 @@ function get_next_visible(message_row) {
return row;
}
return message_row.nextUntil('.message_row').next('.message_row');
}
},
function get_prev_visible(message_row) {
prev_visible: function (message_row) {
if (message_row === undefined)
return [];
var row = message_row.prev('.message_row');
@ -19,22 +23,23 @@ function get_prev_visible(message_row) {
return row;
}
return message_row.prevUntil('.message_row').prev('.message_row');
}
},
function get_first_visible() {
first_visible: function () {
return $('.focused_table .message_row:first');
}
},
function get_last_visible() {
last_visible: function () {
return $('.focused_table .message_row:last');
}
},
function get_id(message_row) {
id: function (message_row) {
return message_row.attr('zid');
}
},
function get_message_row(message_id, table_name) {
get: function (message_id, table_name) {
if (table_name === undefined)
table_name = (narrow.active() ? 'zfilt' : 'zhome');
return $('#' + table_name + message_id);
}
}
};

View File

@ -36,7 +36,7 @@ function hide_email() {
function show_email(message_id) {
hide_email();
get_message_row(message_id).find('.sender_email').removeClass('invisible');
rows.get(message_id).find('.sender_email').removeClass('invisible');
}
function report_error(response, xhr, status_box) {

View File

@ -8,7 +8,7 @@ var viewport = $(window);
var reloading_app = false;
var selected_message_id = -1; /* to be filled in on document.ready */
var selected_message; // = get_message_row(selected_message_id)
var selected_message; // = rows.get(selected_message_id)
var get_updates_params = {
first: -1,
last: -1,
@ -197,21 +197,21 @@ function select_message_by_id(message_id) {
if (message_id === selected_message_id) {
return;
}
select_message(get_message_row(message_id), false);
select_message(rows.get(message_id), false);
}
// Called on page load and when we [un]narrow.
// Forces a call to select_message even if the id has not changed,
// because the visible table might have.
function select_and_show_by_id(message_id) {
select_message(get_message_row(message_id), true);
select_message(rows.get(message_id), true);
}
function update_selected_message(message) {
$('.' + selected_message_class).removeClass(selected_message_class);
message.addClass(selected_message_class);
var new_selected_id = get_id(message);
var new_selected_id = rows.id(message);
if (!narrow.active() && new_selected_id !== selected_message_id) {
// Narrowing is a temporary view on top of the home view and
// doesn't permanently affect where you are.
@ -227,7 +227,7 @@ function select_message(next_message, scroll_to) {
/* If the message exists but is hidden, try to find the next visible one. */
if (next_message.length !== 0 && next_message.is(':hidden')) {
next_message = get_next_visible(next_message);
next_message = rows.next_visible(next_message);
}
/* Fall back to the first visible message. */
@ -322,7 +322,7 @@ function add_to_table(messages, table_name, filter_function, where) {
var top_group = message_groups[table_name][0];
var top_messages = [];
$.each(top_group, function (index, id) {
get_message_row(id, table_name).remove();
rows.get(id, table_name).remove();
top_messages.push(message_dict[id]);
});
messages = messages.concat(top_messages);
@ -388,7 +388,7 @@ function add_to_table(messages, table_name, filter_function, where) {
}
$.each(messages_to_render, function (index, message) {
var row = get_message_row(message.id, table_name);
var row = rows.get(message.id, table_name);
register_onclick(row, message.id);
row.find('.message_content a').each(function (index, link) {
@ -400,7 +400,7 @@ function add_to_table(messages, table_name, filter_function, where) {
});
$.each(ids_where_next_is_same_sender, function (index, id) {
get_message_row(id, table_name).find('.messagebox').addClass("next_is_same_sender");
rows.get(id, table_name).find('.messagebox').addClass("next_is_same_sender");
});
}
@ -664,11 +664,11 @@ function at_bottom_of_viewport() {
function keep_pointer_in_view() {
var candidate;
var next_message = get_message_row(selected_message_id);
var next_message = rows.get(selected_message_id);
if (above_view_threshold(next_message) && (!at_top_of_viewport())) {
while (above_view_threshold(next_message)) {
candidate = get_next_visible(next_message);
candidate = rows.next_visible(next_message);
if (candidate.length === 0) {
break;
} else {
@ -677,7 +677,7 @@ function keep_pointer_in_view() {
}
} else if (below_view_threshold(next_message) && (!at_bottom_of_viewport())) {
while (below_view_threshold(next_message)) {
candidate = get_prev_visible(next_message);
candidate = rows.prev_visible(next_message);
if (candidate.length === 0) {
break;
} else {
@ -695,13 +695,13 @@ function keep_pointer_in_view() {
// I'm at the very top or the very bottom of the page.
function move_pointer_at_page_top_and_bottom(delta) {
if (delta !== 0 && (at_top_of_viewport() || at_bottom_of_viewport())) {
var next_message = get_message_row(selected_message_id);
var next_message = rows.get(selected_message_id);
if (delta > 0) {
// Scrolling up (want older messages)
next_message = get_prev_visible(next_message);
next_message = rows.prev_visible(next_message);
} else {
// We're scrolling down (we want more recent messages)
next_message = get_next_visible(next_message);
next_message = rows.next_visible(next_message);
}
if (next_message.length !== 0) {
update_selected_message(next_message);