-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative.js
More file actions
82 lines (72 loc) · 3.1 KB
/
native.js
File metadata and controls
82 lines (72 loc) · 3.1 KB
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
var scrollIntoViewIfNeeded = function(node, centerIfNeeded) {
centerIfNeeded = arguments.length === 0 ? true : !!centerIfNeeded;
var parent = node.parentNode,
parentComputedStyle = window.getComputedStyle(parent, null),
parentBorderTopWidth = parseInt(parentComputedStyle.getPropertyValue('border-top-width')),
parentBorderLeftWidth = parseInt(parentComputedStyle.getPropertyValue('border-left-width')),
overTop = node.offsetTop - parent.offsetTop < parent.scrollTop,
overBottom = (node.offsetTop - parent.offsetTop + node.clientHeight - parentBorderTopWidth) > (parent.scrollTop + parent.clientHeight),
overLeft = node.offsetLeft - parent.offsetLeft < parent.scrollLeft,
overRight = (node.offsetLeft - parent.offsetLeft + node.clientWidth - parentBorderLeftWidth) > (parent.scrollLeft + parent.clientWidth),
alignWithTop = overTop && !overBottom;
if ((overTop || overBottom) && centerIfNeeded) {
parent.scrollTop = node.offsetTop - parent.offsetTop - parent.clientHeight / 2 - parentBorderTopWidth + node.clientHeight / 2;
}
if ((overLeft || overRight) && centerIfNeeded) {
parent.scrollLeft = node.offsetLeft - parent.offsetLeft - parent.clientWidth / 2 - parentBorderLeftWidth + node.clientWidth / 2;
}
if ((overTop || overBottom || overLeft || overRight) && !centerIfNeeded) {
node.scrollIntoView(alignWithTop);
}
};
var handlePorts = function(app) {
app.ports.scrollToVisible.subscribe(function(id) {
setTimeout(function() {
var element = document.getElementById(id);
if (element) {
scrollIntoViewIfNeeded(element);
}
});
});
app.ports.saveDataPort.subscribe(function(data) {
console.log(data);
localStorage.setItem('name', JSON.stringify(data.name));
localStorage.setItem('people', JSON.stringify(data.people));
localStorage.setItem('groups', JSON.stringify(data.groups));
});
app.ports.exportDataPort.subscribe(function(data) {
var body = new Blob([JSON.stringify(data.data)], {type: 'application/json'});
var a = document.createElement('a');
a.href = URL.createObjectURL(body);
a.download = data.name + ".json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});
app.ports.importDataPort.subscribe(function() {
var input = document.createElement('input');
input.type = 'file';
document.body.appendChild(input);
input.addEventListener('change', function(event) {
var files = event.target.files;
if (files.length > 0) {
var file = files[0],
reader = new FileReader();
reader.onload = function() {
app.ports.importedDataPort.send(JSON.parse(reader.result));
};
reader.readAsText(file);
} else {
console.error('Zero files selected.');
}
}, false);
input.click();
document.body.removeChild(input);
});
app.ports.runSolverPort.subscribe(function(data) {
console.log(data);
var solution = runSolvers(data.people, data.numGroups, data.mixed, data.leftOut);
console.log(solution);
app.ports.solverSolutionPort.send(solution);
});
};