import NoteContextAwareWidget from "./note_context_aware_widget.js";
import attributeService from "../services/attributes.js";
import server from "../services/server.js";
const TPL = `
`;
export default class NoteIconWidget extends NoteContextAwareWidget {
doRender() {
this.$widget = $(TPL);
this.$icon = this.$widget.find('button.note-icon');
this.$iconList = this.$widget.find('.icon-list');
this.$iconList.on('click', 'span', async e => {
const clazz = $(e.target).attr('class');
await attributeService.setLabel(this.noteId,
this.note.hasOwnedLabel('workspace') ? 'workspaceIconClass' : 'iconClass',
clazz
);
});
this.$iconCategory = this.$widget.find("select[name='icon-category']");
this.$iconCategory.on('change', () => this.renderDropdown());
this.$iconCategory.on('click', e => e.stopPropagation());
this.$iconSearch = this.$widget.find("input[name='icon-search']");
this.$iconSearch.on('input', () => this.renderDropdown());
this.$notePathList = this.$widget.find(".note-path-list");
this.$widget.on('show.bs.dropdown', async () => {
const {categories} = (await import('./icon_list.js')).default;
this.$iconCategory.empty();
for (const category of categories) {
this.$iconCategory.append(
$("")
.text(category.name)
.attr("value", category.id)
);
}
this.$iconSearch.val('');
this.renderDropdown();
});
}
async refreshWithNote(note) {
this.$icon.removeClass().addClass(`${note.getIcon()} note-icon`);
}
async entitiesReloadedEvent({loadResults}) {
if (loadResults.isNoteReloaded(this.noteId)) {
this.refresh();
return;
}
for (const attr of loadResults.getAttributes()) {
if (attr.type === 'label'
&& ['iconClass', 'workspaceIconClass'].includes(attr.name)
&& attributeService.isAffecting(attr, this.note)) {
this.refresh();
break;
}
}
}
async renderDropdown() {
const iconToCountPromise = this.getIconToCountMap();
this.$iconList.empty();
if (this.getIconLabels().length > 0) {
this.$iconList.append(
$(``)
.append(
$('Reset to default icon ')
.on('click', () => this.getIconLabels()
.forEach(label => attributeService.removeAttributeById(this.noteId, label.attributeId))
)
)
);
}
const {icons} = (await import('./icon_list.js')).default;
const iconToCount = await iconToCountPromise;
const categoryId = parseInt(this.$iconCategory.find('option:selected').val());
const search = this.$iconSearch.val().trim().toLowerCase();
const filteredIcons = icons.filter(icon => {
if (categoryId && icon.category_id !== categoryId) {
return false;
}
if (search) {
if (!icon.name.includes(search) && !icon.term?.find(t => t.includes(search))) {
return false;
}
}
return true;
});
filteredIcons.sort((a, b) => {
const countA = iconToCount[a.className] || 0;
const countB = iconToCount[b.className] || 0;
return countB - countA;
});
for (const icon of filteredIcons) {
this.$iconList.append(this.renderIcon(icon));
}
this.$iconSearch.focus();
}
async getIconToCountMap() {
const {iconClassToCountMap} = await server.get('other/icon-usage');
return iconClassToCountMap;
}
renderIcon(icon) {
return $('')
.addClass("bx " + icon.className)
.attr("title", icon.name);
}
getIconLabels() {
return this.note.getOwnedLabels()
.filter(label => ['workspaceIconClass', 'iconClass'].includes(label.name));
}
}