webdav-js
Version:
WebDAV functionality intended for use as a bookmarklet or to make a simple webserver an interactive WebDAV environment.
97 lines (78 loc) • 2.24 kB
text/typescript
import Element, { on, s } from '@dom111/element';
import joinPath, { trailingSlash } from '../lib/joinPath';
import DAV from '../lib/DAV';
import Entry from '../lib/Entry';
import State from '../lib/State';
import handleFileUpload from '../lib/handleFileUpload';
import { success } from 'melba-toast';
import { t } from 'i18next';
export default class Footer extends Element {
constructor(dav: DAV, state: State) {
super(
s(`<footer class="upload">
<span class="droppable">${t('dropFilesAnywhereToUpload')}</span> ${t('or')}
<span class="files">${t(
'uploadFiles'
)} <input type="file" multiple></span> ${t('or')}
<a href="#" class="create-directory">${t('createNewDirectory')}</a>
</footer>`)
);
this.
this.
this.bindEvents();
}
private bindEvents(): void {
const input = this.query('input[type="file"]') as HTMLInputElement,
createDirectoryLink = this.query(
'.create-directory'
) as HTMLAnchorElement;
on(input, 'change', async (): Promise<void> => {
for (const file of input.files) {
handleFileUpload(this.
}
this.
input.value = null;
});
on(
createDirectoryLink,
'click',
async (event: MouseEvent): Promise<void> => {
event.preventDefault();
const directoryName = prompt('', t('directoryName'));
if (!directoryName) {
return;
}
this.handleCreateDirectory(
trailingSlash(joinPath(location.pathname, directoryName)),
directoryName
);
}
);
}
async handleCreateDirectory(fullPath: string, directoryName: string) {
const result = await this.
if (!result.ok) {
return;
}
success(
t('successfullyCreated', {
interpolation: {
escapeValue: false,
},
directoryName,
})
);
const collection = this.
collection.add(
new Entry({
directory: true,
fullPath,
modified: Date.now(),
collection,
})
);
this.
}
}