2016-11-04 22:34:12 +01:00
|
|
|
var components = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var tab = $("<div class='ind-tab' data-tab-id='" + i + "'>" + value.label + "</div>");
|
|
|
|
if (i === 0) {
|
|
|
|
tab.addClass("first");
|
|
|
|
} else {
|
|
|
|
tab.addClass("second");
|
|
|
|
}
|
|
|
|
_component.append(tab);
|
|
|
|
});
|
|
|
|
return _component;
|
|
|
|
}(opts));
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
retrieved: false,
|
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;
|
|
|
|
opts.callback(last_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (typeof opts.selected === "number") {
|
|
|
|
$(component).find(".ind-tab[data-tab-id='" + opts.selected + "']").click();
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
var prototype = {
|
|
|
|
value: function () {
|
|
|
|
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-12 00:03:20 +01:00
|
|
|
goto: function (name) {
|
|
|
|
var value = _.find(opts.values, function (o) {
|
|
|
|
return o.label === name;
|
|
|
|
});
|
|
|
|
|
|
|
|
var idx = opts.values.indexOf(value);
|
|
|
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
meta.$ind_tab.removeClass("selected");
|
|
|
|
meta.$ind_tab.filter("[data-tab-id='" + idx + "']").addClass("selected");
|
|
|
|
|
|
|
|
value.label = name;
|
|
|
|
opts.callback(name);
|
|
|
|
}
|
|
|
|
},
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
__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;
|
|
|
|
}
|