-
-
+
@@ -81,8 +96,8 @@ class HaPanelCalendar extends LocalizeMixin(PolymerElement) {
@@ -90,99 +105,6 @@ class HaPanelCalendar extends LocalizeMixin(PolymerElement) {
`;
}
- connectedCallback() {
- super.connectedCallback();
- this._fetchData = this._fetchData.bind(this);
- // TODO implement calendar_updated event
- // this.hass.connection.subscribeEvents(this._fetchData, 'calendar_updated')
- // .then(function (unsub) { this._unsubEvents = unsub; }.bind(this));
- this._fetchCalendar();
- }
-
- _fetchCalendar() {
- // Fetch calendar list
- this.hass.callApi('get', 'calendars')
- .then((result) => {
- this.calendars = result;
- });
- }
-
- _fetchData() {
- // TODO: Improve me
- var start = dates.firstVisibleDay(this.currentDate).toISOString();
- var end = dates.lastVisibleDay(this.currentDate).toISOString();
- // var dates = this._getDateRange();
- // var start = dates[0];
- // var end = dates[1];
- // Fetch calendar list
- this._fetchCalendar();
- // Fetch events for selected calendar
- const params = encodeURI(`?start=${start}&end=${end}`);
- const calls = this.selectedCalendars.map(cal => this.hass.callApi('get', `calendar/${cal}${params}`));
- Promise.all(calls).then((results) => {
- var tmpEvents = [];
-
- results.forEach((res) => {
- res.forEach((ev) => {
- ev.start = new Date(ev.start);
- if (ev.end) {
- ev.end = new Date(ev.end);
- } else {
- ev.end = null;
- }
- tmpEvents.push(ev);
- });
- });
- this.events = tmpEvents;
- });
- }
-
- _getDateRange() {
- // TODO: Delete me
- var startDate;
- var endDate;
- if (this.currentView === 'day') {
- startDate = moment(this.currentDate).startOf('day');
- endDate = moment(this.currentDate).startOf('day');
- } else if (this.currentView === 'week') {
- startDate = moment(this.currentDate).startOf('isoWeek');
- endDate = moment(this.currentDate).endOf('isoWeek');
- } else if (this.currentView === 'month') {
- startDate = moment(this.currentDate).startOf('month').subtract(7, 'days');
- endDate = moment(this.currentDate).endOf('month').add(7, 'days');
- } else if (this.currentView === 'agenda') {
- startDate = moment(this.currentDate).startOf('day');
- endDate = moment(this.currentDate).endOf('day').add(1, 'month');
- }
- return [startDate.toISOString(), endDate.toISOString()];
- }
-
- handleView(ev) {
- // Calendar view changed
- this.currentView = ev.detail.viewName;
- this._fetchData();
- }
-
- handleNavigate(ev) {
- // Calendar date range changed
- this.currentDate = ev.detail.date;
- this.currentView = ev.detail.viewName;
- this._fetchData();
- }
-
- checkAll(ev) {
- // Check all calendars
- if (ev.target.checked) {
- const selectedIndex = this.selectedCalendars
- .map(x => this.calendars.map(y => y.entity_id).indexOf(x));
- for (let i = 0; i < this.calendars.length; i++) {
- if (selectedIndex.indexOf(i) === -1) {
- this.$.calendar_list.selectIndex(i);
- }
- }
- }
- }
-
static get properties() {
return {
hass: Object,
@@ -214,7 +136,7 @@ class HaPanelCalendar extends LocalizeMixin(PolymerElement) {
narrow: {
type: Boolean,
- value: false,
+ reflectToAttribute: true,
},
showMenu: {
@@ -224,6 +146,74 @@ class HaPanelCalendar extends LocalizeMixin(PolymerElement) {
};
}
+
+ connectedCallback() {
+ super.connectedCallback();
+ this._fetchCalendars();
+ }
+
+ _fetchCalendars() {
+ this.hass.callApi('get', 'calendars')
+ .then((result) => {
+ this.calendars = result;
+ this.selectedCalendars = result.map(cal => cal.entity_id);
+ });
+ }
+
+ _fetchData() {
+ const start = dates.firstVisibleDay(this.currentDate).toISOString();
+ const end = dates.lastVisibleDay(this.currentDate).toISOString();
+ const params = encodeURI(`?start=${start}&end=${end}`);
+ const calls = this.selectedCalendars.map(cal => this.hass.callApi('get', `calendars/${cal}${params}`));
+ Promise.all(calls).then((results) => {
+ const tmpEvents = [];
+
+ results.forEach((res) => {
+ res.forEach((ev) => {
+ ev.start = new Date(ev.start);
+ if (ev.end) {
+ ev.end = new Date(ev.end);
+ } else {
+ ev.end = null;
+ }
+ tmpEvents.push(ev);
+ });
+ });
+ this.events = tmpEvents;
+ });
+ }
+
+ _getDateRange() {
+ let startDate;
+ let endDate;
+ if (this.currentView === 'day') {
+ startDate = moment(this.currentDate).startOf('day');
+ endDate = moment(this.currentDate).startOf('day');
+ } else if (this.currentView === 'week') {
+ startDate = moment(this.currentDate).startOf('isoWeek');
+ endDate = moment(this.currentDate).endOf('isoWeek');
+ } else if (this.currentView === 'month') {
+ startDate = moment(this.currentDate).startOf('month').subtract(7, 'days');
+ endDate = moment(this.currentDate).endOf('month').add(7, 'days');
+ } else if (this.currentView === 'agenda') {
+ startDate = moment(this.currentDate).startOf('day');
+ endDate = moment(this.currentDate).endOf('day').add(1, 'month');
+ }
+ return [startDate.toISOString(), endDate.toISOString()];
+ }
+
+ _handleViewChanged(ev) {
+ // Calendar view changed
+ this.currentView = ev.detail.viewName;
+ this._fetchData();
+ }
+
+ _handleNavigate(ev) {
+ // Calendar date range changed
+ this.currentDate = ev.detail.date;
+ this.currentView = ev.detail.viewName;
+ this._fetchData();
+ }
}
customElements.define('ha-panel-calendar', HaPanelCalendar);