Add ignoreTimezone option to datepicker field and column types.

Adds the ability to specify an `ignoreTimezone: true` option on the config for a datepicker form widget or date column type. This will ignore both October's and the backend user's timezone settings to display the date exactly as it is stored.
This commit is contained in:
Luke Towers 2017-09-10 22:39:15 -06:00
parent 50d6db7d1c
commit 9565565865
8 changed files with 74 additions and 15 deletions

View File

@ -1167,6 +1167,8 @@ if(this.options.formatAlias){this.options.format=this.getFormatFromAlias(this.op
this.appTimezone=$('meta[name="app-timezone"]').attr('content')
if(!this.appTimezone){this.appTimezone='UTC'}}
DateTimeConverter.prototype.getDateTimeValue=function(){this.datetime=this.$el.attr('datetime')
if(this.$el.get(0).hasAttribute('data-ignore-timezone')){this.appTimezone='UTC'
this.options.timezone='UTC'}
var momentObj=moment.tz(this.datetime,this.appTimezone),result
if(this.options.locale){momentObj=momentObj.locale(this.options.locale)}
if(this.options.timezone){momentObj=momentObj.tz(this.options.timezone)}

View File

@ -43,7 +43,6 @@
DateTimeConverter.prototype.constructor = DateTimeConverter
DateTimeConverter.prototype.init = function() {
this.initDefaults()
this.$el.text(this.getDateTimeValue())
@ -77,6 +76,11 @@
DateTimeConverter.prototype.getDateTimeValue = function() {
this.datetime = this.$el.attr('datetime')
if (this.$el.get(0).hasAttribute('data-ignore-timezone')) {
this.appTimezone = 'UTC'
this.options.timezone = 'UTC'
}
var momentObj = moment.tz(this.datetime, this.appTimezone),
result

View File

@ -52,6 +52,11 @@ class DatePicker extends FormWidgetBase
*/
public $firstDay = 0;
/**
* @var bool change datetime exactly as is in database
*/
public $ignoreTimezone = false;
//
// Object properties
//
@ -73,6 +78,7 @@ class DatePicker extends FormWidgetBase
'maxDate',
'yearRange',
'firstDay',
'ignoreTimezone',
]);
$this->mode = strtolower($this->mode);
@ -120,6 +126,7 @@ class DatePicker extends FormWidgetBase
$this->vars['maxDate'] = $this->maxDate;
$this->vars['yearRange'] = $this->yearRange;
$this->vars['firstDay'] = $this->firstDay;
$this->vars['ignoreTimezone'] = $this->ignoreTimezone;
$this->vars['format'] = $this->format;
$this->vars['formatMoment'] = $this->getDateFormatMoment();
$this->vars['formatAlias'] = $this->getDateFormatAlias();

View File

@ -15,6 +15,7 @@
<?php if ($maxDate): ?>data-max-date="<?= $maxDate ?>"<?php endif ?>
<?php if ($yearRange): ?>data-year-range="<?= $yearRange ?>"<?php endif ?>
<?php if ($firstDay): ?>data-first-day="<?= $firstDay ?>"<?php endif ?>
<?php if ($ignoreTimezone): ?>data-ignore-timezone<?php endif ?>
>
<?php if ($mode == 'date'): ?>

View File

@ -116,6 +116,7 @@ class Backend
'jsFormat' => null,
'timeTense' => false,
'timeSince' => false,
'ignoreTimezone' => false,
], $options));
$carbon = DateTimeHelper::makeCarbon($dateTime);
@ -132,6 +133,10 @@ class Backend
'data-datetime-control' => 1,
];
if ($ignoreTimezone) {
$attributes['data-ignore-timezone'] = true;
}
if ($timeTense) {
$attributes['data-time-tense'] = 1;
}

View File

@ -1006,11 +1006,17 @@ class Lists extends WidgetBase
$value = $dateTime->toDayDateTimeString();
}
return Backend::dateTime($dateTime, [
$options = [
'defaultValue' => $value,
'format' => $column->format,
'formatAlias' => 'dateTimeLongMin'
]);
];
if (!empty($column->config['ignoreTimezone'])) {
$options['ignoreTimezone'] = true;
}
return Backend::dateTime($dateTime, $options);
}
/**
@ -1028,11 +1034,17 @@ class Lists extends WidgetBase
$value = $dateTime->format($format);
return Backend::dateTime($dateTime, [
$options = [
'defaultValue' => $value,
'format' => $column->format,
'formatAlias' => 'time'
]);
];
if (!empty($column->config['ignoreTimezone'])) {
$options['ignoreTimezone'] = true;
}
return Backend::dateTime($dateTime, $options);
}
/**
@ -1053,11 +1065,17 @@ class Lists extends WidgetBase
$value = $dateTime->toFormattedDateString();
}
return Backend::dateTime($dateTime, [
$options = [
'defaultValue' => $value,
'format' => $column->format,
'formatAlias' => 'dateLongMin'
]);
];
if (!empty($column->config['ignoreTimezone'])) {
$options['ignoreTimezone'] = true;
}
return Backend::dateTime($dateTime, $options);
}
/**
@ -1073,10 +1091,16 @@ class Lists extends WidgetBase
$value = DateTimeHelper::timeSince($dateTime);
return Backend::dateTime($dateTime, [
$options = [
'defaultValue' => $value,
'timeSince' => true
]);
];
if (!empty($column->config['ignoreTimezone'])) {
$options['ignoreTimezone'] = true;
}
return Backend::dateTime($dateTime, $options);
}
/**
@ -1092,10 +1116,16 @@ class Lists extends WidgetBase
$value = DateTimeHelper::timeTense($dateTime);
return Backend::dateTime($dateTime, [
$options = [
'defaultValue' => $value,
'timeTense' => true
]);
];
if (!empty($column->config['ignoreTimezone'])) {
$options['ignoreTimezone'] = true;
}
return Backend::dateTime($dateTime, $options);
}
/**

View File

@ -46,6 +46,7 @@
this.$timePicker = $('[data-timepicker]', this.$el)
this.hasDate = !!this.$datePicker.length
this.hasTime = !!this.$timePicker.length
this.ignoreTimezone = this.$el.get(0).hasAttribute('data-ignore-timezone')
this.initRegion()
@ -292,6 +293,12 @@
if (!this.timezone) {
this.timezone = 'UTC'
}
// Set both timezones to UTC to disable converting between them
if (this.ignoreTimezone) {
this.appTimezone = 'UTC'
this.timezone = 'UTC'
}
}
DatePicker.prototype.getLang = function(name, defaultValue) {

View File

@ -2861,6 +2861,7 @@ this.$datePicker=$('[data-datepicker]',this.$el)
this.$timePicker=$('[data-timepicker]',this.$el)
this.hasDate=!!this.$datePicker.length
this.hasTime=!!this.$timePicker.length
this.ignoreTimezone=this.$el.get(0).hasAttribute('data-ignore-timezone')
this.initRegion()
if(this.hasDate){this.initDatePicker()}
if(this.hasTime){this.initTimePicker()}
@ -2923,7 +2924,9 @@ DatePicker.prototype.initRegion=function(){this.locale=$('meta[name="backend-loc
this.timezone=$('meta[name="backend-timezone"]').attr('content')
this.appTimezone=$('meta[name="app-timezone"]').attr('content')
if(!this.appTimezone){this.appTimezone='UTC'}
if(!this.timezone){this.timezone='UTC'}}
if(!this.timezone){this.timezone='UTC'}
if(this.ignoreTimezone){this.appTimezone='UTC'
this.timezone='UTC'}}
DatePicker.prototype.getLang=function(name,defaultValue){if($.oc===undefined||$.oc.lang===undefined){return defaultValue}
return $.oc.lang.get(name,defaultValue)}
DatePicker.DEFAULTS={minDate:null,maxDate:null,format:null,yearRange:10,firstDay:0}
@ -2989,7 +2992,7 @@ FilterWidget.prototype.getPopoverTemplate=function(){return'
type="text" \
name="search" \
autocomplete="off" \
class="filter-search-input form-control icon search" \
class="filter-search-input form-control icon search popup-allow-focus" \
data-request="{{ optionsHandler }}" \
data-load-indicator-opaque \
data-load-indicator \
@ -3168,7 +3171,7 @@ FilterWidget.prototype.getPopoverDateTemplate=function(){return'
type="text" \
name="date" \
value="{{ date }}" \
class="form-control align-right" \
class="form-control align-right popup-allow-focus" \
autocomplete="off" \
placeholder="{{ date_placeholder }}" /> \
</div> \
@ -3194,7 +3197,7 @@ FilterWidget.prototype.getPopoverRangeTemplate=function(){return'
type="text" \
name="date" \
value="{{ date }}" \
class="form-control align-right" \
class="form-control align-right popup-allow-focus" \
autocomplete="off" \
placeholder="{{ after_placeholder }}" /> \
</div> \