Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Reset the active index.
this._activeIndex = (
query ? ArrayExt.findFirstIndex(results, Private.canActivate) : -1
);
}
// If there is no query and no results, clear the content.
if (!query && results.length === 0) {
VirtualDOM.render(null, contentNode);
return;
}
// If the is a query but no results, render the empty message.
if (query && results.length === 0) {
let content = this.renderer.renderEmptyMessage({ query });
VirtualDOM.render(content, contentNode);
return;
}
// Create the render content for the search results.
let renderer = this.renderer;
let activeIndex = this._activeIndex;
let content = new Array(results.length);
for (let i = 0, n = results.length; i < n; ++i) {
let result = results[i];
if (result.type === 'header') {
let indices = result.indices;
let category = result.category;
content[i] = renderer.renderHeader({ category, indices });
} else {
let item = result.item;
let indices = result.indices;
// Ensure the search results are generated.
let results = this._results;
if (!results) {
// Generate and store the new search results.
results = this._results = Private.search(this._items, query);
// Reset the active index.
this._activeIndex = (
query ? ArrayExt.findFirstIndex(results, Private.canActivate) : -1
);
}
// If there is no query and no results, clear the content.
if (!query && results.length === 0) {
VirtualDOM.render(null, contentNode);
return;
}
// If the is a query but no results, render the empty message.
if (query && results.length === 0) {
let content = this.renderer.renderEmptyMessage({ query });
VirtualDOM.render(content, contentNode);
return;
}
// Create the render content for the search results.
let renderer = this.renderer;
let activeIndex = this._activeIndex;
let content = new Array(results.length);
for (let i = 0, n = results.length; i < n; ++i) {
let result = results[i];
for (let i = 0, n = results.length; i < n; ++i) {
let result = results[i];
if (result.type === 'header') {
let indices = result.indices;
let category = result.category;
content[i] = renderer.renderHeader({ category, indices });
} else {
let item = result.item;
let indices = result.indices;
let active = i === activeIndex;
content[i] = renderer.renderItem({ item, indices, active });
}
}
// Render the search result content.
VirtualDOM.render(content, contentNode);
// Adjust the scroll position as needed.
if (activeIndex < 0 || activeIndex >= results.length) {
contentNode.scrollTop = 0;
} else {
let element = contentNode.children[activeIndex];
ElementExt.scrollIntoViewIfNeeded(contentNode, element);
}
}
formatLabel(data: IRenderData): h.Child {
// Fetch the label text and mnemonic index.
let { label, mnemonic } = data.item;
// If the index is out of range, do not modify the label.
if (mnemonic < 0 || mnemonic >= label.length) {
return label;
}
// Split the label into parts.
let prefix = label.slice(0, mnemonic);
let suffix = label.slice(mnemonic + 1);
let char = label[mnemonic];
// Wrap the mnemonic character in a span.
let span = h.span({ className: 'p-Menu-itemMnemonic' }, char);
// Return the content parts.
return [prefix, span, suffix];
}
formatLabel(data: IRenderData): h.Child {
// Fetch the label text and mnemonic index.
let { label, mnemonic } = data.title;
// If the index is out of range, do not modify the label.
if (mnemonic < 0 || mnemonic >= label.length) {
return label;
}
// Split the label into parts.
let prefix = label.slice(0, mnemonic);
let suffix = label.slice(mnemonic + 1);
let char = label[mnemonic];
// Wrap the mnemonic character in a span.
let span = h.span({ className: 'p-MenuBar-itemMnemonic' }, char);
// Return the content parts.
return [prefix, span, suffix];
}
}
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, undefined)
});
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();
});
});
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, undefined)
});
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();
});
});
renderCloseIcon(data: IRenderData): VirtualElement {
return h.div({ className: 'p-TabBar-tabCloseIcon' });
}
renderLabel(data: IRenderData): VirtualElement {
let content = this.formatLabel(data);
return h.div({ className: 'p-MenuBar-itemLabel' }, content);
}
renderItemIcon(data: IItemRenderData): VirtualElement {
let className = this.createIconClass(data);
return h.div({ className }, data.item.iconLabel);
}