diff --git a/src/index-editor.ts.old b/src/index-editor.ts.old deleted file mode 100644 index f216fe2a..00000000 --- a/src/index-editor.ts.old +++ /dev/null @@ -1,1199 +0,0 @@ -import { LitElement, html, TemplateResult, CSSResult } from 'lit'; -import { property, customElement, state } from 'lit/decorators.js'; -import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin' -import { HomeAssistant, LovelaceCardEditor, fireEvent } from 'custom-card-helpers'; - -import { localize } from './localize/localize'; -import { style } from './style-editor'; -import { atomicCardConfig } from './types'; -import { formfieldDefinition } from '../elements/formfield'; -import { textfieldDefinition } from '../elements/textfield'; -import { switchDefinition } from '../elements/switch'; -import { selectDefinition } from '../elements/select'; - -const linkTargets: string[] = ['_blank', '_self', '_parent', '_top']; -const defaultModes: string[] = ['Event', 'Calendar']; - -const options = { - required: { - icon: 'tune', - show: false, - }, - main: { - icon: 'eye-settings', - show: false, - }, - event: { - icon: 'calendar-check', - show: false, - }, - calendar: { - icon: 'calendar-month-outline', - show: false, - }, - appearance: { - icon: 'palette', - show: false, - }, -}; - -@customElement('atomic-calendar-revive-editor') -export class AtomicCalendarReviveEditor extends ScopedRegistryHost(LitElement) implements LovelaceCardEditor { - @property({ attribute: false }) - public hass!: HomeAssistant; - @state() private _config!: atomicCardConfig; - @state() private _toggle?: boolean; - @state() private _helpers?: any; - private _initialized = false; - - static elementDefinitions = { - ...textfieldDefinition, - ...formfieldDefinition, - ...switchDefinition, - ...selectDefinition, - }; - - static get styles(): CSSResult { - return style; - } - - public setConfig(config: atomicCardConfig): void { - this._config = config; - - this.loadCardHelpers(); - } - - protected shouldUpdate(): boolean { - if (!this._initialized) { - this._initialize(); - } - - return true; - } - - // ENTITY SETTINGS - get _entityOptions() { - const entities = Object.keys(this.hass.states).filter(eid => eid.substr(0, eid.indexOf('.')) === 'calendar'); - let entityOptions - if (typeof this._config?.entities != 'undefined') { - entityOptions = entities.map(eid => { - let matchingConfigEnitity = this._config?.entities.find(entity => (entity && entity.entity || entity) === eid); - const originalEntity = this.hass.states[eid]; - if (matchingConfigEnitity === undefined) { - - matchingConfigEnitity = { - entity: eid, - name: originalEntity.attributes.friendly_name || eid, - checked: !!matchingConfigEnitity - } - - } else { - if (!('name' in matchingConfigEnitity)) { - matchingConfigEnitity = { ...matchingConfigEnitity, name: (matchingConfigEnitity && matchingConfigEnitity.name) || originalEntity.attributes.friendly_name || eid } - } - matchingConfigEnitity = { ...matchingConfigEnitity, checked: !!matchingConfigEnitity } - - } - return matchingConfigEnitity - }); - } else { - entityOptions = entities.map(eid => { - const originalEntity = this.hass.states[eid]; - return { - entity: eid, - name: originalEntity.attributes.friendly_name || eid, - checked: false - } - }); - } - return entityOptions; - } - - //MAIN SETTINGS - get _name(): string { - return this._config?.name || ''; - } - - get _titleLength(): number { - return this._config?.titleLength || 0; - } - - get _descLength(): number { - return this._config?.descLength || 0; - } - - get _firstDayOfWeek(): number { - return this._config?.firstDayOfWeek || 1; - } - - get _maxDaysToShow(): number { - return this._config?.maxDaysToShow || 7; - } - - get _linkTarget(): string { - return this._config?.linkTarget || '_blank'; - } - get _defaultMode(): string { - return this._config?.defaultMode || 'Event'; - } - get _cardHeight(): string { - return this._config?.cardHeight || '100%'; - } - - get _showLocation(): boolean { - return this._config?.showLocation || true; - } - get _showLoader(): boolean { - return this._config?.showLoader || true; - } - get _sortByStartTime(): boolean { - return this._config?.sortByStartTime || false; - } - get _showDeclined(): boolean { - return this._config?.showDeclined || false; - } - get _hideDuplicates(): boolean { - return this._config?.hideDuplicates || false; - } - get _showMultiDay(): boolean { - return this._config?.showMultiDay || false; - } - get _showMultiDayEventParts(): boolean { - return this._config?.showMultiDayEventParts || false; - } - - get _dateFormat(): string { - return this._config?.dateFormat || 'LL'; - } - get _hoursFormat(): string { - return this._config?.hoursFormat || ''; - } - get _refreshInterval(): number { - return this._config?.refreshInterval || 60; - } - get _showDate(): boolean { - return this._config?.showDate || false; - } - get _showRelativeTime(): boolean { - return this._config?.showRelativeTime || false; - } - get _eventTitle(): string { - return this._config?.eventTitle || ''; - } - get _compactMode(): boolean { - return this._config?.compactMode || false; - } - get _hoursOnSameLine(): boolean { - return this._config?.hoursOnSameLine || false; - } - get _showAllDayEvents(): boolean { - return this._config?.showAllDayEvents || true; - } - get _offsetHeaderDate(): boolean { - return this._config?.offsetHeaderDate || false; - } - - // MAIN SETTINGS END - - // EVENT SETTINGS - - get _showCurrentEventLine(): boolean { - return this._config?.showCurrentEventLine || false; - } - - get _showProgressBar(): boolean { - return this._config?.showProgressBar || true; - - } - - get _showMonth(): boolean { - return this._config?.showMonth || false; - } - get _showWeekDay(): boolean { - return this._config?.showWeekDay || false; - } - get _showDescription(): boolean { - return this._config?.showDescription || true; - } - get _disableEventLink(): boolean { - return this._config?.disableEventLink || false; - } - get _disableLocationLink(): boolean { - return this._config?.disableLocationLink || false; - } - get _showNoEventsForToday(): boolean { - return this._config?.showNoEventsForToday || false; - } - get _showCalNameInEvent(): boolean { - return this._config?.showCalNameInEvent || false; - } - get _showFullDayProgress(): boolean { - return this._config?.showFullDayProgress || false; - } - get _hideFinishedEvents(): boolean { - return this._config?.hideFinishedEvents || false; - } - get _showEventIcon(): boolean { - return this._config?.showEventIcon || false; - } - get _untilText(): string { - return this._config?.untilText || ''; - } - get _fullDayEventText(): string { - return this._config?.fullDayEventText || ''; - } - get _noEventsForNextDaysText(): string { - return this._config?.noEventsForNextDaysText || ''; - } - get _noEventText(): string { - return this._config?.noEventText || ''; - } - get _showHiddenText(): boolean { - return this._config?.showHiddenText || false; - } - get _showCalendarName(): boolean { - return this._config?.showCalendarName || false; - } - get _hiddenEventText(): string { - return this._config?.hiddenEventText || ''; - } - get _showWeekNumber(): boolean { - return this._config?.showWeekNumber || false; - } - get _showEventDate(): boolean { - return this._config?.showEventDate || false; - } - get _showDatePerEvent(): boolean { - return this._config?.showDatePerEvent || false; - } - get _showTimeRemaining(): boolean { - return this._config?.showTimeRemaining || false; - } - - get _showAllDayHours(): boolean { - return this._config?.showTimeRemaining || false; - } - - // EVENT SETTINGS END - - // CALENDAR SETTINGS - get _showLastCalendarWeek(): boolean { - return this._config?.showLastCalendarWeek || false; - } - get _disableCalEventLink(): boolean { - return this._config?.disableCalEventLink || false; - } - get _disableCalLocationLink(): boolean { - return this._config?.disableCalLocationLink || false; - } - get _calShowDescription(): boolean { - return this._config?.calShowDescription || false; - } - - get _disableCalLink(): boolean { - return this._config?.disableCalLink || false; - } - - // CALENDAR SETTINGS END - - // APPEARENCE SETTINGS - - get _locationLinkColor(): string { - return this._config?.locationLinkColor || ''; - } - get _dimFinishedEvents(): boolean { - return this._config?.dimFinishedEvents || true; - } - - // APPEARENCE SETTINGS END - - protected render(): TemplateResult | void { - if (!this.hass || !this._helpers) { - return html``; - } - - // You can restrict on domain type - const entities = Object.keys(this.hass.states).filter(eid => eid.substr(0, eid.indexOf('.')) === 'calendar'); - - return html` -
-
-
- -
${localize('required.name')}
-
-
${localize('required.secondary')}
-
- ${options.required.show - ? html` -
- ${this._entityOptions.map(entity => { - return html` -
- - - ${entity.checked ? html` -
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
-
-
` : html`` - } - -
- `; - }) - } -
- ` - : ''} - -
-
- -
${localize('main.name')}
-
-
${l${localize('main.name')}}
-
- ${options.main.show - ? html` -
-
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- ev.stopPropagation()} - > - ${defaultModes.map((mode) => { - return html`${mode} `; - })} - -
-
- ev.stopPropagation()} - > - ${linkTargets.map((linkTarget) => { - return html`${linkTarget} `; - })} - -
-
-
-
- -
-
-
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
- - ` - : ''} - - -
-
- -
${localize('event.name')}
-
-
${localize('event.secondary')}
-
- ${options.event.show - ? html` -
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
-
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
-
-
-
- ` - : ''} - - -
-
- -
${localize('calendar.name')}
-
-
${localize('calendar.secondary')}
-
- ${options.calendar.show - ? html` -
- - -
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
-
- ` - : ''} - - -
-
- -
${localize('appearance.name')}
-
-
${localize('appearance.secondary')}
-
- ${options.appearance.show - ? html` -
-
-
- - -
-
-
- ` - : ''} - -
- `; - } - private _initialize(): void { - if (this.hass === undefined) { - return; - } - if (this._config === undefined) { - return; - } - if (this._helpers === undefined) { - return; - } - this._initialized = true; - } - - private async loadCardHelpers(): Promise { - this._helpers = await (window as any).loadCardHelpers(); - } - private _toggleOption(ev): void { - this._toggleThing(ev, options); - } - - private _toggleThing(ev, optionList): void { - const show = !optionList[ev.target.option].show; - for (const [key] of Object.entries(optionList)) { - optionList[key].show = false; - } - optionList[ev.target.option].show = show; - this._toggle = !this._toggle; - } - - private _valueChanged(ev): void { - if (this.cantFireEvent) { - return; - } - - const { target } = ev; - if (this[`_${target.configValue}`] === target.value) { - return; - } - if (target.configValue) { - if (target.value === '') { - const tmpConfig = { ...this._config }; - delete tmpConfig[target.configValue]; - this._config = tmpConfig; - } else { - this._config = { - ...this._config, - [target.configValue]: target.checked !== undefined ? target.checked : (isNaN(target.value)) ? target.value : parseInt(target.value), - }; - } - } - fireEvent(this, 'config-changed', { config: this._config }); - } - - get entities() { - const entities = [...(this._config.entities || [])]; - - // convert any legacy entity strings into objects - return entities.map(entity => { - if (entity.entity) { - return entity; - } - return { entity, name: entity }; - }); - } - /** - * change the entity configuration - * @param {*} ev - */ - private _entityValueChanged(ev) { - if (this.cantFireEvent) { - return; - } - - const { target } = ev - let entityObjects = [...this.entities]; - - entityObjects = entityObjects.map(entity => { - if (entity.entity === target.entityId) { - if (this[`_${target.configValue}`] === target.value) { - return; - } - if (target.configValue && target.value === '') { - delete entity[target.configValue]; - return entity; - } else { - entity = { - ...entity, - [target.configValue]: target.checked !== undefined ? target.checked : (isNaN(target.value)) ? target.value : parseInt(target.value), - } - } - } - return entity; - }); - - this._config = Object.assign({}, this._config, { entities: entityObjects }); - fireEvent(this, 'config-changed', { config: this._config }); - } - /** - * add or remove calendar entities from config - * @param {*} ev - */ - private _entityChanged(ev) { - const { target } = ev; - - if (this.cantFireEvent) { - return; - } - let entityObjects = [...this.entities]; - if (target.checked) { - const originalEntity = this.hass.states[target.entityId]; - entityObjects.push({ entity: target.entityId, name: originalEntity.attributes.friendly_name || target.entityId }); - } else { - entityObjects = entityObjects.filter(entity => entity.entity !== target.entityId); - } - - this._config = Object.assign({}, this._config, { entities: entityObjects }); - fireEvent(this, 'config-changed', { config: this._config }); - } - /** - * stop events from firing if certains conditions not met - */ - get cantFireEvent() { - return (!this._config || !this.hass); - } -} diff --git a/src/lib/event.class.ts b/src/lib/event.class.ts index 2df8b99f..84f2ee78 100644 --- a/src/lib/event.class.ts +++ b/src/lib/event.class.ts @@ -95,9 +95,13 @@ export default class EventClass { } get daysLong() { - const fullDays = Math.round(this.endDateTime.subtract(1, 'minutes').endOf('day').diff(this.startDateTime.startOf('day'), 'hours') / 24) + if (this._globalConfig.showMultiDay) { + return this.rawEvent.daysLong; + } else { + const fullDays = Math.round(this.endDateTime.subtract(1, 'minutes').endOf('day').diff(this.startDateTime.startOf('day'), 'hours') / 24) - return fullDays > 1 ? fullDays : undefined; + return fullDays > 1 ? fullDays : undefined; + } } get isFirstDay() { @@ -221,7 +225,7 @@ export default class EventClass { } // check for days that are between multi days - they ARE all day - if (!this.isFirstDay && !this.isLastDay && this.daysLong) { + if (!this.isFirstDay && !this.isLastDay && this.daysLong && this._globalConfig.showMultiDay) { return true; } @@ -240,7 +244,6 @@ export default class EventClass { // TODO: Confirm this works as expected let daysLong = 2; const fullDays = Math.round(this.endDateTime.subtract(1, 'minutes').endOf('day').diff(this.startDateTime.startOf('day'), 'hours') / 24) - if (fullDays) { daysLong = fullDays; } @@ -252,7 +255,7 @@ export default class EventClass { copiedEvent.daysLong = daysLong; copiedEvent._isFirstDay = i === 0; - copiedEvent._isLastDay = i <= daysLong - 1 && i > 0; + copiedEvent._isLastDay = i === (daysLong - 1) && i > 0; // Create event object for each of the days the multi-event occurs on const partialEvent: EventClass = new EventClass(copiedEvent, this._globalConfig); diff --git a/src/lib/event.func.ts b/src/lib/event.func.ts index 49592597..3924de5c 100644 --- a/src/lib/event.func.ts +++ b/src/lib/event.func.ts @@ -299,10 +299,10 @@ export function processEvents(allEvents: any[], config: atomicCardConfig) { if (config.showMultiDay && newEvent.isMultiDay) { const partialEvents = newEvent.splitIntoMultiDay(newEvent); events = events.concat(partialEvents); + } else { events.push(newEvent); } - return events; }, []);