-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathpower-calendar.js
127 lines (114 loc) · 3.32 KB
/
power-calendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { layout, tagName } from "@ember-decorators/component";
import Component from '@ember/component';
import { computed, action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import templateLayout from '../templates/components/power-calendar';
import { assert } from '@ember/debug';
import {
add,
normalizeDate,
normalizeCalendarValue
} from 'ember-power-calendar-utils';
export default @layout(templateLayout) @tagName('') class extends Component {
@service('power-calendar') powerCalendarService
navComponent = 'power-calendar/nav'
daysComponent = 'power-calendar/days'
center = null
_calendarType = 'single'
// Lifecycle hooks
init() {
super.init(...arguments);
this.registerCalendar();
if (this.onInit) {
this.onInit(this.publicAPI);
}
}
willDestroy() {
super.willDestroy(...arguments);
this.unregisterCalendar();
}
// CPs
@computed('onSelect', 'onCenterChange')
get publicActions() {
let actions = {};
if (this.onSelect) {
actions.select = (...args) => this.select(...args)
}
if (this.onCenterChange) {
let changeCenter = (newCenter, calendar, e) => {
return this.changeCenterTask.perform(newCenter, calendar, e);
};
actions.changeCenter = changeCenter;
actions.moveCenter = (step, unit, calendar, e) => {
let newCenter = add(this.currentCenter, step, unit);
return changeCenter(newCenter, calendar, e);
};
}
return actions;
}
@computed
get selected() {
return undefined;
}
set selected(v) {
return normalizeDate(v);
}
@computed('center')
get currentCenter() {
let center = this.center;
if (!center) {
center = this.selected || this.powerCalendarService.getDate()
}
return normalizeDate(center);
}
@computed('_publicAPI')
get publicAPI() {
return this._publicAPI;
}
@computed('selected', 'currentCenter', 'locale', 'powerCalendarService.locale', 'changeCenterTask.isRunning', 'publicActions')
get _publicAPI() {
return {
uniqueId: guidFor(this),
type: this._calendarType,
selected: this.selected,
loading: this.changeCenterTask.isRunning,
center: this.currentCenter,
locale: this.locale || this.powerCalendarService.locale,
actions: this.publicActions
};
}
@computed('tag')
get tagWithDefault() {
if (this.tag === undefined || this.tag === null) {
return 'div';
}
return this.tag;
}
// Actions
@action
select(day, calendar, e) {
if (this.onSelect) {
this.onSelect(day, calendar, e);
}
}
// Tasks
@(task(function* (newCenter, calendar, e) {
assert('You attempted to move the center of a calendar that doesn\'t receive an `@onCenterChange` action.', typeof this.onCenterChange === 'function');
let value = normalizeCalendarValue({ date: newCenter });
yield this.onCenterChange(value, calendar, e);
})) changeCenterTask
// Methods
registerCalendar() {
if (window) {
window.__powerCalendars = window.__powerCalendars || {}; // TODO: weakmap??
window.__powerCalendars[this.publicAPI.uniqueId] = this;
}
}
unregisterCalendar() {
if (window) {
delete window.__powerCalendars[guidFor(this)];
}
}
}