Skip to content

Commit

Permalink
add format method
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0n committed Oct 19, 2015
1 parent 41b938e commit ca94663
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
48 changes: 45 additions & 3 deletions dist/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ var Datepicker;
start: '', // Start date
weekends: [6, 0],
defaultView: 'days',
format: 'dd.mm.yyyy',
dateFormat: 'dd.mm.yyyy',

// navigation
prevHtml: '«',
nextHtml: '»'
nextHtml: '»',

// events
onChange: ''
};

Datepicker = function (el, options) {
Expand Down Expand Up @@ -87,6 +90,12 @@ var Datepicker;

},

_triggerOnChange: function (cellType) {
var dateString = this.formatDate(this.opts.dateFormat, this.date);

this.opts.onChange(dateString, this.date, this);
},

next: function () {
var d = this.parsedDate;
switch (this.view) {
Expand Down Expand Up @@ -118,6 +127,30 @@ var Datepicker;
}
},

formatDate: function (string, date) {
var result = string,
d = this.parsedDate;

switch (true) {
case /dd/.test(result):
result = result.replace('dd', d.fullDate);
case /d/.test(result):
result = result.replace('d', d.date);
case /mm/.test(result):
result = result.replace('mm',d.fullMonth);
case /m/.test(result):
result = result.replace('m',d.month + 1);
case /MM/.test(result):
result = result.replace('MM', this.loc.months[d.month]);
case /yyyy/.test(result):
result = result.replace('yyyy', d.year);
case /yy/.test(result):
result = result.replace('yy', d.year.toString().slice(-2));
}

return result;
},

get parsedDate() {
return Datepicker.getParsedDate(this.date);
},
Expand Down Expand Up @@ -161,7 +194,6 @@ var Datepicker;
}
};


Datepicker.getDaysCount = function (date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
Expand All @@ -170,6 +202,9 @@ var Datepicker;
return {
year: date.getFullYear(),
month: date.getMonth(),
fullMonth: (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1, // One based
date: date.getDate(),
fullDate: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
day: date.getDay()
}
};
Expand Down Expand Up @@ -473,7 +508,14 @@ Datepicker.Cell = function () {

_handleClick: {
days: function (el) {
var date = el.data('date'),
d = this.d.parsedDate;

this.d.date = new Date(d.year, d.month, date);

if (this.d.opts.onChange) {
this.d._triggerOnChange()
}
},
months: function (el) {
var month = el.data('month'),
Expand Down
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
</div>
<script type="text/javascript" src="dist/js/datepicker.js"></script>
<script type="text/javascript">
$('.calendar').datepicker();
$('.calendar').datepicker({
onChange: function (dateString, date, inst) {

}
});
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions js/datepicker/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@

_handleClick: {
days: function (el) {
var date = el.data('date'),
d = this.d.parsedDate;

this.d.date = new Date(d.year, d.month, date);

if (this.d.opts.onChange) {
this.d._triggerOnChange()
}
},
months: function (el) {
var month = el.data('month'),
Expand Down
41 changes: 38 additions & 3 deletions js/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ var Datepicker;
start: '', // Start date
weekends: [6, 0],
defaultView: 'days',
format: 'dd.mm.yyyy',
dateFormat: 'dd.mm.yyyy',

// navigation
prevHtml: '&laquo;',
nextHtml: '&raquo;'
nextHtml: '&raquo;',

// events
onChange: ''
};

Datepicker = function (el, options) {
Expand Down Expand Up @@ -87,6 +90,12 @@ var Datepicker;

},

_triggerOnChange: function (cellType) {
var dateString = this.formatDate(this.opts.dateFormat, this.date);

this.opts.onChange(dateString, this.date, this);
},

next: function () {
var d = this.parsedDate;
switch (this.view) {
Expand Down Expand Up @@ -118,6 +127,30 @@ var Datepicker;
}
},

formatDate: function (string, date) {
var result = string,
d = this.parsedDate;

switch (true) {
case /dd/.test(result):
result = result.replace('dd', d.fullDate);
case /d/.test(result):
result = result.replace('d', d.date);
case /mm/.test(result):
result = result.replace('mm',d.fullMonth);
case /m/.test(result):
result = result.replace('m',d.month + 1);
case /MM/.test(result):
result = result.replace('MM', this.loc.months[d.month]);
case /yyyy/.test(result):
result = result.replace('yyyy', d.year);
case /yy/.test(result):
result = result.replace('yy', d.year.toString().slice(-2));
}

return result;
},

get parsedDate() {
return Datepicker.getParsedDate(this.date);
},
Expand Down Expand Up @@ -161,7 +194,6 @@ var Datepicker;
}
};


Datepicker.getDaysCount = function (date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
Expand All @@ -170,6 +202,9 @@ var Datepicker;
return {
year: date.getFullYear(),
month: date.getMonth(),
fullMonth: (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1, // One based
date: date.getDate(),
fullDate: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
day: date.getDay()
}
};
Expand Down
3 changes: 1 addition & 2 deletions sass/_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ html {
article {
margin: 60px 0 30px;
}
}

}

0 comments on commit ca94663

Please sign in to comment.