Skip to content

Commit

Permalink
Merge 3a2f2b5 into 4f3a616
Browse files Browse the repository at this point in the history
  • Loading branch information
anhuynh committed Jul 30, 2018
2 parents 4f3a616 + 3a2f2b5 commit 32f578b
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 31 deletions.
8 changes: 7 additions & 1 deletion addon/components/inputs/abstract-input.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getCellDefaults, utils} from 'bunsen-core'
const {getLabel, parseVariables} = utils
import Ember from 'ember'
const {Component, Logger, get, merge, typeOf} = Ember
const {Component, Logger, get, getWithDefault, merge, typeOf} = Ember
import computed, {readOnly} from 'ember-computed-decorators'
import {HookMixin} from 'ember-hook'
import PropTypeMixin, {PropTypes} from 'ember-prop-types'
Expand Down Expand Up @@ -156,6 +156,12 @@ export default Component.extend(HookMixin, PropTypeMixin, {
return required && valueEmpty
},

@readOnly
@computed('cellConfig')
alwaysShowRequiredLabel (cellConfig) {
return getWithDefault(cellConfig, 'renderer.alwaysShowRequiredLabel', false)
},

// == Functions ==============================================================

/**
Expand Down
57 changes: 52 additions & 5 deletions addon/components/inputs/datetime.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Ember from 'ember'
import computed, {readOnly} from 'ember-computed-decorators'
import moment from 'moment'

import AbstractInput from './abstract-input'
import layout from 'ember-frost-bunsen/templates/components/frost-bunsen-input-datetime'
import {DEFAULT_TIMEZONE, getFormattedTime, getMomentTimezone} from 'ember-frost-bunsen/timezone-utils'

const {getWithDefault} = Ember
const DATE_FORMAT = 'YYYY-MM-DD'
const TIME_FORMAT = 'HH:mm:ss'
const DATE_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ'
Expand All @@ -19,9 +22,16 @@ export default AbstractInput.extend({
],

layout,
DEFAULT_TIMEZONE,

// == Computed Properties ====================================================

@readOnly
@computed('cellConfig')
timezone (cellConfig) {
return getWithDefault(cellConfig, 'renderer.options.timezone', '')
},

@readOnly
@computed('cellConfig.renderer.options.defaultToCurrentDateTime')
defaultToCurrentDateTime (defaultToCurrentDateTime = true) {
Expand All @@ -43,12 +53,14 @@ export default AbstractInput.extend({
},

@readOnly
@computed('defaultToCurrentDateTime', 'value')
currentValue (defaultToCurrentDateTime, value) {
if (!defaultToCurrentDateTime || value) {
return value
@computed('defaultToCurrentDateTime', 'timezone', 'storedDateTimeValue')
displayStoredDateTimeValue (defaultToCurrentDateTime, timezone, storedDateTimeValue) {
if (!defaultToCurrentDateTime) {
return storedDateTimeValue
}
return moment().format(DATE_TIME_FORMAT)

// need to get rid of timezone offset so the time picker component doesn't alter the time
return getMomentTimezone(storedDateTimeValue, timezone).format('YYYY-MM-DDTHH:mm:ss')
},

// == Functions ==============================================================
Expand All @@ -57,8 +69,43 @@ export default AbstractInput.extend({
return moment(value).format(DATE_TIME_FORMAT)
},

// == Lifecycle Hooks =======================================================

init () {
this._super(...arguments)

const value = this.get('value')
const timezone = this.get('timezone')
let currentDateTime = getMomentTimezone(value, timezone)

if (!this.get('defaultToCurrentDateTime')) {
currentDateTime = value
} else {
currentDateTime = getFormattedTime(currentDateTime, this.get('dateTimeFormat'), timezone)
}

this.setProperties({
storedDateTimeValue: currentDateTime,
localTimezone: moment().format('Z')
})
},

// == Actions ===============================================================

actions: {
/**
* Sets the user's selected date and time to the bunsen model and stores it in the
* case that the user selects away from the date-time picker radio button but then returns
* @param {Object} value - the users selected date/time moment() object
* @returns {undefined}
*/
selectDate (value) {
const timezone = this.get('timezone')
const datetime = getMomentTimezone(value, timezone, true)
const formattedTime = getFormattedTime(datetime, this.get('dateTimeFormat'), timezone)

this.set('storedDateTimeValue', formattedTime)
this.onChange(this.get('bunsenId'), formattedTime)
}
}
})
39 changes: 29 additions & 10 deletions addon/components/inputs/when.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Ember from 'ember'
const {$, get, guidFor, isPresent, run} = Ember
const {$, get, getWithDefault, guidFor, isPresent, run} = Ember
import computed, {readOnly} from 'ember-computed-decorators'
import {PropTypes} from 'ember-prop-types'
import moment from 'moment'

import AbstractInput from './abstract-input'
import layout from 'ember-frost-bunsen/templates/components/frost-bunsen-input-when'
import {DEFAULT_TIMEZONE, getFormattedTime, getMomentTimezone} from 'ember-frost-bunsen/timezone-utils'

export const DATE_VALUE = 'DATE'

Expand All @@ -20,6 +21,7 @@ export default AbstractInput.extend({
],

layout,
DEFAULT_TIMEZONE,

propTypes: {
// private
Expand Down Expand Up @@ -106,6 +108,19 @@ export default AbstractInput.extend({
return get(cellConfig, 'renderer.size') || 'small'
},

@readOnly
@computed('cellConfig')
timezone (cellConfig) {
return getWithDefault(cellConfig, 'renderer.timezone', '')
},

@readOnly
@computed('storedDateTimeValue', 'timezone')
displayStoredDateTimeValue (storedDateTimeValue, timezone) {
// need to get rid of timezone offset so the time picker component doesn't alter the time
return getMomentTimezone(storedDateTimeValue, timezone).format('YYYY-MM-DDTHH:mm:ss')
},

// == Functions ==============================================================

/**
Expand All @@ -118,24 +133,26 @@ export default AbstractInput.extend({

const firstButtonValue = get(this, 'cellConfig.renderer.value')
const value = this.get('value')
const timezone = this.get('timezone')

let selectedValue = firstButtonValue
let currentDateTime = moment()
let currentDateTime = getMomentTimezone(undefined, timezone)

if (isPresent(value) && value !== firstButtonValue) {
selectedValue = DATE_VALUE
currentDateTime = value
currentDateTime = getMomentTimezone(value, timezone)
}

const date = moment(currentDateTime).format(this.get('dateFormat'))
const time = moment(currentDateTime).format(this.get('timeFormat'))
const date = currentDateTime.format(this.get('dateFormat'))
const time = currentDateTime.format(this.get('timeFormat'))

this.setProperties({
date,
firstButtonValue,
selectedValue,
storedDateTimeValue: moment(currentDateTime).format(this.get('dateTimeFormat')),
time
storedDateTimeValue: getFormattedTime(currentDateTime, this.get('dateTimeFormat'), timezone),
time,
localTimezone: moment().format('Z')
})

if (!isPresent(value)) {
Expand Down Expand Up @@ -212,10 +229,12 @@ export default AbstractInput.extend({
* @returns {undefined}
*/
selectDate (value) {
const datetime = moment(value).format(this.get('dateTimeFormat'))
const timezone = this.get('timezone')
const datetime = getMomentTimezone(value, timezone, true)
const formattedTime = getFormattedTime(datetime, this.get('dateTimeFormat'), timezone)

this.set('storedDateTimeValue', datetime)
this.onChange(this.get('bunsenId'), datetime)
this.set('storedDateTimeValue', formattedTime)
this.onChange(this.get('bunsenId'), formattedTime)
}
}
})
14 changes: 11 additions & 3 deletions addon/templates/components/frost-bunsen-input-datetime.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if (not cellConfig.hideLabel)}}
<label class={{labelWrapperClassName}}>
{{renderLabel}}
{{#if showRequiredLabel}}
{{#if (or showRequiredLabel alwaysShowRequiredLabel)}}
<div class='frost-bunsen-required'>Required</div>
{{/if}}
</label>
Expand All @@ -12,16 +12,24 @@
<div class={{inputWrapperClassName}}>
{{frost-date-time-picker
hook=(concat hook '-datetimePicker')
value=(readonly currentValue)
value=(readonly displayStoredDateTimeValue)
defaultDate=(readonly date)
defaultTime=(readonly time)
onFocusIn=(action 'hideErrorMessage')
onFocusOut=(action 'showErrorMessage')
onChange=(action 'handleChange')
onChange=(action 'selectDate')
options=cellConfig.renderer.options
}}
</div>
{{/if}}
{{#if timezone}}
<div class="frost-bunsen-cell timezone-container">
<div class="frost-bunsen-left-label timezone-label">Timezone</div>
<div class='timezone-value'>
{{if (eq timezone DEFAULT_TIMEZONE) localTimezone timezone}}
</div>
</div>
{{/if}}
{{#if renderErrorMessage}}
<div class="frost-bunsen-error">
{{renderErrorMessage}}
Expand Down
34 changes: 22 additions & 12 deletions addon/templates/components/frost-bunsen-input-when.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@
size=size
value=dateValue
}}
{{frost-date-time-picker
id=dateId
hook=(concat hook '-radio-button')
dateFormat=dateFormat
timeFormat=timeFormat
defaultDate=(readonly date)
defaultTime=(readonly time)
onFocusIn=(action 'hideErrorMessage')
onFocusOut=(action 'showErrorMessage')
onChange=(action 'selectDate')
value=storedDateTimeValue
}}
<div class='date-time-label'>
{{frost-date-time-picker
id=dateId
hook=(concat hook '-radio-button')
dateFormat=dateFormat
timeFormat=timeFormat
defaultDate=(readonly date)
defaultTime=(readonly time)
onFocusIn=(action 'hideErrorMessage')
onFocusOut=(action 'showErrorMessage')
onChange=(action 'selectDate')
value=displayStoredDateTimeValue
}}
</div>
{{/controls.button}}
{{#if timezone}}
<div class="frost-bunsen-cell timezone-container">
<div class="frost-bunsen-left-label timezone-label">Timezone</div>
<div class='timezone-value'>
{{if (eq timezone DEFAULT_TIMEZONE) localTimezone timezone}}
</div>
</div>
{{/if}}
{{/frost-radio-group}}
{{#if renderErrorMessage}}
<div class="frost-bunsen-error">
Expand Down
18 changes: 18 additions & 0 deletions addon/timezone-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Ember from 'ember'
import moment from 'moment'
const {isBlank} = Ember

export const DEFAULT_TIMEZONE = 'local'
const DATE_TIME_TIMEZONE_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ'

export function getMomentTimezone (time, timezone, keepLocalTime) {
if (isBlank(timezone) || timezone === DEFAULT_TIMEZONE) {
return moment(time).local()
}

return moment(time).utcOffset(timezone, keepLocalTime)
}

export function getFormattedTime (time, dateTimeFormat, timezone) {
return isBlank(timezone) ? time.format(dateTimeFormat) : time.format(DATE_TIME_TIMEZONE_FORMAT)
}
9 changes: 9 additions & 0 deletions app/styles/ember-frost-bunsen/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ form > div > .frost-bunsen-section {
}
}

.frost-bunsen-input-when,
.frost-bunsen-input-datetime {
.timezone-container {
display: flex;
justify-content: flex-end;
font-weight: 200;
}
}

.frost-bunsen-input-form {
.error,
.frost-bunsen-error {
Expand Down
20 changes: 20 additions & 0 deletions tests/dummy/app/pods/view/renderers/documentation/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,24 @@ By default, is no value is given to the renderer, it will display the current da
}
}
}
```

##### timezone

The `timezone` option can be used to add a timezone to the date.
By default, if no value is given to the renderer, it will use a local timezone. If the
`timezone` option is given, the timezone will be shown in the renderer and the value will use
that timezone.

```json
{
"label": "Bar",
"model": "foo",
"renderer": {
"name": "datetime",
"options": {
"timezone": "+08:00"
}
}
}
```
17 changes: 17 additions & 0 deletions tests/dummy/app/pods/view/renderers/documentation/when.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@ The value for the first radio button
}
}
```

#### renderer.timezone

The `timezone` option can be used to add a timezone to the date.
By default, if no value is given to the renderer, it will use a local timezone. If the
`timezone` option is given, the timezone will be shown in the renderer and the value will use
that timezone.

```json
{
"model": "foo",
"renderer": {
"name": "when",
"timezone": "+08:00"
}
}
```
Loading

0 comments on commit 32f578b

Please sign in to comment.