Skip to content

Commit

Permalink
Merge pull request #7832 from opf/fix/version-autocompleter-initializ…
Browse files Browse the repository at this point in the history
…ation

Avoid setting createAllowed=false after initialization of versions
  • Loading branch information
ulferts authored Oct 28, 2019
2 parents 517a01e + 877156b commit be0f900
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {I18nService} from "core-app/modules/common/i18n/i18n.service";
import {CurrentProjectService} from "core-components/projects/current-project.service";
import {PathHelperService} from "core-app/modules/common/path-helper/path-helper.service";
import {HalResource} from "core-app/modules/hal/resources/hal-resource";
import {AddTagFn} from "@ng-select/ng-select/lib/ng-select.component";

@Component({
templateUrl: './create-autocompleter.component.html',
Expand All @@ -57,28 +58,12 @@ export class CreateAutocompleterComponent implements AfterViewInit {

@ViewChild('ngSelectComponent', {static: false}) public ngSelectComponent:NgSelectComponent;

@Input()
public set createAllowed(val:boolean) {
if (val) {
this._createAllowed = this.createNewElement.bind(this);
} else {
this._createAllowed = false;
}
this.cdRef.detectChanges();

setTimeout(() => {
if (this.openDirectly) {
this.openSelect();
}
this.ngAfterViewInit();
});
}

public text:any = {
add_new_action: this.I18n.t('js.label_create'),
};

private _createAllowed:boolean = false;
public createAllowed:boolean|AddTagFn = false;

private _openDirectly:boolean = false;

constructor(readonly I18n:I18nService,
Expand Down Expand Up @@ -107,12 +92,6 @@ export class CreateAutocompleterComponent implements AfterViewInit {
this.ngSelectComponent && this.ngSelectComponent.close();
}

public createNewElement(newElement:string) {
if (this.createAllowed) {
this.performCreate(newElement);
}
}

public changeModel(element:HalResource) {
this.onChange.emit(element);
}
Expand All @@ -137,10 +116,6 @@ export class CreateAutocompleterComponent implements AfterViewInit {
this.onKeydown.emit(event);
}

public get createAllowed() {
return this._createAllowed;
}

public get openDirectly() {
return this._openDirectly;
}
Expand All @@ -155,10 +130,6 @@ export class CreateAutocompleterComponent implements AfterViewInit {
public focusInputField() {
this.ngSelectComponent && this.ngSelectComponent.focus();
}

protected performCreate(newElement:string) {
// Nothing to do in this base component.
}
}

DynamicBootstrapper.register({selector: 'add-attribute-autocompleter', cls: CreateAutocompleterComponent});
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {HalResourceNotificationService} from "core-app/modules/hal/services/hal-
templateUrl: './create-autocompleter.component.html',
selector: 'version-autocompleter'
})
export class VersionAutocompleterComponent extends CreateAutocompleterComponent implements OnInit {
export class VersionAutocompleterComponent extends CreateAutocompleterComponent implements AfterViewInit {
@Input() public openDirectly:boolean = false;
@Output() public onCreate = new EventEmitter<VersionResource>();

Expand All @@ -62,9 +62,14 @@ export class VersionAutocompleterComponent extends CreateAutocompleterComponent
super(I18n, cdRef, currentProject, pathHelper);
}

ngOnInit() {
ngAfterViewInit() {
super.ngAfterViewInit();

this.canCreateNewActionElements().then((val) => {
this.createAllowed = val;
if (val) {
this.createAllowed = (input:string) => this.createNewVersion(input);
this.cdRef.detectChanges();
}
});
}

Expand All @@ -79,7 +84,7 @@ export class VersionAutocompleterComponent extends CreateAutocompleterComponent
.catch(() => false);
}

protected performCreate(name:string) {
protected createNewVersion(name:string) {
this.versionDm.createVersion(this.getVersionPayload(name))
.then((version) => {
this.onCreate.emit(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
required: required,
disabled: inFlight,
id: handler.htmlId,
createAllowed: false,
finishedLoading: true,
classes: 'inline-edit--field ' + handler.fieldName }"
[ndcDynamicOutputs]="referenceOutputs">
Expand Down

0 comments on commit be0f900

Please sign in to comment.