Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
preferredWidgetFactories(path: string): DocumentRegistry.WidgetFactory[] {
let factories = new Set();
// Get the ordered matching file types.
let fts = this.getFileTypesForPath(PathExt.basename(path));
// Start with any user overrides for the defaults.
fts.forEach(ft => {
if (ft.name in this._defaultWidgetFactoryOverrides) {
factories.add(this._defaultWidgetFactoryOverrides[ft.name]);
}
});
// Next add the file type default factories.
fts.forEach(ft => {
if (ft.name in this._defaultWidgetFactories) {
factories.add(this._defaultWidgetFactories[ft.name]);
}
});
// Add the file type default rendered factories.
preferredWidgetFactories(path: string): DocumentRegistry.WidgetFactory[] {
let factories = new Set();
// Get the ordered matching file types.
let fts = this.getFileTypesForPath(PathExt.basename(path));
// Start with the file type default factories.
fts.forEach(ft => {
if (ft.name in this._defaultWidgetFactories) {
factories.add(this._defaultWidgetFactories[ft.name]);
}
});
// Add the file type default rendered factories.
fts.forEach(ft => {
if (ft.name in this._defaultRenderedWidgetFactories) {
factories.add(this._defaultRenderedWidgetFactories[ft.name]);
}
});
// Add the global default factory.
basePath = PathExt.join(basePath, items[index].name);
}
const manager = this._manager;
// Handle the items.
const promises: Promise[] = [];
const paths = event.mimeData.getData(CONTENTS_MIME) as string[];
if (event.ctrlKey && event.proposedAction === 'move') {
event.dropAction = 'copy';
} else {
event.dropAction = event.proposedAction;
}
for (let path of paths) {
let localPath = manager.services.contents.localPath(path);
let name = PathExt.basename(localPath);
let newPath = PathExt.join(basePath, name);
// Skip files that are not moving.
if (newPath === path) {
continue;
}
if (event.dropAction === 'copy') {
promises.push(manager.copy(path, basePath));
} else {
promises.push(renameFile(manager, path, newPath));
}
}
Promise.all(promises).catch(error => {
void showErrorMessage('Error while copying/moving files', error);
});
}
getFileTypesForPath(path: string): DocumentRegistry.IFileType[] {
let fts: DocumentRegistry.IFileType[] = [];
let name = PathExt.basename(path);
// Look for a pattern match first.
let ft = find(this._fileTypes, ft => {
return ft.pattern && ft.pattern.match(name) !== null;
});
if (ft) {
fts.push(ft);
}
// Then look by extension name, starting with the longest
let ext = Private.extname(name);
while (ext.length > 1) {
ft = find(this._fileTypes, ft => ft.extensions.indexOf(ext) !== -1);
if (ft) {
fts.push(ft);
}
getFileTypesForPath(path: string): DocumentRegistry.IFileType[] {
let fts: DocumentRegistry.IFileType[] = [];
let name = PathExt.basename(path);
// Look for a pattern match first.
let ft = find(this._fileTypes, ft => {
return ft.pattern && ft.pattern.match(name) !== null;
});
if (ft) {
fts.push(ft);
}
// Then look by extension name, starting with the longest
let ext = Private.extname(name);
while (ext.length > 1) {
ft = find(this._fileTypes, ft => ft.extensions.indexOf(ext) !== -1);
if (ft) {
fts.push(ft);
}
it('should return the last portion of a path', () => {
expect(PathExt.basename(TESTPATH)).to.equal('test-path.js');
});
});
export function extname(path: string): string {
let parts = PathExt.basename(path).split('.');
parts.shift();
let ext = '.' + parts.join('.');
return ext.toLowerCase();
}
/**
private _onPathChange = (
_documentModel: DocumentRegistry.IContext,
newPath: string
) => {
const oldState = this._getAllState();
this._path = newPath;
this._name = PathExt.basename(newPath);
this._triggerChange(oldState, this._getAllState());
};
getKernelPreference(
path: string,
widgetName: string,
kernel?: Partial
): IClientSession.IKernelPreference | undefined {
widgetName = widgetName.toLowerCase();
let widgetFactory = this._widgetFactories[widgetName];
if (!widgetFactory) {
return void 0;
}
let modelFactory = this.getModelFactory(widgetFactory.modelName || 'text');
if (!modelFactory) {
return void 0;
}
let language = modelFactory.preferredLanguage(PathExt.basename(path));
let name = kernel && kernel.name;
let id = kernel && kernel.id;
return {
id,
name,
language,
shouldStart: widgetFactory.preferKernel,
canStart: widgetFactory.canStartKernel,
shutdownOnClose: widgetFactory.shutdownOnClose
};
}
constructor(options: FileBrowserModel.IOptions) {
this.manager = options.manager;
this._driveName = options.driveName || '';
let rootPath = this._driveName ? this._driveName + ':' : '';
this._model = {
path: rootPath,
name: PathExt.basename(rootPath),
type: 'directory',
content: undefined,
writable: false,
created: 'unknown',
last_modified: 'unknown',
mimetype: 'text/plain',
format: 'text'
};
this._state = options.state || null;
const { services } = options.manager;
services.contents.fileChanged.connect(this._onFileChanged, this);
services.sessions.runningChanged.connect(this._onRunningChanged, this);
this._scheduleUpdate();
this._startTimer();