-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigbite.js
247 lines (218 loc) · 4.78 KB
/
bigbite.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
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
(function () {
var
updating = false, // exclusive control
table, // forcused table
start, // drag start-id
latest, // drag latest-id
data = {}; // selected data
const
SELECTED_CELL = "BigBiteSelected";
var cancelEvent = function (e) {
e.stopPropagation();
e.preventDefault();
};
var reset = function () {
// unselected
for (var key in data) {
var pos = toPos(key);
unset(pos.x, pos.y);
}
table = start = latest = null;
data = {};
};
var diff = function (cell1, cell2, dest) {
var small = {
x: cell1.x <= cell2.x ? cell1.x : cell2.x,
y: cell1.y <= cell2.y ? cell1.y : cell2.y,
};
var large = {
x: cell1.x > cell2.x ? cell1.x : cell2.x,
y: cell1.y > cell2.y ? cell1.y : cell2.y,
};
for (var y = small.y; y <= large.y; y++) {
for (var x = small.x; x <= large.x; x++) {
var key = toId(x, y);
if (dest[key]) {
// intersection
delete dest[key];
} else {
// diff
dest[key] = key;
}
}
}
};
var unset = function(x, y) {
table.rows[y].cells[x].classList.remove(SELECTED_CELL);
};
var getCell = function (event) {
var o = {}, e = event.target, name;
while (e) {
switch ((name = e.nodeName.toLowerCase())) {
case "th":
name = "td";
/* FALLTHROUGH */
case "td":
case "tr":
case "table":
o[name] = e;
if (name === "table") {
return o;
}
}
e = e.parentNode;
}
return o;
};
var toId = function (x, y) {
return y + "-" + x;
};
var toPos = function(id) {
var pos = id.split("-");
return {x: +pos[1], y: +pos[0]};
};
var move = function (event) {
if (!event.ctrlKey || event.which !== 1) {
return;
}
var cell = getCell(event);
if (cell.table) {
// on table, default selection event is canceled
cancelEvent(event);
event.currentTarget.defaultView.getSelection().removeAllRanges();
}
if (!cell.td) {
return;
}
if (!table) {
// selection begins
table = cell.table;
} else if (table !== cell.table) {
// new table
reset();
cancelEvent(event);
return;
}
// update selection
var
cursor = {
x: cell.td.cellIndex,
y: cell.tr.rowIndex,
},
currentId = toId(cursor.x, cursor.y);
if (latest === currentId) {
return;
}
// exclusive control
if (updating) {
return;
}
updating = true;
if (start == null) {
start = currentId;
}
var startCell = toPos(start), flip = {};
if (latest) {
diff(startCell, toPos(latest), flip);
}
diff(startCell, cursor , flip);
for (id in flip) {
var pos = toPos(id);
if (data[id]) {
// to unselected
delete data[id];
unset(pos.x, pos.y);
} else {
// to selected
data[id] = 1;
table.rows[pos.y].cells[pos.x].classList.add(SELECTED_CELL);
}
}
latest = currentId;
updating = false;
};
var compare = function(a, b) {
if (a.y === b.y) {
return a.x - b.x;
} else {
return a.y - b.y
}
};
var copy = function(event) {
if (Object.keys(data).length) {
// data sort
var order = [];
for (var key in data) {
order[order.length] = toPos(key);
}
order.sort(compare);
// format
var currentRowId, dest = [], line = 0, colId = 0;
for (var i = 0, o; o = order[i]; i++) {
if (currentRowId == null) {
currentRowId = o.y;
}
if (currentRowId !== o.y) {
// newline
currentRowId = o.y;
line++;
colId = 0;
}
if (!dest[line]) {
dest[line] = [];
}
dest[line][colId++] = table.rows[o.y].cells[o.x].textContent.trim();
}
// to string
var text = "";
for (var i = 0, row; row = dest[i]; i++) {
text += row.join("\t") + "\r\n";
}
// copy
event.clipboardData.setData("text", text.slice(0, -2));
// finalize
reset();
cancelEvent(event);
}
};
var down = function(event) {
if (event.which !== 1) {
return;
} else if (!event.ctrlKey) {
reset();
event.stopPropagation();
return;
}
// selection begins
return move(event);
};
var up = function(event) {
if (start || latest) {
// terminate drag
start = latest = null;
cancelEvent(event);
}
};
var init = function(window, document) {
if (window.BigBite && window.BigBite.init) {
return;
}
window.BigBite = { init: true };
document.addEventListener("mouseup", up, true);
document.addEventListener("mousedown", down, true);
document.addEventListener("mousemove", move, true);
document.addEventListener("copy", copy, true);
var style = document.createElement("style");
style.type = "text/css";
document.getElementsByTagName("head").item(0).appendChild(style);
style.sheet.insertRule("." + SELECTED_CELL + "{box-shadow:-2px -1px blue inset}", 0);
};
for (var i = 0, w; w = window.frames[i]; i++) {
w.addEventListener("load", function() {
init(this, this.document);
});
init(w, w.document);
}
init(window, document);
}
)();