ctucx.git: ctucx.things

simple inventory management web-app

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 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359 
360 
361 
362 
363 
364 
365 
366 
367 
368 
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382 
383 
384 
385 
386 
387 
388 
389 
390 
391 
392 
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
406 
407 
408 
409 
410 
411 
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427 
import colorUtils from './utils/color.js';

export default function (args) {
	let container;
	let context;
	let srcData;
	let data = {};
	let bounds;
	let center;
	let radius;
	const pieHole = 0.3;
	const spacing = 0.1;
	let hovered = null;
	let hoverCallback = null;
	let clickCallback = null;
	const backgroundColor = 'rgb(245,245,245)';
	const firstRing = { inner: 25, outer: 70 };
	const secondRing = { inner: 80, outer: 120 };
	let tooltip;
	const isAnimating = false;
	const frameRate = 10;

	function init() {
		if (!args.container || (!args.data && !args.processedData)) {
			console.warn('invalid params!!');
			return;
		}
		container = args.container;
		container.style.position = 'relative';
		context = container.getContext('2d');
		if (args.data) {
			srcData = args.data;
			data = preprocess(srcData);
		} else if (args.processedData) {
			data = args.processedData;
		}

		bounds = { x: context.canvas.width, y: context.canvas.height };
		center = { x: bounds.x / 2, y: bounds.y / 2 };

		if (center.x < center.y) radius = center.x;
		else radius = center.y;

		if (args.clickCallback) clickCallback = args.clickCallback;
		if (args.hoverCallback) hoverCallback = args.hoverCallback;
		drawGraph();

		tooltip = document.createElement('div');
		tooltip.classList.add('tooltip');
		document.getElementsByTagName('body')[0].appendChild(tooltip);
	}

	function update(args) {
		const oldVisibleRings = getVisibleRings(data);

		if (args.data) {
			data = preprocess(args.data);
		} else if (args.processedData) {
			data = args.processedData;
		} else {
			return;
		}
		setVisibleRings(oldVisibleRings, data);

		context.clearRect(0, 0, bounds.x, bounds.y);
		drawGraph();
	}

	function preprocess(srcData, parent) {
		const data = {};
		let total = 0;
		data.points = {};

		for (var key in srcData) {
			var value = srcData[key];
			if (typeof (value) === 'object') {
				total += preprocess(value).total;
			} else {
				total += value;
			}
		}
		data.total = total;

		for (var key in srcData) {
			var value = srcData[key];
			if (typeof (value) === 'object') {
				data.points[key] = preprocess(value, data);
				data.points[key].name = key;
				data.points[key].visiblePoints = false;
			} else {
				data.points[key] = {
					value,
					percent: value / total,
					name: key,
					parent: data,
				};
			}
		}

		if (parent) {
			data.percent = total / parent.total;
			data.parent = parent;
		}
		return data;
	}

	function render(myData) {
		const offset = 5;
		let parentColor = false;
		let angleMultiplier = 1;
		let lastAngle = 0.0;
		const minRadius = myData.innerMinRadius;
		const maxRadius = myData.innerMaxRadius;

		if (myData.startAngle) lastAngle = myData.startAngle;
		const startAngle = lastAngle;
		if (myData.angleMultiplier) angleMultiplier = myData.angleMultiplier;

		if (myData.color) {
			parentColor = myData.color;
		}

		let count = 0;
		for (const key in myData.points) {
			const slice = myData.points[key];

			slice.startAngle = startAngle;

			slice.minRadius = minRadius;
			slice.maxRadius = maxRadius;
			slice.myAngle = slice.percent * Math.PI * 2 * angleMultiplier;
			slice.minAngle = lastAngle;
			slice.maxAngle = lastAngle + slice.myAngle;
			slice.avgAngle = (slice.minAngle + slice.maxAngle) / 2;
			slice.outsideMinPoint = {
				x: center.x + Math.cos(slice.minAngle) * maxRadius,
				y: center.y + Math.sin(slice.minAngle) * maxRadius,
			};
			slice.outsideMaxPoint = {
				x: center.x + Math.cos(slice.maxAngle) * maxRadius,
				y: center.y + Math.sin(slice.maxAngle) * maxRadius,
			};
			slice.insideMinPoint = {
				x: center.x + Math.cos(slice.minAngle) * slice.minRadius,
				y: center.y + Math.sin(slice.minAngle) * slice.minRadius,
			};
			slice.insideMaxPoint = {
				x: center.x + Math.cos(slice.maxAngle) * slice.minRadius,
				y: center.y + Math.sin(slice.maxAngle) * slice.minRadius,
			};

			if (!slice.color) {
				slice.color = colorUtils.getColor(count, parentColor);
			}

			slice.startAngle = lastAngle;

			drawSlice(slice);

			lastAngle = slice.maxAngle;
			count++;
		}
	}

	function drawSlice(slice, color) {
		if (color) {
			context.strokeStyle = color;
			context.lineWidth = 2;
		} else {
			context.strokeStyle = backgroundColor;
			context.lineWidth = 3;
		}

		context.fillStyle = colorUtils.rgbToString(slice.color);

		context.beginPath();
		context.moveTo(slice.outsideMinPoint.x, slice.outsideMinPoint.y);
		context.arc(center.x, center.y, slice.maxRadius, slice.minAngle, slice.maxAngle);
		context.lineTo(slice.insideMaxPoint.x, slice.insideMaxPoint.y);
		context.arc(center.x, center.y, slice.minRadius, slice.maxAngle, slice.minAngle, true);
		context.lineTo(slice.outsideMinPoint.x, slice.outsideMinPoint.y);
		context.stroke();
		context.fill();
		context.closePath();
	}

	function animateAdd() {
		const rings = getVisibleRings(data);
		const duration = 600;
		const d = new Date();
		for (let i = 0; i < rings.length; i++) {
			const ring = rings[i];
			const minMax = getMinMax(i + 1, rings.length);
			if (ring.innerMinRadius) {
				ring.startMinRadius = ring.innerMinRadius;
				ring.startMaxRadius = ring.innerMaxRadius;
			} else {
				ring.startMinRadius = minMax[1];
				ring.startMaxRadius = minMax[1];
			}

			ring.startAngleMultiplier = 1;
			if (i == rings.length - 1 && rings.length > 1) {
				ring.startAngleMultiplier = ring.percent;
				ring.startMinRadius = minMax[1];
			}

			ring.targetMinRadius = minMax[0];
			ring.targetMaxRadius = minMax[1];
			ring.targetAngleMultiplier = 1;

			ring.deltaMinRadius = ring.targetMinRadius - ring.startMinRadius;
			ring.deltaMaxRadius = ring.targetMaxRadius - ring.startMaxRadius;
			ring.deltaAngleMultiplier = ring.targetAngleMultiplier - ring.startAngleMultiplier;

			ring.startTime = d.getTime();
			ring.finishTime = ring.startTime + duration;
			ring.deltaTime = duration;

			ring.angleMultiplierFunction = delayThenEase;
			ring.radiusFunction = easeThenDelay;
		}

		setTimeout(animateStep, frameRate);
	}

	function animateStep() {
		const rings = getVisibleRings(data);
		const d = new Date();
		if (d.getTime() - frameRate > rings[0].finishTime) return;

		context.clearRect(0, 0, bounds.x, bounds.y);

		for (const i in rings) {
			const ring = rings[i];

			let percentAnimated = (d.getTime() - ring.startTime) / ring.deltaTime;
			if (percentAnimated > 1) percentAnimated = 1;
			ring.innerMinRadius = ring.startMinRadius + (ring.deltaMinRadius * ring.radiusFunction(percentAnimated));
			ring.innerMaxRadius = ring.startMaxRadius + (ring.deltaMaxRadius * ring.radiusFunction(percentAnimated));
			ring.angleMultiplier = ring.startAngleMultiplier + (ring.deltaAngleMultiplier * ring.angleMultiplierFunction(percentAnimated));
			render(ring);
		}
		setTimeout(animateStep, frameRate);
	}

	function attachEvents() {
		container.addEventListener('mousemove', hoverHandle);
		container.addEventListener('click', clickHandle);
	}

	function hoverHandle(evt) {
		const containerRect = container.getBoundingClientRect();
		const size = { x: containerRect.width, y: containerRect.height };
		const offset = { x: (evt.pageX - containerRect.left) * (bounds.x / size.x), y: (evt.pageY - containerRect.top) * (bounds.y / size.y) };
		const dX = (offset.x - center.x);
		const dY = (offset.y - center.y);
		let angle = Math.atan(dY / dX);
		if (dX < 0) angle += Math.PI;
		else if (dY < 0) angle += Math.PI * 2;
		const radius = Math.sqrt(dX * dX + dY * dY);

		const newHovered = findHovered(data, angle, radius);

		if (newHovered) {
			if (newHovered != hovered) {
				if (hovered) drawSlice(hovered);
				hovered = newHovered;
				drawSlice(hovered, 'rgb(50,50,50)');
				container.classList.add('activeHover');
				if (hoverCallback) hoverCallback(newHovered);
			}
			tooltip.style.display = 'block';
			tooltip.innerText = newHovered.name;
			tooltip.style.top = `${evt.pageY - 10}px`;
			tooltip.style.left = `${evt.pageX + 15}px`;
		} else {
			if (hovered) {
				drawSlice(hovered);
				if (hoverCallback) hoverCallback(newHovered);
			}
			hovered = 0;
			container.classList.remove('activeHover');
			tooltip.style.display = 'none';
		}
	}

	function clickHandle(evt) {
		if (!hovered) {
			recursivelySetAttribute(data, 'visiblePoints', false);
			data.visiblePoints = true;
			animateAdd();
			return;
		}
		if (!hovered.points) return;

		recursivelySetAttribute(hovered.parent, 'visiblePoints', false);
		hovered.parent.visiblePoints = true;
		hovered.visiblePoints = true;

		animateAdd();
		if (clickCallback) clickCallback(hovered);
	}

	function open(key) {
		if (key) {
			hovered = data.points[key];
		}
		if (!hovered) {
			return;
		}
		recursivelySetAttribute(hovered.parent, 'visiblePoints', false);
		hovered.parent.visiblePoints = true;
		hovered.visiblePoints = true;
		animateAdd();
	}

	function close() {
		recursivelySetAttribute(data, 'visiblePoints', false);
		data.visiblePoints = true;
		context.clearRect(0, 0, bounds.x, bounds.y);
		animateAdd();
	}

	function findHovered(data, angle, radius) {
		for (const key in data.points) {
			const i = data.points[key];
			if (i.points && i.visiblePoints) {
				const found = findHovered(i, angle, radius);
				if (found) return found;
			}
			let startAngle = 0;
			if (typeof i.parent.startAngle !== 'undefined') startAngle = i.parent.startAngle;

			if (radius < i.maxRadius && radius > i.minRadius) {
				if (i.minAngle <= Math.PI * 2 && i.maxAngle > Math.PI * 2 && (angle > i.minAngle || angle + Math.PI * 2 < i.maxAngle)) {
					return i;
				} if (i.minAngle >= Math.PI * 2 && i.maxAngle > Math.PI * 2 && angle + Math.PI * 2 > i.minAngle && angle + Math.PI * 2 < i.maxAngle) {
					return i;
				} if (angle > i.minAngle && angle < i.maxAngle) {
					return i;
				}
			}
		}
		return null;
	}

	function findSectionById(id) {
		for (const i in data.points) {
			if (data.points[i].id && data.points[i].id == id) return data.points[i];
		}
		return false;
	}

	function recursivelySetAttribute(data, key, value) {
		data[key] = value;
		for (const i in data.points) {
			recursivelySetAttribute(data.points[i], key, value);
		}
	}

	function drawGraph() {
		data.visiblePoints = true;
		const rings = getVisibleRings(data);

		for (let i = 0; i < rings.length; i++) {
			const ring = rings[i];
			const minMax = getMinMax(i + 1, rings.length);
			ring.innerMinRadius = minMax[0];
			ring.innerMaxRadius = minMax[1];
			render(ring);
		}

		attachEvents();
	}

	function getVisibleRings(data) {
		let out = [data];
		for (const i in data.points) {
			if (data.points[i].visiblePoints) {
				if (data.points[i].points) out = out.concat(getVisibleRings(data.points[i]));
				break;
			}
		}
		return out;
	}

	function setVisibleRings(rings, data) {
		// pass in old visible rings to translate to new data
		if (typeof data.id !== 'undefined') {
			for (var i in rings) {
				if (data.id == rings[i].id) {
					data.visiblePoints = true;
				}
			}
		}
		for (var i in data.points) {
			setVisibleRings(rings, data.points[i]);
		}
	}

	function getMinMax(level, levels) {
		const minRadius = pieHole * (1 / levels) + (((level - 1) / levels) * (1 - pieHole));
		let maxRadius = pieHole * (1 / levels) + ((level / levels - spacing) * (1 - pieHole));
		if (levels == 1) {
			maxRadius *= 0.8;
		}
		return [minRadius * radius, maxRadius * radius];
	}

	function easeThenDelay(x) {
		if (x > 0.5) return 1;
		let out = (1 - Math.cos(x * 2 * Math.PI)) / 2;
		if (out > 1) out = 1;
		return out;
	}

	function delayThenEase(x) {
		if (x < 0.5) return 0;
		let out = (1 - Math.cos((x - 0.5) * 2 * Math.PI)) / 2;
		if (out > 1) out = 1;
		return out;
	}

	init();
	return { update, open, close };
};