Example #1
0
 it('should be a new array', () => {
   registry.addCommand('test0', NULL_COMMAND);
   registry.addCommand('test1', NULL_COMMAND);
   let cmds = registry.listCommands();
   cmds.push('test2');
   expect(registry.listCommands()).to.deep.equal(['test0', 'test1']);
 });
Example #2
0
      it('should update the node content on command change event', async () => {
        const id = 'to-be-removed';
        let iconClassValue: string | null = null;
        const cmd = commands.addCommand(id, {
          execute: () => {
            /* no op */
          },
          label: 'Label-only button',
          iconClass: () => iconClassValue
        });
        const button = new CommandToolbarButton({
          commands,
          id
        });
        await render(button);
        const buttonNode = button.node.firstChild as HTMLButtonElement;
        expect(buttonNode.textContent).to.equal('Label-only button');
        expect(buttonNode.classList.contains(iconClassValue)).to.equal(false);

        iconClassValue = 'updated-icon-class';
        commands.notifyCommandChanged(id);
        await render(button);
        const wrapperNode = buttonNode.firstChild as HTMLElement;
        const iconNode = wrapperNode.firstChild as HTMLElement;
        expect(iconNode.classList.contains(iconClassValue)).to.equal(true);

        cmd.dispose();
      });
      it('should restore the widgets in a tracker', done => {
        let tracker = new InstanceTracker<Widget>({
          namespace: 'foo-widget',
          shell: new ApplicationShell()
        });
        let registry = new CommandRegistry();
        let state = new StateDB({ namespace: NAMESPACE });
        let ready = new PromiseDelegate<void>();
        let restorer = new LayoutRestorer({
          first: ready.promise, registry, state
        });
        let called = false;
        let key = `${tracker.namespace}:${tracker.namespace}`;

        registry.addCommand(tracker.namespace, {
          execute: () => { called = true; }
        });
        state.save(key, { data: null }).then(() => {
          return restorer.restore(tracker, {
            args: () => null,
            name: () => tracker.namespace,
            command: tracker.namespace
          });
        }).catch(done);
        ready.resolve(void 0);
        restorer.restored.then(() => { expect(called).to.be(true); })
          .then(() => state.remove(key))
          .then(() => { done(); })
          .catch(done);
      });
      it('should connect a node to a command', () => {
        let called = false;
        const command = 'commandlinker:connect-node';
        const commands = new CommandRegistry();
        const linker = new CommandLinker({ commands });
        let node: HTMLElement;
        let vnode: VirtualNode;
        const disposable = commands.addCommand(command, {
          execute: () => {
            called = true;
          }
        });

        vnode = h.div({ dataset: linker.populateVNodeDataset(command, null) });
        node = VirtualDOM.realize(vnode);
        document.body.appendChild(node);

        expect(called).to.equal(false);
        simulate(node, 'click');
        expect(called).to.equal(true);

        document.body.removeChild(node);
        linker.dispose();
        disposable.dispose();
      });
Example #5
0
      it('should stop routing if returned by a routed command', done => {
        const wanted = ['a', 'b'];
        const recorded: string[] = [];

        commands.addCommand('a', {
          execute: () => {
            recorded.push('a');
          }
        });
        commands.addCommand('b', {
          execute: () => {
            recorded.push('b');
          }
        });
        commands.addCommand('c', { execute: () => router.stop });
        commands.addCommand('d', {
          execute: () => {
            recorded.push('d');
          }
        });

        router.register({ command: 'a', pattern: /.*/, rank: 10 });
        router.register({ command: 'b', pattern: /.*/, rank: 20 });
        router.register({ command: 'c', pattern: /.*/, rank: 30 });
        router.register({ command: 'd', pattern: /.*/, rank: 40 });

        router.routed.connect(() => {
          expect(recorded).to.deep.equal(wanted);
          done();
        });
        router.route();
      });
Example #6
0
 it('should register partial and exact matches', () => {
   let count1 = 0;
   let count2 = 0;
   registry.addCommand('test1', {
     execute: () => { count1++; }
   });
   registry.addCommand('test2', {
     execute: () => { count2++; }
   });
   registry.addKeyBinding({
     keys: ['Ctrl S'],
     selector: `#${elem.id}`,
     command: 'test1'
   });
   registry.addKeyBinding({
     keys: ['Ctrl S', 'Ctrl D'],
     selector: `#${elem.id}`,
     command: 'test2'
   });
   let event1 = generate('keydown', { keyCode: 83, ctrlKey: true });
   let event2 = generate('keydown', { keyCode: 68, ctrlKey: true });
   expect(count1).to.equal(0);
   expect(count2).to.equal(0);
   elem.dispatchEvent(event1);
   expect(count1).to.equal(0);
   expect(count2).to.equal(0);
   elem.dispatchEvent(event2);
   expect(count1).to.equal(0);
   expect(count2).to.equal(1);
 });
Example #7
0
 it('should pick the selector with greater specificity', () => {
   elem.classList.add('test');
   let called1 = false;
   let called2 = false;
   registry.addCommand('test1', {
     execute: () => { called1 = true; }
   });
   registry.addCommand('test2', {
     execute: () => { called2 = true; }
   });
   registry.addKeyBinding({
     keys: ['Ctrl ;'],
     selector: '.test',
     command: 'test1'
   });
   registry.addKeyBinding({
     keys: ['Ctrl ;'],
     selector: `#${elem.id}`,
     command: 'test2'
   });
   let event = generate('keydown', { keyCode: 59, ctrlKey: true });
   elem.dispatchEvent(event);
   expect(called1).to.equal(false);
   expect(called2).to.equal(true);
 });
Example #8
0
      it('should disconnect a node from a command', () => {
        let called = false;
        let command = 'commandlinker:disconnect-node';
        let commands =new CommandRegistry();
        let linker = new CommandLinker({ commands });
        let node = document.createElement('div');
        let disposable = commands.addCommand(command, {
          execute: () => { called = true; }
        });

        document.body.appendChild(node);
        linker.connectNode(node, command, null);

        // Make sure connection is working.
        expect(called).to.be(false);
        simulate(node, 'click');
        expect(called).to.be(true);

        // Reset flag.
        called = false;

        // Make sure disconnection is working.
        linker.disconnectNode(node);
        expect(called).to.be(false);
        simulate(node, 'click');
        expect(called).to.be(false);

        document.body.removeChild(node);
        linker.dispose();
        disposable.dispose();
      });
Example #9
0
  before(() => {
    commands = new CommandRegistry();
    bkoMenu = new BkoMenu({ commands });

    commands.addCommand('test', { execute: () => {} });
    bkoMenu.addItem({command: 'test', submenu: bkoMenu, type: 'submenu'});
  });
Example #10
0
      it('should restore the widgets in a tracker', async () => {
        const tracker = new InstanceTracker<Widget>({
          namespace: 'foo-widget'
        });
        const registry = new CommandRegistry();
        const state = new StateDB({ namespace: NAMESPACE });
        const ready = new PromiseDelegate<void>();
        const restorer = new LayoutRestorer({
          first: ready.promise,
          registry,
          state
        });
        let called = false;
        const key = `${tracker.namespace}:${tracker.namespace}`;

        registry.addCommand(tracker.namespace, {
          execute: () => {
            called = true;
          }
        });
        await state.save(key, { data: null });
        ready.resolve(undefined);
        await restorer.restore(tracker, {
          args: () => null,
          name: () => tracker.namespace,
          command: tracker.namespace
        });
        await restorer.restored;
        expect(called).to.equal(true);
      });