mirror of
https://github.com/frappe/air-datepicker.git
synced 2026-01-14 11:01:22 +08:00
add 12 hours mode
This commit is contained in:
parent
1ca88e6d0a
commit
f5b208dbc5
93
dist/js/datepicker.js
vendored
93
dist/js/datepicker.js
vendored
@ -64,6 +64,8 @@ var Datepicker;
|
|||||||
|
|
||||||
// timepicker
|
// timepicker
|
||||||
timepicker: false,
|
timepicker: false,
|
||||||
|
dateTimeSeparator: ' ',
|
||||||
|
timeFormat: '',
|
||||||
minHours: 0,
|
minHours: 0,
|
||||||
minMinutes: 0,
|
minMinutes: 0,
|
||||||
maxHours: 24,
|
maxHours: 24,
|
||||||
@ -225,9 +227,21 @@ var Datepicker;
|
|||||||
this.loc.dateFormat = this.opts.dateFormat
|
this.loc.dateFormat = this.opts.dateFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.opts.timeFormat) {
|
||||||
|
this.loc.timeFormat = this.opts.timeFormat
|
||||||
|
}
|
||||||
|
|
||||||
if (this.opts.firstDay !== '') {
|
if (this.opts.firstDay !== '') {
|
||||||
this.loc.firstDay = this.opts.firstDay
|
this.loc.firstDay = this.opts.firstDay
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.opts.timepicker) {
|
||||||
|
this.loc.dateFormat = [this.loc.dateFormat, this.loc.timeFormat].join(this.opts.dateTimeSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.loc.timeFormat.match(this._getWordBoundaryRegExp('a'))) {
|
||||||
|
this.ampm = true;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_buildDatepickersContainer: function () {
|
_buildDatepickersContainer: function () {
|
||||||
@ -324,12 +338,26 @@ var Datepicker;
|
|||||||
var result = string,
|
var result = string,
|
||||||
boundary = this._getWordBoundaryRegExp,
|
boundary = this._getWordBoundaryRegExp,
|
||||||
locale = this.loc,
|
locale = this.loc,
|
||||||
|
leadingZero = datepicker.getLeadingZeroNum,
|
||||||
decade = datepicker.getDecade(date),
|
decade = datepicker.getDecade(date),
|
||||||
d = datepicker.getParsedDate(date);
|
d = datepicker.getParsedDate(date),
|
||||||
|
fullHours = d.fullHours,
|
||||||
|
hours = d.hours,
|
||||||
|
dayPeriod = 'am',
|
||||||
|
validHours;
|
||||||
|
|
||||||
|
if (this.opts.timepicker && this.timepicker && this.ampm) {
|
||||||
|
validHours = this.timepicker._getValidHoursFromDate(date);
|
||||||
|
fullHours = leadingZero(validHours.hours);
|
||||||
|
hours = validHours.hours;
|
||||||
|
dayPeriod = validHours.dayPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case /@/.test(result):
|
case /@/.test(result):
|
||||||
result = result.replace(/@/, date.getTime());
|
result = result.replace(/@/, date.getTime());
|
||||||
|
case /a/.test(result):
|
||||||
|
result = result.replace(boundary('a'), dayPeriod);
|
||||||
case /dd/.test(result):
|
case /dd/.test(result):
|
||||||
result = result.replace(boundary('dd'), d.fullDate);
|
result = result.replace(boundary('dd'), d.fullDate);
|
||||||
case /d/.test(result):
|
case /d/.test(result):
|
||||||
@ -351,9 +379,9 @@ var Datepicker;
|
|||||||
case /i/.test(result):
|
case /i/.test(result):
|
||||||
result = result.replace(/\bi(?!>)\b/, d.minutes);
|
result = result.replace(/\bi(?!>)\b/, d.minutes);
|
||||||
case /hh/.test(result):
|
case /hh/.test(result):
|
||||||
result = result.replace(/\bhh\b/, d.fullHours);
|
result = result.replace(/\bhh\b/, fullHours);
|
||||||
case /h/.test(result):
|
case /h/.test(result):
|
||||||
result = result.replace(/\bh\b/, d.hours);
|
result = result.replace(/\bh\b/, hours);
|
||||||
case /yyyy/.test(result):
|
case /yyyy/.test(result):
|
||||||
result = result.replace(boundary('yyyy'), d.year);
|
result = result.replace(boundary('yyyy'), d.year);
|
||||||
case /yyyy1/.test(result):
|
case /yyyy1/.test(result):
|
||||||
@ -1283,6 +1311,7 @@ var Datepicker;
|
|||||||
today: 'Сегодня',
|
today: 'Сегодня',
|
||||||
clear: 'Очистить',
|
clear: 'Очистить',
|
||||||
dateFormat: 'dd.mm.yyyy',
|
dateFormat: 'dd.mm.yyyy',
|
||||||
|
timeFormat: 'hh:ii',
|
||||||
firstDay: 1
|
firstDay: 1
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1877,8 +1906,8 @@ var Datepicker;
|
|||||||
},
|
},
|
||||||
|
|
||||||
_updateCurrentTime: function () {
|
_updateCurrentTime: function () {
|
||||||
var h = this.hours < 10 ? '0' + this.hours : this.hours,
|
var h = datepicker.getLeadingZeroNum(this.displayHours),
|
||||||
m = this.minutes < 10 ? '0' + this.minutes : this.minutes;
|
m = datepicker.getLeadingZeroNum(this.minutes);
|
||||||
|
|
||||||
this.$hoursText.html(h);
|
this.$hoursText.html(h);
|
||||||
this.$minutesText.html(m)
|
this.$minutesText.html(m)
|
||||||
@ -1918,6 +1947,47 @@ var Datepicker;
|
|||||||
this._validateHoursMinutes();
|
this._validateHoursMinutes();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates valid hour value to display in text input and datepicker's body.
|
||||||
|
* @param date {Date|Number} - date or hours
|
||||||
|
* @returns {{hours: *, dayPeriod: string}}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_getValidHoursFromDate: function (date) {
|
||||||
|
var d = date,
|
||||||
|
hours = date;
|
||||||
|
|
||||||
|
if (date instanceof Date) {
|
||||||
|
d = datepicker.getParsedDate(date);
|
||||||
|
hours = d.hours;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ampm = this.d.ampm,
|
||||||
|
dayPeriod = 'am';
|
||||||
|
|
||||||
|
if (ampm) {
|
||||||
|
switch(true) {
|
||||||
|
case hours == 0:
|
||||||
|
hours = 12;
|
||||||
|
break;
|
||||||
|
case hours == 12:
|
||||||
|
dayPeriod = 'pm';
|
||||||
|
break;
|
||||||
|
case hours > 11:
|
||||||
|
hours = hours - 12;
|
||||||
|
dayPeriod = 'pm';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
hours: hours,
|
||||||
|
dayPeriod: dayPeriod
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
@ -1934,6 +2004,19 @@ var Datepicker;
|
|||||||
this._handleDate(data);
|
this._handleDate(data);
|
||||||
this._updateRanges();
|
this._updateRanges();
|
||||||
this._updateCurrentTime();
|
this._updateCurrentTime();
|
||||||
|
},
|
||||||
|
|
||||||
|
set hours (val) {
|
||||||
|
this._hours = val;
|
||||||
|
|
||||||
|
var displayHours = this._getValidHoursFromDate(val);
|
||||||
|
|
||||||
|
this.displayHours = displayHours.hours;
|
||||||
|
this.dayPeriod = displayHours.dayPeriod;
|
||||||
|
},
|
||||||
|
|
||||||
|
get hours() {
|
||||||
|
return this._hours;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})(window, jQuery, Datepicker);
|
})(window, jQuery, Datepicker);
|
||||||
|
|||||||
3
dist/js/datepicker.min.js
vendored
3
dist/js/datepicker.min.js
vendored
File diff suppressed because one or more lines are too long
@ -64,6 +64,8 @@ var Datepicker;
|
|||||||
|
|
||||||
// timepicker
|
// timepicker
|
||||||
timepicker: false,
|
timepicker: false,
|
||||||
|
dateTimeSeparator: ' ',
|
||||||
|
timeFormat: '',
|
||||||
minHours: 0,
|
minHours: 0,
|
||||||
minMinutes: 0,
|
minMinutes: 0,
|
||||||
maxHours: 24,
|
maxHours: 24,
|
||||||
@ -225,9 +227,21 @@ var Datepicker;
|
|||||||
this.loc.dateFormat = this.opts.dateFormat
|
this.loc.dateFormat = this.opts.dateFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.opts.timeFormat) {
|
||||||
|
this.loc.timeFormat = this.opts.timeFormat
|
||||||
|
}
|
||||||
|
|
||||||
if (this.opts.firstDay !== '') {
|
if (this.opts.firstDay !== '') {
|
||||||
this.loc.firstDay = this.opts.firstDay
|
this.loc.firstDay = this.opts.firstDay
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.opts.timepicker) {
|
||||||
|
this.loc.dateFormat = [this.loc.dateFormat, this.loc.timeFormat].join(this.opts.dateTimeSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.loc.timeFormat.match(this._getWordBoundaryRegExp('a'))) {
|
||||||
|
this.ampm = true;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_buildDatepickersContainer: function () {
|
_buildDatepickersContainer: function () {
|
||||||
@ -324,12 +338,26 @@ var Datepicker;
|
|||||||
var result = string,
|
var result = string,
|
||||||
boundary = this._getWordBoundaryRegExp,
|
boundary = this._getWordBoundaryRegExp,
|
||||||
locale = this.loc,
|
locale = this.loc,
|
||||||
|
leadingZero = datepicker.getLeadingZeroNum,
|
||||||
decade = datepicker.getDecade(date),
|
decade = datepicker.getDecade(date),
|
||||||
d = datepicker.getParsedDate(date);
|
d = datepicker.getParsedDate(date),
|
||||||
|
fullHours = d.fullHours,
|
||||||
|
hours = d.hours,
|
||||||
|
dayPeriod = 'am',
|
||||||
|
validHours;
|
||||||
|
|
||||||
|
if (this.opts.timepicker && this.timepicker && this.ampm) {
|
||||||
|
validHours = this.timepicker._getValidHoursFromDate(date);
|
||||||
|
fullHours = leadingZero(validHours.hours);
|
||||||
|
hours = validHours.hours;
|
||||||
|
dayPeriod = validHours.dayPeriod;
|
||||||
|
}
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case /@/.test(result):
|
case /@/.test(result):
|
||||||
result = result.replace(/@/, date.getTime());
|
result = result.replace(/@/, date.getTime());
|
||||||
|
case /a/.test(result):
|
||||||
|
result = result.replace(boundary('a'), dayPeriod);
|
||||||
case /dd/.test(result):
|
case /dd/.test(result):
|
||||||
result = result.replace(boundary('dd'), d.fullDate);
|
result = result.replace(boundary('dd'), d.fullDate);
|
||||||
case /d/.test(result):
|
case /d/.test(result):
|
||||||
@ -351,9 +379,9 @@ var Datepicker;
|
|||||||
case /i/.test(result):
|
case /i/.test(result):
|
||||||
result = result.replace(/\bi(?!>)\b/, d.minutes);
|
result = result.replace(/\bi(?!>)\b/, d.minutes);
|
||||||
case /hh/.test(result):
|
case /hh/.test(result):
|
||||||
result = result.replace(/\bhh\b/, d.fullHours);
|
result = result.replace(/\bhh\b/, fullHours);
|
||||||
case /h/.test(result):
|
case /h/.test(result):
|
||||||
result = result.replace(/\bh\b/, d.hours);
|
result = result.replace(/\bh\b/, hours);
|
||||||
case /yyyy/.test(result):
|
case /yyyy/.test(result):
|
||||||
result = result.replace(boundary('yyyy'), d.year);
|
result = result.replace(boundary('yyyy'), d.year);
|
||||||
case /yyyy1/.test(result):
|
case /yyyy1/.test(result):
|
||||||
@ -1283,6 +1311,7 @@ var Datepicker;
|
|||||||
today: 'Сегодня',
|
today: 'Сегодня',
|
||||||
clear: 'Очистить',
|
clear: 'Очистить',
|
||||||
dateFormat: 'dd.mm.yyyy',
|
dateFormat: 'dd.mm.yyyy',
|
||||||
|
timeFormat: 'hh:ii',
|
||||||
firstDay: 1
|
firstDay: 1
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -119,8 +119,8 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
_updateCurrentTime: function () {
|
_updateCurrentTime: function () {
|
||||||
var h = this.hours < 10 ? '0' + this.hours : this.hours,
|
var h = datepicker.getLeadingZeroNum(this.displayHours),
|
||||||
m = this.minutes < 10 ? '0' + this.minutes : this.minutes;
|
m = datepicker.getLeadingZeroNum(this.minutes);
|
||||||
|
|
||||||
this.$hoursText.html(h);
|
this.$hoursText.html(h);
|
||||||
this.$minutesText.html(m)
|
this.$minutesText.html(m)
|
||||||
@ -160,6 +160,47 @@
|
|||||||
this._validateHoursMinutes();
|
this._validateHoursMinutes();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates valid hour value to display in text input and datepicker's body.
|
||||||
|
* @param date {Date|Number} - date or hours
|
||||||
|
* @returns {{hours: *, dayPeriod: string}}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_getValidHoursFromDate: function (date) {
|
||||||
|
var d = date,
|
||||||
|
hours = date;
|
||||||
|
|
||||||
|
if (date instanceof Date) {
|
||||||
|
d = datepicker.getParsedDate(date);
|
||||||
|
hours = d.hours;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ampm = this.d.ampm,
|
||||||
|
dayPeriod = 'am';
|
||||||
|
|
||||||
|
if (ampm) {
|
||||||
|
switch(true) {
|
||||||
|
case hours == 0:
|
||||||
|
hours = 12;
|
||||||
|
break;
|
||||||
|
case hours == 12:
|
||||||
|
dayPeriod = 'pm';
|
||||||
|
break;
|
||||||
|
case hours > 11:
|
||||||
|
hours = hours - 12;
|
||||||
|
dayPeriod = 'pm';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
hours: hours,
|
||||||
|
dayPeriod: dayPeriod
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
|
|
||||||
@ -176,6 +217,19 @@
|
|||||||
this._handleDate(data);
|
this._handleDate(data);
|
||||||
this._updateRanges();
|
this._updateRanges();
|
||||||
this._updateCurrentTime();
|
this._updateCurrentTime();
|
||||||
|
},
|
||||||
|
|
||||||
|
set hours (val) {
|
||||||
|
this._hours = val;
|
||||||
|
|
||||||
|
var displayHours = this._getValidHoursFromDate(val);
|
||||||
|
|
||||||
|
this.displayHours = displayHours.hours;
|
||||||
|
this.dayPeriod = displayHours.dayPeriod;
|
||||||
|
},
|
||||||
|
|
||||||
|
get hours() {
|
||||||
|
return this._hours;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})(window, jQuery, Datepicker);
|
})(window, jQuery, Datepicker);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user