Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c6075729

Přidáno uživatelem Pavel Fidranský před více než 6 roky(ů)

reworked VertexContextMenuList to ES6 class

Zobrazit rozdíly:

sources/src/main/webapp/js/components/vertex.js
806 806
		var nodeList = [].concat(excludedNodeList, includedGroupList);
807 807
		if (nodeList.length === 0) return;
808 808

  
809
		app.viewportComponent.contextMenuComponent.setVertex(this);
809
		app.viewportComponent.contextMenuComponent.vertex = this;
810
		app.viewportComponent.contextMenuComponent.position = new Coordinates(e.clientX, e.clientY);
810 811

  
811 812
		// fill list with items
812 813
		nodeList.forEach(function(node) {
813 814
			app.viewportComponent.contextMenuComponent.addNode(node);
814 815
		});
815 816

  
816
		app.viewportComponent.contextMenuComponent.setPosition(new Coordinates(e.clientX, e.clientY));
817 817
		app.viewportComponent.contextMenuComponent.open();
818 818
	}
819 819
	
sources/src/main/webapp/js/components/vertexContextMenuList.js
2 2
 * Class representing a context menu of a vertex displayed on right clicking the vertex in viewport. The menu displays a list of
3 3
 * groups and excluded vertices that the parental vertex can be added to.
4 4
 * @see Vertex
5
 * @constructor
6 5
 */
7
function VertexContextMenuList() {
8
	var rootElement;
9
	var listElement;
6
class VertexContextMenuList {
7
	/**
8
	 * @constructor
9
	 */
10
	constructor() {
11
		this._vertex = null;
12
	}
13

  
14
	/**
15
	 * Creates a new DOM element representing the context menu in memory.
16
	 * @public
17
	 * @returns {HTMLElement} HTML DOM element.
18
	 */
19
	render() {
20
		this._listElement = DOM.h('ul');
10 21

  
11
	var vertex;
22
		this._rootElement = DOM.h('div', {
23
			class: 'context-menu hidden',
24
			onMouseDown: Utils.stopPropagation,
25
		}, [
26
			this._listElement,
27
		]);
28

  
29
		return this._rootElement;
30
	};
12 31

  
13 32
	/**
14 33
	 * Sets a vertex that this context menu is bound to. The vertex is then added to a group.
34
	 * @public
15 35
	 * @param {Vertex} newValue Vertex this context menu is bound to.
16 36
	 */
17
	this.setVertex = function(newValue) {
18
		vertex = newValue;
19
	};
37
	set vertex(newValue) {
38
		this._vertex = newValue;
39
	}
20 40

  
21 41
	/**
22 42
	 * Moves the context menu to the coordinates.
43
	 * @public
23 44
	 * @param {Coordinates} coords Coordinates to display the context menu at.
24 45
	 */
25
	this.setPosition = function(coords) {
26
		rootElement.style.top = coords.y + 'px';
27
		rootElement.style.left = coords.x + 'px';
28
	};
46
	set position(coords) {
47
		this._rootElement.style.top = coords.y + 'px';
48
		this._rootElement.style.left = coords.x + 'px';
49
	}
29 50

  
30 51
	/**
31 52
	 * Adds a new graph node (vertex or group) to be displayed in the context menu.
53
	 * @public
32 54
	 * @param {(Vertex|Group)} node Graph node to be displayed in the context menu.
33 55
	 */
34
	this.addNode = function(node) {
56
	addNode(node) {
35 57
		if (node instanceof Vertex && node.getGroup() !== null) {
36 58
			throw new Error('Vertex is already a member of some group.');
37 59
		}
38 60

  
39
		var nodeListItemElement = DOM.createHtmlElement('li', {
40
			'title': node.name,
41
		});
42
		nodeListItemElement.addEventListener('click', nodeListItemClick.bind(node));
43
		listElement.appendChild(nodeListItemElement);
44

  
45
		var nodeSymbolText = DOM.createHtmlElement('span', {
46
			'class': 'group-symbol',
47
			'style': 'background-color: ' + node.symbol[1] + ';',
48
		});
49
		nodeSymbolText.appendChild(document.createTextNode(node.symbol[0]));
50
		nodeListItemElement.appendChild(nodeSymbolText);
51

  
52
		var nodeNameText = DOM.createHtmlElement('span', {
53
			'class': 'group-name',
54
		});
55
		nodeNameText.appendChild(document.createTextNode(node.name));
56
		nodeListItemElement.appendChild(nodeNameText);
57
	};
61
		this._listElement.appendChild(DOM.h('li', {
62
			title: node.name,
63
			onClick: this._nodeListItemClick.bind(this, node),
64
		}, [
65
			DOM.h('span', {
66
				class: 'group-symbol',
67
				style: 'background-color: ' + node.symbol[1] + ';',
68
				innerText: node.symbol[0],
69
			}),
70
			DOM.h('span', {
71
				class: 'group-name',
72
				innerText: node.name,
73
			}),
74
		]));
75
	}
58 76

  
59 77
	/**
60 78
	 * Opens the context menu.
79
	 * @public
61 80
	 */
62
	this.open = function() {
63
		rootElement.classList.remove('hidden');
64
	};
81
	open() {
82
		this._rootElement.classList.remove('hidden');
83
	}
65 84

  
66 85
	/**
67 86
	 * Closes the menu and clears its content.
87
	 * @public
68 88
	 */
69
	this.close = function() {
70
		rootElement.classList.add('hidden');
89
	close() {
90
		this._rootElement.classList.add('hidden');
71 91

  
72
		listElement.innerHTML = '';
73
	};
74

  
75
	/**
76
	 * Creates a new DOM element representing the context menu in memory.
77
	 * @returns {Element} HTML DOM element.
78
	 */
79
	this.render = function() {
80
		rootElement = DOM.createHtmlElement('div', {
81
			'class': 'context-menu hidden',
82
		});
83
		rootElement.addEventListener('mousedown', Utils.stopPropagation);
84

  
85
		listElement = DOM.createHtmlElement('ul');
86
		rootElement.appendChild(listElement);
87

  
88
		return rootElement;
89
	};
92
		this._listElement.innerHTML = '';
93
	}
90 94

  
91 95
	/**
92 96
	 * Context menu item click interaction. The vertex this context menu is bound to is either added to the group or merged 
93 97
	 * with the vertex to created a new group.
94
	 * @param {Event} e Click event.
98
	 * @private
95 99
	 */
96
	function nodeListItemClick(e) {
97
		if (this instanceof Vertex) {
100
	_nodeListItemClick(node) {
101
		let group;
102
		if (node instanceof Vertex) {
98 103
			// create a new group
99
			var group = new Group({});
104
			group = new Group({});
100 105
			group.setExcluded(true);
101
			group.addVertex(this);
106
			group.addVertex(node);
102 107

  
103 108
			app.nodeList.push(group);
104 109
			app.groupList.push(group);
105 110

  
106 111
			app.sidebarComponent.excludedNodeListComponent.add(group);
107
			app.sidebarComponent.excludedNodeListComponent.remove(this);
112
			app.sidebarComponent.excludedNodeListComponent.remove(node);
108 113

  
109
			this.remove(true);
114
			node.remove(true);
110 115

  
111 116
		} else {
112
			var group = this;
117
			group = node;
113 118
		}
114 119

  
115 120
		// add the vertex to the group
116
		group.addVertex(vertex);
121
		group.addVertex(this._vertex);
117 122

  
118 123
		app.viewportComponent.contextMenuComponent.close();
119 124
	}

Také k dispozici: Unified diff