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({
|
|
|
|
name: String toggle_name,
|
|
|
|
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();
|
|
|
|
*/
|
|
|
|
|
2016-11-04 22:34:12 +01:00
|
|
|
exports.toggle = (function () {
|
|
|
|
var keys = {};
|
|
|
|
|
|
|
|
var __toggle = function (opts) {
|
2016-12-05 07:02:18 +01:00
|
|
|
var component = (function render_component(opts) {
|
2016-11-04 22:34:12 +01:00
|
|
|
var _component = $("<div class='tab-switcher'></div>");
|
|
|
|
opts.values.forEach(function (value, i) {
|
2017-01-30 20:32:47 +01:00
|
|
|
// create a tab with a tab-id so they don't have to be referenced
|
|
|
|
// by text value which can be inconsistent.
|
2017-10-27 01:35:12 +02:00
|
|
|
var tab = $("<div class='ind-tab' data-tab-key='" + value.key + "' data-tab-id='" + i + "' tabindex='0'>" + value.label + "</div>");
|
2017-01-30 20:32:47 +01:00
|
|
|
|
|
|
|
// add proper classes for styling in CSS.
|
2016-11-04 22:34:12 +01:00
|
|
|
if (i === 0) {
|
2017-02-21 20:30:34 +01:00
|
|
|
// this should be default selected unless otherwise specified.
|
|
|
|
tab.addClass("first selected");
|
2017-01-30 20:32:47 +01:00
|
|
|
} else if (i === opts.values.length - 1) {
|
|
|
|
tab.addClass("last");
|
2016-11-04 22:34:12 +01:00
|
|
|
} else {
|
2017-01-30 20:32:47 +01:00
|
|
|
tab.addClass("middle");
|
2016-11-04 22:34:12 +01:00
|
|
|
}
|
|
|
|
_component.append(tab);
|
|
|
|
});
|
|
|
|
return _component;
|
|
|
|
}(opts));
|
|
|
|
|
|
|
|
var meta = {
|
2017-01-12 00:17:43 +01:00
|
|
|
$ind_tab: component.find(".ind-tab"),
|
2018-03-29 20:48:49 +02:00
|
|
|
idx: -1,
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
|
2018-03-29 20:48:49 +02:00
|
|
|
function select_tab(idx, payload) {
|
|
|
|
meta.$ind_tab.removeClass("selected");
|
|
|
|
|
|
|
|
var elem = meta.$ind_tab.eq(idx);
|
|
|
|
elem.addClass("selected");
|
2017-02-14 03:12:11 +01:00
|
|
|
|
2018-03-29 20:48:49 +02:00
|
|
|
if (idx !== meta.idx) {
|
2018-04-04 21:06:58 +02:00
|
|
|
meta.idx = idx;
|
2018-03-29 20:48:49 +02:00
|
|
|
if (opts.callback) {
|
|
|
|
opts.callback(
|
|
|
|
opts.values[idx].label,
|
|
|
|
opts.values[idx].key,
|
|
|
|
payload || {}
|
|
|
|
);
|
2017-02-14 03:12:11 +01:00
|
|
|
}
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 17:01:34 +02:00
|
|
|
if (!opts.child_wants_focus) {
|
|
|
|
elem.focus();
|
|
|
|
}
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function maybe_go_left() {
|
|
|
|
if (meta.idx > 0) {
|
|
|
|
select_tab(meta.idx - 1);
|
2018-04-04 16:36:49 +02:00
|
|
|
return true;
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybe_go_right() {
|
|
|
|
if (meta.idx < opts.values.length - 1) {
|
|
|
|
select_tab(meta.idx + 1);
|
2018-04-04 16:36:49 +02:00
|
|
|
return true;
|
2018-03-29 20:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
meta.$ind_tab.click(function () {
|
|
|
|
var idx = $(this).data("tab-id");
|
|
|
|
select_tab(idx);
|
2017-10-27 01:35:12 +02:00
|
|
|
});
|
|
|
|
|
2018-04-04 16:36:49 +02:00
|
|
|
keydown_util.handle({
|
|
|
|
elem: meta.$ind_tab,
|
|
|
|
handlers: {
|
|
|
|
left_arrow: maybe_go_left,
|
|
|
|
right_arrow: maybe_go_right,
|
|
|
|
},
|
2016-11-04 22:34:12 +01:00
|
|
|
});
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2016-11-04 22:34:12 +01:00
|
|
|
if (typeof opts.selected === "number") {
|
2018-03-29 20:48:49 +02:00
|
|
|
select_tab(opts.selected);
|
2016-11-04 22:34:12 +01:00
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
var prototype = {
|
2018-04-04 17:01:34 +02:00
|
|
|
maybe_go_left: maybe_go_left,
|
|
|
|
maybe_go_right: maybe_go_right,
|
|
|
|
|
2016-11-04 22:34:12 +01:00
|
|
|
value: function () {
|
2018-03-29 20:48:49 +02:00
|
|
|
if (meta.idx >= 0) {
|
|
|
|
return opts.values[meta.idx].label;
|
2016-11-04 22:34:12 +01:00
|
|
|
}
|
|
|
|
},
|
2018-03-29 20:48:49 +02:00
|
|
|
|
2016-11-04 22:34:12 +01:00
|
|
|
get: function () {
|
|
|
|
return component;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2017-01-30 20:32:47 +01:00
|
|
|
// go through the process of finding the correct tab for a given name,
|
|
|
|
// and when found, select that one and provide the proper callback.
|
2017-08-01 20:37:14 +02:00
|
|
|
// 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.
|
2017-01-12 00:03:20 +01:00
|
|
|
var value = _.find(opts.values, function (o) {
|
2017-01-30 21:54:09 +01:00
|
|
|
return o.label === name || o.key === name;
|
2017-01-12 00:03:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var idx = opts.values.indexOf(value);
|
|
|
|
|
2018-03-29 20:48:49 +02:00
|
|
|
if (idx >= 0) {
|
|
|
|
select_tab(idx, payload);
|
2017-01-12 00:03:20 +01:00
|
|
|
}
|
|
|
|
},
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (opts.name) {
|
|
|
|
keys[opts.name] = {
|
|
|
|
opts: opts,
|
|
|
|
component: component,
|
2017-01-12 00:17:43 +01:00
|
|
|
value: prototype.value,
|
2017-01-12 00:03:20 +01:00
|
|
|
goto: prototype.goto,
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return prototype;
|
|
|
|
};
|
|
|
|
|
2017-01-30 20:32:47 +01:00
|
|
|
// look up a toggle globally by the name you set it as and return
|
|
|
|
// the prototype.
|
2016-11-04 22:34:12 +01:00
|
|
|
__toggle.lookup = function (id) {
|
|
|
|
return keys[id];
|
|
|
|
};
|
|
|
|
|
|
|
|
return __toggle;
|
|
|
|
}());
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = components;
|
|
|
|
}
|