import BasicWidget from "../basic_widget.js";
import froca from "../../services/froca.js";
import bulkActionService from "../../services/bulk_action.js";
import utils from "../../services/utils.js";
import server from "../../services/server.js";
import toastService from "../../services/toast.js";
const TPL = `
`;
export default class BulkActionsDialog extends BasicWidget {
doRender() {
this.$widget = $(TPL);
this.$availableActionList = this.$widget.find(".bulk-available-action-list");
this.$existingActionList = this.$widget.find(".bulk-existing-action-list");
this.$widget.on('click', '[data-action-add]', async event => {
const actionName = $(event.target).attr('data-action-add');
await bulkActionService.addAction('bulkaction', actionName);
await this.refresh();
});
this.$executeButton = this.$widget.find(".execute-bulk-actions");
this.$executeButton.on("click", async () => {
await server.post("bulk-action", { noteIds: this.selectedOrActiveNoteIds });
toastService.showMessage("Bulk actions have been executed successfully.", 3000);
utils.closeActiveDialog();
});
}
async refresh() {
this.renderAvailableActions();
const bulkActionNote = await froca.getNote('bulkaction');
const actions = bulkActionService.parseActions(bulkActionNote);
this.$existingActionList.empty();
if (actions.length > 0) {
this.$existingActionList.append(...actions.map(action => action.render()));
} else {
this.$existingActionList.append($("None yet ... add an action by clicking one of the available ones above.
"))
}
}
renderAvailableActions() {
this.$availableActionList.empty();
for (const actionGroup of bulkActionService.ACTION_GROUPS) {
const $actionGroupList = $("");
const $actionGroup = $(" ")
.append($("").text(actionGroup.title + ": "))
.append($actionGroupList);
for (const action of actionGroup.actions) {
$actionGroupList.append(
$('')
.attr('data-action-add', action.actionName)
.text(action.actionTitle)
);
}
this.$availableActionList.append($actionGroup);
}
}
entitiesReloadedEvent({loadResults}) {
// only refreshing deleted attrs, otherwise components update themselves
if (loadResults.getAttributes().find(attr => attr.type === 'label' && attr.name === 'action' && attr.isDeleted)) {
this.refresh();
}
}
async bulkActionsEvent({selectedOrActiveNoteIds}) {
this.selectedOrActiveNoteIds = selectedOrActiveNoteIds;
await this.refresh();
utils.openDialog(this.$widget);
}
}