/** Timebar
* require jQuery
* require momentjs;
* require format() in BasicoFinal;
* autor: jhones william
*/
(function ($) {
$.fn.timebar = function (options) {
var $this = $(this);
var plugin = {}
plugin.config = jQuery.extend({
locale: 'pt-br',
autoload: true,
debug: false,
min_date: undefined,
max_date: undefined,
month_active: parseInt(moment().format("M")),
year_active: parseInt(moment().format("YYYY")),
amount_month_visible: 5,
amount_per_browsing_window: 2,
label_current_year: undefined,
abbreviation_month: true,
events: [],
components: {
month: '
\
\
{0} / {1}\
\
',
timebar: '',
navigation: '\
{0}\
',
},
on: {
select_month: undefined,
},
}, options);
var components = {
month: function (month, year) {
var monthName = moment().month(month - 1).format(plugin.config.abbreviation_month ? 'MMM' : 'MMMM');
var monthTitle = "{0} de {1}".format(monthName, year);
return $(plugin.config.components.month.format(monthName, year, monthTitle));
},
timebar: function () {
return $(plugin.config.components.timebar);
},
navigation: function (icone) {
return $(plugin.config.components.navigation.format(icone));
}
};
var run_event = function (eventName) {
var args = Array.prototype.slice.call(arguments);
args.splice(0, 1);
if (plugin.config.debug) console.log(eventName);
(plugin.config.events[eventName] || []).map(function (func) {
try {
func.apply(null, args);
} catch (error) {
if (plugin.config.debug) console.log(error);
}
});
}
var insert_navigation_buttons = function () {
var browsing_window = function (amount) {
var $months = $this.find(".tab-month");
var find_extremes_window = function () {
for (var index = 0; index < $months.length; index++) {
var $month = $($months[index]);
if ($month.hasClass('visible')) {
if (plugin.window_min == undefined) plugin.window_min = index;
plugin.window_max = index;
}
}
}
if (plugin.window_max == undefined || plugin.window_min == undefined) {
find_extremes_window();
}
var interations = Math.abs(amount);
while (interations > 0) {
if (amount > 0) {
if (plugin.window_max < ($months.length - 1)) {
$($months[plugin.window_min]).removeClass('visible');
plugin.window_max = plugin.window_max + 1;
plugin.window_min = plugin.window_min + 1;
$($months[plugin.window_max]).addClass('visible');
}
} else {
if (plugin.window_min > 0) {
$($months[plugin.window_max]).removeClass('visible');
plugin.window_max = plugin.window_max - 1;
plugin.window_min = plugin.window_min - 1;
$($months[plugin.window_min]).addClass('visible');
}
}
interations = interations - 1;
}
}
var $back = components.navigation('chevron_left');
var $after = components.navigation('chevron_right');
$back.on('click', function () {
browsing_window(plugin.config.amount_per_browsing_window * -1)
});
$after.on('click', function () {
browsing_window(plugin.config.amount_per_browsing_window)
});
$this.find(".tab-months").prepend($back);
$this.find(".tab-months").append($after);
}
var insert_months = function (min_date, max_date) {
var month_visibles = plugin.config.amount_month_visible;
var date_interator_min = moment({ year: min_date.get("year"), month: min_date.get("month") });
var date_interator_max = moment({ year: max_date.get("year"), month: max_date.get("month") });
var insert_month = function (month, year) {
var $month = components.month(month, year);
if (plugin.config.month_active == month && plugin.config.year_active == year) {
$month.addClass("active");
$month.addClass("visible");
month_visibles--;
}
$month.on("click", function () {
$this.find(".tab-month").removeClass('active');
$(this).addClass("active")
run_event("select_month", month, year);
});
$this.find(".tab-months").prepend($month);
}
while (date_interator_max.isAfter(date_interator_min)) {
var month = parseInt(date_interator_max.get('month')) + 1;
var year = date_interator_max.get('year');
insert_month(month, year);
date_interator_max.subtract(1, 'month');
}
var set_active = function ($meses, indexElementActive, ammontVisibles, distance) {
if (distance == null) distance = 1;
if (ammontVisibles == 0 || distance > $meses.length) return;
var leftX = indexElementActive + distance;
var rightX = indexElementActive - distance;
if ($meses.hasOwnProperty(leftX) && ammontVisibles > 0) {
$($meses[leftX]).addClass('visible');
ammontVisibles--;
}
if ($meses.hasOwnProperty(rightX) && ammontVisibles > 0) {
$($meses[rightX]).addClass('visible');
ammontVisibles--;
}
return set_active($meses, indexElementActive, ammontVisibles, distance + 1)
}
var $meses = $this.find(".tab-month");
var index = $meses.index($this.find(".tab-month.active"));
set_active($meses, index, month_visibles)
}
plugin.events = {};
plugin.on = function (event, func) {
if (typeof func !== 'function') return false;
if (plugin.config.events[event] === undefined) {
plugin.config.events[event] = [];
}
plugin.config.events[event].push(func);
}
plugin.start = function () {
if (plugin.started) return;
$this.append(components.timebar());
insert_months(plugin.config.min_date, plugin.config.max_date);
insert_navigation_buttons();
if (plugin.config.autoload) run_event("select_month", plugin.config.month_active, plugin.config.year_active)
plugin.started = true;
}
return plugin;
};
})(jQuery);