Clean up code to open "Create stream" panel.

This fixes a bug where hitting the "n" hotkey was
causing double work related to the hashchange system.

The code is now organized like this:

    do_open_create_stream() does the GUI piece

    We call the above directly for hash changes.

    For in-app actions, whether clicks or hotkeys,
    we call open_create_stream(), which delegates
    most of the work to do_open_create_stream() but
    also updates the hash.
This commit is contained in:
Steve Howell 2018-12-01 20:18:20 +00:00 committed by Tim Abbott
parent 13af91869d
commit 43f25eb4a5
4 changed files with 18 additions and 13 deletions

View File

@ -227,7 +227,7 @@ run_test('basic_chars', () => {
overlays.is_active = return_true; overlays.is_active = return_true;
assert_mapping('S', 'subs.keyboard_sub'); assert_mapping('S', 'subs.keyboard_sub');
assert_mapping('V', 'subs.view_stream'); assert_mapping('V', 'subs.view_stream');
assert_mapping('n', 'subs.new_stream_clicked'); assert_mapping('n', 'subs.open_create_stream');
page_params.can_create_streams = false; page_params.can_create_streams = false;
assert_unmapped('n'); assert_unmapped('n');
overlays.streams_open = return_false; overlays.streams_open = return_false;

View File

@ -613,7 +613,7 @@ exports.process_hotkey = function (e, hotkey) {
return true; return true;
} }
if (event_name === 'n_key' && overlays.streams_open() && page_params.can_create_streams) { if (event_name === 'n_key' && overlays.streams_open() && page_params.can_create_streams) {
subs.new_stream_clicked(); subs.open_create_stream();
return true; return true;
} }
return false; return false;

View File

@ -234,11 +234,6 @@ exports.new_stream_clicked = function (stream_name) {
if (window.innerWidth > 700) { if (window.innerWidth > 700) {
$('#create_stream_name').focus(); $('#create_stream_name').focus();
} }
// change the hash to #streams/new to allow for linking and
// easy discovery.
window.location.hash = "#streams/new";
}; };
function clear_error_display() { function clear_error_display() {

View File

@ -609,7 +609,7 @@ exports.change_state = (function () {
if (hash.arguments.length > 0) { if (hash.arguments.length > 0) {
// if in #streams/new form. // if in #streams/new form.
if (hash.arguments[0] === "new") { if (hash.arguments[0] === "new") {
exports.new_stream_clicked(); exports.do_open_create_stream();
} else if (hash.arguments[0] === "all") { } else if (hash.arguments[0] === "all") {
maybe_select_tab("all-streams"); maybe_select_tab("all-streams");
} else if (hash.arguments[0] === "subscribed") { } else if (hash.arguments[0] === "subscribed") {
@ -772,7 +772,10 @@ function ajaxUnsubscribe(sub) {
}); });
} }
exports.new_stream_clicked = function () { exports.do_open_create_stream = function () {
// Only call this directly for hash changes.
// Prefer open_create_stream().
var stream = $.trim($("#search_stream_name").val()); var stream = $.trim($("#search_stream_name").val());
if (!should_list_all_streams()) { if (!should_list_all_streams()) {
@ -785,6 +788,16 @@ exports.new_stream_clicked = function () {
stream_create.new_stream_clicked(stream); stream_create.new_stream_clicked(stream);
}; };
exports.open_create_stream = function () {
exports.do_open_create_stream();
// this will change the hash which will attempt to retrigger the create
// stream code, so we prevent this once.
exports.change_state.prevent_once();
window.location.hash = "#streams/new";
};
exports.sub_or_unsub = function (sub) { exports.sub_or_unsub = function (sub) {
if (sub.subscribed) { if (sub.subscribed) {
ajaxUnsubscribe(sub); ajaxUnsubscribe(sub);
@ -806,10 +819,7 @@ exports.initialize = function () {
$("#subscriptions_table").on("click", ".create_stream_button", function (e) { $("#subscriptions_table").on("click", ".create_stream_button", function (e) {
e.preventDefault(); e.preventDefault();
exports.new_stream_clicked(); exports.open_create_stream();
// this will change the hash which will attempt to retrigger the create
// stream code, so we prevent this once.
exports.change_state.prevent_once();
}); });
$(".subscriptions").on("click", "[data-dismiss]", function (e) { $(".subscriptions").on("click", "[data-dismiss]", function (e) {