Vue/graf/src/middleware/GrafModel.js
// Given a newly selected/unselected element, updates the selection correspondingly
Given a newly selected/unselected element, updates the selection correspondingly
Given a newly selected / unselected element updates the selection correspondingly
updateSelection(type, id, was_selected) {
		let new_type = type;
		let new_amount = 1;
		let selection_type = null;
		let amount = 0;
		let length = 0;
		let is_empty = this.selection == null;
		let is_node = type == "Node";
		if (!is_empty) {
			selection_type = this.selection.getSelectionType();
			amount = this.selection.getSelectionAmount();
			length = this.selection.getSelectionLength();
		}
		let types_match = type == selection_type;
		if (!is_empty && !was_selected) {
			new_amount = Math.min(length + 1, 3);
		}
		else if (!is_empty) {
			new_amount = Math.min(3, length - 1);
		}
		if (!is_empty && !types_match && !was_selected) {
			new_type = "Hybrid";
		}
		else if (!is_empty && !types_match && was_selected && is_node && this.selection.getNodeCount() == 1) {
			new_type = "Edge";
		}
		else if (!is_empty && !types_match && was_selected && !is_node && this.selection.getEdgeCount() == 1) {
			new_type = "Node";
		}
		else if (!is_empty && !types_match && was_selected && selection_type != "Hybrid") {
			console.log("ERROR: Tried to remove an invalid type from selection.");
		}
		if (!is_empty && new_amount == 2 && (new_type == "Hybrid" || new_type == "Edge")) {
			new_amount = 3;
		}
		if (!is_empty && new_type == selection_type && new_amount == amount) {
			new_type = null;
			new_amount = null;
		}
		let needs_update = new_type != null && new_amount != null;
		if (new_amount == 0) {
			this.selection = null;
			is_empty = true;
			needs_update = false;
		}
		let new_selection = null;
		if (needs_update) {
			new_selection = this.createSelection(new_type, new_amount);
		}
		if (!is_empty && needs_update) {
			new_selection.setNodes(this.selection.getSelectedNodeIds());
			new_selection.setEdges(this.selection.getSelectedEdgeIds());
		}
		if (needs_update) {
			this.selection = new_selection;
			is_empty = false;
		}
		if (!is_empty && type == "Node" && !was_selected) {
			this.selection.addNode(id);
		}
		else if (!is_empty && type == "Edge" && !was_selected) {
			this.selection.addEdge(id);
		}
		else if (!is_empty && type == "Node" && was_selected) {
			this.selection.removeNode(id);
		}
		else if (!is_empty && type == "Edge" && was_selected) {
			this.selection.removeEdge(id);
		}
	}
updateSelection ( type , id , was_selected ) { let new_type = type ; let new_amount = 1 ; let selection_type = null ; let amount = 0 ; let length = 0 ; let is_empty = this . selection == null ; let is_node = type == "Node" ; if ( ! is_empty ) { selection_type = this . selection . getSelectionType ( ) ; amount = this . selection . getSelectionAmount ( ) ; length = this . selection . getSelectionLength ( ) ; } let types_match = type == selection_type ; if ( ! is_empty && ! was_selected ) { new_amount = Math . min ( length + 1 , 3 ) ; } else if ( ! is_empty ) { new_amount = Math . min ( 3 , length - 1 ) ; } if ( ! is_empty && ! types_match && ! was_selected ) { new_type = "Hybrid" ; } else if ( ! is_empty && ! types_match && was_selected && is_node && this . selection . getNodeCount ( ) == 1 ) { new_type = "Edge" ; } else if ( ! is_empty && ! types_match && was_selected && ! is_node && this . selection . getEdgeCount ( ) == 1 ) { new_type = "Node" ; } else if ( ! is_empty && ! types_match && was_selected && selection_type != "Hybrid" ) { console . log ( "ERROR: Tried to remove an invalid type from selection." ) ; } if ( ! is_empty && new_amount == 2 && ( new_type == "Hybrid" || new_type == "Edge" ) ) { new_amount = 3 ; } if ( ! is_empty && new_type == selection_type && new_amount == amount ) { new_type = null ; new_amount = null ; } let needs_update = new_type != null && new_amount != null ; if ( new_amount == 0 ) { this . selection = null ; is_empty = true ; needs_update = false ; } let new_selection = null ; if ( needs_update ) { new_selection = this . createSelection ( new_type , new_amount ) ; } if ( ! is_empty && needs_update ) { new_selection . setNodes ( this . selection . getSelectedNodeIds ( ) ) ; new_selection . setEdges ( this . selection . getSelectedEdgeIds ( ) ) ; } if ( needs_update ) { this . selection = new_selection ; is_empty = false ; } if ( ! is_empty && type == "Node" && ! was_selected ) { this . selection . addNode ( id ) ; } else if ( ! is_empty && type == "Edge" && ! was_selected ) { this . selection . addEdge ( id ) ; } else if ( ! is_empty && type == "Node" && was_selected ) { this . selection . removeNode ( id ) ; } else if ( ! is_empty && type == "Edge" && was_selected ) { this . selection . removeEdge ( id ) ; } }
Given a newly selected/unselected element, updates the selection correspondingly
Given a newly selected / unselected element updates the selection correspondingly
// was_selected is whether the node or edge is selected *before* it was clicked // Default values, i.e. adding one element to an empty selection // If adding to the selection, cap the new amount at 3 (many) // When unselecting changes the selection type // Case of removing last node // Case of removing last edge // Shouldn't happen, hypothetically // Ensure the new amount is correct for Hybrid/Edge selections // Selection type remains the same // Set new changes to null so we skip the next two if statements // If the selection type changes // Nullify selection if new_amount is zero // Choose new selection type if changed // Update selection by adding or removing elements to appropriate arrays