Skip to content

Commit

Permalink
use mixin for drop
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Jun 27, 2024
1 parent 723dcd9 commit 1d7ea2d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 87 deletions.
37 changes: 2 additions & 35 deletions app/static/src/app/components/graphConfig/GraphConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
<script lang="ts">
import { computed, defineComponent } from "vue";
import { useStore } from "vuex";
import { GraphsAction } from "../../store/graphs/actions";
import DragVariables from "../mixins/dragVariables";
import SelectVariables from "../mixins/selectVariables";
export default defineComponent({
name: "GraphConfig",
Expand All @@ -43,7 +42,7 @@ export default defineComponent({
},
setup(props, { emit }) {
const store = useStore();
const { startDrag, endDrag } = DragVariables(store, emit, false, props.graphIndex);
const { startDrag, endDrag, onDrop, removeVariable } = SelectVariables(store, emit, false, props.graphIndex);
const selectedVariables = computed<string[]>(
() => store.state.graphs.config[props.graphIndex].selectedVariables
);
Expand All @@ -57,38 +56,6 @@ export default defineComponent({
return { "background-color": bgcolor };
};
const updateSelectedVariables = (graphIndex: number, newVariables: string[]) => {
store.dispatch(`graphs/${GraphsAction.UpdateSelectedVariables}`, {
graphIndex,
selectedVariables: newVariables
});
};
// Remove variable from the graph it was dragged from
const removeVariable = (srcGraphIdx: number, variable: string) => {
const srcVariables = [...store.state.graphs.config[srcGraphIdx].selectedVariables].filter(
(v) => v !== variable
);
updateSelectedVariables(srcGraphIdx, srcVariables);
};
const onDrop = (evt: DragEvent) => {
const { dataTransfer } = evt;
const variable = dataTransfer!!.getData("variable");
const srcGraph = dataTransfer!!.getData("srcGraph");
if (srcGraph !== props.graphIndex.toString()) {
// remove from source graph
if (srcGraph !== "hidden") {
removeVariable(parseInt(srcGraph), variable);
}
// add to this graph if necessary
if (!selectedVariables.value.includes(variable)) {
const newVars = [...selectedVariables.value, variable];
updateSelectedVariables(props.graphIndex, newVars);
}
}
};
return {
selectedVariables,
removeVariable,
Expand Down
22 changes: 2 additions & 20 deletions app/static/src/app/components/graphConfig/HiddenVariables.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import VueFeather from "vue-feather";
import tooltip from "../../directives/tooltip";
import * as Color from "color";
import { GraphsGetter } from "../../store/graphs/getters";
import { GraphsAction } from "../../store/graphs/actions";
import DragVariables from "../mixins/dragVariables";
import SelectVariables from "../mixins/selectVariables";
export default defineComponent({
name: "HiddenVariables",
Expand All @@ -46,7 +45,7 @@ export default defineComponent({
},
setup(props, { emit }) {
const store = useStore();
const { startDrag, endDrag } = DragVariables(store, emit, true);
const { startDrag, endDrag, onDrop } = SelectVariables(store, emit, true);
const hiddenVariables = computed<string[]>(() => store.getters[`graphs/${GraphsGetter.hiddenVariables}`]);
const palette = computed(() => store.state.model.paletteModel!);
Expand All @@ -56,23 +55,6 @@ export default defineComponent({
return { "background-color": desatBgColor };
};
const onDrop = (evt: DragEvent) => {
const { dataTransfer } = evt;
const variable = dataTransfer!!.getData("variable");
const srcGraph = dataTransfer!!.getData("srcGraph");
if (srcGraph !== "hidden") {
// remove from source graph
const srcGraphInt = parseInt(srcGraph);
const srcVariables = [...store.state.graphs.config[srcGraphInt].selectedVariables].filter(
(v) => v !== variable
);
store.dispatch(`graphs/${GraphsAction.UpdateSelectedVariables}`, {
graphIndex: srcGraphInt,
selectedVariables: srcVariables
});
}
};
return {
hiddenVariables,
getStyle,
Expand Down
32 changes: 0 additions & 32 deletions app/static/src/app/components/mixins/dragVariables.ts

This file was deleted.

77 changes: 77 additions & 0 deletions app/static/src/app/components/mixins/selectVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {AppState} from "../../store/appState/state";
import {Store} from "vuex";
import {GraphsAction} from "../../store/graphs/actions";
import {computed} from "vue";

export interface SelectVariablesMixin {
startDrag: (evt: DragEvent, variable: string) => void;
endDrag: () => void,
onDrop: (evt: DragEvent) => void,
removeVariable: (srcGraphIdx: number, variable: string) => void
}

export default (
store: Store<AppState>,
emit: (event: string, ...args: any[]) => void,
hiddenVariables: boolean,
graphIndex?: number): SelectVariablesMixin => {

const thisSrcGraph = hiddenVariables ? "hidden" : graphIndex!.toString();

const startDrag = (evt: DragEvent, variable: string) => {
const { dataTransfer } = evt;
dataTransfer!!.dropEffect = "move";
dataTransfer!!.effectAllowed = "move";
dataTransfer!!.setData("variable", variable);
dataTransfer!!.setData("srcGraph", thisSrcGraph);

emit("setDragging", true);
};

const endDrag = () => {
emit("setDragging", false);
};

const updateSelectedVariables = (graphIndex: number, newVariables: string[]) => {
store.dispatch(`graphs/${GraphsAction.UpdateSelectedVariables}`, {
graphIndex,
selectedVariables: newVariables
});
};

// Remove variable from a given graph
const removeVariable = (srcGraphIdx: number, variable: string) => {
const srcVariables = [...store.state.graphs.config[srcGraphIdx].selectedVariables].filter(
(v) => v !== variable
);
updateSelectedVariables(srcGraphIdx, srcVariables);
};

const selectedVariables = computed<string[]>(
() => hiddenVariables ? [] : store.state.graphs.config[graphIndex!].selectedVariables
);

const onDrop = (evt: DragEvent) => {
const { dataTransfer } = evt;
const variable = dataTransfer!!.getData("variable");
const srcGraph = dataTransfer!!.getData("srcGraph");
if (srcGraph !== thisSrcGraph) {
// remove from source graph
if (srcGraph !== "hidden") {
removeVariable(parseInt(srcGraph), variable);
}
// add to this graph if necessary
if (!hiddenVariables && !selectedVariables.value.includes(variable)) {
const newVars = [...selectedVariables.value, variable];
updateSelectedVariables(graphIndex!, newVars);
}
}
};

return {
removeVariable,
startDrag,
endDrag,
onDrop
};
}

0 comments on commit 1d7ea2d

Please sign in to comment.