2019-11-02 00:06:25 +01:00
|
|
|
const list_cursor = function (opts) {
|
|
|
|
const self = {};
|
2018-04-21 14:59:03 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const config_ok =
|
2018-04-21 14:59:03 +02:00
|
|
|
opts.highlight_class &&
|
|
|
|
opts.list &&
|
2018-07-26 22:13:37 +02:00
|
|
|
opts.list.scroll_container_sel &&
|
2018-04-21 14:59:03 +02:00
|
|
|
opts.list.find_li &&
|
|
|
|
opts.list.first_key &&
|
|
|
|
opts.list.prev_key &&
|
|
|
|
opts.list.next_key
|
2018-06-06 18:19:09 +02:00
|
|
|
;
|
2018-04-21 14:59:03 +02:00
|
|
|
|
|
|
|
if (!config_ok) {
|
|
|
|
blueslip.error('Programming error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.clear = function () {
|
|
|
|
if (self.curr_key === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = self.get_row(self.curr_key);
|
2018-04-21 14:59:03 +02:00
|
|
|
if (row) {
|
|
|
|
row.clear();
|
|
|
|
}
|
|
|
|
self.curr_key = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_key = function () {
|
|
|
|
return self.curr_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_row = function (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.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;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const li = opts.list.find_li({
|
2018-07-16 15:16:33 +02:00
|
|
|
key: key,
|
|
|
|
force_render: true,
|
|
|
|
});
|
2018-04-21 14:59:03 +02:00
|
|
|
|
2020-06-18 16:22:48 +02:00
|
|
|
if (!li || li.length === 0) {
|
2018-04-21 14:59:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
highlight: function () {
|
|
|
|
li.addClass(opts.highlight_class);
|
|
|
|
self.adjust_scroll(li);
|
|
|
|
},
|
|
|
|
clear: function () {
|
|
|
|
li.removeClass(opts.highlight_class);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
self.adjust_scroll = function (li) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const scroll_container = $(opts.list.scroll_container_sel);
|
2018-07-26 22:13:37 +02:00
|
|
|
scroll_util.scroll_element_into_container(li, scroll_container);
|
2018-04-21 14:59:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.redraw = function () {
|
|
|
|
// 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.
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = self.get_row(self.curr_key);
|
2018-04-21 14:59:03 +02:00
|
|
|
|
|
|
|
if (row === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
row.highlight();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.go_to = function (key) {
|
|
|
|
if (key === undefined) {
|
|
|
|
blueslip.error('Caller is not checking keys for list_cursor.go_to');
|
|
|
|
return;
|
|
|
|
}
|
2018-08-24 14:37:54 +02:00
|
|
|
if (key === self.curr_key) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-21 14:59:03 +02:00
|
|
|
self.clear();
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = self.get_row(key);
|
2018-04-21 14:59:03 +02:00
|
|
|
if (row === undefined) {
|
|
|
|
blueslip.error('Cannot highlight key for list_cursor: ' + key);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-24 14:37:54 +02:00
|
|
|
self.curr_key = key;
|
2018-04-21 14:59:03 +02:00
|
|
|
row.highlight();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.reset = function () {
|
|
|
|
self.clear();
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = opts.list.first_key();
|
2018-04-21 14:59:03 +02:00
|
|
|
if (key === undefined) {
|
|
|
|
self.curr_key = undefined;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.go_to(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.prev = function () {
|
|
|
|
if (self.curr_key === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = opts.list.prev_key(self.curr_key);
|
2018-04-21 14:59:03 +02:00
|
|
|
if (key === undefined) {
|
|
|
|
// leave the current key
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.go_to(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.next = function () {
|
|
|
|
if (self.curr_key === undefined) {
|
|
|
|
// This is sort of a special case where we went from
|
|
|
|
// an empty filter to having data.
|
|
|
|
self.reset();
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const key = opts.list.next_key(self.curr_key);
|
2018-04-21 14:59:03 +02:00
|
|
|
if (key === undefined) {
|
|
|
|
// leave the current key
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.go_to(key);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
2019-10-25 09:45:13 +02:00
|
|
|
module.exports = list_cursor;
|
2018-05-28 08:04:36 +02:00
|
|
|
window.list_cursor = list_cursor;
|