@progress/kendo-angular-spreadsheet
Version:
A Spreadsheet Component for Angular
157 lines (156 loc) • 8.69 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Directive, Renderer2 } from '@angular/core';
import { take } from 'rxjs/operators';
import { ToolBarButtonComponent } from '@progress/kendo-angular-toolbar';
import { DialogService } from '@progress/kendo-angular-dialog';
import { SpreadsheetLocalizationService } from '../localization/spreadsheet-localization.service';
import { SpreadsheetService } from '../common/spreadsheet.service';
import { SpreadsheetToolsService } from './tools.service';
import { SpreadsheetCommandButton } from './shared/spreadsheet-command-button';
import { DataValidationDialogComponent } from './data-validation-dialog.component';
import * as i0 from "@angular/core";
import * as i1 from "@progress/kendo-angular-toolbar";
import * as i2 from "../localization/spreadsheet-localization.service";
import * as i3 from "../common/spreadsheet.service";
import * as i4 from "./tools.service";
import * as i5 from "@progress/kendo-angular-dialog";
const COMPARERS_LIST = [
{ type: 'greaterThan', name: 'greater than' },
{ type: 'lessThan', name: 'less than' },
{ type: 'between', name: 'between' },
{ type: 'notBetween', name: 'not between' },
{ type: 'equalTo', name: 'equal to' },
{ type: 'notEqualTo', name: 'not equal to' },
{ type: 'greaterThanOrEqualTo', name: 'greater than or equal to' },
{ type: 'lessThanOrEqualTo', name: 'less than or equal to' }
];
const CRITERIA_LIST = [
{ type: 'any', name: 'Any value' },
{ type: 'number', name: 'Number' },
{ type: 'text', name: 'Text' },
{ type: 'date', name: 'Date' },
{ type: 'custom', name: 'Custom Formula' },
{ type: 'list', name: 'List' }
];
/**
* @hidden
*/
export class SpreadsheetDataValidationDirective extends SpreadsheetCommandButton {
dialogService;
renderer;
constructor(button, localization, spreadsheetService, toolsService, dialogService, renderer) {
super('dataValidation', button, localization, spreadsheetService, toolsService, {
command: 'EditValidationCommand', options: { value: '' }
});
this.dialogService = dialogService;
this.renderer = renderer;
button.showText = 'always';
}
get currentRange() {
return this.spreadsheetService.spreadsheet.options.nameBoxRef.current.value();
}
ngAfterViewInit() {
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-haspopup', 'dialog');
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-expanded', 'false');
}
clickHandler() {
this.openDialog();
}
/**
* @hidden
*/
openDialog() {
const currentSheet = this.spreadsheetService.spreadsheet.activeSheet();
const currentRangeValidation = currentSheet.range(currentSheet.activeCell()).validation();
const dialogSettings = {
appendTo: this.spreadsheetService.dialogContainer,
title: this.localization.get(this.command),
content: DataValidationDialogComponent,
minWidth: 400,
maxHeight: '90%',
};
const dialog = this.dialogService.open(dialogSettings);
const dialogContent = dialog.content.instance;
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-expanded', 'true');
let contentData;
if (currentRangeValidation) {
contentData = {
cellRange: this.currentRange,
to: currentRangeValidation.to,
from: currentRangeValidation.from,
criteria: { type: currentRangeValidation.dataType, name: CRITERIA_LIST.find(i => i.type === currentRangeValidation.dataType).name },
comparer: currentRangeValidation.dataType === 'custom' || currentRangeValidation.dataType === 'list' ? null : { type: currentRangeValidation.comparerType, name: COMPARERS_LIST.find(i => i.type === currentRangeValidation.comparerType).name },
ignoreBlank: currentRangeValidation.allowNulls,
showButton: currentRangeValidation.showButton,
onInvalidData: currentRangeValidation.type
};
if (currentRangeValidation.messageTemplate || currentRangeValidation.titleTemplate) {
contentData = Object.assign(contentData, {
hintMessage: currentRangeValidation.messageTemplate,
hintTitle: currentRangeValidation.titleTemplate,
showHint: true
});
}
}
else {
contentData = { cellRange: this.currentRange };
}
dialogContent.setData(contentData);
dialogContent.dialogAction.pipe(take(1)).subscribe((actionType) => {
if (actionType === this.localization.get('dialogCancel')) {
dialog.close();
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-expanded', 'false');
}
else {
let options = {
type: dialogContent.onInvalidData,
dataType: dialogContent.criteria.type,
comparerType: dialogContent.comparer.type,
from: dialogContent.from,
to: dialogContent.to,
allowNulls: dialogContent.ignoreBlank,
showButton: dialogContent.showButton,
messageTemplate: dialogContent.showHint ? dialogContent.hintMessage : null,
titleTemplate: dialogContent.showHint ? dialogContent.hintTitle : null
};
if (dialogContent.criteria.type === 'list' || dialogContent.criteria.type === 'text') {
const isWrappedInQuotes = options.from.startsWith('"') && options.from.endsWith('"');
let isRangeValue = true;
try {
isRangeValue = Boolean(this.spreadsheetService.spreadsheet.activeSheet().range(dialogContent.from));
}
catch (e) {
isRangeValue = false;
}
if (!isWrappedInQuotes && !isRangeValue) {
options = Object.assign(options, { from: `"${options.from}"` });
}
}
if (actionType === this.localization.get('dialogRemove') || dialogContent.criteria.type === 'any') {
options = null;
}
this.spreadsheetService.spreadsheet.executeCommand({
command: 'EditValidationCommand',
options: { value: options }
});
dialog.close();
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-expanded', 'false');
}
});
dialog.dialog.instance.close.pipe(take(1)).subscribe(() => {
this.renderer.setAttribute(this.button.toolbarButtonElement.nativeElement, 'aria-expanded', 'false');
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SpreadsheetDataValidationDirective, deps: [{ token: i1.ToolBarButtonComponent }, { token: i2.SpreadsheetLocalizationService }, { token: i3.SpreadsheetService }, { token: i4.SpreadsheetToolsService }, { token: i5.DialogService }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: SpreadsheetDataValidationDirective, isStandalone: true, selector: "kendo-toolbar-button[kendoSpreadsheetDataValidation]", usesInheritance: true, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: SpreadsheetDataValidationDirective, decorators: [{
type: Directive,
args: [{
selector: 'kendo-toolbar-button[kendoSpreadsheetDataValidation]',
standalone: true
}]
}], ctorParameters: function () { return [{ type: i1.ToolBarButtonComponent }, { type: i2.SpreadsheetLocalizationService }, { type: i3.SpreadsheetService }, { type: i4.SpreadsheetToolsService }, { type: i5.DialogService }, { type: i0.Renderer2 }]; } });