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..63a630d1d 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 { DefaultSelectionModel } from './selection-model'; @NgModule({ declarations: [ @@ -53,6 +54,7 @@ import { NgDropdownPanelComponent } from './ng-dropdown-panel.component'; NgTagTemplateDirective ], providers: [ + { provide: SELECTION_MODEL_FACTORY, useValue: () => new DefaultSelectionModel() }, { provide: NG_SELECT_DEFAULT_CONFIG, useValue: { diff --git a/src/ng-select/selection-model.ts b/src/ng-select/selection-model.ts index 83eca0a6f..f6dd673f1 100644 --- a/src/ng-select/selection-model.ts +++ b/src/ng-select/selection-model.ts @@ -1,19 +1,15 @@ import { NgOption } from './ng-select.types'; -import { Injectable } from '@angular/core'; -export function DEFAULT_SELECTION_MODEL_FACTORY() { - return new DefaultSelectionModel(); -} +export type SelectionModelFactory = () => SelectionModel; -@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[] {