Extract typing_events.js.

This commit is contained in:
Steve Howell 2017-03-22 07:11:41 -07:00
parent cde1861655
commit 4fb8339954
6 changed files with 110 additions and 87 deletions

View File

@ -36,6 +36,7 @@
"resize": false, "resize": false,
"loading": false, "loading": false,
"typing": false, "typing": false,
"typing_events": false,
"typing_data": false, "typing_data": false,
"typing_status": false, "typing_status": false,
"compose": false, "compose": false,

View File

@ -252,9 +252,9 @@ function dispatch_normal_event(event) {
} }
if (event.op === 'start') { if (event.op === 'start') {
typing.display_notification(event); typing_events.display_notification(event);
} else if (event.op === 'stop') { } else if (event.op === 'stop') {
typing.hide_notification(event); typing_events.hide_notification(event);
} }
break; break;

View File

@ -1,13 +1,9 @@
var typing = (function () { var typing = (function () {
var exports = {}; var exports = {};
// How long before we assume a client has gone away
// and expire its typing status
var TYPING_STARTED_EXPIRY_PERIOD = 15000; // 15s
// Note!: There are also timing constants in typing_status.js // This module handles the outbound side of typing indicators.
// that make typing indicators work. // We detect changes in the compose box and notify the server
// when we are typing. For the inbound side see typing_events.js.
var stop_typing_timers = new Dict();
function send_typing_notification_ajax(recipients, operation) { function send_typing_notification_ajax(recipients, operation) {
channel.post({ channel.post({
@ -95,83 +91,6 @@ $(document).on('compose_canceled.zulip compose_finished.zulip', function () {
typing_status.stop(worker); typing_status.stop(worker);
}); });
function get_users_typing_for_narrow() {
if (!narrow.narrowed_to_pms()) {
// Narrow is neither pm-with nor is: private
return [];
}
if (narrow.operators()[0].operator === 'pm-with') {
// Get list of users typing in this conversation
var narrow_emails_string = narrow.operators()[0].operand;
// TODO: Create people.emails_strings_to_user_ids.
var narrow_user_ids_string = people.emails_strings_to_user_ids_string(narrow_emails_string);
var narrow_user_ids = narrow_user_ids_string.split(',').map(function (user_id_string) {
return parseInt(user_id_string, 10);
});
var group = narrow_user_ids.concat([page_params.user_id]);
return typing_data.get_group_typists(group);
}
// Get all users typing (in all private conversations with current user)
return typing_data.get_all_typists();
}
function render_notifications_for_narrow() {
var user_ids = get_users_typing_for_narrow();
var users_typing = user_ids.map(people.get_person_from_user_id);
if (users_typing.length === 0) {
$('#typing_notifications').hide();
} else {
$('#typing_notifications').html(templates.render('typing_notifications', {users: users_typing}));
$('#typing_notifications').show();
}
}
$(document).on('narrow_activated.zulip', render_notifications_for_narrow);
$(document).on('narrow_deactivated.zulip', render_notifications_for_narrow);
exports.hide_notification = function (event) {
var recipients = event.recipients.map(function (user) {
return user.user_id;
});
recipients.sort();
// If there's an existing timer for this typing notifications
// thread, clear it.
if (stop_typing_timers[recipients] !== undefined) {
clearTimeout(stop_typing_timers[recipients]);
stop_typing_timers[recipients] = undefined;
}
var removed = typing_data.remove_typist(recipients, event.sender.user_id);
if (removed) {
render_notifications_for_narrow();
}
};
exports.display_notification = function (event) {
var recipients = event.recipients.map(function (user) {
return user.user_id;
});
recipients.sort();
var sender_id = event.sender.user_id;
event.sender.name = people.get_person_from_user_id(sender_id).full_name;
typing_data.add_typist(recipients, sender_id);
render_notifications_for_narrow();
// If there's an existing timeout for this typing notifications
// thread, clear it.
if (stop_typing_timers[recipients] !== undefined) {
clearTimeout(stop_typing_timers[recipients]);
}
// Set a time to expire the data if the sender stops transmitting
stop_typing_timers[recipients] = setTimeout(function () {
exports.hide_notification(event);
}, TYPING_STARTED_EXPIRY_PERIOD);
};
return exports; return exports;
}()); }());

102
static/js/typing_events.js Normal file
View File

@ -0,0 +1,102 @@
var typing_events = (function () {
var exports = {};
// This code handles the inbound side of typing notifications.
// When another user is typing, we process the events here.
//
// We also handle the local event of re-narrowing.
// (For the outbound code, see typing.js.)
// How long before we assume a client has gone away
// and expire its typing status
var TYPING_STARTED_EXPIRY_PERIOD = 15000; // 15s
// Note!: There are also timing constants in typing_status.js
// that make typing indicators work.
var stop_typing_timers = new Dict();
function get_users_typing_for_narrow() {
if (!narrow.narrowed_to_pms()) {
// Narrow is neither pm-with nor is: private
return [];
}
if (narrow.operators()[0].operator === 'pm-with') {
// Get list of users typing in this conversation
var narrow_emails_string = narrow.operators()[0].operand;
// TODO: Create people.emails_strings_to_user_ids.
var narrow_user_ids_string = people.emails_strings_to_user_ids_string(narrow_emails_string);
var narrow_user_ids = narrow_user_ids_string.split(',').map(function (user_id_string) {
return parseInt(user_id_string, 10);
});
var group = narrow_user_ids.concat([page_params.user_id]);
return typing_data.get_group_typists(group);
}
// Get all users typing (in all private conversations with current user)
return typing_data.get_all_typists();
}
function render_notifications_for_narrow() {
var user_ids = get_users_typing_for_narrow();
var users_typing = user_ids.map(people.get_person_from_user_id);
if (users_typing.length === 0) {
$('#typing_notifications').hide();
} else {
$('#typing_notifications').html(templates.render('typing_notifications', {users: users_typing}));
$('#typing_notifications').show();
}
}
exports.hide_notification = function (event) {
var recipients = event.recipients.map(function (user) {
return user.user_id;
});
recipients.sort();
// If there's an existing timer for this typing notifications
// thread, clear it.
if (stop_typing_timers[recipients] !== undefined) {
clearTimeout(stop_typing_timers[recipients]);
stop_typing_timers[recipients] = undefined;
}
var removed = typing_data.remove_typist(recipients, event.sender.user_id);
if (removed) {
render_notifications_for_narrow();
}
};
exports.display_notification = function (event) {
var recipients = event.recipients.map(function (user) {
return user.user_id;
});
recipients.sort();
var sender_id = event.sender.user_id;
event.sender.name = people.get_person_from_user_id(sender_id).full_name;
typing_data.add_typist(recipients, sender_id);
render_notifications_for_narrow();
// If there's an existing timeout for this typing notifications
// thread, clear it.
if (stop_typing_timers[recipients] !== undefined) {
clearTimeout(stop_typing_timers[recipients]);
}
// Set a time to expire the data if the sender stops transmitting
stop_typing_timers[recipients] = setTimeout(function () {
exports.hide_notification(event);
}, TYPING_STARTED_EXPIRY_PERIOD);
};
$(document).on('narrow_activated.zulip', render_notifications_for_narrow);
$(document).on('narrow_deactivated.zulip', render_notifications_for_narrow);
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = typing_events;
}

View File

@ -5,7 +5,7 @@ var exports = {};
// The following constants are tuned to work with // The following constants are tuned to work with
// TYPING_STARTED_EXPIRY_PERIOD, which is what the other // TYPING_STARTED_EXPIRY_PERIOD, which is what the other
// users will use to time out our messages. (Or us, // users will use to time out our messages. (Or us,
// depending on your perspective.) See typing.js. // depending on your perspective.) See typing_events.js.
// How frequently 'still typing' notifications are sent // How frequently 'still typing' notifications are sent
// to extend the expiry // to extend the expiry

View File

@ -904,6 +904,7 @@ JS_SPECS = {
'js/typing.js', 'js/typing.js',
'js/typing_status.js', 'js/typing_status.js',
'js/typing_data.js', 'js/typing_data.js',
'js/typing_events.js',
'js/ui_init.js', 'js/ui_init.js',
'js/shim.js', 'js/shim.js',
# JS bundled by webpack is also included here if PIPELINE_ENABLED setting is true # JS bundled by webpack is also included here if PIPELINE_ENABLED setting is true