mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 17:36:27 +00:00
stream_data: Extract function to get stream options for dropdown.
To be used by various dropdown widgets in the app.
This commit is contained in:
@@ -246,22 +246,7 @@ function item_click_callback(event, dropdown) {
|
||||
}
|
||||
|
||||
function get_options_for_recipient_widget() {
|
||||
const options = stream_data
|
||||
.subscribed_subs()
|
||||
.map((stream) => ({
|
||||
name: stream.name,
|
||||
unique_id: stream.stream_id,
|
||||
stream,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
if (a.name.toLowerCase() < b.name.toLowerCase()) {
|
||||
return -1;
|
||||
}
|
||||
if (a.name.toLowerCase() > b.name.toLowerCase()) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
const options = stream_data.get_options_for_dropdown_widget();
|
||||
|
||||
const direct_messages_option = {
|
||||
is_direct_message: true,
|
||||
|
||||
@@ -849,3 +849,25 @@ export function initialize(params) {
|
||||
export function remove_default_stream(stream_id) {
|
||||
default_stream_ids.delete(stream_id);
|
||||
}
|
||||
|
||||
export function get_options_for_dropdown_widget() {
|
||||
return (
|
||||
subscribed_subs()
|
||||
.map((stream) => ({
|
||||
name: stream.name,
|
||||
unique_id: stream.stream_id,
|
||||
stream,
|
||||
}))
|
||||
// eslint-disable-next-line consistent-return, array-callback-return
|
||||
.sort((a, b) => {
|
||||
if (a.name.toLowerCase() < b.name.toLowerCase()) {
|
||||
return -1;
|
||||
}
|
||||
if (a.name.toLowerCase() > b.name.toLowerCase()) {
|
||||
return 1;
|
||||
}
|
||||
// Not possible since two streams cannot have same name.
|
||||
// return 0;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -169,6 +169,35 @@ test("basics", () => {
|
||||
assert.equal(sub_store.get(-3), undefined);
|
||||
assert.equal(sub_store.get(undefined), undefined);
|
||||
assert.equal(sub_store.get(1), denmark);
|
||||
|
||||
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
|
||||
{
|
||||
name: "social",
|
||||
stream: {
|
||||
color: "red",
|
||||
history_public_to_subscribers: false,
|
||||
invite_only: true,
|
||||
is_muted: false,
|
||||
name: "social",
|
||||
stream_id: 2,
|
||||
stream_post_policy: 2,
|
||||
subscribed: true,
|
||||
},
|
||||
unique_id: 2,
|
||||
},
|
||||
{
|
||||
name: "test",
|
||||
stream: {
|
||||
color: "yellow",
|
||||
invite_only: false,
|
||||
is_muted: true,
|
||||
name: "test",
|
||||
stream_id: 3,
|
||||
subscribed: true,
|
||||
},
|
||||
unique_id: 3,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("get_subscribed_streams_for_user", () => {
|
||||
@@ -981,3 +1010,103 @@ test("can_unsubscribe_others", () => {
|
||||
page_params.is_admin = false;
|
||||
assert.equal(stream_data.can_unsubscribe_others(sub), false);
|
||||
});
|
||||
|
||||
test("options for dropdown widget", () => {
|
||||
const denmark = {
|
||||
subscribed: true,
|
||||
color: "blue",
|
||||
name: "Denmark",
|
||||
stream_id: 1,
|
||||
is_muted: true,
|
||||
invite_only: true,
|
||||
history_public_to_subscribers: true,
|
||||
};
|
||||
const social = {
|
||||
subscribed: true,
|
||||
color: "red",
|
||||
name: "social",
|
||||
stream_id: 2,
|
||||
is_muted: false,
|
||||
invite_only: true,
|
||||
history_public_to_subscribers: false,
|
||||
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
|
||||
};
|
||||
const test = {
|
||||
subscribed: true,
|
||||
color: "yellow",
|
||||
name: "test",
|
||||
stream_id: 3,
|
||||
is_muted: true,
|
||||
invite_only: false,
|
||||
};
|
||||
const web_public_stream = {
|
||||
subscribed: true,
|
||||
color: "yellow",
|
||||
name: "web_public_stream",
|
||||
stream_id: 4,
|
||||
is_muted: false,
|
||||
invite_only: false,
|
||||
history_public_to_subscribers: true,
|
||||
is_web_public: true,
|
||||
};
|
||||
stream_data.add_sub(denmark);
|
||||
stream_data.add_sub(social);
|
||||
stream_data.add_sub(web_public_stream);
|
||||
stream_data.add_sub(test);
|
||||
|
||||
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
|
||||
{
|
||||
name: "Denmark",
|
||||
stream: {
|
||||
subscribed: true,
|
||||
color: "blue",
|
||||
name: "Denmark",
|
||||
stream_id: 1,
|
||||
is_muted: true,
|
||||
invite_only: true,
|
||||
history_public_to_subscribers: true,
|
||||
},
|
||||
unique_id: 1,
|
||||
},
|
||||
{
|
||||
name: "social",
|
||||
stream: {
|
||||
color: "red",
|
||||
history_public_to_subscribers: false,
|
||||
invite_only: true,
|
||||
is_muted: false,
|
||||
name: "social",
|
||||
stream_id: 2,
|
||||
stream_post_policy: 2,
|
||||
subscribed: true,
|
||||
},
|
||||
unique_id: 2,
|
||||
},
|
||||
{
|
||||
name: "test",
|
||||
stream: {
|
||||
color: "yellow",
|
||||
invite_only: false,
|
||||
is_muted: true,
|
||||
name: "test",
|
||||
stream_id: 3,
|
||||
subscribed: true,
|
||||
},
|
||||
unique_id: 3,
|
||||
},
|
||||
{
|
||||
name: "web_public_stream",
|
||||
stream: {
|
||||
subscribed: true,
|
||||
color: "yellow",
|
||||
name: "web_public_stream",
|
||||
stream_id: 4,
|
||||
is_muted: false,
|
||||
invite_only: false,
|
||||
history_public_to_subscribers: true,
|
||||
is_web_public: true,
|
||||
},
|
||||
unique_id: 4,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user