This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 466
/
class.js
589 lines (478 loc) · 18 KB
/
class.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* Core plugin implementation
*/
function QTip(target, options, id, attr) {
// Elements and ID
this.id = id;
this.target = target;
this.tooltip = NULL;
this.elements = elements = { target: target };
// Internal constructs
this._id = NAMESPACE + '-' + id;
this.timers = { img: {} };
this.options = options;
this.plugins = {};
// Cache object
this.cache = cache = {
event: {},
target: $(),
disabled: FALSE,
attr: attr,
onTarget: FALSE,
lastClass: ''
};
// Set the initial flags
this.rendered = this.destroyed = this.disabled = this.waiting =
this.hiddenDuringWait = this.positioning = this.triggering = FALSE;
}
PROTOTYPE = QTip.prototype;
PROTOTYPE.render = function(show) {
if(this.rendered || this.destroyed) { return this; } // If tooltip has already been rendered, exit
var self = this,
options = this.options,
cache = this.cache,
elements = this.elements,
text = options.content.text,
title = options.content.title,
button = options.content.button,
posOptions = options.position,
deferreds = [];
// Add ARIA attributes to target
$.attr(this.target[0], 'aria-describedby', this._id);
// Create tooltip element
this.tooltip = elements.tooltip = tooltip = $('<div/>', {
'id': this._id,
'class': [ NAMESPACE, defaultClass, options.style.classes, NAMESPACE + '-pos-' + options.position.my.abbrev() ].join(' '),
'width': options.style.width || '',
'height': options.style.height || '',
'tracking': posOptions.target === 'mouse' && posOptions.adjust.mouse,
/* ARIA specific attributes */
'role': 'alert',
'aria-live': 'polite',
'aria-atomic': FALSE,
'aria-describedby': this._id + '-content',
'aria-hidden': TRUE
})
.toggleClass(disabledClass, this.disabled)
.data(NAMESPACE, this)
.appendTo(posOptions.container)
.append(
// Create content element
elements.content = $('<div />', {
'class': NAMESPACE + '-content',
'id': this._id + '-content',
'aria-atomic': TRUE
})
);
// Set rendered flag and prevent redundant reposition calls for now
this.rendered = -1;
this.positioning = TRUE;
// Create title...
if(title) {
this._createTitle();
// Update title only if its not a callback (called in toggle if so)
if(!$.isFunction(title)) {
deferreds.push( this._updateTitle(title, FALSE) );
}
}
// Create button
if(button) { this._createButton(); }
// Set proper rendered flag and update content if not a callback function (called in toggle)
if(!$.isFunction(text)) {
deferreds.push( this._updateContent(text, FALSE) );
}
this.rendered = TRUE;
// Setup widget classes
this._setWidget();
// Assign passed event callbacks (before plugins!)
$.each(options.events, function(name, callback) {
$.isFunction(callback) && tooltip.bind(
name === 'toggle' ? ['tooltipshow', 'tooltiphide'] : 'tooltip'+name, callback
);
});
// Initialize 'render' plugins
$.each(PLUGINS, function(name) {
var instance;
if(this.initialize === 'render' && (instance = this(self))) {
(self.plugins[name] = instance).qtip = self;
}
});
// Assign events
this._assignEvents();
// When deferreds have completed
$.when.apply($, deferreds).then(function() {
// tooltiprender event
self._trigger('render');
// Reset flags
self.positioning = FALSE;
// Show tooltip if not hidden during wait period
if(!self.hiddenDuringWait && (options.show.ready || show)) {
self.toggle(TRUE, cache.event, FALSE);
}
self.hiddenDuringWait = FALSE;
});
return this;
};
PROTOTYPE.toggle = function(state, event) {
var cache = this.cache,
options = this.options,
tooltip = this.tooltip;
// Try to prevent flickering when tooltip overlaps show element
if(event) {
if((/over|enter/).test(event.type) && (/out|leave/).test(cache.event.type) &&
options.show.target.add(event.target).length === options.show.target.length &&
tooltip.has(event.relatedTarget).length) {
return this;
}
// Cache event
cache.event = $.extend({}, event);
}
// If we're currently waiting and we've just hidden... stop it
this.waiting && !state && (this.hiddenDuringWait = TRUE);
// Render the tooltip if showing and it isn't already
if(!this.rendered) { return state ? this.render(1) : this; }
else if(this.destroyed) { return this; }
var type = state ? 'show' : 'hide',
opts = this.options[type],
otherOpts = this.options[ !state ? 'show' : 'hide' ],
posOptions = this.options.position,
contentOptions = this.options.content,
width = this.tooltip.css('width'),
visible = this.tooltip[0].offsetWidth > 0,
animate = state || opts.target.length === 1,
sameTarget = !event || opts.target.length < 2 || cache.target[0] === event.target,
identicalState, allow, showEvent, delay;
// Detect state if valid one isn't provided
if((typeof state).search('boolean|number')) { state = !visible; }
// Check if the tooltip is in an identical state to the new would-be state
identicalState = !tooltip.is(':animated') && visible === state && sameTarget;
// Fire tooltip(show/hide) event and check if destroyed
allow = !identicalState ? !!this._trigger(type, [90]) : NULL;
// If the user didn't stop the method prematurely and we're showing the tooltip, focus it
if(allow !== FALSE && state) { this.focus(event); }
// If the state hasn't changed or the user stopped it, return early
if(!allow || identicalState) { return this; }
// Set ARIA hidden attribute
$.attr(tooltip[0], 'aria-hidden', !!!state);
// Execute state specific properties
if(state) {
// Store show origin coordinates
cache.origin = $.extend({}, this.mouse);
// Update tooltip content & title if it's a dynamic function
if($.isFunction(contentOptions.text)) { this._updateContent(contentOptions.text, FALSE); }
if($.isFunction(contentOptions.title)) { this._updateTitle(contentOptions.title, FALSE); }
// Cache mousemove events for positioning purposes (if not already tracking)
if(!trackingBound && posOptions.target === 'mouse' && posOptions.adjust.mouse) {
$(document).bind('mousemove.'+NAMESPACE, this._storeMouse);
trackingBound = TRUE;
}
// Update the tooltip position (set width first to prevent viewport/max-width issues)
if(!width) { tooltip.css('width', tooltip.outerWidth(FALSE)); }
this.reposition(event, arguments[2]);
if(!width) { tooltip.css('width', ''); }
// Hide other tooltips if tooltip is solo
if(!!opts.solo) {
(typeof opts.solo === 'string' ? $(opts.solo) : $(selector, opts.solo))
.not(tooltip).not(opts.target).qtip('hide', $.Event('tooltipsolo'));
}
}
else {
// Clear show timer if we're hiding
clearTimeout(this.timers.show);
// Remove cached origin on hide
delete cache.origin;
// Remove mouse tracking event if not needed (all tracking qTips are hidden)
if(trackingBound && !$(selector+'[tracking="true"]:visible', opts.solo).not(tooltip).length) {
$(document).unbind('mousemove.'+NAMESPACE);
trackingBound = FALSE;
}
// Blur the tooltip
this.blur(event);
}
// Define post-animation, state specific properties
after = $.proxy(function() {
if(state) {
// Prevent antialias from disappearing in IE by removing filter
if(PLUGINS.ie) { tooltip[0].style.removeAttribute('filter'); }
// Remove overflow setting to prevent tip bugs
tooltip.css('overflow', '');
// Autofocus elements if enabled
if('string' === typeof opts.autofocus) {
$(this.options.show.autofocus, tooltip).focus();
}
// If set, hide tooltip when inactive for delay period
this.options.show.target.trigger('qtip-'+this.id+'-inactive');
}
else {
// Reset CSS states
tooltip.css({
display: '',
visibility: '',
opacity: '',
left: '',
top: ''
});
}
// tooltipvisible/tooltiphidden events
this._trigger(state ? 'visible' : 'hidden');
}, this);
// If no effect type is supplied, use a simple toggle
if(opts.effect === FALSE || animate === FALSE) {
tooltip[ type ]();
after();
}
// Use custom function if provided
else if($.isFunction(opts.effect)) {
tooltip.stop(1, 1);
opts.effect.call(tooltip, this);
tooltip.queue('fx', function(n) {
after(); n();
});
}
// Use basic fade function by default
else { tooltip.fadeTo(90, state ? 1 : 0, after); }
// If inactive hide method is set, active it
if(state) { opts.target.trigger('qtip-'+this.id+'-inactive'); }
return this;
};
PROTOTYPE.show = function(event) { return this.toggle(TRUE, event); };
PROTOTYPE.hide = function(event) { return this.toggle(FALSE, event); };
PROTOTYPE.focus = function(event) {
if(!this.rendered || this.destroyed) { return this; }
var qtips = $(selector),
tooltip = this.tooltip,
curIndex = parseInt(tooltip[0].style.zIndex, 10),
newIndex = QTIP.zindex + qtips.length,
focusedElem;
// Only update the z-index if it has changed and tooltip is not already focused
if(!tooltip.hasClass(focusClass)) {
// tooltipfocus event
if(this._trigger('focus', [newIndex], event)) {
// Only update z-index's if they've changed
if(curIndex !== newIndex) {
// Reduce our z-index's and keep them properly ordered
qtips.each(function() {
if(this.style.zIndex > curIndex) {
this.style.zIndex = this.style.zIndex - 1;
}
});
// Fire blur event for focused tooltip
qtips.filter('.' + focusClass).qtip('blur', event);
}
// Set the new z-index
tooltip.addClass(focusClass)[0].style.zIndex = newIndex;
}
}
return this;
};
PROTOTYPE.blur = function(event) {
if(!this.rendered || this.destroyed) { return this; }
// Set focused status to FALSE
this.tooltip.removeClass(focusClass);
// tooltipblur event
this._trigger('blur', [ this.tooltip.css('zIndex') ], event);
return this;
};
PROTOTYPE.reposition = function(event, effect) {
if(!this.rendered || this.positioning || this.destroyed) { return this; }
// Set positioning flag
this.positioning = TRUE;
var cache = this.cache,
tooltip = this.tooltip,
posOptions = this.options.position,
target = posOptions.target,
my = posOptions.my,
at = posOptions.at,
viewport = posOptions.viewport,
container = posOptions.container,
adjust = posOptions.adjust,
method = adjust.method.split(' '),
elemWidth = tooltip.outerWidth(FALSE),
elemHeight = tooltip.outerHeight(FALSE),
targetWidth = 0,
targetHeight = 0,
type = tooltip.css('position'),
position = { left: 0, top: 0 },
visible = tooltip[0].offsetWidth > 0,
isScroll = event && event.type === 'scroll',
win = $(window),
mouse = this.mouse,
pluginCalculations, offset;
// Check if absolute position was passed
if($.isArray(target) && target.length === 2) {
// Force left top and set position
at = { x: LEFT, y: TOP };
position = { left: target[0], top: target[1] };
}
// Check if mouse was the target
else if(target === 'mouse' && ((event && event.pageX) || cache.event.pageX)) {
// Force left top to allow flipping
at = { x: LEFT, y: TOP };
// Use cached event if one isn't available for positioning
event = mouse && mouse.pageX && (adjust.mouse || !event || !event.pageX) ? { pageX: mouse.pageX, pageY: mouse.pageY } :
(event && (event.type === 'resize' || event.type === 'scroll') ? cache.event :
event && event.pageX && event.type === 'mousemove' ? event :
(!adjust.mouse || this.options.show.distance) && cache.origin && cache.origin.pageX ? cache.origin :
event) || event || cache.event || mouse || {};
// Use event coordinates for position
if(type !== 'static') { position = container.offset(); }
position = { left: event.pageX - position.left, top: event.pageY - position.top };
// Scroll events are a pain, some browsers
if(adjust.mouse && isScroll) {
position.left -= mouse.scrollX - win.scrollLeft();
position.top -= mouse.scrollY - win.scrollTop();
}
}
// Target wasn't mouse or absolute...
else {
// Check if event targetting is being used
if(target === 'event' && event && event.target && event.type !== 'scroll' && event.type !== 'resize') {
cache.target = $(event.target);
}
else if(target !== 'event'){
cache.target = $(target.jquery ? target : elements.target);
}
target = cache.target;
// Parse the target into a jQuery object and make sure there's an element present
target = $(target).eq(0);
if(target.length === 0) { return this; }
// Check if window or document is the target
else if(target[0] === document || target[0] === window) {
targetWidth = PLUGINS.iOS ? window.innerWidth : target.width();
targetHeight = PLUGINS.iOS ? window.innerHeight : target.height();
if(target[0] === window) {
position = {
top: (viewport || target).scrollTop(),
left: (viewport || target).scrollLeft()
};
}
}
// Check if the target is an <AREA> element
else if(PLUGINS.imagemap && target.is('area')) {
pluginCalculations = PLUGINS.imagemap(this, target, at, PLUGINS.viewport ? method : FALSE);
}
// Check if the target is an SVG element
else if(PLUGINS.svg && target[0].ownerSVGElement) {
pluginCalculations = PLUGINS.svg(this, target, at, PLUGINS.viewport ? method : FALSE);
}
// Otherwise use regular jQuery methods
else {
targetWidth = target.outerWidth(FALSE);
targetHeight = target.outerHeight(FALSE);
position = target.offset();
}
// Parse returned plugin values into proper variables
if(pluginCalculations) {
targetWidth = pluginCalculations.width;
targetHeight = pluginCalculations.height;
offset = pluginCalculations.offset;
position = pluginCalculations.position;
}
// Adjust position to take into account offset parents
position = PLUGINS.offset(target, position, container);
// Adjust for position.fixed tooltips (and also iOS scroll bug in v3.2-4.0 & v4.3-4.3.2)
if((PLUGINS.iOS > 3.1 && PLUGINS.iOS < 4.1) ||
(PLUGINS.iOS >= 4.3 && PLUGINS.iOS < 4.33) ||
(!PLUGINS.iOS && type === 'fixed')
){
position.left -= win.scrollLeft();
position.top -= win.scrollTop();
}
// Adjust position relative to target
if(!pluginCalculations || (pluginCalculations && pluginCalculations.adjustable !== FALSE)) {
position.left += at.x === RIGHT ? targetWidth : at.x === CENTER ? targetWidth / 2 : 0;
position.top += at.y === BOTTOM ? targetHeight : at.y === CENTER ? targetHeight / 2 : 0;
}
}
// Adjust position relative to tooltip
position.left += adjust.x + (my.x === RIGHT ? -elemWidth : my.x === CENTER ? -elemWidth / 2 : 0);
position.top += adjust.y + (my.y === BOTTOM ? -elemHeight : my.y === CENTER ? -elemHeight / 2 : 0);
// Use viewport adjustment plugin if enabled
if(PLUGINS.viewport) {
position.adjusted = PLUGINS.viewport(
this, position, posOptions, targetWidth, targetHeight, elemWidth, elemHeight
);
// Apply offsets supplied by positioning plugin (if used)
if(offset && position.adjusted.left) { position.left += offset.left; }
if(offset && position.adjusted.top) { position.top += offset.top; }
}
// Viewport adjustment is disabled, set values to zero
else { position.adjusted = { left: 0, top: 0 }; }
// tooltipmove event
if(!this._trigger('move', [position, viewport.elem || viewport], event)) { return this; }
delete position.adjusted;
// If effect is disabled, target it mouse, no animation is defined or positioning gives NaN out, set CSS directly
if(effect === FALSE || !visible || isNaN(position.left) || isNaN(position.top) || target === 'mouse' || !$.isFunction(posOptions.effect)) {
tooltip.css(position);
}
// Use custom function if provided
else if($.isFunction(posOptions.effect)) {
posOptions.effect.call(tooltip, this, $.extend({}, position));
tooltip.queue(function(next) {
// Reset attributes to avoid cross-browser rendering bugs
$(this).css({ opacity: '', height: '' });
if(PLUGINS.ie) { this.style.removeAttribute('filter'); }
next();
});
}
// Set positioning flag
this.positioning = FALSE;
return this;
};
PROTOTYPE.disable = function(state) {
if(this.destroyed) { return this; }
if('boolean' !== typeof state) {
state = !(this.tooltip.hasClass(disabledClass) || this.disabled);
}
if(this.rendered) {
this.tooltip.toggleClass(disabledClass, state)
.attr('aria-disabled', state);
}
this.disabled = !!state;
return this;
};
PROTOTYPE.enable = function() { return this.disable(FALSE); };
PROTOTYPE.destroy = function(immediate) {
// Set flag the signify destroy is taking place to plugins
// and ensure it only gets destroyed once!
if(this.destroyed) { return this.target; }
function process() {
var target = this.target,
title = target.attr(oldtitle);
// Set flag
this.destroyed = TRUE;
// Destroy tooltip if rendered
if(this.rendered) {
this.tooltip.stop(1,0).find('*').remove().end().remove();
}
// Destroy all plugins
$.each(this.plugins, function(name) {
this.destroy && this.destroy();
});
// Clear timers and remove bound events
clearTimeout(this.timers.show);
clearTimeout(this.timers.hide);
this._unassignEvents();
// Remove api object and ARIA attributes
target.removeData(NAMESPACE).removeAttr(HASATTR)
.removeAttr('aria-describedby');
// Reset old title attribute if removed
if(this.options.suppress && title) {
target.attr('title', title).removeAttr(oldtitle);
}
// Remove qTip events associated with this API
target.unbind('.'+this._id);
// Remove ID from used id objects, and delete object references
// for better garbage collection and leak protection
this.options = this.elements = this.cache = this.timers =
this.plugins = this.mouse = usedIDs[this.id] = null;
}
// If an immediate destory is needed
if(immediate !== TRUE && this.rendered) {
tooltip.one('tooltiphidden', $.proxy(process, this));
!this.triggering && this.hide();
}
// If we're not in the process of hiding... process
else { process.call(this); }
return this.target;
};