Skip to content

Commit

Permalink
Merge 4071d61 into b5b14d9
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Sep 5, 2018
2 parents b5b14d9 + 4071d61 commit d6b4c7b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 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';
2 changes: 1 addition & 1 deletion src/ng-select/items-list.spec.ts
Expand Up @@ -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);
}
10 changes: 5 additions & 5 deletions src/ng-select/ng-select.component.ts
Expand Up @@ -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<NgSelectConfig>('ng-select-default-options');
export const SELECTION_MODEL_FACTORY = new InjectionToken<NgSelectConfig>('ng-select-selection-model');
export type DropdownPosition = 'bottom' | 'top' | 'auto';
export type AddTagFn = ((term: string) => any | Promise<any>);
export type CompareWithFn = (a: any, b: any) => boolean;
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 11 additions & 9 deletions 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: [
Expand Down Expand Up @@ -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: {
Expand Down
18 changes: 9 additions & 9 deletions 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[] {
Expand Down

0 comments on commit d6b4c7b

Please sign in to comment.