Skip to content
Merged
66 changes: 33 additions & 33 deletions MonthPicker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
https://github.com/KidSysco/jquery-ui-month-picker/

Version 2.6.1
Version 2.6.2

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -16,6 +16,7 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
*/
;
(function ($, window, document) {
'use strict';
var _speeds = $.fx.speeds;
var _eventsNs = '.MonthPicker';
var _disabledClass = 'month-picker-disabled';
Expand All @@ -42,6 +43,14 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
MaxMonth: '_setMaxMonth'
};

function _toMonth(date) {
return date.getMonth() + (date.getFullYear() * 12);
}

function _toYear(month) {
return Math.floor(month / 12);
}

// This test must be run before any rererence is made to jQuery.
// In case the user didn't load jQuery or jQuery UI the plugin
// will fail before it get's to this test + there is no reason
Expand Down Expand Up @@ -294,7 +303,7 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
var me = this, Month = 'Month';
$.each(['Min', 'Max'], function(i, type) {
me["_set" + type + Month] = function(val) {
if ((me['_' + type + Month] = this._toDate(val)) === false) {
if ((me['_' + type + Month] = this._toMonth(val)) === false) {
alert(_badMinMaxVal.replace(/%/, type).replace(/_/, val));
}
};
Expand Down Expand Up @@ -366,8 +375,8 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
this._validationMessage.hide();
},

Toggle: function () {
return this._visible ? this.Close() : this.Open();
Toggle: function (event) {
return this._visible ? this.Close(event) : this.Open(event);
},

Open: function (event) {
Expand Down Expand Up @@ -689,8 +698,8 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.

var _minDate = this._MinMonth;
var _maxDate = this._MaxMonth;
_minYear = _minDate ? _minDate.getFullYear() : 0;
_maxYear = _maxDate ? _maxDate.getFullYear() : 0;
var _minYear = _minDate ? _toYear(_minDate) : 0;
var _maxYear = _maxDate ? _toYear(_maxDate) : 0;

this._prevButton
.attr('title', this._i18n('prev5Years'))
Expand Down Expand Up @@ -725,7 +734,7 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.

_onMonthClick: function(event) {
this._chooseMonth(event.data.month);
this.Close();
this.Close(event);
},

_onYearClick: function(event) {
Expand Down Expand Up @@ -754,10 +763,10 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
_ajustYear: function(_opts) {
var _year = _opts.StartYear || this.GetSelectedYear() || new Date().getFullYear();
if (this._MinMonth !== null) {
_year = Math.max(this._MinMonth.getFullYear(), _year);
_year = Math.max(_toYear(this._MinMonth), _year);
}
if (this._MaxMonth !== null) {
_year = Math.min(this._MaxMonth.getFullYear(), _year);
_year = Math.min(_toYear(this._MaxMonth), _year);
}

this._setPickerYear( _year );
Expand All @@ -768,37 +777,32 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.

var _minDate = this._MinMonth, _maxDate = this._MaxMonth;

this._prevButton.button('option', 'disabled', _minDate && _curYear == _minDate.getFullYear());
this._nextButton.button('option', 'disabled', _maxDate && _curYear == _maxDate.getFullYear());
this._prevButton.button('option', 'disabled', _minDate && _curYear == _toYear(_minDate));
this._nextButton.button('option', 'disabled', _maxDate && _curYear == _toYear(_maxDate));

for (var i = 0; i < 12; i++) {
// Disable the button if the month is not between the
// min and max interval.
var _currMonth = ((_curYear * 12) + i);
$(this._buttons[i]).button('option', 'disabled', (
(_minDate && new Date(_curYear, i, 1) < _minDate) ||
(_maxDate && new Date(_curYear, i, 0) > _maxDate)
(_minDate && _currMonth < _minDate) ||
(_maxDate && _currMonth > _maxDate)
));
}
},

_toDate: function(_val) {
_toMonth: function(_val) {
if (_val === null) {
return _val;
} else if (_val instanceof Date) {
_val = new Date(_val.getTime());
_val.setDate( 0 );

return _val;
return _toMonth(_val);
} else if ($.isNumeric(_val)) {
var _date = new Date;
_date.setMonth( _date.getMonth() + parseInt(_val, 10) );
_date.setDate( 0 );
return _date;
return _toMonth(new Date) + parseInt(_val, 10);
}
var _date = this._parseMonth(_val);
if (_date) {
return _date;
return _toMonth(_date);
}

return this._parsePeriod(_val);
Expand All @@ -814,25 +818,21 @@ http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
_json = _json.replace(/m/i, '":"m"');

try {
var _rev = JSON.parse( '{"' + _json.replace(/ /g, ',"') + '}' ),
obj = {};
var _rev = JSON.parse( '{"' + _json.replace(/ /g, ',"') + '}' ), obj = {};

for (var key in _rev) {
obj[ _rev[key] ] = key;
}

var _date = new Date;

_date.setFullYear( _date.getFullYear() + (parseInt(obj.y, 10) || 0) );
_date.setMonth( _date.getMonth() + (parseInt(obj.m, 10) || 0) );
_date.setDate( 0 );
return _date;
var _month = _toMonth(new Date);
_month += (parseInt(obj.m, 10) || 0);
return _month + (parseInt(obj.y, 10) || 0) * 12;
} catch (e) {
return false;
}
}
});

// Added in version 2.4.
$.MonthPicker.VERSION = '2.6.1';
$.MonthPicker.VERSION = '2.6.2';
}(jQuery, window, document));
Loading