forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_cursor.js
144 lines (129 loc) · 3.74 KB
/
list_cursor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"use strict";
class ListCursor {
constructor({highlight_class, list}) {
const config_ok =
highlight_class &&
list &&
list.scroll_container_sel &&
list.find_li &&
list.first_key &&
list.prev_key &&
list.next_key;
if (!config_ok) {
blueslip.error("Programming error");
return;
}
this.highlight_class = highlight_class;
this.list = list;
}
clear() {
if (this.curr_key === undefined) {
return;
}
const row = this.get_row(this.curr_key);
if (row) {
row.clear();
}
this.curr_key = undefined;
}
get_key() {
return this.curr_key;
}
get_row(key) {
// TODO: The list class should probably do more of the work
// here, so we're not so coupled to jQuery, and
// so we instead just get back a widget we can say
// something like widget.trigger("select") on. This will
// be especially important if we do lazy rendering.
// It would also give the caller more flexibility on
// the actual styling.
if (key === undefined) {
return;
}
const li = this.list.find_li({
key,
force_render: true,
});
if (!li || li.length === 0) {
return;
}
return {
highlight: () => {
li.addClass(this.highlight_class);
this.adjust_scroll(li);
},
clear: () => {
li.removeClass(this.highlight_class);
},
};
}
adjust_scroll(li) {
const scroll_container = $(this.list.scroll_container_sel);
scroll_util.scroll_element_into_container(li, scroll_container);
}
redraw() {
// We should only call this for situations like the buddy
// list where we redraw the whole list without necessarily
// changing it, so we just want to re-highlight the current
// row in the new DOM. If you are filtering, for now you
// should call the 'reset()' method.
const row = this.get_row(this.curr_key);
if (row === undefined) {
return;
}
row.highlight();
}
go_to(key) {
if (key === undefined) {
blueslip.error("Caller is not checking keys for ListCursor.go_to");
return;
}
if (key === this.curr_key) {
return;
}
this.clear();
const row = this.get_row(key);
if (row === undefined) {
blueslip.error("Cannot highlight key for ListCursor: " + key);
return;
}
this.curr_key = key;
row.highlight();
}
reset() {
this.clear();
const key = this.list.first_key();
if (key === undefined) {
this.curr_key = undefined;
return;
}
this.go_to(key);
}
prev() {
if (this.curr_key === undefined) {
return;
}
const key = this.list.prev_key(this.curr_key);
if (key === undefined) {
// leave the current key
return;
}
this.go_to(key);
}
next() {
if (this.curr_key === undefined) {
// This is sort of a special case where we went from
// an empty filter to having data.
this.reset();
return;
}
const key = this.list.next_key(this.curr_key);
if (key === undefined) {
// leave the current key
return;
}
this.go_to(key);
}
}
module.exports = ListCursor;
window.ListCursor = ListCursor;