2023-06-16 17:08:43 +12:00
|
|
|
import {COperationLi} from "./c-operation-li.mjs";
|
2023-07-24 14:54:34 +12:00
|
|
|
import Sortable from "sortablejs";
|
2023-06-16 17:08:43 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* c(ustom element)-operation-list
|
|
|
|
*
|
|
|
|
* @param {App} app - The main view object for CyberChef
|
|
|
|
* @param {string[]} opNames - A list of operation names
|
|
|
|
* @param {boolean} includeStarIcon - optionally add the 'star' icon to the left of the operation
|
|
|
|
* @param {Object} icon ( { class: string, innerText: string } ). check-icon by default
|
|
|
|
*/
|
|
|
|
export class COperationList extends HTMLElement {
|
2023-07-24 14:54:34 +12:00
|
|
|
constructor(
|
|
|
|
app,
|
|
|
|
opNames,
|
|
|
|
includeStarIcon,
|
2023-07-25 14:54:43 +12:00
|
|
|
isSortable = false,
|
|
|
|
isCloneable = true,
|
2023-07-24 14:54:34 +12:00
|
|
|
icon,
|
2023-07-25 14:54:43 +12:00
|
|
|
|
2023-07-24 14:54:34 +12:00
|
|
|
) {
|
2023-06-16 17:08:43 +12:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.app = app;
|
|
|
|
this.opNames = opNames;
|
|
|
|
this.includeStarIcon = includeStarIcon;
|
2023-07-24 14:54:34 +12:00
|
|
|
this.isSortable = isSortable;
|
2023-07-25 14:54:43 +12:00
|
|
|
this.isCloneable = isCloneable;
|
|
|
|
this.icon = icon;
|
2023-06-16 17:08:43 +12:00
|
|
|
}
|
|
|
|
|
2023-07-21 11:47:00 +12:00
|
|
|
/**
|
|
|
|
* Build c-operation-list
|
|
|
|
*/
|
2023-06-16 17:08:43 +12:00
|
|
|
build() {
|
|
|
|
const ul = document.createElement("ul");
|
|
|
|
ul.classList.add("op-list");
|
|
|
|
|
|
|
|
this.opNames.forEach((opName => {
|
2023-08-02 17:38:52 +12:00
|
|
|
const cOpLi = new COperationLi(
|
2023-06-16 17:08:43 +12:00
|
|
|
this.app,
|
|
|
|
opName,
|
|
|
|
{
|
|
|
|
class: this.icon ? this.icon.class : "check-icon",
|
|
|
|
innerText: this.icon ? this.icon.innerText : "check"
|
|
|
|
},
|
|
|
|
this.includeStarIcon
|
|
|
|
);
|
|
|
|
|
2023-08-02 17:38:52 +12:00
|
|
|
ul.appendChild(cOpLi);
|
2023-06-16 17:08:43 +12:00
|
|
|
}))
|
|
|
|
|
2023-07-24 14:54:34 +12:00
|
|
|
if (this.isSortable) {
|
2023-08-02 17:38:52 +12:00
|
|
|
this.createSortableList(ul);
|
2023-07-25 14:54:43 +12:00
|
|
|
} else if (!this.app.isMobileView() && this.isCloneable) {
|
2023-08-02 17:38:52 +12:00
|
|
|
this.createCloneableList(ul, "recipe", "rec-list"); // target name and id can be component params if needed to make it reusable
|
2023-07-24 14:54:34 +12:00
|
|
|
}
|
|
|
|
|
2023-06-16 17:08:43 +12:00
|
|
|
this.append(ul);
|
|
|
|
}
|
2023-08-02 17:38:52 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sortable ( not cloneable ) list
|
|
|
|
*
|
|
|
|
* @param { HTMLElement } ul
|
|
|
|
* */
|
|
|
|
createSortableList(ul) {
|
|
|
|
const sortableList = Sortable.create(ul, {
|
|
|
|
group: "sorting",
|
|
|
|
sort: true,
|
|
|
|
draggable: "c-operation-li",
|
|
|
|
onFilter: function (e) {
|
|
|
|
const el = sortableList.closest(e.item);
|
|
|
|
if (el && el.parentNode) {
|
|
|
|
$(el).popover("dispose");
|
|
|
|
el.parentNode.removeChild(el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onEnd: function(e) {
|
|
|
|
if (this.app.manager.recipe.removeIntent) {
|
|
|
|
$(e.item).popover("dispose");
|
|
|
|
e.item.remove();
|
|
|
|
}
|
|
|
|
}.bind(this),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a cloneable ( not sortable ) list
|
|
|
|
*
|
|
|
|
* @param { HTMLElement } ul
|
|
|
|
* @param { string } targetListName
|
|
|
|
* @param { string } targetListId
|
|
|
|
* */
|
|
|
|
createCloneableList(ul, targetListName, targetListId) {
|
|
|
|
Sortable.create(ul, {
|
|
|
|
group: {
|
|
|
|
name: targetListName,
|
|
|
|
pull: "clone",
|
|
|
|
put: false,
|
|
|
|
},
|
|
|
|
draggable: "c-operation-li",
|
|
|
|
sort: false,
|
|
|
|
setData: function(dataTransfer, dragEl) {
|
|
|
|
dataTransfer.setData("Text", dragEl.querySelector("li").getAttribute("data-name"));
|
|
|
|
},
|
|
|
|
onStart: function(e) {
|
|
|
|
// Removes popover element and event bindings from the dragged operation but not the
|
|
|
|
// event bindings from the one left in the operations list. Without manually removing
|
|
|
|
// these bindings, we cannot re-initialise the popover on the stub operation.
|
|
|
|
$(e.item)
|
|
|
|
.popover("dispose")
|
|
|
|
.removeData("bs.popover")
|
|
|
|
.off("mouseenter")
|
|
|
|
.off("mouseleave")
|
|
|
|
.attr("data-toggle", "popover-disabled");
|
|
|
|
$(e.clone)
|
|
|
|
.off(".popover")
|
|
|
|
.removeData("bs.popover");
|
|
|
|
},
|
|
|
|
// @todo: popovers dont display anymore after dragging into recipe list and then hovering the op
|
|
|
|
onEnd: ({item}) => {
|
|
|
|
if (item.parentNode.id === targetListId) {
|
|
|
|
this.app.manager.recipe.addOperation(item.name);
|
|
|
|
item.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-06-16 17:08:43 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("c-operation-list", COperationList);
|