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> [
|
|
|
|
{ label: String title }
|
|
|
|
],
|
|
|
|
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-01-30 21:54:09 +01:00
|
|
|
var tab = $("<div class='ind-tab' data-tab-key='" + value.key + "' data-tab-id='" + i + "'>" + 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) {
|
|
|
|
tab.addClass("first");
|
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));
|
|
|
|
|
2017-01-30 20:32:47 +01:00
|
|
|
// store once a copy of the tabs inside the parent in a jQuery object/array.
|
2016-11-04 22:34:12 +01:00
|
|
|
var meta = {
|
2017-01-12 00:17:43 +01:00
|
|
|
$ind_tab: component.find(".ind-tab"),
|
2016-11-04 22:34:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
var last_value = null;
|
|
|
|
meta.$ind_tab.each(function () {
|
|
|
|
$(this).click(function () {
|
|
|
|
meta.$ind_tab.removeClass("selected");
|
|
|
|
$(this).addClass("selected");
|
|
|
|
if (opts.callback) {
|
|
|
|
var id = +$(this).data("tab-id");
|
|
|
|
if (last_value !== opts.values[id].label) {
|
|
|
|
last_value = opts.values[id].label;
|
2017-01-30 21:54:09 +01:00
|
|
|
opts.callback(last_value, opts.values[id].key);
|
2016-11-04 22:34:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (typeof opts.selected === "number") {
|
|
|
|
$(component).find(".ind-tab[data-tab-id='" + opts.selected + "']").click();
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
var prototype = {
|
|
|
|
value: function () {
|
2017-01-30 20:32:47 +01:00
|
|
|
// find whatever is visually selected in the tab switcher.
|
2016-11-04 22:34:12 +01:00
|
|
|
var sel = component.find(".selected");
|
|
|
|
|
|
|
|
if (sel.length > 0) {
|
|
|
|
var id = +sel.eq(0).data("tab-id");
|
|
|
|
return opts.values[id].label;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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-01-12 00:03:20 +01:00
|
|
|
goto: function (name) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
meta.$ind_tab.removeClass("selected");
|
|
|
|
meta.$ind_tab.filter("[data-tab-id='" + idx + "']").addClass("selected");
|
|
|
|
|
2017-01-30 21:54:09 +01:00
|
|
|
opts.callback(value.label, value.key);
|
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;
|
|
|
|
}
|