2019-11-02 00:06:25 +01:00
|
|
|
const render_subscription = require('../templates/subscription.hbs');
|
|
|
|
const render_subscription_settings = require('../templates/subscription_settings.hbs');
|
|
|
|
const render_subscription_table_body = require('../templates/subscription_table_body.hbs');
|
|
|
|
const render_subscriptions = require('../templates/subscriptions.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.show_subs_pane = {
|
|
|
|
nothing_selected: function () {
|
|
|
|
$(".nothing-selected, #stream_settings_title").show();
|
|
|
|
$("#add_new_stream_title, .settings, #stream-creation").hide();
|
|
|
|
},
|
|
|
|
settings: function () {
|
|
|
|
$(".settings, #stream_settings_title").show();
|
|
|
|
$("#add_new_stream_title, #stream-creation, .nothing-selected").hide();
|
|
|
|
},
|
|
|
|
};
|
2016-11-04 22:18:23 +01:00
|
|
|
|
2019-03-26 05:38:37 +01:00
|
|
|
exports.check_button_for_sub = function (sub) {
|
2020-01-16 22:27:27 +01:00
|
|
|
return $(".stream-row[data-stream-id='" + sub.stream_id + "'] .check");
|
2019-03-26 05:38:37 +01:00
|
|
|
};
|
2016-11-04 22:18:23 +01:00
|
|
|
|
2019-04-01 11:04:41 +02:00
|
|
|
exports.row_for_stream_id = function (stream_id) {
|
2017-02-15 17:38:44 +01:00
|
|
|
return $(".stream-row[data-stream-id='" + stream_id + "']");
|
2019-04-01 11:04:41 +02:00
|
|
|
};
|
2017-02-15 17:38:44 +01:00
|
|
|
|
2019-03-31 13:00:32 +02:00
|
|
|
exports.settings_button_for_sub = function (sub) {
|
2018-03-23 23:21:57 +01:00
|
|
|
// We don't do expectOne() here, because this button is only
|
|
|
|
// visible if the user has that stream selected in the streams UI.
|
2020-01-16 22:27:27 +01:00
|
|
|
return $(".subscription_settings[data-stream-id='" + sub.stream_id + "'] .subscribe-button");
|
2019-03-31 13:00:32 +02:00
|
|
|
};
|
2017-01-27 00:40:42 +01:00
|
|
|
|
2017-03-23 05:15:16 +01:00
|
|
|
function get_row_data(row) {
|
2020-01-16 20:40:40 +01:00
|
|
|
const row_id = parseInt(row.attr('data-stream-id'), 10);
|
2017-03-23 05:15:16 +01:00
|
|
|
if (row_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row_object = stream_data.get_sub_by_id(row_id);
|
2017-03-25 01:27:52 +01:00
|
|
|
return {
|
|
|
|
id: row_id,
|
|
|
|
object: row_object,
|
|
|
|
};
|
2017-03-23 05:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 13:30:12 +02:00
|
|
|
exports.get_active_data = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_row = $('div.stream-row.active');
|
2020-01-16 20:40:40 +01:00
|
|
|
const valid_active_id = parseInt(active_row.attr('data-stream-id'), 10);
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_tab = $('.subscriptions-container').find('div.ind-tab.selected');
|
2017-03-25 01:27:52 +01:00
|
|
|
return {
|
|
|
|
row: active_row,
|
|
|
|
id: valid_active_id,
|
|
|
|
tab: active_tab,
|
|
|
|
};
|
2019-04-06 13:30:12 +02:00
|
|
|
};
|
2017-03-23 05:15:16 +01:00
|
|
|
|
2017-10-16 19:18:02 +02:00
|
|
|
function get_hash_safe() {
|
|
|
|
if (typeof window !== "undefined" && typeof window.location.hash === "string") {
|
|
|
|
return window.location.hash.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-11-08 10:12:05 +01:00
|
|
|
function selectText(element) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let range;
|
|
|
|
let sel;
|
2013-11-08 10:12:05 +01:00
|
|
|
if (window.getSelection) {
|
|
|
|
sel = window.getSelection();
|
|
|
|
range = document.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
|
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
2016-06-09 23:05:34 +02:00
|
|
|
} else if (document.body.createTextRange) {
|
2013-11-08 10:12:05 +01:00
|
|
|
range = document.body.createTextRange();
|
|
|
|
range.moveToElementText(element);
|
|
|
|
range.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:51:01 +01:00
|
|
|
function should_list_all_streams() {
|
2017-04-20 08:03:44 +02:00
|
|
|
return !page_params.realm_is_zephyr_mirror_realm;
|
2013-01-23 17:51:01 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 23:45:03 +02:00
|
|
|
// this finds the stream that is actively open in the settings and focused in
|
|
|
|
// the left side.
|
|
|
|
exports.active_stream = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash_components = window.location.hash.substr(1).split(/\//);
|
2017-10-11 23:45:03 +02:00
|
|
|
|
|
|
|
// if the string casted to a number is valid, and another component
|
|
|
|
// after exists then it's a stream name/id pair.
|
|
|
|
if (typeof parseFloat(hash_components[1]) === "number" && hash_components[2]) {
|
|
|
|
return {
|
|
|
|
id: parseFloat(hash_components[1]),
|
|
|
|
name: hash_components[2],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-04 20:27:37 +01:00
|
|
|
exports.toggle_home = function (sub) {
|
2019-05-21 10:41:53 +02:00
|
|
|
stream_muting.update_is_muted(sub, !sub.is_muted);
|
2019-05-15 08:54:25 +02:00
|
|
|
stream_edit.set_stream_property(sub, 'is_muted', sub.is_muted);
|
2013-05-14 21:00:28 +02:00
|
|
|
};
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2017-03-04 20:32:30 +01:00
|
|
|
exports.toggle_pin_to_top_stream = function (sub) {
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_edit.set_stream_property(sub, 'pin_to_top', !sub.pin_to_top);
|
2016-07-01 07:26:09 +02:00
|
|
|
};
|
|
|
|
|
2019-02-13 13:22:22 +01:00
|
|
|
exports.maybe_update_realm_default_stream_name = function (stream_id, new_name) {
|
2020-02-08 05:32:25 +01:00
|
|
|
const idx = page_params.realm_default_streams.findIndex(
|
|
|
|
stream => stream.stream_id === stream_id
|
|
|
|
);
|
2019-02-13 13:22:22 +01:00
|
|
|
if (idx === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
page_params.realm_default_streams[idx].name = new_name;
|
|
|
|
};
|
|
|
|
|
2019-04-01 10:59:02 +02:00
|
|
|
exports.is_subscribed_stream_tab_active = function () {
|
|
|
|
// Returns true if "Subscribed" tab in stream settings is open
|
|
|
|
// otherwise false.
|
|
|
|
if ($("#subscriptions_table .search-container .tab-switcher .first").hasClass("selected")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-03-19 16:03:07 +01:00
|
|
|
exports.update_stream_name = function (sub, new_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const old_name = sub.name;
|
2019-07-19 20:12:06 +02:00
|
|
|
|
2013-08-21 23:21:31 +02:00
|
|
|
// Rename the stream internally.
|
2017-03-05 00:35:45 +01:00
|
|
|
stream_data.rename_sub(sub, new_name);
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = sub.stream_id;
|
2013-08-21 23:21:31 +02:00
|
|
|
|
2013-10-21 22:26:19 +02:00
|
|
|
// Update the left sidebar.
|
2016-10-30 17:33:23 +01:00
|
|
|
stream_list.rename_stream(sub, new_name);
|
2013-08-21 23:21:31 +02:00
|
|
|
|
2019-02-13 13:22:22 +01:00
|
|
|
// Update the default streams page in organization settings.
|
|
|
|
exports.maybe_update_realm_default_stream_name(stream_id, new_name);
|
|
|
|
|
2016-11-05 00:11:45 +01:00
|
|
|
// Update the stream settings
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_edit.update_stream_name(sub, new_name);
|
2016-11-05 00:11:45 +01:00
|
|
|
|
2016-10-29 01:30:51 +02:00
|
|
|
// Update the subscriptions page
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_row = exports.row_for_stream_id(stream_id);
|
2016-11-05 00:10:01 +01:00
|
|
|
sub_row.find(".stream-name").text(new_name);
|
2016-10-29 01:30:51 +02:00
|
|
|
|
2013-08-21 23:21:31 +02:00
|
|
|
// Update the message feed.
|
2017-01-05 17:34:27 +01:00
|
|
|
message_live_update.update_stream_name(stream_id, new_name);
|
2019-07-19 19:42:10 +02:00
|
|
|
|
|
|
|
// Clear rendered typeahead cache
|
|
|
|
typeahead_helper.clear_rendered_stream(stream_id);
|
2019-07-19 20:12:06 +02:00
|
|
|
|
|
|
|
// Update compose_state if needed
|
|
|
|
if (compose_state.stream_name() === old_name) {
|
|
|
|
compose_state.stream_name(new_name);
|
|
|
|
}
|
2017-03-19 16:03:07 +01:00
|
|
|
};
|
2013-08-21 23:21:31 +02:00
|
|
|
|
2019-02-05 16:39:46 +01:00
|
|
|
exports.update_stream_description = function (sub, description, rendered_description) {
|
2014-01-24 18:19:27 +01:00
|
|
|
sub.description = description;
|
2019-02-05 16:39:46 +01:00
|
|
|
sub.rendered_description = rendered_description.replace('<p>', '').replace('</p>', '');
|
2014-01-24 18:19:27 +01:00
|
|
|
|
2016-11-04 22:11:23 +01:00
|
|
|
// Update stream row
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_row = exports.row_for_stream_id(sub.stream_id);
|
2017-03-18 19:08:36 +01:00
|
|
|
sub_row.find(".description").html(sub.rendered_description);
|
2016-11-04 22:11:23 +01:00
|
|
|
|
|
|
|
// Update stream settings
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_edit.update_stream_description(sub);
|
2017-03-21 07:19:14 +01:00
|
|
|
};
|
2014-01-24 18:19:27 +01:00
|
|
|
|
2019-05-07 07:12:14 +02:00
|
|
|
exports.update_stream_privacy = function (sub, values) {
|
|
|
|
stream_data.update_stream_privacy(sub, values);
|
|
|
|
stream_data.update_calculated_fields(sub);
|
|
|
|
|
|
|
|
// Update UI elements
|
|
|
|
stream_ui_updates.update_stream_privacy_type_icon(sub);
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_ui_updates.update_stream_subscription_type_text(sub);
|
2019-05-07 07:12:14 +02:00
|
|
|
stream_ui_updates.update_change_stream_privacy_settings(sub);
|
2019-05-07 07:13:17 +02:00
|
|
|
stream_ui_updates.update_settings_button_for_sub(sub);
|
|
|
|
stream_ui_updates.update_subscribers_count(sub);
|
|
|
|
stream_ui_updates.update_add_subscriptions_elements(sub);
|
2019-05-07 07:12:14 +02:00
|
|
|
stream_list.redraw_stream_privacy(sub);
|
|
|
|
};
|
|
|
|
|
2020-02-04 21:50:55 +01:00
|
|
|
exports.update_stream_post_policy = function (sub, new_value) {
|
|
|
|
stream_data.update_stream_post_policy(sub, new_value);
|
2019-05-07 07:12:14 +02:00
|
|
|
stream_data.update_calculated_fields(sub);
|
|
|
|
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_ui_updates.update_stream_subscription_type_text(sub);
|
2019-05-07 07:12:14 +02:00
|
|
|
};
|
|
|
|
|
2016-11-05 00:24:55 +01:00
|
|
|
exports.set_color = function (stream_id, color) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_edit.set_stream_property(sub, 'color', color);
|
2013-05-17 21:35:17 +02:00
|
|
|
};
|
|
|
|
|
2018-04-06 15:32:10 +02:00
|
|
|
exports.rerender_subscriptions_settings = function (sub) {
|
2018-04-10 15:49:03 +02:00
|
|
|
// This rerendes the subscriber data for a given sub object
|
|
|
|
// where it might have already been rendered in the subscriptions UI.
|
2018-04-06 15:32:10 +02:00
|
|
|
if (typeof sub === "undefined") {
|
|
|
|
blueslip.error('Undefined sub passed to function rerender_subscriptions_settings');
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 07:24:07 +02:00
|
|
|
stream_data.update_subscribers_count(sub);
|
|
|
|
stream_ui_updates.update_subscribers_count(sub);
|
2019-05-03 07:26:56 +02:00
|
|
|
stream_ui_updates.update_subscribers_list(sub);
|
2018-04-06 15:32:10 +02:00
|
|
|
};
|
|
|
|
|
2018-02-16 07:19:18 +01:00
|
|
|
exports.add_sub_to_table = function (sub) {
|
2018-04-08 07:06:42 +02:00
|
|
|
if (exports.is_sub_already_present(sub)) {
|
2018-04-10 15:49:03 +02:00
|
|
|
// If a stream is already listed/added in subscription modal,
|
2019-04-01 11:21:29 +02:00
|
|
|
// display stream in `Subscribed` tab and return.
|
|
|
|
// This can happen in some corner cases (which might
|
2018-04-10 15:49:03 +02:00
|
|
|
// be backend bugs) where a realm adminsitrator is subscribed
|
|
|
|
// to a private stream, in which case they might get two
|
|
|
|
// stream-create events.
|
2019-04-01 11:21:29 +02:00
|
|
|
stream_ui_updates.update_stream_row_in_settings_tab(sub);
|
2018-04-08 07:06:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_subscription(sub);
|
|
|
|
const settings_html = render_subscription_settings(sub);
|
2017-04-22 22:22:25 +02:00
|
|
|
if (stream_create.get_name() === sub.name) {
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_content_element($(".streams-list")).prepend(html);
|
|
|
|
ui.reset_scrollbar($(".streams-list"));
|
2017-02-18 00:03:43 +01:00
|
|
|
} else {
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_content_element($(".streams-list")).append(html);
|
2017-02-18 00:03:43 +01:00
|
|
|
}
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_content_element($(".subscriptions .settings")).append($(settings_html));
|
2016-11-01 22:32:10 +01:00
|
|
|
|
2017-04-22 22:22:25 +02:00
|
|
|
if (stream_create.get_name() === sub.name) {
|
2018-03-10 18:39:49 +01:00
|
|
|
// This `stream_create.get_name()` check tells us whether the
|
|
|
|
// stream was just created in this browser window; it's a hack
|
|
|
|
// to work around the server_events code flow not having a
|
|
|
|
// good way to associate with this request because the stream
|
|
|
|
// ID isn't known yet. These are appended to the top of the
|
|
|
|
// list, so they are more visible.
|
2019-04-01 11:04:41 +02:00
|
|
|
exports.row_for_stream_id(sub.stream_id).click();
|
2017-04-22 22:22:25 +02:00
|
|
|
stream_create.reset_created_stream();
|
2016-11-01 22:32:10 +01:00
|
|
|
}
|
2018-02-16 07:19:18 +01:00
|
|
|
};
|
2013-01-23 19:43:11 +01:00
|
|
|
|
2018-04-08 07:06:42 +02:00
|
|
|
exports.is_sub_already_present = function (sub) {
|
2018-04-10 15:49:03 +02:00
|
|
|
// This checks if a stream is already listed the "Manage streams"
|
|
|
|
// UI, by checking for its subscribe/unsubscribe checkmark button.
|
2019-11-02 00:06:25 +01:00
|
|
|
const button = exports.check_button_for_sub(sub);
|
2018-04-08 07:06:42 +02:00
|
|
|
if (button.length !== 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2017-02-15 17:38:44 +01:00
|
|
|
exports.remove_stream = function (stream_id) {
|
|
|
|
// It is possible that row is empty when we deactivate a
|
|
|
|
// stream, but we let jQuery silently handle that.
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = exports.row_for_stream_id(stream_id);
|
2017-02-15 17:38:44 +01:00
|
|
|
row.remove();
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2018-08-07 15:41:00 +02:00
|
|
|
if (stream_edit.is_sub_settings_active(sub)) {
|
|
|
|
exports.show_subs_pane.nothing_selected();
|
|
|
|
}
|
2017-02-15 17:38:44 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 16:47:30 +01:00
|
|
|
exports.update_settings_for_subscribed = function (sub) {
|
2019-05-03 07:59:31 +02:00
|
|
|
stream_ui_updates.update_add_subscriptions_elements(sub);
|
2018-04-30 15:03:23 +02:00
|
|
|
$(".subscription_settings[data-stream-id='" + sub.stream_id + "'] #preview-stream-button").show();
|
2017-01-20 22:03:52 +01:00
|
|
|
|
2019-03-26 05:38:37 +01:00
|
|
|
if (exports.is_sub_already_present(sub)) {
|
2019-05-03 07:24:07 +02:00
|
|
|
stream_data.update_subscribers_count(sub);
|
2019-04-01 11:21:29 +02:00
|
|
|
stream_ui_updates.update_stream_row_in_settings_tab(sub);
|
2019-05-03 07:24:07 +02:00
|
|
|
stream_ui_updates.update_subscribers_count(sub, true);
|
2019-03-26 05:38:37 +01:00
|
|
|
stream_ui_updates.update_check_button_for_sub(sub);
|
2019-03-31 13:00:32 +02:00
|
|
|
stream_ui_updates.update_settings_button_for_sub(sub);
|
2019-04-02 18:43:02 +02:00
|
|
|
stream_ui_updates.update_change_stream_privacy_settings(sub);
|
2013-01-22 23:16:04 +01:00
|
|
|
} else {
|
2018-02-16 07:19:18 +01:00
|
|
|
exports.add_sub_to_table(sub);
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
2013-03-21 22:13:17 +01:00
|
|
|
|
2019-05-03 07:26:56 +02:00
|
|
|
stream_ui_updates.update_subscribers_list(sub);
|
2017-10-11 23:45:03 +02:00
|
|
|
|
2017-03-17 23:07:20 +01:00
|
|
|
// Display the swatch and subscription stream_settings
|
2019-04-02 18:37:24 +02:00
|
|
|
stream_ui_updates.update_regular_sub_settings(sub);
|
2014-02-05 22:53:53 +01:00
|
|
|
};
|
2012-10-18 21:37:07 +02:00
|
|
|
|
2018-11-29 22:22:21 +01:00
|
|
|
exports.show_active_stream_in_left_panel = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_row = get_hash_safe().split(/\//)[1];
|
2018-11-29 22:22:21 +01:00
|
|
|
|
|
|
|
if (parseFloat(selected_row)) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_row = exports.row_for_stream_id(selected_row);
|
2018-11-29 22:22:21 +01:00
|
|
|
sub_row.addClass("active");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-29 22:42:09 +01:00
|
|
|
exports.add_tooltips_to_left_panel = function () {
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const row of $("#subscriptions_table .stream-row")) {
|
2018-11-29 22:42:09 +01:00
|
|
|
$(row).find('.sub-info-box [class$="-bar"] [class$="-count"]').tooltip({
|
|
|
|
placement: 'left', animation: false,
|
|
|
|
});
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-11-29 22:42:09 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 16:47:30 +01:00
|
|
|
exports.update_settings_for_unsubscribed = function (sub) {
|
2018-04-06 15:32:10 +02:00
|
|
|
exports.rerender_subscriptions_settings(sub);
|
2019-03-26 05:38:37 +01:00
|
|
|
stream_ui_updates.update_check_button_for_sub(sub);
|
2019-03-31 13:00:32 +02:00
|
|
|
stream_ui_updates.update_settings_button_for_sub(sub);
|
2019-04-02 18:37:24 +02:00
|
|
|
stream_ui_updates.update_regular_sub_settings(sub);
|
2019-04-02 18:43:02 +02:00
|
|
|
stream_ui_updates.update_change_stream_privacy_settings(sub);
|
2017-04-24 04:11:25 +02:00
|
|
|
|
2018-04-07 06:05:52 +02:00
|
|
|
stream_data.update_stream_email_address(sub, "");
|
2019-05-03 07:59:31 +02:00
|
|
|
// If user unsubscribed from private stream then user cannot subscribe to
|
|
|
|
// stream without invitation and cannot add subscribers to stream.
|
|
|
|
if (!sub.should_display_subscription_button) {
|
|
|
|
stream_ui_updates.update_add_subscriptions_elements(sub);
|
2017-10-11 23:45:03 +02:00
|
|
|
}
|
2019-05-09 07:34:31 +02:00
|
|
|
if (page_params.is_guest) {
|
|
|
|
stream_edit.open_edit_panel_empty();
|
|
|
|
}
|
2017-10-11 23:45:03 +02:00
|
|
|
|
2018-02-09 19:45:40 +01:00
|
|
|
// Remove private streams from subscribed streams list.
|
2019-04-01 11:04:41 +02:00
|
|
|
stream_ui_updates.update_stream_row_in_settings_tab(sub);
|
2014-02-05 22:53:53 +01:00
|
|
|
};
|
2013-03-29 20:27:41 +01:00
|
|
|
|
2018-07-29 19:40:32 +02:00
|
|
|
function triage_stream(query, sub) {
|
2018-07-29 19:28:06 +02:00
|
|
|
if (query.subscribed_only) {
|
|
|
|
// reject non-subscribed streams
|
|
|
|
if (!sub.subscribed) {
|
2018-07-30 20:51:08 +02:00
|
|
|
return 'rejected';
|
2018-07-29 19:28:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_terms = search_util.get_search_terms(query.input);
|
2018-07-29 16:11:48 +02:00
|
|
|
|
2018-07-29 19:40:32 +02:00
|
|
|
function match(attr) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const val = sub[attr];
|
2018-07-29 19:40:32 +02:00
|
|
|
|
|
|
|
return search_util.vanilla_match({
|
|
|
|
val: val,
|
|
|
|
search_terms: search_terms,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match('name')) {
|
|
|
|
return 'name_match';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match('description')) {
|
|
|
|
return 'desc_match';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'rejected';
|
2016-12-26 10:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 20:20:46 +02:00
|
|
|
function get_stream_id_buckets(stream_ids, query) {
|
|
|
|
// When we simplify the settings UI, we can get
|
|
|
|
// rid of the "others" bucket.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const buckets = {
|
2018-07-29 20:20:46 +02:00
|
|
|
name: [],
|
|
|
|
desc: [],
|
|
|
|
other: [],
|
|
|
|
};
|
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const stream_id of stream_ids) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
|
|
|
const match_status = triage_stream(query, sub);
|
2018-07-29 20:20:46 +02:00
|
|
|
|
|
|
|
if (match_status === 'name_match') {
|
|
|
|
buckets.name.push(stream_id);
|
|
|
|
} else if (match_status === 'desc_match') {
|
|
|
|
buckets.desc.push(stream_id);
|
|
|
|
} else {
|
|
|
|
buckets.other.push(stream_id);
|
|
|
|
}
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-07-29 20:20:46 +02:00
|
|
|
|
|
|
|
stream_data.sort_for_stream_settings(buckets.name);
|
|
|
|
stream_data.sort_for_stream_settings(buckets.desc);
|
|
|
|
|
|
|
|
return buckets;
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:27:32 +02:00
|
|
|
exports.populate_stream_settings_left_panel = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub_rows = stream_data.get_updated_unsorted_subs();
|
2020-01-15 15:05:44 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const template_data = {
|
2018-07-24 14:27:32 +02:00
|
|
|
subscriptions: sub_rows,
|
|
|
|
};
|
2020-01-15 15:05:44 +01:00
|
|
|
|
|
|
|
const finish = blueslip.start_timing('render_subscriptions');
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_subscriptions(template_data);
|
2020-01-15 15:05:44 +01:00
|
|
|
finish();
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_content_element($('#subscriptions_table .streams-list')).html(html);
|
2018-07-24 14:27:32 +02:00
|
|
|
};
|
|
|
|
|
2016-12-26 10:14:18 +01:00
|
|
|
// query is now an object rather than a string.
|
|
|
|
// Query { input: String, subscribed_only: Boolean }
|
|
|
|
exports.filter_table = function (query) {
|
2018-11-29 22:22:21 +01:00
|
|
|
exports.show_active_stream_in_left_panel();
|
2017-10-11 23:00:36 +02:00
|
|
|
|
2019-12-30 12:51:16 +01:00
|
|
|
function stream_id_for_row(row) {
|
|
|
|
return parseInt($(row).attr('data-stream-id'), 10);
|
|
|
|
}
|
|
|
|
|
2020-02-12 07:49:46 +01:00
|
|
|
const widgets = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
const streams_list_scrolltop = ui.get_scroll_element($(".streams-list")).scrollTop();
|
2017-01-03 13:26:48 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_ids = [];
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const row of $("#subscriptions_table .stream-row")) {
|
2019-12-30 12:51:16 +01:00
|
|
|
const stream_id = stream_id_for_row(row);
|
2018-07-29 20:20:46 +02:00
|
|
|
stream_ids.push(stream_id);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2016-10-12 08:53:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const buckets = get_stream_id_buckets(stream_ids, query);
|
2018-07-29 19:40:32 +02:00
|
|
|
|
2018-07-29 20:20:46 +02:00
|
|
|
// If we just re-built the DOM from scratch we wouldn't need
|
|
|
|
// all this hidden/notdisplayed logic.
|
2020-02-12 07:50:20 +01:00
|
|
|
const hidden_ids = new Set();
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const stream_id of buckets.other) {
|
2020-02-12 07:50:20 +01:00
|
|
|
hidden_ids.add(stream_id);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-07-29 20:20:46 +02:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const row of $("#subscriptions_table .stream-row")) {
|
2019-12-30 12:51:16 +01:00
|
|
|
const stream_id = stream_id_for_row(row);
|
2018-07-29 20:20:46 +02:00
|
|
|
|
|
|
|
// Below code goes away if we don't do sort-DOM-in-place.
|
2020-02-12 07:50:20 +01:00
|
|
|
if (hidden_ids.has(stream_id)) {
|
2018-07-29 20:20:46 +02:00
|
|
|
$(row).addClass('notdisplayed');
|
2018-05-06 21:43:17 +02:00
|
|
|
} else {
|
2018-07-29 20:20:46 +02:00
|
|
|
$(row).removeClass('notdisplayed');
|
2016-10-12 08:53:57 +02:00
|
|
|
}
|
2018-03-15 05:24:00 +01:00
|
|
|
|
2020-02-12 07:49:46 +01:00
|
|
|
widgets.set(stream_id, $(row).detach());
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2016-11-01 22:32:10 +01:00
|
|
|
|
2018-11-29 22:42:09 +01:00
|
|
|
exports.add_tooltips_to_left_panel();
|
|
|
|
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar($("#subscription_overlay .streams-list"));
|
2017-07-28 13:26:19 +02:00
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
const all_stream_ids = [
|
|
|
|
...buckets.name,
|
|
|
|
...buckets.desc,
|
|
|
|
...buckets.other,
|
|
|
|
];
|
2017-01-03 13:26:48 +01:00
|
|
|
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const stream_id of all_stream_ids) {
|
2020-02-12 07:49:46 +01:00
|
|
|
ui.get_content_element($('#subscriptions_table .streams-list')).append(widgets.get(stream_id));
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2017-01-03 13:26:48 +01:00
|
|
|
|
2018-11-29 23:21:06 +01:00
|
|
|
exports.maybe_reset_right_panel();
|
2017-09-20 20:37:26 +02:00
|
|
|
|
|
|
|
// this puts the scrollTop back to what it was before the list was updated again.
|
2019-03-01 01:40:05 +01:00
|
|
|
ui.get_scroll_element($(".streams-list")).scrollTop(streams_list_scrolltop);
|
2016-10-12 08:53:57 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let subscribed_only = true;
|
2018-04-04 21:29:32 +02:00
|
|
|
|
2018-11-29 23:11:07 +01:00
|
|
|
exports.get_search_params = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_box = $("#add_new_subscription input[type='text']");
|
|
|
|
const input = search_box.expectOne().val().trim();
|
|
|
|
const params = {
|
2018-11-29 23:11:07 +01:00
|
|
|
input: input,
|
|
|
|
subscribed_only: subscribed_only,
|
|
|
|
};
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
2018-11-29 23:21:06 +01:00
|
|
|
exports.maybe_reset_right_panel = function () {
|
|
|
|
if ($(".stream-row.active").hasClass("notdisplayed")) {
|
|
|
|
$(".right .settings").hide();
|
|
|
|
$(".nothing-selected").show();
|
|
|
|
$(".stream-row.active").removeClass("active");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-29 23:11:07 +01:00
|
|
|
exports.actually_filter_streams = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const search_params = exports.get_search_params();
|
2018-11-29 23:11:07 +01:00
|
|
|
exports.filter_table(search_params);
|
2017-10-03 00:41:13 +02:00
|
|
|
};
|
2016-10-12 08:53:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const filter_streams = _.throttle(exports.actually_filter_streams, 50);
|
2016-10-12 08:53:57 +02:00
|
|
|
|
2018-04-14 14:24:10 +02:00
|
|
|
// Make it explicit that our toggler is not created right away.
|
|
|
|
exports.toggler = undefined;
|
|
|
|
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.switch_stream_tab = function (tab_name) {
|
|
|
|
/*
|
|
|
|
This switches the stream tab, but it doesn't update
|
|
|
|
the toggler widget. You may instead want to
|
|
|
|
use `toggler.goto`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tab_name === "all-streams") {
|
|
|
|
subscribed_only = false;
|
|
|
|
} else if (tab_name === "subscribed") {
|
|
|
|
subscribed_only = true;
|
2018-04-14 14:24:10 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.actually_filter_streams();
|
2019-04-06 12:55:16 +02:00
|
|
|
stream_edit.setup_subscriptions_tab_hash(tab_name);
|
2018-12-02 01:09:10 +01:00
|
|
|
};
|
2018-04-14 14:24:10 +02:00
|
|
|
|
2016-11-01 22:32:10 +01:00
|
|
|
exports.setup_page = function (callback) {
|
2018-04-14 15:04:35 +02:00
|
|
|
// We should strongly consider only setting up the page once,
|
|
|
|
// but I am writing these comments write before a big release,
|
|
|
|
// so it's too risky a change for now.
|
|
|
|
//
|
|
|
|
// The history behind setting up the page from scratch every
|
|
|
|
// time we go into "Manage Streams" is that we used to have
|
|
|
|
// some live-update issues, so being able to re-launch the
|
|
|
|
// streams page is kind of a workaround for those bugs, since
|
|
|
|
// we will re-populate the widget.
|
|
|
|
//
|
|
|
|
// For now, every time we go back into the widget we'll
|
|
|
|
// continue the strategy that we re-render everything from scratch.
|
|
|
|
// Also, we'll always go back to the "Subscribed" tab.
|
2016-11-01 22:32:10 +01:00
|
|
|
function initialize_components() {
|
2019-12-01 22:04:53 +01:00
|
|
|
// Reset our internal state to reflect that we're initially in
|
|
|
|
// the "Subscribed" tab if we're reopening "Manage streams".
|
|
|
|
subscribed_only = true;
|
2018-04-14 14:24:10 +02:00
|
|
|
exports.toggler = components.toggle({
|
2018-07-15 20:10:41 +02:00
|
|
|
child_wants_focus: true,
|
2016-11-01 22:32:10 +01:00
|
|
|
values: [
|
2017-08-01 20:14:38 +02:00
|
|
|
{ label: i18n.t("Subscribed"), key: "subscribed" },
|
|
|
|
{ label: i18n.t("All streams"), key: "all-streams" },
|
2016-11-01 22:32:10 +01:00
|
|
|
],
|
2017-08-01 20:14:38 +02:00
|
|
|
callback: function (value, key) {
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.switch_stream_tab(key);
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2018-04-14 14:24:10 +02:00
|
|
|
});
|
2016-11-01 22:32:10 +01:00
|
|
|
|
|
|
|
if (should_list_all_streams()) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const toggler_elem = exports.toggler.get();
|
2018-04-14 14:24:10 +02:00
|
|
|
$("#subscriptions_table .search-container").prepend(toggler_elem);
|
2016-11-01 22:32:10 +01:00
|
|
|
}
|
2019-05-08 09:12:02 +02:00
|
|
|
if (page_params.is_guest) {
|
|
|
|
exports.toggler.disable_tab('all-streams');
|
|
|
|
}
|
2016-11-01 22:32:10 +01:00
|
|
|
|
2017-01-15 20:21:33 +01:00
|
|
|
// show the "Stream settings" header by default.
|
2016-11-01 22:32:10 +01:00
|
|
|
$(".display-type #stream_settings_title").show();
|
|
|
|
}
|
2013-10-25 17:27:07 +02:00
|
|
|
|
2018-05-30 18:10:43 +02:00
|
|
|
function populate_and_fill() {
|
2013-01-23 17:41:04 +01:00
|
|
|
|
2013-07-26 20:43:14 +02:00
|
|
|
$('#subscriptions_table').empty();
|
2016-10-12 08:53:57 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const template_data = {
|
2014-01-15 23:30:36 +01:00
|
|
|
can_create_streams: page_params.can_create_streams,
|
2017-01-12 00:17:43 +01:00
|
|
|
hide_all_streams: !should_list_all_streams(),
|
2018-04-30 11:48:00 +02:00
|
|
|
max_name_length: page_params.stream_name_max_length,
|
|
|
|
max_description_length: page_params.stream_description_max_length,
|
2019-01-15 22:20:52 +01:00
|
|
|
is_admin: page_params.is_admin,
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy_values: stream_data.stream_post_policy_values,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.everyone.code,
|
2014-01-15 23:30:36 +01:00
|
|
|
};
|
2017-10-11 23:00:36 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const rendered = render_subscription_table_body(template_data);
|
2013-07-26 20:43:14 +02:00
|
|
|
$('#subscriptions_table').append(rendered);
|
2018-07-24 14:27:32 +02:00
|
|
|
|
|
|
|
exports.populate_stream_settings_left_panel();
|
2016-11-01 22:32:10 +01:00
|
|
|
initialize_components();
|
2017-10-03 00:41:13 +02:00
|
|
|
exports.actually_filter_streams();
|
2018-03-25 18:46:10 +02:00
|
|
|
stream_create.set_up_handlers();
|
2013-08-26 20:22:22 +02:00
|
|
|
|
2016-11-01 22:32:10 +01:00
|
|
|
$("#add_new_subscription input[type='text']").on("input", function () {
|
2016-12-15 07:36:30 +01:00
|
|
|
// Debounce filtering in case a user is typing quickly
|
|
|
|
filter_streams();
|
2016-11-01 22:32:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
|
|
|
|
2016-10-25 21:50:14 +02:00
|
|
|
populate_and_fill();
|
2013-04-04 22:30:28 +02:00
|
|
|
|
2016-10-25 21:50:14 +02:00
|
|
|
if (!should_list_all_streams()) {
|
2017-09-29 19:17:03 +02:00
|
|
|
$('.create_stream_button').val(i18n.t("Subscribe"));
|
2013-01-23 17:51:01 +01:00
|
|
|
}
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-03 22:24:17 +02:00
|
|
|
|
2018-12-02 00:16:08 +01:00
|
|
|
exports.switch_to_stream_row = function (stream_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_row = exports.row_for_stream_id(stream_id);
|
2018-12-02 00:16:08 +01:00
|
|
|
|
2019-04-06 13:30:12 +02:00
|
|
|
exports.get_active_data().row.removeClass("active");
|
2018-12-02 00:16:08 +01:00
|
|
|
stream_row.addClass("active");
|
|
|
|
|
|
|
|
scroll_util.scroll_element_into_container(stream_row, stream_row.parent());
|
|
|
|
|
|
|
|
// It's dubious that we need this timeout any more.
|
|
|
|
setTimeout(function () {
|
2019-04-06 13:30:12 +02:00
|
|
|
if (stream_id === exports.get_active_data().id) {
|
2018-12-02 00:16:08 +01:00
|
|
|
stream_row.click();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
2018-12-05 20:41:20 +01:00
|
|
|
exports.change_state = function (section) {
|
|
|
|
// if in #streams/new form.
|
|
|
|
if (section === "new") {
|
2019-05-10 07:28:20 +02:00
|
|
|
if (!page_params.is_guest) {
|
|
|
|
exports.do_open_create_stream();
|
|
|
|
} else {
|
|
|
|
exports.toggler.goto('subscribed');
|
|
|
|
}
|
2018-12-02 00:24:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-02-21 18:13:32 +01:00
|
|
|
|
2018-12-05 20:41:20 +01:00
|
|
|
if (section === "all") {
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.toggler.goto('all-streams');
|
2018-12-05 20:41:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (section === "subscribed") {
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.toggler.goto('subscribed');
|
2018-12-05 20:41:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the section is a valid number.
|
|
|
|
if (/\d+/.test(section)) {
|
2020-01-28 23:17:29 +01:00
|
|
|
const stream_id = parseInt(section, 10);
|
2019-05-10 07:28:20 +02:00
|
|
|
// Guest users can not access unsubscribed streams
|
|
|
|
// So redirect guest users to 'subscribed' tab
|
|
|
|
// for any unsubscribed stream settings hash
|
|
|
|
if (page_params.is_guest && !stream_data.id_is_subscribed(stream_id)) {
|
|
|
|
exports.toggler.goto('subscribed');
|
|
|
|
} else {
|
|
|
|
exports.switch_to_stream_row(stream_id);
|
|
|
|
}
|
2018-12-05 20:41:20 +01:00
|
|
|
return;
|
2018-12-02 00:24:45 +01:00
|
|
|
}
|
2018-12-05 20:41:20 +01:00
|
|
|
|
|
|
|
blueslip.warn('invalid section for streams: ' + section);
|
|
|
|
exports.toggler.goto('subscribed');
|
2018-12-02 00:24:45 +01:00
|
|
|
};
|
2017-02-21 18:13:32 +01:00
|
|
|
|
2018-12-05 20:41:20 +01:00
|
|
|
exports.launch = function (section) {
|
2016-11-01 22:32:10 +01:00
|
|
|
exports.setup_page(function () {
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.open_overlay({
|
2017-05-06 01:11:18 +02:00
|
|
|
name: 'subscriptions',
|
|
|
|
overlay: $("#subscription_overlay"),
|
|
|
|
on_close: exports.close,
|
|
|
|
});
|
2018-12-05 20:41:20 +01:00
|
|
|
exports.change_state(section);
|
2016-11-01 22:32:10 +01:00
|
|
|
});
|
2019-04-06 13:30:12 +02:00
|
|
|
if (!exports.get_active_data().id) {
|
2017-03-26 12:26:22 +02:00
|
|
|
$('#search_stream_name').focus();
|
|
|
|
}
|
2016-11-01 22:32:10 +01:00
|
|
|
};
|
|
|
|
|
2017-01-19 23:23:25 +01:00
|
|
|
exports.close = function () {
|
2017-05-27 18:55:41 +02:00
|
|
|
hashchange.exit_overlay();
|
2017-01-19 23:23:25 +01:00
|
|
|
};
|
|
|
|
|
2017-03-22 22:18:34 +01:00
|
|
|
exports.switch_rows = function (event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_data = exports.get_active_data();
|
|
|
|
let switch_row;
|
2017-04-05 10:49:19 +02:00
|
|
|
if (window.location.hash === '#streams/new') {
|
|
|
|
// Prevent switching stream rows when creating a new stream
|
|
|
|
return false;
|
|
|
|
} else if (!active_data.id || active_data.row.hasClass('notdisplayed')) {
|
2019-06-06 05:50:36 +02:00
|
|
|
switch_row = $('div.stream-row:not(.notdisplayed)').first();
|
2017-03-26 12:26:22 +02:00
|
|
|
if ($('#search_stream_name').is(":focus")) {
|
|
|
|
$('#search_stream_name').blur();
|
|
|
|
}
|
2017-05-25 23:53:19 +02:00
|
|
|
} else {
|
|
|
|
if (event === 'up_arrow') {
|
|
|
|
switch_row = active_data.row.prevAll().not('.notdisplayed').first();
|
|
|
|
} else if (event === 'down_arrow') {
|
|
|
|
switch_row = active_data.row.nextAll().not('.notdisplayed').first();
|
|
|
|
}
|
2017-03-26 12:26:22 +02:00
|
|
|
if ($('#search_stream_name').is(":focus")) {
|
|
|
|
// remove focus from Filter streams input instead of switching rows
|
|
|
|
// if Filter streams input is focused
|
|
|
|
return $('#search_stream_name').blur();
|
|
|
|
}
|
2017-03-22 02:49:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const row_data = get_row_data(switch_row);
|
2017-05-25 23:53:19 +02:00
|
|
|
if (row_data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = row_data.id;
|
2018-12-02 02:33:06 +01:00
|
|
|
exports.switch_to_stream_row(stream_id);
|
2017-03-23 06:15:47 +01:00
|
|
|
} else if (event === 'up_arrow' && !row_data) {
|
|
|
|
$('#search_stream_name').focus();
|
2017-03-22 02:49:52 +01:00
|
|
|
}
|
2017-04-05 10:49:19 +02:00
|
|
|
return true;
|
2017-03-22 02:49:52 +01:00
|
|
|
};
|
|
|
|
|
2017-03-23 03:06:38 +01:00
|
|
|
exports.keyboard_sub = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_data = exports.get_active_data();
|
|
|
|
const row_data = get_row_data(active_data.row);
|
2017-03-23 05:15:16 +01:00
|
|
|
if (row_data) {
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.sub_or_unsub(row_data.object);
|
2017-03-25 01:27:52 +01:00
|
|
|
if (row_data.object.subscribed && active_data.tab.text() === 'Subscribed') {
|
|
|
|
active_data.row.addClass('notdisplayed');
|
|
|
|
active_data.row.removeClass('active');
|
2017-03-23 05:15:16 +01:00
|
|
|
}
|
2017-03-23 03:06:38 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-23 06:02:01 +01:00
|
|
|
exports.toggle_view = function (event) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_data = exports.get_active_data();
|
2018-12-02 01:09:10 +01:00
|
|
|
|
2017-03-25 01:27:52 +01:00
|
|
|
if (event === 'right_arrow' && active_data.tab.text() === 'Subscribed') {
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.toggler.goto('all-streams');
|
2017-03-25 01:27:52 +01:00
|
|
|
} else if (event === 'left_arrow' && active_data.tab.text() === 'All streams') {
|
2018-12-02 01:09:10 +01:00
|
|
|
exports.toggler.goto('subscribed');
|
2017-03-23 06:02:01 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-23 06:41:31 +01:00
|
|
|
exports.view_stream = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const active_data = exports.get_active_data();
|
|
|
|
const row_data = get_row_data(active_data.row);
|
2017-03-23 06:41:31 +01:00
|
|
|
if (row_data) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_narrow_hash = '#narrow/stream/' + hash_util.encode_stream_name(row_data.object.name);
|
2018-12-02 17:12:47 +01:00
|
|
|
hashchange.go_to_location(stream_narrow_hash);
|
2017-03-23 06:41:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-10 15:03:15 +01:00
|
|
|
function ajaxSubscribe(stream, color) {
|
2013-01-31 21:09:34 +01:00
|
|
|
// Subscribe yourself to a single stream.
|
2019-11-02 00:06:25 +01:00
|
|
|
let true_stream_name;
|
2013-01-31 21:09:34 +01:00
|
|
|
|
2013-12-18 19:55:18 +01:00
|
|
|
return channel.post({
|
2015-11-30 21:39:40 +01:00
|
|
|
url: "/json/users/me/subscriptions",
|
2019-01-10 15:03:15 +01:00
|
|
|
data: {subscriptions: JSON.stringify([{name: stream, color: color}]) },
|
2016-12-02 14:06:06 +01:00
|
|
|
success: function (resp, statusText, xhr) {
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.streams_open()) {
|
2017-01-19 23:23:37 +01:00
|
|
|
$("#create_stream_name").val("");
|
|
|
|
}
|
2013-01-31 21:09:34 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = JSON.parse(xhr.responseText);
|
2013-01-31 21:09:34 +01:00
|
|
|
if (!$.isEmptyObject(res.already_subscribed)) {
|
|
|
|
// Display the canonical stream capitalization.
|
2017-01-20 23:11:08 +01:00
|
|
|
true_stream_name = res.already_subscribed[people.my_current_email()][0];
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.success(i18n.t("Already subscribed to __stream__", {stream: true_stream_name}),
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info"));
|
2012-11-07 23:25:04 +01:00
|
|
|
}
|
2013-03-29 20:49:05 +01:00
|
|
|
// The rest of the work is done via the subscribe event we will get
|
2012-11-07 23:25:04 +01:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.error(i18n.t("Error adding subscription"), xhr,
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info"));
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2012-11-07 23:25:04 +01:00
|
|
|
});
|
|
|
|
}
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2017-03-05 03:47:28 +01:00
|
|
|
function ajaxUnsubscribe(sub) {
|
|
|
|
// TODO: use stream_id when backend supports it
|
2016-12-23 02:37:10 +01:00
|
|
|
return channel.del({
|
|
|
|
url: "/json/users/me/subscriptions",
|
2017-03-05 03:47:28 +01:00
|
|
|
data: {subscriptions: JSON.stringify([sub.name]) },
|
2016-12-02 14:06:06 +01:00
|
|
|
success: function () {
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info").hide();
|
2013-03-29 20:49:05 +01:00
|
|
|
// The rest of the work is done via the unsubscribe event we will get
|
2012-10-03 22:24:17 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.error(i18n.t("Error removing subscription"), xhr,
|
2018-01-03 14:24:49 +01:00
|
|
|
$(".stream_change_property_info"));
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2012-10-03 22:24:17 +02:00
|
|
|
});
|
2013-01-10 22:15:55 +01:00
|
|
|
}
|
2012-11-16 20:11:08 +01:00
|
|
|
|
2018-12-01 21:18:20 +01:00
|
|
|
exports.do_open_create_stream = function () {
|
|
|
|
// Only call this directly for hash changes.
|
|
|
|
// Prefer open_create_stream().
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream = $.trim($("#search_stream_name").val());
|
2013-01-31 21:09:34 +01:00
|
|
|
|
2017-04-22 22:22:25 +02:00
|
|
|
if (!should_list_all_streams()) {
|
|
|
|
// Realms that don't allow listing streams should simply be subscribed to.
|
|
|
|
stream_create.set_name(stream);
|
|
|
|
ajaxSubscribe($("#search_stream_name").val());
|
|
|
|
return;
|
2013-08-27 19:14:18 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 22:22:25 +02:00
|
|
|
stream_create.new_stream_clicked(stream);
|
|
|
|
};
|
2013-01-31 21:09:34 +01:00
|
|
|
|
2018-12-01 21:18:20 +01:00
|
|
|
exports.open_create_stream = function () {
|
|
|
|
exports.do_open_create_stream();
|
2018-12-01 20:15:50 +01:00
|
|
|
hashchange.update_browser_history('#streams/new');
|
2018-12-01 21:18:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-04 18:10:23 +01:00
|
|
|
exports.sub_or_unsub = function (sub) {
|
2017-03-04 17:55:16 +01:00
|
|
|
if (sub.subscribed) {
|
2017-03-05 03:47:28 +01:00
|
|
|
ajaxUnsubscribe(sub);
|
2017-03-04 17:55:16 +01:00
|
|
|
} else {
|
2019-01-10 15:03:15 +01:00
|
|
|
ajaxSubscribe(sub.name, sub.color);
|
2017-03-04 17:55:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2017-09-29 19:17:03 +02:00
|
|
|
$("#subscriptions_table").on("click", ".create_stream_button", function (e) {
|
2012-10-31 18:36:08 +01:00
|
|
|
e.preventDefault();
|
2018-12-01 21:18:20 +01:00
|
|
|
exports.open_create_stream();
|
2013-01-31 21:09:34 +01:00
|
|
|
});
|
|
|
|
|
2016-11-01 22:32:10 +01:00
|
|
|
$(".subscriptions").on("click", "[data-dismiss]", function (e) {
|
|
|
|
e.preventDefault();
|
2016-12-13 21:58:11 +01:00
|
|
|
// we want to make sure that the click is not just a simulated
|
|
|
|
// click; this fixes an issue where hitting "enter" would
|
|
|
|
// trigger this code path due to bootstrap magic.
|
|
|
|
if (e.clientY !== 0) {
|
2017-04-24 04:11:25 +02:00
|
|
|
exports.show_subs_pane.nothing_selected();
|
2016-12-13 21:58:11 +01:00
|
|
|
}
|
2016-11-01 22:32:10 +01:00
|
|
|
});
|
2013-08-27 19:14:18 +02:00
|
|
|
|
2013-11-08 10:12:05 +01:00
|
|
|
$("body").on("mouseover", ".subscribed-button", function (e) {
|
2016-06-13 08:40:35 +02:00
|
|
|
$(e.target).addClass("btn-danger").text(i18n.t("Unsubscribe"));
|
2013-11-08 10:12:05 +01:00
|
|
|
}).on("mouseout", ".subscribed-button", function (e) {
|
2016-06-13 08:40:35 +02:00
|
|
|
$(e.target).removeClass("btn-danger").text(i18n.t("Subscribed"));
|
2013-11-08 10:12:05 +01:00
|
|
|
});
|
|
|
|
|
2016-12-02 14:06:06 +01:00
|
|
|
$("#subscriptions_table").on("click", ".email-address", function () {
|
2013-11-08 10:12:05 +01:00
|
|
|
selectText(this);
|
|
|
|
});
|
|
|
|
|
2016-03-22 16:50:09 +01:00
|
|
|
$('.empty_feed_sub_unsub').click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
$('#subscription-status').hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_name = narrow_state.stream();
|
2016-03-22 16:50:09 +01:00
|
|
|
if (stream_name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub(stream_name);
|
2017-03-04 18:40:46 +01:00
|
|
|
exports.sub_or_unsub(sub);
|
2016-03-22 16:50:09 +01:00
|
|
|
|
|
|
|
$('.empty_feed_notice').hide();
|
|
|
|
$('#empty_narrow_message').show();
|
|
|
|
});
|
|
|
|
|
2017-09-29 19:17:03 +02:00
|
|
|
$("#subscriptions_table").on("click", ".stream-row, .create_stream_button", function () {
|
2017-02-09 00:07:52 +01:00
|
|
|
$(".right").addClass("show");
|
|
|
|
$(".subscriptions-header").addClass("slide-left");
|
|
|
|
});
|
|
|
|
|
2017-06-12 22:44:09 +02:00
|
|
|
$("#subscriptions_table").on("click", ".fa-chevron-left", function () {
|
2017-02-09 00:07:52 +01:00
|
|
|
$(".right").removeClass("show");
|
|
|
|
$(".subscriptions-header").removeClass("slide-left");
|
|
|
|
});
|
|
|
|
|
2016-11-01 22:32:10 +01:00
|
|
|
(function defocus_sub_settings() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sel = ".search-container, .streams-list, .subscriptions-header";
|
2016-11-01 22:32:10 +01:00
|
|
|
|
|
|
|
$("#subscriptions_table").on("click", sel, function (e) {
|
|
|
|
if ($(e.target).is(sel)) {
|
2018-12-01 21:53:09 +01:00
|
|
|
stream_edit.open_edit_panel_empty();
|
2016-11-01 22:32:10 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2012-10-18 21:37:07 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.subs = exports;
|