forked from Chuqiaoo/foamtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carrotsearch.foamtree.util.hints.js
453 lines (408 loc) · 12.9 KB
/
carrotsearch.foamtree.util.hints.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Copyright 2002-2018, Carrot Search s.c, All Rights Reserved.
*
*
* A utility that displays contextual interaction hints
* and the full interaction guide when requested. The utility
* will insert all required HTML into the page.
*
* Please see demos/hints.html for the usage example.
*
* @param foamtree the FoamTree instance to be the source
* of interaction events for this utility.
*/
window.CarrotSearchFoamTree.hints = function(foamtree) {
var macOs = /Mac/.test(window["navigator"]["userAgent"]);
var touch = ('ontouchstart' in window) || (!!window["DocumentTouch"] && document instanceof window["DocumentTouch"]);
// The list of available interactions, used to build the
// complete interaction guide screen.
var interactions = [
[
{
desktop: "Left click",
touch: "Tap",
action: "select group, again to deselect"
},
// comments by Chuqiao
//{
// desktop: (macOs ? "[⌘]" : "[Ctrl]") + " + Left click",
// action: "select multiple groups"
//}
],
// custom action by Chuqiao
[
{desktop: "Left click/Double click +Hold",
action: "Reactome pathway diagram page"
}
],
// **************custom ends by Chuqiao
[
{
desktop: "Left double click",
touch: "Double tap",
action: "expose group"
},
{
desktop: "Right double click<br /><small>or [Shift] + Left double click</small>",
touch: "Two-finger double tap",
action: "unexpose group"
}
],
[
{
desktop: "Left click-and-hold",
touch: "Tap-and-hold",
action: "open group"
},
{
desktop: "Right click-and-hold<br /><small>or [Shift] + Left click-and-hold</small>",
touch: "Two-finger tap-and-hold",
action: "close group"
}
],
[
{
desktop: "Mouse wheel",
touch: "Pinch",
action: "zoom in / out"
},
{
desktop: "Mouse drag",
touch: "Drag",
action: "pan around zoomed visualization"
},
{
desktop: "[Esc] or rapid zoom out",
touch: "Three-finger pinch",
action: "unexpose & close all groups"
}
],
[
{
desktop: "[?]",
action: "show / hide this help"
}
]
];
// A simple persistent state manager
var state = (function(key, def) {
var hasStorage = (function() {
try {
var key = "ftap5caavc";
window.localStorage.setItem(key, key);
window.localStorage.removeItem(key);
return true;
} catch(e) {
return false;
}
}());
var json = hasStorage && window.localStorage[key];
var object;
if (json === undefined) {
object = def;
save();
} else {
object = JSON.parse(json);
}
function save() {
if (hasStorage) {
window.localStorage[key] = JSON.stringify(object);
}
}
return {
get: function(prop) {
return object[prop];
},
set: function(prop, value) {
object[prop] = value;
save();
}
};
})(
"foamtree.help.state", {
hints: true
});
var showHints = state.get("hints");
// The hints box
// The default one is below, remove the class slideable hint to stick the hint.
/*
var hintsHtml =
'<div class="visualization-hint">\
<span class="slidable hint"></span>\
<a href="#" class="slidable dont-show">don\'t show again</a>\
<span class="slidable help">help</span><span class="info">i</span>\
</div>';
*/
var hintsHtml =
'<div class="visualization-hint">\
<span> Left double click to zoom in, hold for details, right double click to zoom out</Span>\
<a href="#" class="slidable dont-show"> : Don\'t show again: </a>\
<span class="slidable help">Help</span><span class="info">i</span>\
</div>';
// The complete HTML, including interaction guide
var html =
(showHints ? hintsHtml : "") +
'<div class="visualization-help fadeout">\
<a href="#close">×</a>' +
interactions.reduce(function(html, group) {
return html +
"<dl>" +
group.reduce(function (html, interaction) {
if ((touch && !interaction.touch) || (!touch && !interaction.desktop)) {
return html;
}
var trigger = touch ? interaction.touch : interaction.desktop;
var event = trigger.replace(/\[/, "<span class='key'>").replace(/\]/, "</span>");
return html +
"<dt>" + event + "</dt>" +
"<dd>" + interaction.action + "</dd>";
}, "") +
"</dl>";
}, "") +
'</div>';
// Insert the HTML into the page
var element = document.createElement("div");
element.className = "visualization-hints";
element.innerHTML = html;
var foamtreeElement = foamtree.get("element");
foamtreeElement.appendChild(element);
// For quick element selection
var $ = function(selector) {
return foamtreeElement.querySelector(selector);
};
// Manages HTML event listeners
var listeners = (function() {
var listeners = [];
return {
on: function(element, events, listener) {
if (!element) {
return;
}
events.split(/\s+/).forEach(function(event) {
element.addEventListener(event, listener, false);
listeners.push({
element: element,
event: event,
listener: listener
});
});
},
dispose: function() {
for (var i = listeners.length - 1; i >= 0; i--) {
var info = listeners[i];
info.element.removeEventListener(info.event, info.listener);
}
}
}
})();
//comments by chuqiao to stick hints
// Shows/hides the contextual interaction hints
//var hint = (function() {
// var hint = $(".visualization-hint .hint"),
// hintContainer = $(".visualization-hint");
//
// return {
// show: function(text) {
// hint.innerHTML = text;
// hintContainer.setAttribute("class", "visualization-hint shown");
// },
//
// hide: function() {
// hint.innerHTML = "";
// hintContainer.setAttribute("class", "visualization-hint");
// }
// }
//})();
// The list of interactions that trigger contextual hints.
var hints = (function() {
var current;
//all texts replaced by Chuqiao, but keep original one.
var conditions = [
{
for: "expose",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return hasChildren;
},
//text: "To zoom in to sub-groups, double click the parent group"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
},
{
for: "unexpose",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return parentState && parentState.exposed;
},
//text: "To zoom out to parent group, double click with right mouse button"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
},
{
for: "open",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return hasChildren;
},
//text: "To access subgroups, click and hold"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
},
{
for: "close",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return parent && parentState.open;
},
//text: "To access parent group, click and hold right mouse button"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
},
{
for: "reset",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return foamtree.get("exposure").length > 0;
},
//text: "To zoom out and close all groups, press Esc"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
},
{
for: "mousewheel",
complete: false,
condition: function(group, groupState, hasChildren, parent, parentState) {
return true;
},
//text: "Use mouse wheel to zoom in and out"
text: "Left double click to zoom in, hold for details, right double click to zoom out"
}
];
function actionPerformed(action) {
if (current && current.for === action) {
hint.hide();
current = undefined;
}
for (var i = conditions.length - 1; i >= 0; i--) {
var condition = conditions[i];
if (condition.for === action) {
condition.complete = true;
}
}
}
return {
hovered: function(event) {
var group = event.group;
var state = foamtree.get("state", group);
var hasChildren = group.groups && group.groups.length > 0;
var parent = event.bottommostOpenGroup;
var parentState = parent && foamtree.get("state", parent);
// comments by Chuqiao to stick the text
//for (var i = 0; i < conditions.length; i++) {
// var condition = conditions[i];
// if (!condition.complete && condition.condition(group, state, hasChildren, parent, parentState)) {
// hint.show(condition.text);
// current = condition;
// break;
// }
//}
},
performed: actionPerformed
};
})();
// Attach FoamTree listeners that will drive the contextual hints
var foamtreeListeners = (function() {
if (!state.get("hints")) {
return;
}
var timeout;
function clear() {
window.clearTimeout(timeout);
}
foamtree.on("groupHover", function(event) {
if (!event.group) {
return;
}
window.clearTimeout(timeout);
// comments by Chuqiao to stick the hints
//timeout = window.setTimeout(function() {
// hints.hovered(event);
//}, 1000);
});
foamtree.on("groupClick", clear);
foamtree.on("groupDoubleClick", clear);
foamtree.on("groupHold", clear);
foamtree.on("groupMouseWheel", clear);
var createDeltaCounter = function(onIncrease, onDecrease) {
var previous = 0;
return function(event) {
if (!event.indirect) {
hints.performed(previous <= event.groups.length ? onIncrease : onDecrease);
previous = event.groups.length;
}
};
};
foamtree.on("groupExposureChanged", createDeltaCounter("expose", "unexpose"));
foamtree.on("groupOpenOrCloseChanged", createDeltaCounter("open", "close"));
foamtree.on("viewReset", function() {
hints.performed("reset");
});
foamtree.on("groupMouseWheel", function() {
hints.performed("mousewheel");
})
})();
// Handle clicks on the hints and guide elements
var guideElement = $(".visualization-help");
listeners.on($(".visualization-hint"), "mousedown mouseup touchstart", function(event) {
if (event.type !== "mousedown") {
showHelp();
}
event.preventDefault();
event.stopPropagation();
});
listeners.on($(".visualization-hint .dont-show"), "mousedown mouseup touchstart click", function(event) {
if (event.type !== "mousedown") {
$(".visualization-hint").style.display = "none";
state.set("hints", false);
}
event.preventDefault();
event.stopPropagation();
});
listeners.on($(".visualization-help a[href='#close']"), "mousedown mouseup touchstart click", function(event) {
if (event.type !== "mousedown") {
hideHelp();
}
event.preventDefault();
event.stopPropagation();
});
listeners.on(document, "keyup", function (event) {
switch (event.keyCode) {
case 27:
hideHelp();
break;
case 191:
if (event.shiftKey) {
if (guideElement.getAttribute("class").indexOf("fadeout") >= 0) {
showHelp();
} else {
hideHelp();
}
}
break;
}
});
function showHelp() {
guideElement.setAttribute("class", "visualization-help");
}
function hideHelp() {
guideElement.setAttribute("class", "visualization-help fadeout");
}
};