/* * Copyright (c) 2010, 2023 BSI Business Systems Integration AG * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 */ import { Action, arrays, BoxButtons, ClickActiveElementKeyStroke, CloseKeyStroke, Device, DisplayParent, dragAndDrop, DragAndDropHandler, Event, FileChooserEventMap, FileChooserModel, FileInput, FileInputChangeEvent, files as fileUtil, FocusAdjacentElementKeyStroke, FocusRule, Form, FormLayout, GlassPaneRenderer, HtmlComponent, InitModelOf, keys, KeyStrokeContext, MessageBoxes, scout, scrollbars, Status, Widget } from '../index'; export class FileChooser extends Widget implements FileChooserModel { declare model: FileChooserModel; declare eventMap: FileChooserEventMap; declare self: FileChooser; maximumUploadSize: number; acceptTypes: string; multiSelect: boolean; displayParent: DisplayParent; files: File[]; boxButtons: BoxButtons; uploadButton: Action; cancelButton: Action; fileInput: FileInput; $content: JQuery; $title: JQuery; $files: JQuery; dragAndDropHandler: DragAndDropHandler; protected _glassPaneRenderer: GlassPaneRenderer; constructor() { super(); this.displayParent = null; this.files = []; this._glassPaneRenderer = null; this.maximumUploadSize = FileInput.DEFAULT_MAXIMUM_UPLOAD_SIZE; this.boxButtons = null; this.uploadButton = null; this.cancelButton = null; this.inheritAccessibility = false; // inherit not necessary. if the FileChooser can be opened, it must be editable. Opening a disabled chooser makes no sense. this._addWidgetProperties(['boxButtons', 'uploadButton', 'cancelButton']); } protected override _init(model: InitModelOf) { super._init(model); this._setDisplayParent(this.displayParent); this._glassPaneRenderer = new GlassPaneRenderer(this); this.fileInput = scout.create(FileInput, { parent: this, acceptTypes: this.acceptTypes, maximumUploadSize: this.maximumUploadSize, multiSelect: this.multiSelect, visible: !Device.get().supportsFile() }); this.fileInput.on('change', this._onFileChange.bind(this)); this.boxButtons = scout.create(BoxButtons, {parent: this}); let addFileButton = this.boxButtons.addButton({text: this.session.text('ui.Browse')}); addFileButton.on('action', event => this._onAddFileButtonClicked(event)); this.uploadButton = this.boxButtons.addButton({ text: this.session.text('ui.Upload'), enabled: false }); this.uploadButton.on('action', event => this._onUploadButtonClicked(event)); this.cancelButton = this.boxButtons.addButton({text: this.session.text('Cancel')}); this.cancelButton.on('action', event => this._onCancelButtonClicked(event)); } protected override _createKeyStrokeContext(): KeyStrokeContext { return new KeyStrokeContext(); } protected override _initKeyStrokeContext() { super._initKeyStrokeContext(); this.keyStrokeContext.registerKeyStrokes([ new FocusAdjacentElementKeyStroke(this.session, this), new ClickActiveElementKeyStroke(this, [keys.SPACE, keys.ENTER]), new CloseKeyStroke(this, (() => { if (!this.cancelButton) { return null; } return this.cancelButton.$container; })) ]); } protected override _render() { this.$container = this.$parent.appendDiv('file-chooser') .on('mousedown', this._onMouseDown.bind(this)); let $handle = this.$container.appendDiv('drag-handle'); this.$container.draggable($handle); this.$content = this.$container.appendDiv('file-chooser-content'); this.$title = this.$content.appendDiv('file-chooser-title') .text(this.session.text(this.multiSelect ? 'ui.ChooseFiles' : 'ui.ChooseFile')); this.fileInput.render(this.$content); // explanation for file chooser this.$content.appendDiv('file-chooser-label') .text(this.session.text('ui.FileChooserHint')); // List of files this.$files = this.$content.appendElement('