File size: 7,486 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | import {
getMoleculesFromStore,
setStoreVar,
} from "@/Store/StoreExternalAccess";
import { TreeNodeType } from "@/UI/Navigation/TreeView/TreeInterfaces";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import isEqual from "lodash.isequal";
import { ISelAndStyle } from "./SelAndStyleInterfaces";
import { defaultStyles } from "./SelAndStyleDefinitions";
import { messagesApi } from "@/Api/Messages"; // Added import
import { reactive } from "vue"; // Import reactive
// These are the styles actually used. It is initially set to be the same as the
// defaults, but it will change per user specifications.
export const currentSelsAndStyles: { [key in TreeNodeType]: ISelAndStyle[] } =
JSON.parse(JSON.stringify(defaultStyles));
// These are the custom styles that the user can add. They are applied to every
// molecule.
export const customSelsAndStyles: { [key: string]: ISelAndStyle } = reactive({
// "Blue LYS": {
// selection: {
// resn: "LYS",
// },
// sphere: {
// color: "blue",
// },
// },
// "TRP red": {
// selection: {
// resn: "TRP",
// },
// stick: {
// color: "red",
// },
// },
});
// setInterval(() => {
// console.log(JSON.stringify(customSelsAndStyles, null, 2));
// }, 1000);
const disabledCustomStyleNames: Set<string> = reactive(new Set<string>());
/**
* Checks if a custom style is currently enabled.
*
* @param {string} name The name of the custom style.
* @returns {boolean} True if the style is enabled, false otherwise.
*/
export function isCustomStyleEnabled(name: string): boolean {
return !disabledCustomStyleNames.has(name);
}
/**
* Toggles the enabled/disabled state of a custom style.
*
* @param {string} name The name of the custom style to toggle.
*/
export function toggleCustomStyle(name: string): void {
if (disabledCustomStyleNames.has(name)) {
disabledCustomStyleNames.delete(name);
} else {
disabledCustomStyleNames.add(name);
}
updateStylesInViewer();
}
/**
* Deletes a custom style.
*
* @param {string} name The name of the custom style to delete.
*/
export function deleteCustomStyle(name: string): void {
delete customSelsAndStyles[name];
disabledCustomStyleNames.delete(name); // Ensure it's also removed from disabled set
updateStylesInViewer();
}
/**
* Adds a new custom style to the application.
*
* @param {string} name The name of the custom style.
* @param {ISelAndStyle} style The custom style object.
* @param {boolean} [overwrite=false] Whether to overwrite if a style with the same name exists.
* @returns {boolean} True if the style was added/updated, false if a name collision occurred and overwrite was false.
*/
export function addCustomStyle(
name: string,
style: ISelAndStyle,
overwrite = false
): boolean {
if (customSelsAndStyles[name] && !overwrite) {
messagesApi.popupError(
`A custom visualization with the name "${name}" already exists.`
);
return false;
}
customSelsAndStyles[name] = style;
updateStylesInViewer(); // Trigger viewer update
// The VizualizationsCustom.vue component uses a computed property that directly reads
// from customSelsAndStyles. Vue's reactivity should handle the update
// automatically if customSelsAndStyles is a reactive object.
// If it doesn't, we might need an event bus or a different reactivity trigger.
return true;
}
/**
* Replaces all custom styles with a new set.
*
* @param {{ string: ISelAndStyle }} newStyles The new styles to apply.
*/
export function replaceAllCustomStyles(newStyles: {
[key: string]: ISelAndStyle;
}): void {
// Clear existing styles
for (const name in customSelsAndStyles) {
delete customSelsAndStyles[name];
}
disabledCustomStyleNames.clear();
// Add new styles
for (const name in newStyles) {
customSelsAndStyles[name] = newStyles[name];
}
updateStylesInViewer();
}
/**
* Updates the styles in the viewer.
*
* @param {TreeNodeType} [treeNodeType] The type of node to update. If
* undefined, all node types are updated.
*/
export function updateStylesInViewer(treeNodeType?: TreeNodeType) {
// If treeNodeType is undefined, update all node types.
const treeNodeTypes: TreeNodeType[] = treeNodeType
? [treeNodeType]
: Object.values(TreeNodeType);
// Get all molecules from the store
const molecules = getMoleculesFromStore();
// iterate through terminal nodes
const terminalNodes = molecules.filters.onlyTerminal;
for (let idx = 0; idx < terminalNodes.length; idx++) {
const terminalNode = terminalNodes.get(idx);
// Terminal node must have a type, styles, and be visible.
if (
!terminalNode.type ||
!terminalNode.styles ||
terminalNode.type === TreeNodeType.Other // ||
// !terminalNode.visible
) {
// Note that regions do not have styles. Also, don't mess with Other nodes. The styles of these
// must be set explicitly (TreeNode.styles = [{...}])
continue;
}
// Iterate through the node types you're considering.
for (let i = 0; i < treeNodeTypes.length; i++) {
const molType = treeNodeTypes[i];
const selStyle = currentSelsAndStyles[molType];
// Check if the node type matches this type. If not, skip to the
// next node.
if (terminalNode.type !== molType) {
continue;
}
// Add the styles to the node list if it's not empty ({}).
terminalNode.styles = [];
if (!isEqual(selStyle, {})) {
terminalNode.styles.push(...selStyle);
}
// Also add all custom styles to the node list.
if (Object.keys(customSelsAndStyles).length > 0) {
// Add custom styles to the node list.
for (const [styleName, customSelAndStyle] of Object.entries(
customSelsAndStyles
)) {
if (disabledCustomStyleNames.has(styleName)) {
continue;
}
// Check if the custom style is not empty ({}).
if (!isEqual(customSelAndStyle, {})) {
if (customSelAndStyle.moleculeId) {
// This style is for a specific molecule.
if (
customSelAndStyle.moleculeId === terminalNode.id
) {
terminalNode.styles.push(customSelAndStyle);
}
} else {
// This style is for all molecules.
terminalNode.styles.push(customSelAndStyle);
}
}
}
}
// Mark this for rerendering in viewer.
// console.log("MOO", JSON.stringify(terminalNode.styles, null, 2));
terminalNode.viewerDirty = true;
}
}
// Update all molecules. Note that this triggers reactivity
// onTreeviewChanged() in ViewerPanel.vue.
setStoreVar("molecules", molecules);
}
|