-
Notifications
You must be signed in to change notification settings - Fork 26
/
GroupExplorer.js
320 lines (281 loc) · 12.2 KB
/
GroupExplorer.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
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
// @flow
import {CayleyDiagramView, createUnlabelledCayleyDiagramView} from './js/CayleyDiagramView.js';
import {CycleGraphView, createUnlabelledCycleGraphView} from './js/CycleGraphView.js';
import GroupURLs from './GroupURLs.js';
import * as Library from './js/Library.js';
import Log from './js/Log.js';
import Menu from './js/Menu.js';
import {MulttableView, createMinimalMulttableView} from './js/MulttableView.js';
import {SymmetryObjectView, createStaticSymmetryObjectView} from './js/SymmetryObjectView.js';
import Template from './js/Template.js';
import XMLGroup from './js/XMLGroup.js';
// $FlowFixMe -- external module imports described in flow-typed directory
import {THREE} from './lib/externals.js';
export {load};
// Module variables
let Cayley_Diagram_View /*: CayleyDiagramView */;
let Symmetry_Object_View /*: SymmetryObjectView */;
let Multtable_View /*: MulttableView */;
let Cycle_Graph_View /*: CycleGraphView */;
// Static event managers (setup after document is available)
function registerEventHandlers() {
$('#GroupTableHeaders th.sortable').on('click', columnSort);
HoverHelp.init();
};
// Load group library from urls
function load () {
Cayley_Diagram_View = createUnlabelledCayleyDiagramView({height: 32, width: 32});
Symmetry_Object_View = createStaticSymmetryObjectView({height: 32, width: 32});
Multtable_View = createMinimalMulttableView({height: 32, width: 32});
Cycle_Graph_View = createUnlabelledCycleGraphView({height: 32, width: 32});
// only read in a few groups if URL specifies 'debug' (for impatient programmers)
const urlDebug = new URL(window.location.href).searchParams.get('debug');
const resolvedURLs = GroupURLs.urls.map( (url) => Library.resolveURL(url) );
const urlsToDisplay /*: Array<string> */ =
(urlDebug == undefined) ? Array.from(new Set(resolvedURLs.concat(Library.getAllLocalURLs())))
: resolvedURLs.slice(0, (parseInt(urlDebug) || 10));
// display locally stored group defintions
for (const url of urlsToDisplay) {
const g = Library.getLocalGroup(url);
if (g != undefined && g.CayleyThumbnail != undefined && g.rowHTML != undefined) {
const $img = $('<img>').attr({src: g.CayleyThumbnail, height: 32, width: 32});
const $row = $(g.rowHTML);
$row.find('td.cayleyDiagram a div').empty().append($img);
$('#GroupTable tbody').append($row);
}
}
// update locally stored group definitions, if needed
$( '#loadingMessage' ).show();
// check that the locally stored definitions currently displayed are the latest; refresh if not
urlsToDisplay.forEach(async (url, urlIndex) => {
const localGroup = Library.getLocalGroup(url)
const latestGroup = await Library.getLatestGroup(url)
// if we don't have the latest group in the Library, format it in display and add to Library
if (localGroup == null ||
localGroup != latestGroup ||
localGroup.CayleyThumbnail == null ||
localGroup.rowHTML == null
) {
// format new group
const $row = displayGroup(latestGroup)
const $cayleyDiagram = $row.find('td.cayleyDiagram img').detach();
latestGroup.rowHTML = $row[0].outerHTML;
$row
.find('td.cayleyDiagram a div')
.empty()
.append($cayleyDiagram)
$row.appendTo('#GroupTable tbody');
Library.saveGroup(latestGroup);
}
if (urlIndex + 1 == urlsToDisplay.length) {
registerEventHandlers();
$( '#loadingMessage' ).hide();
$('#GroupTableHeaders th.sort-down').triggerHandler('click');
} else {
const pct = ( (urlIndex + 1) * 100 / urlsToDisplay.length ) | 0;
$( '#loadingMessage i' ).html( `Loading groups (${pct}%)...` );
}
})
}
// add row to table that displays this name, order, etc. of group
function displayGroup(group) {
const cayleyTitle = (group.cayleyDiagrams.length == 0) ?
undefined :
group.cayleyDiagrams[0].name;
const symmetryTitle = (group.symmetryObjects.length == 0) ?
undefined :
group.symmetryObjects[0].name;
$(`tr[group="${group.URL}"]`).remove();
let $row = $(eval(Template.HTML('row_template')));
// draw Cayley diagram
{
Cayley_Diagram_View.setDiagram(group, cayleyTitle);
const img = Cayley_Diagram_View.getImage();
group.CayleyThumbnail = img.src;
$row.find("td.cayleyDiagram a div").html(img.outerHTML);
}
// draw Multtable
{
Multtable_View.group = group;
const img = Multtable_View.getImage();
$row.find("td.multiplicationTable a div").html(img.outerHTML);
}
// draw Symmetry Object
if (symmetryTitle == undefined) {
$row.find("td.symmetryObject").html('none').addClass('noDiagram').removeAttr('title');
} else {
const img = Symmetry_Object_View.setObject(group.symmetryObjects[0]).getImage();
$row.find("td.symmetryObject a div").html(img.outerHTML);
}
// draw Cycle Graph
{
Cycle_Graph_View.group = group;
const img = Cycle_Graph_View.getImage();
$row.find("td.cycleGraph a div").html(img.outerHTML);
}
return $row;
}
// callback to sort table on column value, invoked by clicking on column head
function columnSort(event /*: JQueryEventObject */) {
const column = event.currentTarget;
const columnIndex = $('#GroupTableHeaders th.sortable').toArray().findIndex( (th) => th == column );
if (columnIndex == -1) {
Log.err(`unknown event in columnSort`);
return;
}
const makeSortUp = !$(column).hasClass('sort-up');
$('th.sortable').removeClass('sort-down')
.removeClass('sort-up')
.addClass('sort-none');
$(column).removeClass('sort-none')
.addClass(makeSortUp ? 'sort-up' : 'sort-down');
const getCellValue = (tr, idx) /*: string */ => tr.children[idx].textContent;
const compareFunction =
(idx, asc) =>
(a /*: HTMLTableCellElement */, b /*: HTMLTableCellElement */) =>
((v1, v2) => (!isNaN(v1) && !isNaN(v2)) ? Number(v1) - Number(v2) : v1.toString().localeCompare(v2))(
getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx)
);
$('#GroupTable tbody').find('tr:nth-child(n+1)')
.sort(compareFunction(columnIndex, makeSortUp))
.each((_,tr) => $('#GroupTable tbody').append(tr))
}
class HoverHelp {
/*::
static lastEntry: ?{cell: HTMLElement, timeStamp: number}
*/
static init() {
const tbody = $('#GroupTable tbody')[0];
if (window.hasOwnProperty('ontouchstart')) {
$('#GroupTable')[0].addEventListener('click', HoverHelp.clickHandler); // tap anywhere in table to clear tooltip
// set up touch event listeners
['touchstart', 'touchmove', 'touchend'].forEach( (event) => tbody.addEventListener(event, HoverHelp.highlightHandler) );
['touchstart', 'touchmove', 'touchend'].forEach( (event) => tbody.addEventListener(event, HoverHelp.tipHandler) );
} else {
tbody.addEventListener('mousemove', HoverHelp.highlightHandler);
tbody.addEventListener('mouseleave', HoverHelp.clearHighlighting);
}
}
static clickHandler(clickEvent /*: MouseEvent */) {
// skip modified events
if (clickEvent.altKey || clickEvent.ctrlKey || clickEvent.metaKey || clickEvent.shiftKey) {
return;
}
const $tooltip = $('#tooltip');
const $clickedElement = $(document.elementFromPoint(clickEvent.clientX, clickEvent.clientY));
if ($tooltip.length > 0) {
$tooltip.remove();
} else {
const $link = $clickedElement.closest('td').find('a[title]');
if ($link.length != 0) {
window.open($link.attr('href'));
}
}
if ($clickedElement.closest('div.top-right-menu').length == 0) {
clickEvent.preventDefault(); // let click event propagate to the top-right-menu
}
HoverHelp.lastEntry = null;
}
static tipHandler(touchEvent /*: TouchEvent */) {
// skip modified events, multi-touches
if ( touchEvent.altKey || touchEvent.ctrlKey || touchEvent.metaKey || touchEvent.shiftKey
|| touchEvent.touches.length > 1 || touchEvent.changedTouches.length > 1) {
return;
}
const lastEntry = HoverHelp.lastEntry;
const touch /*: Touch */ = (touchEvent.changedTouches[0] /*: any */);
const $cell /*: JQuery */ = $(document.elementFromPoint(touch.clientX, touch.clientY)).closest('td');
const $tooltip = $('#tooltip');
switch (touchEvent.type) {
case 'touchstart':
if ($tooltip.length != 0) {
$tooltip.remove();
HoverHelp.clearHighlighting();
touchEvent.preventDefault();
}
HoverHelp.lastEntry = {cell: $cell[0], timeStamp: touchEvent.timeStamp};
break;
case 'touchmove':
if (lastEntry == undefined) {
HoverHelp.lastEntry = {cell: $cell[0], timeStamp: touchEvent.timeStamp};
} else if (lastEntry.cell != $cell[0]) {
$tooltip.remove();
HoverHelp.lastEntry = {cell: $cell[0], timeStamp: touchEvent.timeStamp};
} else if ( $cell.find('#tooltip').length == 0
&& !$cell.hasClass('noDiagram')
&& touchEvent.timeStamp - lastEntry.timeStamp > 350) {
$tooltip.remove();
const $newTip = HoverHelp.getTooltip($cell);
if ($newTip) {
Menu.setMenuLocation($newTip, touch);
$newTip.css('visibility', 'visible');
}
}
touchEvent.preventDefault(); // prevents scrolling while user moves around display showing highlighting, tooltips
break;
case 'touchend':
if (lastEntry != undefined) {
if (lastEntry.cell != $cell[0]) {
$tooltip.remove();
} else if ( $cell.find('#tooltip').length == 0
&& !$cell.hasClass('noDiagram')
&& touchEvent.timeStamp - lastEntry.timeStamp > 350) {
$tooltip.remove();
const $newTip = HoverHelp.getTooltip($cell);
if ($newTip) {
Menu.setMenuLocation($newTip, touch);
$newTip.css('visibility', 'visible');
}
}
}
HoverHelp.lastEntry = null;
break;
}
}
static getTooltip($cell /*: JQuery */) /*: ?JQuery */ {
const $anchor = $cell.find('a[title]');
if ($anchor.length == 0) {
Log.err("tooltip can't find anchor in td");
return null;
}
const tooltipText = $anchor.attr('title');
const $newTip = $('<div id="tooltip" class="menu">')
.text(tooltipText)
.appendTo($cell);
return $newTip;
}
static highlightHandler(event /*: MouseEvent | TouchEvent */) {
// skip modified events, multi-touches
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
return;
}
if (event.type.startsWith('touch')) {
const touchEvent /*: TouchEvent */ = (event /*: any */);
if (touchEvent.touches.length > 1 || touchEvent.changedTouches.length > 1) {
return;
}
}
switch (event.type) {
case 'touchstart':
case 'touchmove':
case 'mousemove': {
const xy /*: eventLocation */ = event.type.startsWith('touch')
? ((event /*: any */) /*: TouchEvent */).touches[0]
: ((event /*: any */) /*: MouseEvent */);
const $cell = $(document.elementFromPoint(xy.clientX, xy.clientY)).closest('td');
if (!$cell.hasClass('emphasized')) {
HoverHelp.clearHighlighting();
$cell.closest('tr').addClass('highlighted'); // set this element's row's class to 'highlighted'
$cell.addClass('emphasized'); // set this element's class to 'emphasized'
} }
break;
case 'touchend':
HoverHelp.clearHighlighting();
break;
}
}
static clearHighlighting() {
$('#GroupTable > tbody > tr.highlighted').removeClass('highlighted');
$('#GroupTable > tbody td.emphasized').removeClass('emphasized');
}
}