-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
beginning to port to a modifier-based implementation
- Loading branch information
Showing
9 changed files
with
974 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<input | ||
{{pikaday | ||
value=this.value | ||
onSelect=this.onSelect | ||
setDefaultDate=true | ||
defaultDate=@defaultDate | ||
onOpen=@onOpen | ||
onDraw=this.onDraw | ||
onClose=this.onClose | ||
format=this.format | ||
minDate=@minDate | ||
maxDate=@maxDate | ||
theme=@theme | ||
yearRange=this.yearRange | ||
i18n=this.i18n | ||
firstDay=this.firstDay | ||
}} | ||
{{on 'change' this.didChange}} | ||
type='text' | ||
readonly={{@readonly}} | ||
placeholder={{@placeholder}} | ||
required={{@required}} | ||
disabled={{@disabled}} | ||
autocomplete={{@autocomplete}} | ||
...attributes | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { momentOrMomentTimezone as moment } from '../find-moment'; | ||
|
||
export default class extends Component { | ||
constructor(owner, args) { | ||
super(owner, args); | ||
} | ||
|
||
get format() { | ||
return this.args.format || 'DD.MM.YYYY'; | ||
} | ||
|
||
get value() { | ||
if (this.args.useUTC && this.args.value) { | ||
let format = 'YYYY-MM-DD'; | ||
return moment( | ||
moment.utc(this.args.value).format(format), | ||
format | ||
).toDate(); | ||
} | ||
return this.args.value; | ||
} | ||
|
||
get yearRange() { | ||
const yearRange = this.args.yearRange; | ||
if (!yearRange) { | ||
return 10; | ||
} | ||
if (yearRange.indexOf(',') > -1) { | ||
const yearArray = yearRange.split(','); | ||
if (yearArray[1] === 'currentYear') { | ||
yearArray[1] = new Date().getFullYear(); | ||
} | ||
return yearArray; | ||
} else { | ||
return yearRange; | ||
} | ||
} | ||
|
||
get i18n() { | ||
let i18n = this.args.i18n; | ||
if (!i18n) { | ||
return undefined; | ||
} | ||
if (!i18n.t) { | ||
return i18n; | ||
} | ||
return { | ||
previousMonth: i18n.t('previousMonth').toString(), | ||
nextMonth: i18n.t('nextMonth').toString(), | ||
months: i18n.t('months').toString().split(','), | ||
weekdays: i18n.t('weekdays').toString().split(','), | ||
weekdaysShort: i18n.t('weekdaysShort').toString().split(','), | ||
}; | ||
} | ||
|
||
get firstDay() { | ||
return this.args.firstDay == null ? 1 : parseInt(this.args.firstDay, 10); | ||
} | ||
|
||
@action | ||
onClose() { | ||
if (!this.#heardValue) { | ||
this.onSelect(null); | ||
} | ||
this.args.onClose?.(); | ||
} | ||
|
||
#heardValue; | ||
|
||
@action | ||
onDraw() { | ||
// this is here because apparently the classic behavior is to pass no | ||
// arguments to the onDraw callback, but Pikaday's own ownDraw has an | ||
// argument. | ||
this.args.onDraw?.(); | ||
} | ||
|
||
@action | ||
didChange(event) { | ||
this.#heardValue = event.target.value; | ||
} | ||
|
||
@action | ||
onSelect(date) { | ||
if (this.args.useUTC && date) { | ||
date = moment | ||
.utc([date.getFullYear(), date.getMonth(), date.getDate()]) | ||
.toDate(); | ||
} | ||
this.args.onSelection?.(date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Modifier from 'ember-modifier'; | ||
import makePikaday from '../../vendor/pikaday'; | ||
import { momentOrMomentTimezone as moment } from '../find-moment'; | ||
const Pikaday = makePikaday(moment); | ||
|
||
export default class PikadayModifier extends Modifier { | ||
#pikaday; | ||
#observer; | ||
|
||
didInstall() { | ||
let opts = { | ||
field: this.element, | ||
...this.args.named, | ||
}; | ||
if (!opts.i18n) { | ||
delete opts.i18n; | ||
} | ||
this.#pikaday = new Pikaday(opts); | ||
this.#pikaday.setDate(this.args.named.value, true); | ||
this.#observer = new MutationObserver(this.sawMutation.bind(this)); | ||
this.#observer.observe(this.element, { attributes: true }); | ||
} | ||
|
||
didUpdateArguments() { | ||
this.#pikaday.setMinDate(this.args.named.minDate); | ||
this.#pikaday.setMaxDate(this.args.named.maxDate); | ||
this.#pikaday.setDate(this.args.named.value, true); | ||
} | ||
|
||
willDestroy() { | ||
this.#pikaday.destroy(); | ||
this.#observer.disconnect(); | ||
} | ||
|
||
sawMutation() { | ||
if (this.element.hasAttribute('disabled')) { | ||
this.#pikaday.hide(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.