-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcytoscape-qtip.js
368 lines (300 loc) · 10 KB
/
cytoscape-qtip.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
;(function( $, $$ ){ 'use strict';
var isObject = function(o){
return o != null && typeof o === 'object';
};
var isFunction = function(o){
return o != null && typeof o === 'function';
};
var isNumber = function(o){
return o != null && typeof o === 'number';
};
var throttle = function(func, wait, options) {
var leading = true,
trailing = true;
if (options === false) {
leading = false;
} else if (isObject(options)) {
leading = 'leading' in options ? options.leading : leading;
trailing = 'trailing' in options ? options.trailing : trailing;
}
options = options || {};
options.leading = leading;
options.maxWait = wait;
options.trailing = trailing;
return debounce(func, wait, options);
};
var debounce = function(func, wait, options) { // ported lodash debounce function
var args,
maxTimeoutId,
result,
stamp,
thisArg,
timeoutId,
trailingCall,
lastCalled = 0,
maxWait = false,
trailing = true;
if (!isFunction(func)) {
return;
}
wait = Math.max(0, wait) || 0;
if (options === true) {
var leading = true;
trailing = false;
} else if (isObject(options)) {
leading = options.leading;
maxWait = 'maxWait' in options && (Math.max(wait, options.maxWait) || 0);
trailing = 'trailing' in options ? options.trailing : trailing;
}
var delayed = function() {
var remaining = wait - (Date.now() - stamp);
if (remaining <= 0) {
if (maxTimeoutId) {
clearTimeout(maxTimeoutId);
}
var isCalled = trailingCall;
maxTimeoutId = timeoutId = trailingCall = undefined;
if (isCalled) {
lastCalled = Date.now();
result = func.apply(thisArg, args);
if (!timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
}
} else {
timeoutId = setTimeout(delayed, remaining);
}
};
var maxDelayed = function() {
if (timeoutId) {
clearTimeout(timeoutId);
}
maxTimeoutId = timeoutId = trailingCall = undefined;
if (trailing || (maxWait !== wait)) {
lastCalled = Date.now();
result = func.apply(thisArg, args);
if (!timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
}
};
return function() {
args = arguments;
stamp = Date.now();
thisArg = this;
trailingCall = trailing && (timeoutId || !leading);
if (maxWait === false) {
var leadingCall = leading && !timeoutId;
} else {
if (!maxTimeoutId && !leading) {
lastCalled = stamp;
}
var remaining = maxWait - (stamp - lastCalled),
isCalled = remaining <= 0;
if (isCalled) {
if (maxTimeoutId) {
maxTimeoutId = clearTimeout(maxTimeoutId);
}
lastCalled = stamp;
result = func.apply(thisArg, args);
}
else if (!maxTimeoutId) {
maxTimeoutId = setTimeout(maxDelayed, remaining);
}
}
if (isCalled && timeoutId) {
timeoutId = clearTimeout(timeoutId);
}
else if (!timeoutId && wait !== maxWait) {
timeoutId = setTimeout(delayed, wait);
}
if (leadingCall) {
isCalled = true;
result = func.apply(thisArg, args);
}
if (isCalled && !timeoutId && !maxTimeoutId) {
args = thisArg = null;
}
return result;
};
};
function register( $$, $ ){
// use a single dummy dom ele as target for every qtip
var $qtipContainer = $('<div></div>');
var viewportDebounceRate = 250;
function generateOpts( target, passedOpts ){
var qtip = target.scratch().qtip;
var opts = $.extend( {}, passedOpts );
if( !opts.id ){
opts.id = 'cy-qtip-target-' + ( Date.now() + Math.round( Math.random() * 10000) );
}
if( !qtip.$domEle ){
qtip.$domEle = $qtipContainer;
}
// qtip should be positioned relative to cy dom container
opts.position = opts.position || {};
opts.position.container = opts.position.container || $( document.body );
opts.position.viewport = opts.position.viewport || $( document.body );
opts.position.target = [0, 0];
opts.position.my = opts.position.my || 'top center';
opts.position.at = opts.position.at || 'bottom center';
// adjust
var adjust = opts.position.adjust = opts.position.adjust || {};
adjust.method = adjust.method || 'flip';
adjust.mouse = false;
if( adjust.cyAdjustToEleBB === undefined ){
adjust.cyAdjustToEleBB = true;
}
// default show event
opts.show = opts.show || {};
if( !opts.show.event ){
opts.show.event = 'tap';
}
// default hide event
opts.hide = opts.hide || {};
opts.hide.cyViewport = opts.hide.cyViewport === undefined ? true : opts.hide.cyViewport;
if( !opts.hide.event ){
opts.hide.event = 'unfocus';
}
// so multiple qtips can exist at once (only works on recent qtip2 versions)
opts.overwrite = false;
var content;
if( opts.content ){
if( isFunction(opts.content) ){
content = opts.content;
} else if( opts.content.text && isFunction(opts.content.text) ){
content = opts.content.text;
}
if( content ){
opts.content = function(event, api){
return content.apply( target, [event, api] );
};
}
}
return opts;
}
$$('collection', 'qtip', function( passedOpts ){
var eles = this;
var cy = this.cy();
var container = cy.container();
if( passedOpts === 'api' ){
return this.scratch().qtip.api;
}
eles.each(function(i, ele){
var scratch = ele.scratch();
var qtip = scratch.qtip = scratch.qtip || {};
var opts = generateOpts( ele, passedOpts );
var adjNums = opts.position.adjust;
qtip.$domEle.qtip( opts );
var qtipApi = qtip.api = qtip.$domEle.qtip('api'); // save api ref
qtip.$domEle.removeData('qtip'); // remove qtip dom/api ref to be safe
var updatePosition = function(e){
var cOff = container.getBoundingClientRect();
var pos = ele.renderedPosition() || ( e ? e.cyRenderedPosition : undefined );
if( !pos || pos.x == null || isNaN(pos.x) ){ return; }
if( opts.position.adjust.cyAdjustToEleBB && ele.isNode() ){
var my = opts.position.my.toLowerCase();
var at = opts.position.at.toLowerCase();
var z = cy.zoom();
var w = ele.outerWidth() * z;
var h = ele.outerHeight() * z;
if( at.match('top') ){
pos.y -= h/2;
} else if( at.match('bottom') ){
pos.y += h/2;
}
if( at.match('left') ){
pos.x -= w/2;
} else if( at.match('right') ){
pos.x += w/2;
}
if( isNumber(adjNums.x) ){
pos.x += adjNums.x;
}
if( isNumber(adjNums.y) ){
pos.y += adjNums.y;
}
}
qtipApi.set('position.adjust.x', cOff.left + pos.x + window.pageXOffset);
qtipApi.set('position.adjust.y', cOff.top + pos.y + window.pageYOffset);
};
updatePosition();
ele.on( opts.show.event, function(e){
updatePosition(e);
qtipApi.show();
// FIX start
setTimeout(function () {
$('.qtip:visible').data('ele', ele);
}, 0);
// FIX end
} );
ele.on( opts.hide.event, function(e){
qtipApi.hide();
} );
if( opts.hide.cyViewport ){
cy.on('viewport', debounce(function(){
qtipApi.hide();
}, viewportDebounceRate, { leading: true }) );
}
if( opts.position.adjust.cyViewport ){
cy.on('pan zoom', debounce(function(e){
updatePosition(e);
qtipApi.reposition();
}, viewportDebounceRate, { trailing: true }) );
}
});
return this; // chainability
});
$$('core', 'qtip', function( passedOpts ){
var cy = this;
var container = cy.container();
if( passedOpts === 'api' ){
return this.scratch().qtip.api;
}
var scratch = cy.scratch();
var qtip = scratch.qtip = scratch.qtip || {};
var opts = generateOpts( cy, passedOpts );
qtip.$domEle.qtip( opts );
var qtipApi = qtip.api = qtip.$domEle.qtip('api'); // save api ref
qtip.$domEle.removeData('qtip'); // remove qtip dom/api ref to be safe
var updatePosition = function(e){
var cOff = container.getBoundingClientRect();
var pos = e.cyRenderedPosition;
if( !pos || pos.x == null || isNaN(pos.x) ){ return; }
qtipApi.set('position.adjust.x', cOff.left + pos.x + window.pageXOffset);
qtipApi.set('position.adjust.y', cOff.top + pos.y + window.pageYOffset);
};
cy.on( opts.show.event, function(e){
if( !opts.show.cyBgOnly || (opts.show.cyBgOnly && e.cyTarget === cy) ){
updatePosition(e);
qtipApi.show();
}
} );
cy.on( opts.hide.event, function(e){
if( !opts.hide.cyBgOnly || (opts.hide.cyBgOnly && e.cyTarget === cy) ){
qtipApi.hide();
}
} );
if( opts.hide.cyViewport ){
cy.on('viewport', debounce(function(){
qtipApi.hide();
}, viewportDebounceRate, { leading: true }) );
}
return this; // chainability
});
}
if( typeof module !== 'undefined' && module.exports ){ // expose as a commonjs module
module.exports = register;
}
if( typeof define !== 'undefined' && define.amd ){ // expose as an amd/requirejs module
define('cytoscape-qtip', function(){
return register;
});
}
if( $ && $$ ){
register( $$, $ );
}
})(
typeof jQuery !== 'undefined' ? jQuery : null,
typeof cytoscape !== 'undefined' ? cytoscape : null
);