From d6d398e49dbc4ec11e33c3683d8a6d53f63f776d Mon Sep 17 00:00:00 2001 From: ardentia Date: Tue, 29 Aug 2023 11:59:01 +0300 Subject: [PATCH] remove unnecessary code --- projects/ng-datetime-picker/package.json | 2 +- .../datetime-picker.component.ts | 56 ++++++++----------- projects/ng-form-elements/package.json | 2 +- .../lib/tags-input/tags-input.component.ts | 17 ++---- .../src/lib/typeahead/typeahead.component.ts | 24 +++----- .../control-value-accessor-enabler.ts | 2 - 6 files changed, 40 insertions(+), 63 deletions(-) diff --git a/projects/ng-datetime-picker/package.json b/projects/ng-datetime-picker/package.json index 048ba9cc1..1b3d7dba4 100644 --- a/projects/ng-datetime-picker/package.json +++ b/projects/ng-datetime-picker/package.json @@ -1,6 +1,6 @@ { "name": "@sq-ui/ng-datetime-picker", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "private": false, "description": "Simple Quality UI Datetime-Picker for Angular", diff --git a/projects/ng-datetime-picker/src/lib/datetime-picker/datetime-picker/datetime-picker.component.ts b/projects/ng-datetime-picker/src/lib/datetime-picker/datetime-picker/datetime-picker.component.ts index b6a589d36..0e39c51bb 100644 --- a/projects/ng-datetime-picker/src/lib/datetime-picker/datetime-picker/datetime-picker.component.ts +++ b/projects/ng-datetime-picker/src/lib/datetime-picker/datetime-picker/datetime-picker.component.ts @@ -1,6 +1,6 @@ import { Component, forwardRef, OnInit, ViewEncapsulation, - Input, Output, EventEmitter, AfterViewInit, + Input, Output, EventEmitter, OnChanges } from '@angular/core'; import { InputCoreComponent } from '@sq-ui/ng-sq-common'; @@ -28,7 +28,7 @@ const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = { encapsulation: ViewEncapsulation.None, providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] }) -export class DatetimePickerComponent extends InputCoreComponent implements OnInit, AfterViewInit, OnChanges { +export class DatetimePickerComponent extends InputCoreComponent implements OnInit, OnChanges { @Input() locale = 'en'; @Input() maxDate: moment.Moment | Date; @Input() minDate: moment.Moment | Date; @@ -62,16 +62,8 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni moment.locale(this.locale); this.calendarManager.setLocale(this.locale); const now = moment().hours(0).minutes(0).locale(this.locale); - this.selectedDates = List([now.clone()]); this.weekdays = this.calendarManager.getWeekdays(); this.calendar = this.getMonthCalendar(now.clone()); - this.initializeAuthorValuesIfAny(); - } - - ngAfterViewInit() { - setTimeout(() => { - this.setValueResult(); - }); } ngOnChanges(changesObj) { @@ -80,6 +72,28 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni } } + override writeValue(newValue): void { + super.writeValue(newValue); + + if (newValue) { + this.deselectAll(); + + if (Array.isArray(newValue)) { + newValue.forEach((date) => { + const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar); + this.markDateAsSelected(convertedDate); + }); + } else { + const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar); + this.markDateAsSelected(calendarDay); + } + } else { + const now = moment().hours(0).minutes(0).locale(this.locale); + const convertedDate = this.calendarManager.findADateFromCalendar(now, this.calendar); + this.markDateAsSelected(convertedDate); + } + } + onDateClick(date: CalendarDay) { switch (date.relativityToCurrentMonth) { case CalendarPeriodRelativityEnum.After: @@ -196,28 +210,6 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni this.dateSelectionChange.emit(this.value); } - private initializeAuthorValuesIfAny() { - const subscription = this._modelToViewChange.subscribe((newValue) => { - if (this.selectedDates.size === 1 && this.selectedDates.get(0).isSame(moment(), 'day')) { - if (newValue) { - this.deselectAll(); - - if (Array.isArray(newValue)) { - newValue.forEach((date) => { - const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar); - this.markDateAsSelected(convertedDate); - }); - } else { - const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar); - this.markDateAsSelected(calendarDay); - } - } - } - - subscription.unsubscribe(); - }); - } - private markDateAsSelected(date: CalendarDay) { const selectedMomentObj = moment(date.momentObj); const selectedIndex = this.calendarManager.getSelectedItemIndex(selectedMomentObj, this.selectedDates.toArray()); diff --git a/projects/ng-form-elements/package.json b/projects/ng-form-elements/package.json index 4d358fac3..7f4e82192 100644 --- a/projects/ng-form-elements/package.json +++ b/projects/ng-form-elements/package.json @@ -1,6 +1,6 @@ { "name": "@sq-ui/ng-form-elements", - "version": "2.0.1", + "version": "2.0.2", "dependencies": { "@sq-ui/ng-sq-common": "^2.0.1" } diff --git a/projects/ng-form-elements/src/lib/tags-input/tags-input.component.ts b/projects/ng-form-elements/src/lib/tags-input/tags-input.component.ts index fe8f3c605..2b0d4ff25 100644 --- a/projects/ng-form-elements/src/lib/tags-input/tags-input.component.ts +++ b/projects/ng-form-elements/src/lib/tags-input/tags-input.component.ts @@ -31,7 +31,6 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af private isModelEmpty: boolean = false; private enteredItemsSubscription: Subscription; - private valueChangedSubscription: Subscription; private inputEventSubscription: Subscription; private innerEnteredItemsListCopy: List; @@ -51,13 +50,13 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af const itemsCopy = this.innerEnteredItemsListCopy; this.value = itemsCopy.toArray(); }); + } - this.valueChangedSubscription = this._modelToViewChange.subscribe((predefinedEnteredItems) => { - if (this.enteredItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) { - this.enteredItems = List(predefinedEnteredItems); - this.valueChangedSubscription.unsubscribe(); - } - }); + override writeValue(predefinedEnteredItems): void { + super.writeValue(predefinedEnteredItems); + if (this.enteredItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) { + this.enteredItems = List(predefinedEnteredItems); + } } ngAfterViewInit() { @@ -79,10 +78,6 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af if (this.inputEventSubscription) { this.inputEventSubscription.unsubscribe(); } - - if (!this.valueChangedSubscription.closed) { - this.valueChangedSubscription.unsubscribe(); - } } onUserInput($event) { diff --git a/projects/ng-form-elements/src/lib/typeahead/typeahead.component.ts b/projects/ng-form-elements/src/lib/typeahead/typeahead.component.ts index dc625a9be..443a067af 100644 --- a/projects/ng-form-elements/src/lib/typeahead/typeahead.component.ts +++ b/projects/ng-form-elements/src/lib/typeahead/typeahead.component.ts @@ -43,7 +43,6 @@ export class TypeaheadComponent extends InputCoreComponent private onInputValueChangeSubscription: Subscription; private onQueryInputControlSubscription: Subscription; - private valueChangedSubscription: Subscription; selectedItems: List = List(); options: List = List(); @@ -80,18 +79,15 @@ export class TypeaheadComponent extends InputCoreComponent } }, ); + } - this.valueChangedSubscription = this._modelToViewChange.subscribe( - (predefinedEnteredItems) => { - if (this.selectedItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) { - this.transformToLabelValuePairList(predefinedEnteredItems).forEach((item) => { - this.selectItem(item, false, true); - }); - } - - this.valueChangedSubscription.unsubscribe(); - }, - ); + override writeValue(predefinedEnteredItems): void { + super.writeValue(predefinedEnteredItems); + if (this.selectedItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) { + this.transformToLabelValuePairList(predefinedEnteredItems).forEach((item) => { + this.selectItem(item, false, true); + }); + } } ngOnChanges(changesObj: SimpleChanges) { @@ -112,10 +108,6 @@ export class TypeaheadComponent extends InputCoreComponent this.listenForOutsideClick = false; this.onQueryInputControlSubscription.unsubscribe(); this.onInputValueChangeSubscription.unsubscribe(); - - if (!this.valueChangedSubscription.closed) { - this.valueChangedSubscription.unsubscribe(); - } } selectSearchResult(result: LabelValuePair) { diff --git a/projects/ng-sq-common/src/lib/entities/control-value-accessor-enabler.ts b/projects/ng-sq-common/src/lib/entities/control-value-accessor-enabler.ts index 092ed9db8..87f5988ea 100644 --- a/projects/ng-sq-common/src/lib/entities/control-value-accessor-enabler.ts +++ b/projects/ng-sq-common/src/lib/entities/control-value-accessor-enabler.ts @@ -7,7 +7,6 @@ import { ControlValueAccessor } from '@angular/forms'; * Its only purpose is to implement the ControlValueAccessor interface. **/ export class ControlValueAccessorEnabler implements ControlValueAccessor { - protected _modelToViewChange: EventEmitter = new EventEmitter(); protected _value: any; protected _onChange: any = () => {}; protected _onTouched: any = () => {}; @@ -32,7 +31,6 @@ export class ControlValueAccessorEnabler implements ControlValueAccessor { writeValue(newValue: any): void { if (newValue !== this._value) { this._value = newValue; - this._modelToViewChange.emit(newValue); } }