From ac1c1114898fe3160d76bbb7e34fe5d64b367337 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 27 Feb 2013 13:24:56 -0500 Subject: [PATCH] Fill in default recipient when composing while narrowed Fixes #977. (imported from commit 1abe26d94d5179a3dd1e970224a36c63bba9ff48) --- zephyr/static/js/compose.js | 16 +++++++++++----- zephyr/static/js/narrow.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/zephyr/static/js/compose.js b/zephyr/static/js/compose.js index ef46b22c92..9af683611c 100644 --- a/zephyr/static/js/compose.js +++ b/zephyr/static/js/compose.js @@ -129,11 +129,17 @@ exports.start = function (msg_type, opts) { compose.clear(); - opts = $.extend({ message_type: msg_type, - stream: '', - subject: '', - private_message_recipient: '' - }, opts); + var default_opts = { + message_type: msg_type, + stream: '', + subject: '', + private_message_recipient: '' + }; + + // Set default parameters based on the current narrowed view. + narrow.set_compose_defaults(default_opts); + + opts = $.extend(default_opts, opts); compose.stream_name(opts.stream); compose.subject(opts.subject); diff --git a/zephyr/static/js/narrow.js b/zephyr/static/js/narrow.js index 77c41b8f9e..944e773063 100644 --- a/zephyr/static/js/narrow.js +++ b/zephyr/static/js/narrow.js @@ -99,6 +99,43 @@ exports.describe = function (operators) { }).join(', '); }; +// Collect operators which appear only once into an object, +// and discard those which appear more than once. +function collect_single(operators) { + var seen = {}; + var result = {}; + $.each(operators, function (index, elem) { + var key = elem[0]; + if (seen.hasOwnProperty(key)) { + delete result[key]; + } else { + result[key] = elem[1]; + seen [key] = true; + } + }); + return result; +} + +// Modify default compose parameters (stream etc.) based on +// the current narrowed view. +// +// This logic is here and not in the 'compose' module because +// it will get more complicated as we add things to the narrow +// operator language. +exports.set_compose_defaults = function (opts) { + var single = collect_single(exports.operators()); + + // Set the stream, subject, and/or PM recipient if they are + // uniquely specified in the narrow view. + $.each(['stream', 'subject'], function (idx, key) { + if (single[key] !== undefined) + opts[key] = single[key]; + }); + + if (single['pm-with'] !== undefined) + opts.private_message_recipient = single['pm-with']; +}; + // Parse a string into a list of operators (see below). exports.parse = function (str) { var operators = [];