2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 00:36:50 +01:00
|
|
|
import * as keydown_util from "./keydown_util";
|
2017-01-30 20:32:47 +01:00
|
|
|
/* USAGE:
|
|
|
|
Toggle x = components.toggle({
|
|
|
|
selected: Integer selected_index,
|
|
|
|
values: Array<Object> [
|
2021-04-10 10:09:54 +02:00
|
|
|
{label: $t({defaultMessage: "String title"})}
|
2017-01-30 20:32:47 +01:00
|
|
|
],
|
|
|
|
callback: function () {
|
|
|
|
// .. on value change.
|
|
|
|
},
|
|
|
|
}).get();
|
|
|
|
*/
|
|
|
|
|
2021-02-10 16:46:49 +01:00
|
|
|
export function toggle(opts) {
|
2021-05-06 21:49:45 +02:00
|
|
|
const component = $("<div class='tab-switcher'></div>");
|
|
|
|
if (opts.html_class) {
|
|
|
|
// add a check inside passed arguments in case some extra
|
|
|
|
// classes need to be added for correct alignment or other purposes
|
|
|
|
component.addClass(opts.html_class);
|
|
|
|
}
|
|
|
|
for (const [i, value] of opts.values.entries()) {
|
|
|
|
// create a tab with a tab-id so they don't have to be referenced
|
|
|
|
// by text value which can be inconsistent.
|
|
|
|
const tab = $("<div>", {
|
|
|
|
class: "ind-tab",
|
|
|
|
"data-tab-key": value.key,
|
|
|
|
"data-tab-id": i,
|
|
|
|
tabindex: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (value.label_html !== undefined) {
|
|
|
|
const html = value.label_html;
|
|
|
|
tab.html(html);
|
|
|
|
} else {
|
|
|
|
tab.text(value.label);
|
2020-04-20 22:41:03 +02:00
|
|
|
}
|
2018-04-14 16:28:58 +02:00
|
|
|
|
2021-05-06 21:49:45 +02:00
|
|
|
// add proper classes for styling in CSS.
|
|
|
|
if (i === 0) {
|
|
|
|
// this should be default selected unless otherwise specified.
|
|
|
|
tab.addClass("first selected");
|
|
|
|
} else if (i === opts.values.length - 1) {
|
|
|
|
tab.addClass("last");
|
|
|
|
} else {
|
|
|
|
tab.addClass("middle");
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2021-05-06 21:49:45 +02:00
|
|
|
component.append(tab);
|
|
|
|
}
|
2018-04-14 16:28:58 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2018-04-14 16:28:58 +02:00
|
|
|
$ind_tab: component.find(".ind-tab"),
|
|
|
|
idx: -1,
|
|
|
|
};
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2020-07-07 19:48:59 +02:00
|
|
|
// Returns false if the requested tab is disabled.
|
2018-06-04 00:10:36 +02:00
|
|
|
function select_tab(idx) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = meta.$ind_tab.eq(idx);
|
2020-07-15 01:29:15 +02:00
|
|
|
if (elem.hasClass("disabled")) {
|
2020-07-07 19:48:59 +02:00
|
|
|
return false;
|
2019-05-08 09:26:27 +02:00
|
|
|
}
|
2018-04-14 16:28:58 +02:00
|
|
|
meta.$ind_tab.removeClass("selected");
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
elem.addClass("selected");
|
|
|
|
|
toggler: Always call back to callback function.
In our toggler component (the thing that handles tabs in things
like our markdown/search help, settings/org, etc.), we have
a callback mechanism when you switch to the tab. We were
being tricky and only calling it when the tab changed.
It turns out it's better to just always call the callback,
since these things are often in modals that open and close,
and if you open a modal for the second time, you want to do
the callback task for whichever setting you're going to.
There was actually kind of a nasty bug with this, where the
keyboard handling in the keyboard-help modal worked fine the
first time you opened it, but then it didn't work the second
time (if you focused some other element in the interim), and
it was due to not re-setting the focus to the inner modal
because we weren't calling the callback.
Of course, there are pitfalls in calling the same callbacks
twice, but our callbacks should generally be idempotent
for other reasons.
2018-06-03 22:12:10 +02:00
|
|
|
meta.idx = idx;
|
|
|
|
if (opts.callback) {
|
2020-07-15 00:34:28 +02:00
|
|
|
opts.callback(opts.values[idx].label, opts.values[idx].key);
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
if (!opts.child_wants_focus) {
|
2020-07-20 21:24:26 +02:00
|
|
|
elem.trigger("focus");
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
2020-07-07 19:48:59 +02:00
|
|
|
return true;
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
function maybe_go_left() {
|
2020-07-07 19:48:59 +02:00
|
|
|
// Select the first non-disabled tab to the left, if any.
|
|
|
|
let i = 1;
|
|
|
|
while (meta.idx - i >= 0) {
|
|
|
|
if (select_tab(meta.idx - i)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
i += 1;
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return false;
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
2017-10-27 01:35:12 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
function maybe_go_right() {
|
2020-07-07 19:48:59 +02:00
|
|
|
// Select the first non-disabled tab to the right, if any.
|
|
|
|
let i = 1;
|
|
|
|
while (meta.idx + i <= opts.values.length - 1) {
|
|
|
|
if (select_tab(meta.idx + i)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
i += 1;
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return false;
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 21:49:45 +02:00
|
|
|
meta.$ind_tab.on("click", function () {
|
|
|
|
const idx = $(this).data("tab-id");
|
|
|
|
select_tab(idx);
|
|
|
|
});
|
2018-04-14 16:28:58 +02:00
|
|
|
|
2021-05-06 21:49:45 +02:00
|
|
|
keydown_util.handle({
|
|
|
|
elem: meta.$ind_tab,
|
|
|
|
handlers: {
|
2021-05-26 19:51:07 +02:00
|
|
|
ArrowLeft: maybe_go_left,
|
|
|
|
ArrowRight: maybe_go_right,
|
2021-05-06 21:49:45 +02:00
|
|
|
},
|
|
|
|
});
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2021-05-06 21:49:45 +02:00
|
|
|
// We should arguably default opts.selected to 0.
|
|
|
|
if (typeof opts.selected === "number") {
|
|
|
|
select_tab(opts.selected);
|
|
|
|
}
|
2016-11-04 22:34:12 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const prototype = {
|
2020-07-07 19:48:59 +02:00
|
|
|
// Skip disabled tabs and go to the next one.
|
2020-07-20 22:18:43 +02:00
|
|
|
maybe_go_left,
|
|
|
|
maybe_go_right,
|
2018-04-04 17:01:34 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
disable_tab(name) {
|
2020-07-02 01:39:34 +02:00
|
|
|
const value = opts.values.find((o) => o.key === name);
|
2019-05-08 09:26:27 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = opts.values.indexOf(value);
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.$ind_tab.eq(idx).addClass("disabled");
|
2019-05-08 09:26:27 +02:00
|
|
|
},
|
|
|
|
|
2020-07-07 19:48:59 +02:00
|
|
|
enable_tab(name) {
|
|
|
|
const value = opts.values.find((o) => o.key === name);
|
|
|
|
|
|
|
|
const idx = opts.values.indexOf(value);
|
|
|
|
meta.$ind_tab.eq(idx).removeClass("disabled");
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
value() {
|
2018-04-14 16:28:58 +02:00
|
|
|
if (meta.idx >= 0) {
|
|
|
|
return opts.values[meta.idx].label;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
/* istanbul ignore next */
|
|
|
|
return undefined;
|
2018-04-14 16:28:58 +02:00
|
|
|
},
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
get() {
|
2018-04-14 16:28:58 +02:00
|
|
|
return component;
|
|
|
|
},
|
|
|
|
// go through the process of finding the correct tab for a given name,
|
|
|
|
// and when found, select that one and provide the proper callback.
|
2020-07-20 22:18:43 +02:00
|
|
|
goto(name) {
|
2020-07-02 01:39:34 +02:00
|
|
|
const value = opts.values.find((o) => o.label === name || o.key === name);
|
2016-11-04 22:34:12 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = opts.values.indexOf(value);
|
2016-11-04 22:34:12 +01:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
if (idx >= 0) {
|
2018-06-04 00:10:36 +02:00
|
|
|
select_tab(idx);
|
2018-04-14 16:28:58 +02:00
|
|
|
}
|
|
|
|
},
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
return prototype;
|
2021-02-10 16:46:49 +01:00
|
|
|
}
|