2016-11-04 22:34:12 +01:00
|
|
|
var components = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-01-30 20:32:47 +01:00
|
|
|
/* USAGE:
|
|
|
|
Toggle x = components.toggle({
|
|
|
|
selected: Integer selected_index,
|
|
|
|
values: Array<Object> [
|
2017-06-23 21:56:08 +02:00
|
|
|
{ label: i18n.t(String title) }
|
2017-01-30 20:32:47 +01:00
|
|
|
],
|
|
|
|
callback: function () {
|
|
|
|
// .. on value change.
|
|
|
|
},
|
|
|
|
}).get();
|
|
|
|
*/
|
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
exports.toggle = function (opts) {
|
|
|
|
var component = (function render_component(opts) {
|
|
|
|
var _component = $("<div class='tab-switcher'></div>");
|
|
|
|
opts.values.forEach(function (value, i) {
|
|
|
|
// create a tab with a tab-id so they don't have to be referenced
|
|
|
|
// by text value which can be inconsistent.
|
|
|
|
var tab = $("<div class='ind-tab' data-tab-key='" + value.key + "' data-tab-id='" + i + "' tabindex='0'>" + value.label + "</div>");
|
|
|
|
|
|
|
|
// 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");
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
2018-04-14 16:28:58 +02:00
|
|
|
_component.append(tab);
|
|
|
|
});
|
|
|
|
return _component;
|
|
|
|
}(opts));
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
$ind_tab: component.find(".ind-tab"),
|
|
|
|
idx: -1,
|
|
|
|
};
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
function select_tab(idx, payload) {
|
|
|
|
meta.$ind_tab.removeClass("selected");
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
var elem = meta.$ind_tab.eq(idx);
|
|
|
|
elem.addClass("selected");
|
|
|
|
|
|
|
|
if (idx !== meta.idx) {
|
|
|
|
meta.idx = idx;
|
|
|
|
if (opts.callback) {
|
|
|
|
opts.callback(
|
|
|
|
opts.values[idx].label,
|
|
|
|
opts.values[idx].key,
|
|
|
|
payload || {}
|
|
|
|
);
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
if (!opts.child_wants_focus) {
|
|
|
|
elem.focus();
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
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() {
|
|
|
|
if (meta.idx > 0) {
|
|
|
|
select_tab(meta.idx - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 01:35:12 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
function maybe_go_right() {
|
|
|
|
if (meta.idx < opts.values.length - 1) {
|
|
|
|
select_tab(meta.idx + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
meta.$ind_tab.click(function () {
|
|
|
|
var idx = $(this).data("tab-id");
|
|
|
|
select_tab(idx);
|
|
|
|
});
|
|
|
|
|
|
|
|
keydown_util.handle({
|
|
|
|
elem: meta.$ind_tab,
|
|
|
|
handlers: {
|
|
|
|
left_arrow: maybe_go_left,
|
|
|
|
right_arrow: maybe_go_right,
|
|
|
|
},
|
|
|
|
});
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +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
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
var prototype = {
|
|
|
|
maybe_go_left: maybe_go_left,
|
|
|
|
maybe_go_right: maybe_go_right,
|
2018-04-04 17:01:34 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
value: function () {
|
|
|
|
if (meta.idx >= 0) {
|
|
|
|
return opts.values[meta.idx].label;
|
|
|
|
}
|
|
|
|
},
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
get: function () {
|
|
|
|
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.
|
|
|
|
// supply a payload of data; since this is a custom event, we'll pass
|
|
|
|
// the data through to the callback.
|
|
|
|
goto: function (name, payload) {
|
|
|
|
// there are cases in which you would want to set this tab, but
|
|
|
|
// not to run the content inside the callback because it doesn't
|
|
|
|
// need to be initialized.
|
|
|
|
var value = _.find(opts.values, function (o) {
|
|
|
|
return o.label === name || o.key === name;
|
|
|
|
});
|
2016-11-04 22:34:12 +01:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
var idx = opts.values.indexOf(value);
|
2016-11-04 22:34:12 +01:00
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
if (idx >= 0) {
|
|
|
|
select_tab(idx, payload);
|
|
|
|
}
|
|
|
|
},
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
|
2018-04-14 16:28:58 +02:00
|
|
|
return prototype;
|
|
|
|
};
|
2016-11-04 22:34:12 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = components;
|
|
|
|
}
|