/** Timeline * require jQuery * require momentjs; * require format() in BasicoFinal; * autor: jhones william */ (function($) { $.fn.timeline = function(options) { var $this = $(this); var month_enable = function(objDate, objMinDate, objMaxDate) { var monthEnable = false; if (objDate.years >= objMinDate.years && objDate.years <= objMaxDate.years) { if (objMinDate.years === objMaxDate.years) { if (objDate.months >= objMinDate.months && objDate.months <= objMaxDate.months) monthEnable = true; } else if (objDate.years === objMaxDate.years) { if (objDate.months <= objMaxDate.months) monthEnable = true; } else if (objDate.years === objMinDate.years) { if (objDate.months >= objMinDate.months) monthEnable = true; } else { monthEnable = true; } } return monthEnable; }; var define_month_enables = function(timeline) { var months = (timeline || $this).find('li.month'); var dataMinima = options.min_date.toObject(); var dataMaxima = options.max_date.toObject(); $.each(months, function() { $(this).unbind('click', events.select_month); var date = moment({ years: $(this).data('year'), months: $(this).data('month') - 1, }); if (month_enable(date.toObject(), dataMinima, dataMaxima)) { $(this).removeClass('disabled'); $(this).on('click', events.select_month); } else { $(this).addClass('disabled'); } }); }; var events = {}; events.change_year = function(year) { var current_year = typeof year == 'Number' ? parseInt(year) : parseInt($(options.label_current_year).text()); if ($(this).hasClass('next') || $(this).hasClass('back')) current_year = $(this).hasClass('next') ? current_year + 1 : current_year - 1; $(options.button_back_year).find('.label-year').text(current_year - 1); $(options.label_current_year).text(current_year); $(options.button_next_year).find('.label-year').text(current_year + 1); if (options.disable_year_button_off_range_date) { if (current_year < options.max_date.year()) { $(options.button_next_year).prop('disabled', false).removeClass('disabled'); } else { $(options.button_next_year).prop('disabled', true).addClass('disabled'); } if (current_year > options.min_date.year()) { $(options.button_back_year).prop('disabled', false).removeClass('disabled'); } else { $(options.button_back_year).prop('disabled', true).addClass('disabled'); } } $this.find('.month').removeClass('active'); $.each($this.find('.month'), function() { var month = $(this).data('month'); var monthName = moment().month(month - 1).format('MMMM'); var monthTitle = "{0} de {1}".format(monthName, current_year); $(this).data('year', current_year); $(this).prop('title', monthTitle); if (options.month_active == month && options.year_active == current_year) { $(this).addClass('active'); } }); define_month_enables(); try { if (typeof options.on.change_year == 'function') options.on.change_year(current_year); } catch (e) { if (options.debug) console.log(e); } }; events.select_month = function() { //ainda não é multiplo $this.find('.month').removeClass('active'); $(this).addClass('active'); options.month_active = $(this).data('month'); options.year_active = $(this).data('year'); try { if (typeof options.on.select_month == 'function') options.on.select_month(options.month_active, options.year_active); } catch (e) { if (options.debug) console.log(e); } }; var defaultOptions = { 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")), // multiple: false, button_next_year: undefined, button_back_year: undefined, label_current_year: undefined, abbreviation_month: true, disable_year_button_off_range_date: true, disable_year_button_for_future_date: true, components: { month: '\