Skip to content

Commit

Permalink
🔥 PART 2: Quick patch on displaying wrong month number, No longer dep…
Browse files Browse the repository at this point in the history
…end on Lodash, PART 3 OTW
  • Loading branch information
motss committed Feb 29, 2016
1 parent 0e71b4d commit 89777d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
5 changes: 2 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jv-datepicker",
"version": "2.1.11",
"version": "2.1.12",
"authors": [
"motss <wes_ngrongsen@hotmail.com>"
],
Expand Down Expand Up @@ -31,8 +31,7 @@
"iron-list": "polymerelements/iron-list#^1.2.6",
"iron-selector": "PolymerElements/iron-selector#~1.0.8",
"neon-animation": "PolymerElements/neon-animation#^1.0.0",
"paper-icon-button": "PolymerElements/paper-icon-button#~1.0.6",
"lodash": "lodash/lodash#^4.5.1"
"paper-icon-button": "PolymerElements/paper-icon-button#~1.0.6"
},
"devDependencies": {
"iron-component-page": "PolymerElements/iron-component-page#^1.0.0",
Expand Down
50 changes: 32 additions & 18 deletions jv-datepicker.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@
value: function() {
var _now = new Date();
var _fullyear = _now.getFullYear();
var _month = _.padStart(_now.getMonth(), 2, '0');
var _date = _.padStart(_now.getDate(), 2, '0');
var _month = this._padStart(_now.getMonth() + 1, 2, '0');
var _date = this._padStart(_now.getDate() + 1, 2, '0');
return [_fullyear, _month, _date].join('-');
},
notify: true
Expand All @@ -425,9 +425,6 @@

_daysOfWeek: {
type: Array,
value: function() {
return ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
},
computed: '_computeDaysOfWeek(firstDayOfWeek)'
},
_daysOfMonth: {
Expand Down Expand Up @@ -714,7 +711,7 @@
// daysOfMonth is chooseable when:
// a) _target.date is of type Number,
// b) _target.classList.contains('is-disabled-day').
if (_target && _.isNumber(_target.date) &&
if (_target && this._isNumber(_target.date) &&
!_target.classList.contains('is-disabled-day')) {
if (this._chosenDaysOfMonth !== 99) {
var _node = Polymer.dom(this.$.daysOfMonth).querySelectorAll('div');
Expand Down Expand Up @@ -744,7 +741,7 @@
return '';
},
_isEmptyDate: function(_item) {
if (_.isNumber(_item)) {
if (this._isNumber(_item)) {
return '';
}
return ' is-non-selectable';
Expand All @@ -769,17 +766,17 @@
// ------ < _minDate ---------------- _maxDate > ------
// if _item is of type Number.
// if converted _item into new Date() < minDate or > maxDate.
if (_.isNumber(_item)) {
if (this._isNumber(_item)) {
var _minDateObj = this._computeMinDate(_minDate);
var _maxDateObj = this._computeMaxDate(_maxDate);
var _currentDate = new Date(this._activeYear, this._activeMonth, _item);
// run two different obj differently just in case only one of them
// is defined and still be able to update disabled days.
if (!_.isUndefined(_minDateObj)) {
if (!this._isUndefined(_minDateObj)) {
_isLessThanMinDate = _currentDate < new Date(_minDateObj.year,
_minDateObj.month - 1, _minDateObj.date);
}
if (!_.isUndefined(_maxDateObj)) {
if (!this._isUndefined(_maxDateObj)) {
_isMoreThanMaxDate = _currentDate > new Date(_maxDateObj.year,
_maxDateObj.month - 1, _maxDateObj.date);
}
Expand All @@ -801,7 +798,7 @@
// selected: false

// if none is selected is the listOfYears, reset to old value (_activeYear).
if (_.isNull(_year) || _.isEmpty(_year)) {
if (this._isNull(_year) || this._isNumber(_year)) {
// reset unselected listOfYears to _activeYear;
this.$.listOfYears.selectItem(this._activeYear - 1900);
// notifyResize iron-list after selectItem function.
Expand Down Expand Up @@ -833,9 +830,10 @@
// _daysOfWeek needs to be changed as well with firstDayOfWeek.
if (_firstDayOfWeek > 0 && _firstDayOfWeek < 7) {
var _dow = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
var _spliced = _dow.splice(_firstDayOfWeek);
_spliced.push(_dow);
return _.flatten(_spliced);
var _sliced = _dow.slice(_firstDayOfWeek);
var _rest = _dow.slice(0, _firstDayOfWeek);
var _concatted = Array.prototype.concat(_sliced, _rest);
return _concatted;
}
return ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
},
Expand Down Expand Up @@ -918,7 +916,7 @@
if (_format.m === 'mmm') {
_formattedMonth = _formattedMonth.slice(0, 3);
}else if (_format.m === 'mm') {
_formattedMonth = _.padStart(_selectedMonth, 2, '0');
_formattedMonth = this._padStart(_selectedMonth + 1, 2, '0');
}else if (_format.m === 'm') {
_formattedMonth = _selectedMonth + 1;
}
Expand All @@ -932,7 +930,7 @@
}
_formattedDate = _formattedDate + _suffixOrdinal;
}else if (_format.d === 'dd') {
_formattedDate = _.padStart(_formattedDate, 2, '0');
_formattedDate = this._padStart(_formattedDate, 2, '0');
}
// set formatted value with user defined symbols.
_finalFormatted = [_formattedYear, _format.s1, _formattedMonth,
Expand All @@ -956,17 +954,33 @@
// Update date to show long date or short date.
_computeShowLongDate: function(_showLongDate) {
if (_showLongDate) {
var _longDate = _.isUndefined(this._selectedDate) ? new Date().toString().slice(0, 15) :
var _longDate = this._isUndefined(this._selectedDate) ? new Date().toString().slice(0, 15) :
new Date(this._selectedYear, this._selectedMonth,
this._selectedDate).toString().slice(0, 15);
this.set('date', _longDate.slice(0,3) + ',' + _longDate.slice(3));
}else {
this.set('date', _.isUndefined(this._selectedDate) ? this.date :
this.set('date', this._isUndefined(this._selectedDate) ? this.date :
this._bindSelectedFulldate(this._selectedYear, this._selectedMonth, this._selectedDate,
this._format));
}
},

// Lodash's replacements.
_padStart: function(_string, _length, _chars) {
var _len = -_length;
var _str = (_chars + _string).slice(_len);
return _str;
},
_isUndefined: function(_value) {
return _value === undefined;
},
_isNull: function(_value) {
return _value === null;
},
_isNumber: function(_value) {
return typeof _value == 'number' || (!Number.isNaN(parseFloat(_value)) && Number.isFinite(_value));
},

});
</script>
</dom-module>

0 comments on commit 89777d3

Please sign in to comment.