Module pattern for narrow.js

(imported from commit f1df2e884216560cfe80f29a7e3eda5cea17cefe)
This commit is contained in:
Keegan McAllister 2012-10-18 14:12:04 -04:00
parent 1398641aa3
commit c4bf81a312
8 changed files with 62 additions and 42 deletions

View File

@ -4,7 +4,7 @@
<div class="span9" id="top_statusbar">
<div class="narrowed_to_bar">
<div id="narrowlabel">
<div class="close" onclick="show_all_messages()">&times;</div>
<div class="close" onclick="narrow.show_all_messages()">&times;</div>
<span id="currently_narrowed_to"></span>
</div>
</div>

View File

@ -72,7 +72,7 @@ var people_list = [
<span class="logout">(<a href="/accounts/logout?next=/accounts/login">not you?</a>)</span>
<ul class="nav nav-pills nav-stacked nav-zephyr" id="sidebar">
<li title="Home" class="active">
<a href="#home" data-toggle="pill" onclick="show_all_messages()">
<a href="#home" data-toggle="pill" onclick="narrow.show_all_messages()">
<i class="icon-home"></i>
<span class="hidden-phone"> Home</span>
</a>

View File

@ -22,8 +22,7 @@ var globals =
+ ' hotkeys'
// narrow.js
+ ' narrow_target_message_id narrowed show_all_messages'
+ ' narrow_all_personals narrow_by_recipient narrow_subject'
+ ' narrow'
// setup.js
+ ' loading_spinner templates'

View File

@ -23,20 +23,20 @@
<tr class="recipient_row">
<td colspan="2"
class="message_label_clickable message_header message_header_stream left_part"
onclick="target_message_for_narrow({{id}}); narrow_stream();"
onclick="narrow.target({{id}}); narrow.by_stream();"
title="{{display_recipient}}"></td>
<td class="message_label_clickable message_header message_header_stream right_part"
onclick="target_message_for_narrow({{id}}); narrow_subject();"
onclick="narrow.target({{id}}); narrow.by_subject();"
title="{{subject}}">{{display_recipient}} &nbsp; | &nbsp; {{subject}}</td>
</tr>
{{else}}
<tr class="recipient_row">
<td colspan="2" class="message_label_clickable message_header message_header_huddle left_part"
onclick="target_message_for_narrow({{id}}); narrow_huddle();"
onclick="narrow.target({{id}}); narrow.by_huddle();"
title="Huddle with {{display_reply_to}}"></td>
</td>
<td class="message_label_clickable message_header message_header_huddle right_part"
onclick="target_message_for_narrow({{id}}); narrow_huddle();"
onclick="narrow.target({{id}}); narrow.by_huddle();"
title="Huddle with {{display_reply_to}}">Huddle with {{display_reply_to}}</td>
</tr>
{{/if}}

View File

@ -35,6 +35,6 @@ function get_id(message_row) {
function get_message_row(message_id, table_name) {
if (table_name === undefined)
table_name = (narrowed ? 'zfilt' : 'zhome');
table_name = (narrow.active() ? 'zfilt' : 'zhome');
return $('#' + table_name + message_id);
}

View File

@ -83,7 +83,7 @@ function process_hotkey(code) {
if (composing_message()) {
hide_compose();
} else {
show_all_messages();
narrow.show_all_messages();
}
return process_hotkey;
case 99: // 'c': compose
@ -111,15 +111,15 @@ function process_hotkey(code) {
var keydown_handler = process_hotkey;
var goto_hotkeys = {
99: narrow_by_recipient, // 'c'
105: narrow_subject, // 'i'
112: narrow_all_personals, // 'p'
97: show_all_messages, // 'a'
27: hide_compose // Esc
99: narrow.by_recipient, // 'c'
105: narrow.by_subject, // 'i'
112: narrow.all_personals, // 'p'
97: narrow.show_all_messages, // 'a'
27: hide_compose // Esc
};
process_goto_hotkey = function (code) {
narrow_target_message_id = selected_message_id;
narrow.target(selected_message_id);
if (goto_hotkeys.hasOwnProperty(code))
goto_hotkeys[code]();

View File

@ -1,12 +1,29 @@
var narrow = (function () {
var exports = {};
// For tracking where you were before you narrowed.
var persistent_message_id = 0;
// For narrowing based on a particular message
var narrow_target_message_id = 0;
var target_id = 0;
// Narrowing predicate, or 'false' for the home view.
var narrowed = false;
exports.active = function () {
// Cast to bool
return !!narrowed;
};
exports.predicate = function () {
if (narrowed) {
return narrowed;
} else {
return function () { return true; };
}
};
function do_narrow(description, filter_function) {
narrowed = filter_function;
@ -30,31 +47,31 @@ function do_narrow(description, filter_function) {
// is temporarily selected
select_and_show_by_id(selected_message_id);
selected_message_class = "narrowed_selected_message";
select_and_show_by_id(narrow_target_message_id);
select_and_show_by_id(target_id);
scroll_to_selected();
}
function target_message_for_narrow(id) {
narrow_target_message_id = id;
}
exports.target = function (id) {
target_id = id;
};
function narrow_huddle() {
var original = message_dict[narrow_target_message_id];
function by_huddle() {
var original = message_dict[target_id];
do_narrow("Huddles with " + original.display_reply_to, function (other) {
return (other.type === "personal" || other.type === "huddle")
&& other.reply_to === original.reply_to;
});
}
function narrow_all_personals() {
exports.all_personals = function () {
do_narrow("All huddles with you", function (other) {
return other.type === "personal" || other.type === "huddle";
});
}
};
function narrow_personals() {
function by_personals() {
// Narrow to personals with a specific user
var original = message_dict[narrow_target_message_id];
var original = message_dict[target_id];
do_narrow("Huddles with " + original.display_replay_to, function (other) {
return (other.type === 'personal') &&
@ -64,8 +81,8 @@ function narrow_personals() {
}
function narrow_stream() {
var original = message_dict[narrow_target_message_id];
function by_stream() {
var original = message_dict[target_id];
var message = original.display_recipient;
do_narrow(message, function (other) {
return (other.type === 'stream' &&
@ -73,8 +90,8 @@ function narrow_stream() {
});
}
function narrow_subject() {
var original = message_dict[narrow_target_message_id];
exports.by_subject = function () {
var original = message_dict[target_id];
if (original.type !== 'stream')
return;
@ -84,18 +101,18 @@ function narrow_subject() {
original.recipient_id === other.recipient_id &&
original.subject === other.subject);
});
}
};
// Called for the 'narrow by stream' hotkey.
function narrow_by_recipient() {
exports.by_recipient = function () {
switch (message_dict[selected_message_id].type) {
case 'personal': narrow_personals(); break;
case 'huddle': narrow_huddle(); break;
case 'stream': narrow_stream(); break;
case 'personal': by_personals(); break;
case 'huddle': by_huddle(); break;
case 'stream': by_stream(); break;
}
}
};
function show_all_messages() {
exports.show_all_messages = function () {
if (!narrowed) {
return;
}
@ -113,4 +130,8 @@ function show_all_messages() {
select_and_show_by_id(persistent_message_id);
scroll_to_selected();
}
};
return exports;
}());

View File

@ -212,7 +212,7 @@ function update_selected_message(message) {
message.addClass(selected_message_class);
var new_selected_id = get_id(message);
if (!narrowed && new_selected_id !== selected_message_id) {
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.
//
@ -491,8 +491,8 @@ function add_messages(data) {
message_array = message_array.concat(data.messages);
}
if (narrowed)
add_to_table(data.messages, 'zfilt', narrowed, data.where);
if (narrow.active())
add_to_table(data.messages, 'zfilt', narrow.predicate(), data.where);
// Even when narrowed, add messages to the home view so they exist when we un-narrow.
add_to_table(data.messages, 'zhome', function () { return true; }, data.where);