From f1a4f23dd75efc32accff90d4b4694ad9607abbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeu=C5=A1=20Varnas?= Date: Wed, 5 Sep 2018 13:30:30 +0300 Subject: [PATCH] fix: use factory to resolve new instance of selection model (#789) * fix: use factory to resolve new instance of selection model fixes #787 --- src/index.ts | 2 +- src/ng-select/items-list.spec.ts | 2 +- src/ng-select/ng-select.component.ts | 10 +++++----- src/ng-select/ng-select.module.ts | 20 +++++++++++--------- src/ng-select/selection-model.ts | 18 +++++++++--------- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 323b5a59f..03494c295 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export { NgSelectComponent, NG_SELECT_DEFAULT_CONFIG } from './ng-select/ng-select.component'; +export { NgSelectComponent, NG_SELECT_DEFAULT_CONFIG, SELECTION_MODEL_FACTORY } from './ng-select/ng-select.component'; export { NgSelectModule } from './ng-select/ng-select.module'; export { NgOption, NgSelectConfig } from './ng-select/ng-select.types'; export { SelectionModel } from './ng-select/selection-model'; diff --git a/src/ng-select/items-list.spec.ts b/src/ng-select/items-list.spec.ts index bd163ecb2..4f034e2f9 100644 --- a/src/ng-select/items-list.spec.ts +++ b/src/ng-select/items-list.spec.ts @@ -382,5 +382,5 @@ function itemsListFactory(cmp: NgSelectComponent): ItemsList { } function ngSelectFactory(): NgSelectComponent { - return new NgSelectComponent(null, null, {}, {} as any, null, null, null); + return new NgSelectComponent(null, null, {}, () => new DefaultSelectionModel(), {} as any, null, null); } diff --git a/src/ng-select/ng-select.component.ts b/src/ng-select/ng-select.component.ts index 97dc62240..6ddbb2f19 100644 --- a/src/ng-select/ng-select.component.ts +++ b/src/ng-select/ng-select.component.ts @@ -47,9 +47,10 @@ import { NgOption, KeyCode, NgSelectConfig } from './ng-select.types'; import { newId } from './id'; import { NgDropdownPanelComponent } from './ng-dropdown-panel.component'; import { NgOptionComponent } from './ng-option.component'; -import { SelectionModel } from './selection-model'; +import { SelectionModelFactory } from './selection-model'; export const NG_SELECT_DEFAULT_CONFIG = new InjectionToken('ng-select-default-options'); +export const SELECTION_MODEL_FACTORY = new InjectionToken('ng-select-selection-model'); export type DropdownPosition = 'bottom' | 'top' | 'auto'; export type AddTagFn = ((term: string) => any | Promise); export type CompareWithFn = (a: any, b: any) => boolean; @@ -175,14 +176,13 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C @Attribute('class') public classes: string, @Attribute('tabindex') public tabIndex: string, @Inject(NG_SELECT_DEFAULT_CONFIG) config: NgSelectConfig, + @Inject(SELECTION_MODEL_FACTORY) newSelectionModel: SelectionModelFactory, _elementRef: ElementRef, - _selectionModel: SelectionModel, private _cd: ChangeDetectorRef, - private _console: ConsoleService, - + private _console: ConsoleService ) { this._mergeGlobalConfig(config); - this.itemsList = new ItemsList(this, _selectionModel); + this.itemsList = new ItemsList(this, newSelectionModel()); this.element = _elementRef.nativeElement; } diff --git a/src/ng-select/ng-select.module.ts b/src/ng-select/ng-select.module.ts index 40961d80a..8ad1cc9ff 100644 --- a/src/ng-select/ng-select.module.ts +++ b/src/ng-select/ng-select.module.ts @@ -1,21 +1,22 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { NgSelectComponent, NG_SELECT_DEFAULT_CONFIG } from './ng-select.component'; +import { NG_SELECT_DEFAULT_CONFIG, NgSelectComponent, SELECTION_MODEL_FACTORY } from './ng-select.component'; import { - NgOptionTemplateDirective, - NgLabelTemplateDirective, - NgHeaderTemplateDirective, NgFooterTemplateDirective, - NgOptgroupTemplateDirective, - NgNotFoundTemplateDirective, - NgTypeToSearchTemplateDirective, + NgHeaderTemplateDirective, + NgLabelTemplateDirective, NgLoadingTextTemplateDirective, NgMultiLabelTemplateDirective, - NgTagTemplateDirective + NgNotFoundTemplateDirective, + NgOptgroupTemplateDirective, + NgOptionTemplateDirective, + NgTagTemplateDirective, + NgTypeToSearchTemplateDirective } from './ng-templates.directive'; import { NgOptionComponent } from './ng-option.component'; -import { NgOptionHighlightDirective } from './ng-option-highlight.directive' ; +import { NgOptionHighlightDirective } from './ng-option-highlight.directive'; import { NgDropdownPanelComponent } from './ng-dropdown-panel.component'; +import { DefaultSelectionModelFactory } from './selection-model'; @NgModule({ declarations: [ @@ -53,6 +54,7 @@ import { NgDropdownPanelComponent } from './ng-dropdown-panel.component'; NgTagTemplateDirective ], providers: [ + { provide: SELECTION_MODEL_FACTORY, useValue: DefaultSelectionModelFactory }, { provide: NG_SELECT_DEFAULT_CONFIG, useValue: { diff --git a/src/ng-select/selection-model.ts b/src/ng-select/selection-model.ts index 83eca0a6f..5bbd2e066 100644 --- a/src/ng-select/selection-model.ts +++ b/src/ng-select/selection-model.ts @@ -1,19 +1,19 @@ import { NgOption } from './ng-select.types'; -import { Injectable } from '@angular/core'; -export function DEFAULT_SELECTION_MODEL_FACTORY() { +export type SelectionModelFactory = () => SelectionModel; + +export function DefaultSelectionModelFactory() { return new DefaultSelectionModel(); } -@Injectable({ providedIn: 'root', useFactory: DEFAULT_SELECTION_MODEL_FACTORY }) -export abstract class SelectionModel { - abstract get value(): NgOption[]; - abstract select(item: NgOption, multiple: boolean, selectableGroupAsModel: boolean); - abstract unselect(item: NgOption, multiple: boolean); - abstract clear(); +export interface SelectionModel { + value: NgOption[]; + select(item: NgOption, multiple: boolean, selectableGroupAsModel: boolean); + unselect(item: NgOption, multiple: boolean); + clear(); } -export class DefaultSelectionModel { +export class DefaultSelectionModel implements SelectionModel { private _selected: NgOption[] = []; get value(): NgOption[] {