-
Notifications
You must be signed in to change notification settings - Fork 0
/
positions.js
139 lines (118 loc) · 3.32 KB
/
positions.js
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
/*
* Copyright (c) 2013 Ilshad Khabibullin, ilshad.com
* Licensed under the MIT license.
*
* orgtree.positions
*/
var orgtree = (function (orgtree) {
function getActive() {
var el = document.getElementById('active-position');
if (!el) {
el = orgtree.columns.getEntriesContainer(orgtree.columns.getActive()).firstElementChild;
el.id = 'active-position';
};
return el;
}
function remember(el) {
var col = column(el);
col.dataset.lastPositionId = el.dataset.id;
col.dataset.lastPositionType = (el.className.indexOf('entry') > -1) ?
'entry' : 'jump';
}
function column(el) {
return el.parentNode.parentNode;
}
function recall(col) {
var open = null,
econt = orgtree.columns.getEntriesContainer(col);
if (col.dataset.lastPositionType == 'entry') {
for (var i in econt.children) {
if (econt.children[i].dataset &&
econt.children[i].dataset.id == col.dataset.lastPositionId) {
return econt.children[i];
};
if (!open && econt.children[i].className.indexOf('open') > -1) {
open = econt.children[i];
}
}
} else if (col.dataset.lastPositionType == 'jump') {
for (var i in econt.children) {
if (econt.children[i].dataset &&
econt.children[i].dataset.id == col.dataset.lastPositionId) {
return econt.children[i];
};
if (!open && econt.children[i].className.indexOf('open') > -1) {
open = econt.children[i];
}
}
};
return open ? open : (econt.children[1] ? econt.children[1] : econt.children[0]);
}
function up() {
setActive(getActive().previousElementSibling);
}
function down() {
setActive(getActive().nextElementSibling);
}
function right() {
var c = getActive().parentNode.parentNode,
cr = orgtree.columns.getRight(c);
if (cr) {
setActive(recall(cr));
orgtree.columns.setActive(cr);
}
}
function left() {
var cl = orgtree.columns.getLeft(getActive().parentNode.parentNode);
if (cl) {
setActive(recall(cl));
orgtree.columns.setActive(cl);
}
}
function find(el) {
if (el) {
while (el.className.indexOf('position') == -1) {
el = el.parentElement;
};
return el;
}
}
function setActive(el) {
if (!el || el.className.indexOf('column-header') > -1) return;
var previous = getActive();
var x1, x2, y1, y2;
if (previous != undefined) previous.removeAttribute('id');
el.id = 'active-position';
x1 = el.offsetLeft;
x2 = el.parentNode.parentNode.scrollWidth;
if (x1 > x2) {
el.parentNode.parentNode.parentNode.parentNode.scrollLeft = x1 - 100;
} else {
el.parentNode.parentNode.parentNode.parentNode.scrollLeft = 0;
}
y1 = el.offsetTop - el.parentNode.offsetTop;
y2 = (el.offsetTop + el.offsetHeight) - (el.parentNode.offsetTop + el.parentNode.offsetHeight);
if (y1 < el.parentNode.scrollTop + 200) {
el.parentNode.scrollTop = y1 - 200;
} else if (y2 > el.parentNode.scrollTop - 200) {
el.parentNode.scrollTop = y2 + 200;
}
remember(el);
orgtree.router.locate(el);
}
function activeEntry() {
var el = getActive();
return (el.className.indexOf('entry') > -1) ? el : undefined;
}
orgtree.positions = {
up: up,
down: down,
right: right,
left: left,
find: find,
setActive: setActive,
activeEntry: activeEntry,
column: column
}
return orgtree;
}(orgtree || {}));