2020-08-11 01:47:54 +02:00
|
|
|
# UI: input pills
|
2017-10-24 20:15:35 +02:00
|
|
|
|
|
|
|
This is a high level and API explanation of the input pill interface in the
|
|
|
|
frontend of the Zulip web application.
|
|
|
|
|
2017-11-29 06:45:11 +01:00
|
|
|
## Setup
|
2017-10-24 20:15:35 +02:00
|
|
|
|
|
|
|
A pill container should have the following markup:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<div class="pill-container">
|
|
|
|
<div class="input" contenteditable="true"></div>
|
|
|
|
</div>
|
|
|
|
```
|
|
|
|
|
|
|
|
The pills will automatically be inserted in before the ".input" in order.
|
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
## Basic usage
|
2017-10-24 20:15:35 +02:00
|
|
|
|
|
|
|
```js
|
2022-01-25 11:36:19 +01:00
|
|
|
var $pill_containter = $("#input_container");
|
2018-03-06 15:03:20 +01:00
|
|
|
var pills = input_pill.create({
|
2022-01-25 11:36:19 +01:00
|
|
|
$container: $pill_container,
|
2018-03-06 15:03:20 +01:00
|
|
|
create_item_from_text: user_pill.create_item_from_email,
|
|
|
|
get_text_from_item: user_pill.get_email_from_item,
|
2017-10-24 20:15:35 +02:00
|
|
|
});
|
|
|
|
```
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
You can look at `static/js/user_pill.js` to see how the above
|
2021-08-20 21:53:28 +02:00
|
|
|
methods are implemented. Essentially you just need to convert
|
2018-03-06 15:03:20 +01:00
|
|
|
from raw data (like an email) to structured data (like an object
|
|
|
|
with display_value, email, and user_id for a user), and vice
|
2021-08-20 21:53:28 +02:00
|
|
|
versa. The most important field to supply is `display_value`.
|
2018-03-06 15:03:20 +01:00
|
|
|
For user pills `pill_item.display_value === user.full_name`.
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
## Typeahead
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
Pills almost always work in conjunction with typeahead, and
|
|
|
|
you will want to provide a `source` function to typeahead
|
2021-08-20 21:53:28 +02:00
|
|
|
that can exclude items from the prior pills. Here is an
|
2018-03-06 15:03:20 +01:00
|
|
|
example snippet from our user group settings code.
|
2017-10-25 01:05:47 +02:00
|
|
|
|
|
|
|
```js
|
2018-03-06 15:03:20 +01:00
|
|
|
source: function () {
|
|
|
|
return user_pill.typeahead_source(pills);
|
|
|
|
},
|
|
|
|
```
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
And then in `user_pill.js`...
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
```js
|
2021-05-03 12:48:51 +02:00
|
|
|
export function typeahead_source(pill_widget) {
|
2020-03-21 19:53:16 +01:00
|
|
|
const persons = people.get_realm_users();
|
2021-05-03 12:48:51 +02:00
|
|
|
return filter_taken_users(persons, pill_widget);
|
|
|
|
}
|
2020-01-08 15:51:04 +01:00
|
|
|
|
2021-05-03 12:48:51 +02:00
|
|
|
export function filter_taken_users(items, pill_widget) {
|
|
|
|
const taken_user_ids = get_user_ids(pill_widget);
|
|
|
|
items = items.filter((item) => !taken_user_ids.includes(item.user_id));
|
2018-03-06 15:03:20 +01:00
|
|
|
return items;
|
2021-05-03 12:48:51 +02:00
|
|
|
}
|
2017-10-25 01:05:47 +02:00
|
|
|
```
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
### `onPillCreate` and `onPillRemove` methods
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
You can get notifications from the pill code that pills have been
|
|
|
|
created/remove.
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
```js
|
|
|
|
pills.onPillCreate(function () {
|
|
|
|
update_save_state();
|
|
|
|
});
|
2017-10-25 01:05:47 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
pills.onPillRemove(function () {
|
|
|
|
update_save_state();
|
2017-10-25 01:05:47 +02:00
|
|
|
});
|
|
|
|
```
|