it('should override an existing extension default', () => {
   registry.addModelFactory(new TextModelFactory());
   registry.addWidgetFactory(createFactory());
   let factory = createFactory();
   registry.addWidgetFactory(factory);
   expect(registry.defaultWidgetFactory('.foo.bar')).to.be(factory);
 });
Example #2
0
 it('should be removed from the registry when disposed', () => {
   registry = new DocumentRegistry({ initialFileTypes: [] });
   let fileType = { name: 'notebook', extensions: ['.ipynb'] };
   let disposable = registry.addFileType(fileType);
   disposable.dispose();
   expect(toArray(registry.fileTypes()).length).to.be(0);
 });
 it('should be a no-op if a file type of the same name is registered', () => {
   let creator = { name: 'notebook', fileType: 'notebook' };
   registry.addCreator(creator);
   let disposable = registry.addCreator(creator);
   disposable.dispose();
   expect(registry.creators().next()).to.eql(creator);
 });
 it('should be removed from the registry when disposed', () => {
   registry.addModelFactory(new TextModelFactory());
   let factory = createFactory();
   let disposable = registry.addWidgetFactory(factory);
   disposable.dispose();
   expect(registry.getWidgetFactory('test')).to.be(void 0);
 });
Example #5
0
/**
 * Create a context menu for the file browser listing.
 *
 * #### Notes
 * This function generates temporary commands with an incremented name. These
 * commands are disposed when the menu itself is disposed.
 */
function createContextMenu(
  model: Contents.IModel | undefined,
  commands: CommandRegistry,
  registry: DocumentRegistry
): Menu {
  const menu = new Menu({ commands });

  // If the user did not click on any file, we still want to show
  // paste as a possibility.
  if (!model) {
    menu.addItem({ command: CommandIDs.paste });
    return menu;
  }

  menu.addItem({ command: CommandIDs.open });

  const path = model.path;
  if (model.type !== 'directory') {
    const factories = registry.preferredWidgetFactories(path).map(f => f.name);
    const notebookFactory = registry.getWidgetFactory('notebook').name;
    if (
      model.type === 'notebook' &&
      factories.indexOf(notebookFactory) === -1
    ) {
      factories.unshift(notebookFactory);
    }
    if (path && factories.length > 1) {
      const command = 'docmanager:open';
      const openWith = new Menu({ commands });
      openWith.title.label = 'Open With';
      factories.forEach(factory => {
        openWith.addItem({ args: { factory, path }, command });
      });
      menu.addItem({ type: 'submenu', submenu: openWith });
    }
    menu.addItem({ command: CommandIDs.openBrowserTab });
  }

  menu.addItem({ command: CommandIDs.rename });
  menu.addItem({ command: CommandIDs.del });
  menu.addItem({ command: CommandIDs.cut });

  if (model.type !== 'directory') {
    menu.addItem({ command: CommandIDs.copy });
  }

  menu.addItem({ command: CommandIDs.paste });

  if (model.type !== 'directory') {
    menu.addItem({ command: CommandIDs.duplicate });
    menu.addItem({ command: CommandIDs.download });
    menu.addItem({ command: CommandIDs.shutdown });
  }

  menu.addItem({ command: CommandIDs.share });
  menu.addItem({ command: CommandIDs.copyPath });
  menu.addItem({ command: CommandIDs.copyDownloadLink });

  return menu;
}
Example #6
0
 beforeEach(() => {
   registry = new DocumentRegistry();
   registry.addFileType({
     name: 'foobar',
     extensions: ['.foo.bar']
   });
 });
 it('should be a no-op if the extension is already registered for a given widget factory', () => {
   let extension = new WidgetExtension();
   registry.addWidgetExtension('foo', extension);
   let disposable = registry.addWidgetExtension('foo', extension);
   disposable.dispose();
   expect(registry.widgetExtensions('foo').next()).to.be(extension);
 });
 it('should be a no-op if a file type of the same name is registered', () => {
   let fileType = { name: 'notebook', extension: '.ipynb' };
   registry.addFileType(fileType);
   let disposable = registry.addFileType(fileType);
   disposable.dispose();
   expect(registry.fileTypes().next()).to.be(fileType);
 });
Example #9
0
 it('should be a no-op if a file type of the same name is registered', () => {
   registry = new DocumentRegistry({ initialFileTypes: [] });
   let fileType = { name: 'notebook', extensions: ['.ipynb'] };
   registry.addFileType(fileType);
   let disposable = registry.addFileType(fileType);
   disposable.dispose();
   expect(registry.fileTypes().next().name).to.be(fileType.name);
 });
Example #10
0
 it('should add the widget factory to the registry', () => {
   const factory = createFactory();
   registry.addWidgetFactory(factory);
   expect(registry.getWidgetFactory(factory.name)).to.equal(factory);
   expect(registry.getWidgetFactory(factory.name.toUpperCase())).to.equal(
     factory
   );
 });
Example #11
0
 it('should become the global default if `*` is given as a defaultFor', () => {
   let factory = new WidgetFactory({
     name: 'global',
     fileTypes: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(factory);
   expect(registry.defaultWidgetFactory('*').name).to.be('global');
 });
Example #12
0
 it('should get the default widget factory if no default rendered factory is registered', () => {
   let gFactory = new WidgetFactory({
     name: 'global',
     fileTypes: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(gFactory);
   expect(registry.defaultRenderedWidgetFactory('a.md')).to.be(gFactory);
 });
 it('should become the global default if `*` is given as a defaultFor', () => {
   registry.addModelFactory(new TextModelFactory());
   let factory = new WidgetFactory({
     name: 'global',
     fileExtensions: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(factory);
   expect(registry.defaultWidgetFactory('*').name).to.be('global');
 });
Example #14
0
 it('should handle just a multi-part extension', () => {
   let factory = new WidgetFactory({
     name: 'table',
     fileTypes: ['tablejson'],
   });
   registry.addWidgetFactory(factory);
   let factories = registry.preferredWidgetFactories('foo.table.json');
   expect(toArray(factories)).to.eql([factory]);
   factories = registry.preferredWidgetFactories('foo.json');
   expect(toArray(factories)).to.eql([]);
 });
 it('should get a file type by name', () => {
   let fileTypes = [
     { name: 'notebook', extension: '.ipynb' },
     { name: 'python', extension: '.py' }
   ];
   registry.addFileType(fileTypes[0]);
   registry.addFileType(fileTypes[1]);
   expect(registry.getFileType('notebook')).to.be(fileTypes[0]);
   expect(registry.getFileType('python')).to.be(fileTypes[1]);
   expect(registry.getFileType('r')).to.be(void 0);
 });
Example #16
0
 it('should select the factory for a given extension', () => {
   let factory = createFactory();
   registry.addWidgetFactory(factory);
   let mdFactory = new WidgetFactory({
     name: 'markdown',
     fileTypes: ['markdown'],
   });
   registry.addWidgetFactory(mdFactory);
   expect(registry.preferredWidgetFactories('a.txt')[0]).to.be(factory);
   expect(registry.preferredWidgetFactories('a.md')[0]).to.be(mdFactory);
 });
 it('should handle just a multi-part extension', () => {
   registry.addModelFactory(new TextModelFactory());
   let factory = new WidgetFactory({
     name: 'table',
     fileExtensions: ['.table.json'],
   });
   registry.addWidgetFactory(factory);
   let factories = registry.preferredWidgetFactories('.table.json');
   expect(toArray(factories)).to.eql([factory]);
   factories = registry.preferredWidgetFactories('.json');
   expect(toArray(factories)).to.eql([]);
 });
Example #18
0
 it('should get the default rendered widget factory for a given extension', () => {
   let factory = createFactory();
   registry.addWidgetFactory(factory);
   let mdFactory = new WidgetFactory({
     name: 'markdown',
     fileTypes: ['markdown'],
     defaultRendered: ['markdown']
   });
   registry.addWidgetFactory(mdFactory);
   expect(registry.defaultRenderedWidgetFactory('a.baz')).to.be(factory);
   expect(registry.defaultRenderedWidgetFactory('a.md')).to.be(mdFactory);
 });
Example #19
0
 it('should give the valid registered widget factories', () => {
   expect(toArray(registry.preferredWidgetFactories('foo.txt'))).to.eql([]);
   let factory = createFactory();
   registry.addWidgetFactory(factory);
   let gFactory = new WidgetFactory({
     name: 'global',
     fileTypes: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(gFactory);
   let factories = registry.preferredWidgetFactories('a.foo.bar');
   expect(toArray(factories)).to.eql([factory, gFactory]);
 });
Example #20
0
 activate: (): IDocumentRegistry => {
   let registry = new DocumentRegistry();
   registry.addModelFactory(new TextModelFactory());
   registry.addModelFactory(new Base64ModelFactory());
   registry.addFileType({
     name: 'Text',
     extension: '.txt',
     contentType: 'file',
     fileFormat: 'text'
   });
   registry.addCreator({ name: 'Text File', fileType: 'Text', });
   return registry;
 }
Example #21
0
 beforeEach(() => {
   let registry = new DocumentRegistry();
   registry.addModelFactory(modelFactory);
   registry.addWidgetFactory(widgetFactory);
   manager = new DocumentManager({
     registry,
     manager: services,
     opener: {
       open: (widget: Widget) => {
         openedWidget = widget;
       }
     }
   });
 });
 it('should end up in locale order', () => {
   let creators = [
     { name: 'Python Notebook', fileType: 'notebook' },
     { name: 'R Notebook', fileType: 'notebook' },
     { name: 'CSharp Notebook', fileType: 'notebook' }
   ];
   registry.addCreator(creators[0]);
   registry.addCreator(creators[1]);
   registry.addCreator(creators[2]);
   let it = registry.creators();
   expect(it.next()).to.be(creators[2]);
   expect(it.next()).to.be(creators[0]);
   expect(it.next()).to.be(creators[1]);
 });
Example #23
0
 it('should override an existing global default', () => {
   registry.addWidgetFactory(new WidgetFactory({
     name: 'global',
     fileTypes: ['*'],
     defaultFor: ['*']
   }));
   let factory = new WidgetFactory({
     name: 'bar',
     fileTypes: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(factory);
   expect(registry.defaultWidgetFactory('*').name).to.be('bar');
 });
 it('should get the registered file types', () => {
   expect(toArray(registry.fileTypes()).length).to.be(0);
   let fileTypes = [
     { name: 'notebook', extension: '.ipynb' },
     { name: 'python', extension: '.py' },
     { name: 'table', extension: '.table.json' }
   ];
   registry.addFileType(fileTypes[0]);
   registry.addFileType(fileTypes[1]);
   registry.addFileType(fileTypes[2]);
   let values = registry.fileTypes();
   expect(values.next()).to.be(fileTypes[0]);
   expect(values.next()).to.be(fileTypes[1]);
   expect(values.next()).to.be(fileTypes[2]);
 });
Example #25
0
 beforeEach(() => {
   const registry = new DocumentRegistry({ textModelFactory });
   registry.addWidgetFactory(widgetFactory);
   DocumentRegistry.defaultFileTypes.forEach(ft => {
     registry.addFileType(ft);
   });
   manager = new DocumentManager({
     registry,
     manager: services,
     opener: {
       open: (widget: Widget) => {
         // no-op
       }
     }
   });
 });
Example #26
0
  node.addEventListener('contextmenu', (event: MouseEvent) => {
    event.preventDefault();
    let path = fbWidget.pathForClick(event) || '';
    let ext = DocumentRegistry.extname(path);
    let factories = registry.preferredWidgetFactories(ext);
    let widgetNames = toArray(map(factories, factory => factory.name));
    let prefix = `${namespace}-contextmenu-${++Private.id}`;
    let openWith: Menu = null;
    if (path && widgetNames.length > 1) {
      let disposables = new DisposableSet();
      let command: string;

      openWith = new Menu({ commands });
      openWith.title.label = 'Open With...';
      openWith.disposed.connect(() => { disposables.dispose(); });

      for (let widgetName of widgetNames) {
        command = `${prefix}:${widgetName}`;
        disposables.add(commands.addCommand(command, {
          execute: () => fbWidget.openPath(path, widgetName),
          label: widgetName
        }));
        openWith.addItem({ command });
      }
    }

    let menu = createContextMenu(fbWidget, openWith);
    menu.open(event.clientX, event.clientY);
  });
Example #27
0
/**
 * Create a context menu for the file browser listing.
 *
 * #### Notes
 * This function generates temporary commands with an incremented name. These
 * commands are disposed when the menu itself is disposed.
 */
function createContextMenu(path: string, commands: CommandRegistry, registry: IDocumentRegistry):  Menu {
  const menu = new Menu({ commands });

  menu.addItem({ command: CommandIDs.open });

  const ext = DocumentRegistry.extname(path);
  const factories = registry.preferredWidgetFactories(ext).map(f => f.name);
  if (path && factories.length > 1) {
    const command =  'file-operations:open';
    const openWith = new Menu({ commands });
    openWith.title.label = 'Open With...';
    factories.forEach(factory => {
      openWith.addItem({ args: { factory, path }, command });
    });
    menu.addItem({ type: 'submenu', submenu: openWith });
  }

  menu.addItem({ command: CommandIDs.rename });
  menu.addItem({ command: CommandIDs.del });
  menu.addItem({ command: CommandIDs.duplicate });
  menu.addItem({ command: CommandIDs.cut });
  menu.addItem({ command: CommandIDs.copy });
  menu.addItem({ command: CommandIDs.paste });
  menu.addItem({ command: CommandIDs.download });
  menu.addItem({ command: CommandIDs.shutdown });

  return menu;
}
Example #28
0
 it('should respect the priority order', () => {
   let factory = createFactory();
   registry.addWidgetFactory(factory);
   let gFactory = new WidgetFactory({
     name: 'global',
     fileTypes: ['*'],
     defaultFor: ['*']
   });
   registry.addWidgetFactory(gFactory);
   let mdFactory = new WidgetFactory({
     name: 'markdown',
     fileTypes: ['markdown'],
   });
   registry.addWidgetFactory(mdFactory);
   let factories = registry.preferredWidgetFactories('a.txt');
   expect(toArray(factories)).to.eql([factory, gFactory]);
 });
      it('should get a kernel preference', () => {
        registry.addModelFactory(new TextModelFactory());
        registry.addWidgetFactory(createFactory());
        registry.addWidgetFactory(new WidgetFactory({
          name: 'python',
          fileExtensions: ['.py'],
          preferKernel: true,
          canStartKernel: true
        }));
        registry.addWidgetFactory(new WidgetFactory({
          name: 'global',
          fileExtensions: ['*'],
          defaultFor: ['*']
        }));
        let pref = registry.getKernelPreference('.c', 'global');
        expect(pref.language).to.be('clike');
        expect(pref.shouldStart).to.be(false);
        expect(pref.canStart).to.be(false);

        pref = registry.getKernelPreference('.py', 'python');
        expect(pref.language).to.be('python');
        expect(pref.shouldStart).to.be(true);
        expect(pref.canStart).to.be(true);

        pref = registry.getKernelPreference('.py', 'baz');
        expect(pref).to.be(void 0);
      });
Example #30
0
 it('should get the most specific extension first', () => {
   [
     { name: 'json', extensions: ['.json'] },
     { name: 'vega', extensions: ['.vg.json'] }
   ].forEach(ft => {registry.addFileType(ft); });
   let ft = registry.getFileTypesForPath('foo/bar/baz.vg.json');
   expect(ft[0].name).to.be('vega');
   expect(ft[1].name).to.be('json');
 });