@tapsellorg/angular-material-library
Version:
Angular library for Tapsell
128 lines (123 loc) • 5.57 kB
JavaScript
import * as i0 from '@angular/core';
import { input, ContentChildren, Directive, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import * as i1 from '@angular/material/select';
import { MatSelectModule } from '@angular/material/select';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { PghArrayJoinModule } from '@tapsellorg/angular-material-library/src/lib/common';
import { MatOption } from '@angular/material/core';
class PghSelectAllOptionsDirective {
constructor(matSelect) {
this.matSelect = matSelect;
this.valueForAll = input(undefined, {
alias: 'pghValueForOptionAll',
});
this.selected = false;
}
ngAfterViewInit() {
this.handleSelectionChange();
this.onOptionsChange();
this.matOptions.changes.subscribe(() => {
this.onOptionsChange();
});
}
handleSelectionChange() {
this.matSelect.optionSelectionChanges.subscribe(({ isUserInput, source: { selected, value } }) => {
if (!isUserInput)
return;
if (value === this.valueForAll()) {
this.selected = selected;
this.onSelectAllOptions(selected);
}
else {
this.onSelectOtherOptions(selected);
}
});
}
onOptionsChange() {
this.matOptions.forEach(o => {
if (o.value === this.valueForAll()) {
this.allOption = o;
}
});
}
onSelectAllOptions(selected) {
this.matOptions.forEach(o => {
if (selected) {
o.select();
}
else {
o.deselect();
}
});
this.setValueOfControlForOptionAll();
}
onSelectOtherOptions(selected) {
if (selected) {
const areAllOptionsSelect = this.matOptions
.map(o => o)
.every(o => o.selected || o === this.allOption);
if (areAllOptionsSelect) {
this.allOption?.select();
this.setValueOfControlForOptionAll();
}
}
else if (this.allOption?.selected) {
this.allOption?.deselect();
}
}
setValueOfControlForOptionAll() {
this.matSelect.ngControl?.control.setValue(this.valueForAll(), {
emitModelToViewChange: false,
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsDirective, deps: [{ token: i1.MatSelect }], target: i0.ɵɵFactoryTarget.Directive }); }
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "19.2.13", type: PghSelectAllOptionsDirective, isStandalone: false, selector: "[pghSelectAllOptions]", inputs: { valueForAll: { classPropertyName: "valueForAll", publicName: "pghValueForOptionAll", isSignal: true, isRequired: false, transformFunction: null } }, queries: [{ propertyName: "matOptions", predicate: MatOption }], exportAs: ["allOption"], ngImport: i0 }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsDirective, decorators: [{
type: Directive,
args: [{
selector: '[pghSelectAllOptions]',
exportAs: 'allOption',
standalone: false,
}]
}], ctorParameters: () => [{ type: i1.MatSelect }], propDecorators: { matOptions: [{
type: ContentChildren,
args: [MatOption]
}] } });
class PghSelectAllOptionsModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsModule, declarations: [PghSelectAllOptionsDirective], imports: [CommonModule,
MatSelectModule,
ReactiveFormsModule,
FormsModule,
MatFormFieldModule,
PghArrayJoinModule], exports: [PghSelectAllOptionsDirective] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsModule, imports: [CommonModule,
MatSelectModule,
ReactiveFormsModule,
FormsModule,
MatFormFieldModule,
PghArrayJoinModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghSelectAllOptionsModule, decorators: [{
type: NgModule,
args: [{
declarations: [PghSelectAllOptionsDirective],
imports: [
CommonModule,
MatSelectModule,
ReactiveFormsModule,
FormsModule,
MatFormFieldModule,
PghArrayJoinModule,
],
exports: [PghSelectAllOptionsDirective],
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { PghSelectAllOptionsDirective, PghSelectAllOptionsModule };
//# sourceMappingURL=tapsellorg-angular-material-library-src-lib-select-all-options.mjs.map