This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
/
tips.js
507 lines (425 loc) · 14.9 KB
/
tips.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
// Tip coordinates calculator
function calculateTip(corner, width, height)
{
var width2 = Math.ceil(width / 2), height2 = Math.ceil(height / 2),
// Define tip coordinates in terms of height and width values
tips = {
bottomright: [[0,0], [width,height], [width,0]],
bottomleft: [[0,0], [width,0], [0,height]],
topright: [[0,height], [width,0], [width,height]],
topleft: [[0,0], [0,height], [width,height]],
topcenter: [[0,height], [width2,0], [width,height]],
bottomcenter: [[0,0], [width,0], [width2,height]],
rightcenter: [[0,0], [width,height2], [0,height]],
leftcenter: [[width,0], [width,height], [0,height2]]
};
// Set common side shapes
tips.lefttop = tips.bottomright; tips.righttop = tips.bottomleft;
tips.leftbottom = tips.topright; tips.rightbottom = tips.topleft;
return tips[ corner.string() ];
}
function Tip(qTip, command)
{
var self = this,
opts = qTip.options.style.tip,
elems = qTip.elements,
tooltip = elems.tooltip,
cache = {
top: 0,
left: 0,
corner: ''
},
size = {
width: opts.width,
height: opts.height
},
color = { },
border = opts.border || 0,
namespace = '.qtip-tip',
hasCanvas = $('<canvas />')[0].getContext;
self.corner = NULL;
self.mimic = NULL;
// Add new option checks for the plugin
qTip.checks.tip = {
'^position.my|style.tip.(corner|mimic|border)$': function() {
// Make sure a tip can be drawn
if(!self.init()) {
self.destroy();
}
// Reposition the tooltip
qTip.reposition();
},
'^style.tip.(height|width)$': function() {
// Re-set dimensions and redraw the tip
size = {
width: opts.width,
height: opts.height
};
self.create();
self.update();
// Reposition the tooltip
qTip.reposition();
},
'^content.title.text|style.(classes|widget)$': function() {
if(elems.tip) {
self.update();
}
}
};
function reposition(event, api, pos, viewport) {
if(!elems.tip) { return; }
var newCorner = $.extend({}, self.corner),
adjusted = pos.adjusted,
offset;
// Make sure our tip position isn't fixed e.g. doesn't adjust with adjust.screen
if(self.corner.fixed !== TRUE) {
// Adjust tip corners
if(adjusted.left) {
newCorner.x = newCorner.x === 'center' ? (adjusted.left > 0 ? 'left' : 'right') : (newCorner.x === 'left' ? 'right' : 'left');
}
if(adjusted.top) {
newCorner.y = newCorner.y === 'center' ? (adjusted.top > 0 ? 'top' : 'bottom') : (newCorner.y === 'top' ? 'bottom' : 'top');
}
// Update and redraw the tip if needed
if(newCorner.string() !== cache.corner && (cache.top !== adjusted.top || cache.left !== adjusted.left)) {
offset = self.update(newCorner);
}
}
// Adjust position to accomodate tip dimensions
if(!offset) { offset = self.position(newCorner, 0); }
if(offset.right !== undefined) { offset.left = offset.right; }
if(offset.bottom !== undefined) { offset.top = offset.bottom; }
offset.option = Math.max(0, opts.offset);
pos.left -= offset.left.charAt ? offset.option : (offset.right ? -1 : 1) * offset.left;
pos.top -= offset.top.charAt ? offset.option : (offset.bottom ? -1 : 1) * offset.top;
// Cache details
cache.left = adjusted.left; cache.top = adjusted.top;
cache.corner = newCorner.string();
}
/* border width calculator */
function borderWidth(corner, side, backup) {
side = !side ? corner[corner.precedance] : side;
var isTitleTop = elems.titlebar && corner.y === 'top',
elem = isTitleTop ? elems.titlebar : elems.content,
css = 'border-' + side + '-width',
val = parseInt(elem.css(css), 10);
return (backup ? val || parseInt(tooltip.css(css), 10) : val) || 0;
}
function borderRadius(corner) {
var isTitleTop = elems.titlebar && corner.y === 'top',
elem = isTitleTop ? elems.titlebar : elems.content,
moz = $.browser.mozilla,
prefix = moz ? '-moz-' : $.browser.webkit ? '-webkit-' : '',
side = corner.y + (moz ? '' : '-') + corner.x,
css = prefix + (moz ? 'border-radius-' + side : 'border-' + side + '-radius');
return parseInt(elem.css(css), 10) || parseInt(tooltip.css(css), 10) || 0;
}
function calculateSize(corner) {
var y = corner.precedance === 'y',
width = size [ y ? 'width' : 'height' ],
height = size [ y ? 'height' : 'width' ],
isCenter = corner.string().indexOf('center') > -1,
base = width * (isCenter ? 0.5 : 1),
pow = Math.pow,
round = Math.round,
bigHyp, ratio, result,
smallHyp = Math.sqrt( pow(base, 2) + pow(height, 2) ),
hyp = [
(border / base) * smallHyp, (border / height) * smallHyp
];
hyp[2] = Math.sqrt( pow(hyp[0], 2) - pow(border, 2) );
hyp[3] = Math.sqrt( pow(hyp[1], 2) - pow(border, 2) );
bigHyp = smallHyp + hyp[2] + hyp[3] + (isCenter ? 0 : hyp[0]);
ratio = bigHyp / smallHyp;
result = [ round(ratio * height), round(ratio * width) ];
return { height: result[ y ? 0 : 1 ], width: result[ y ? 1 : 0 ] };
}
$.extend(self, {
init: function()
{
var enabled = self.detectCorner() && (hasCanvas || $.browser.msie);
// Determine tip corner and type
if(enabled) {
// Create a new tip and draw it
self.create();
self.update();
// Bind update events
tooltip.unbind(namespace).bind('tooltipmove'+namespace, reposition);
}
return enabled;
},
detectCorner: function()
{
var corner = opts.corner,
posOptions = qTip.options.position,
at = posOptions.at,
my = posOptions.my.string ? posOptions.my.string() : posOptions.my;
// Detect corner and mimic properties
if(corner === FALSE || (my === FALSE && at === FALSE)) {
return FALSE;
}
else {
if(corner === TRUE) {
self.corner = new PLUGINS.Corner(my);
}
else if(!corner.string) {
self.corner = new PLUGINS.Corner(corner);
self.corner.fixed = TRUE;
}
}
return self.corner.string() !== 'centercenter';
},
detectColours: function() {
var i, fill, border,
tip = elems.tip.css({ backgroundColor: '', border: '' }),
corner = self.corner,
precedance = corner[ corner.precedance ],
borderSide = 'border-' + precedance + '-color',
borderSideCamel = 'border' + precedance.charAt(0) + precedance.substr(1) + 'Color',
invalid = /rgba?\(0, 0, 0(, 0)?\)|transparent/i,
backgroundColor = 'background-color',
transparent = 'transparent',
fluid = 'ui-tooltip-fluid',
bodyBorder = $(document.body).css('color'),
contentColour = qTip.elements.content.css('color'),
useTitle = elems.titlebar && (corner.y === 'top' || (corner.y === 'center' && tip.position().top + (size.height / 2) + opts.offset < elems.titlebar.outerHeight(1))),
colorElem = useTitle ? elems.titlebar : elems.content;
// Apply the fluid class so we can see our CSS values properly
tooltip.addClass(fluid);
// Detect tip colours from CSS styles
fill = tip.css(backgroundColor) || transparent;
border = tip[0].style[ borderSideCamel ];
// Make sure colours are valid
if(!fill || invalid.test(fill)) {
color.fill = colorElem.css(backgroundColor);
if(invalid.test(color.fill)) {
color.fill = tooltip.css(backgroundColor) || fill;
}
}
if(!border || invalid.test(border)) {
color.border = tooltip.css(borderSide);
if(invalid.test(color.border) || color.border === bodyBorder) {
color.border = colorElem.css(borderSide);
if(color.border === contentColour) { color.border = border; }
}
}
// Reset background and border colours, and remove fluid class
$('*', tip).add(tip).css(backgroundColor, transparent).css('border', '');
tooltip.removeClass(fluid);
},
create: function()
{
var width = size.width,
height = size.height,
vml;
// Remove previous tip element if present
if(elems.tip) { elems.tip.remove(); }
// Create tip element and prepend to the tooltip
elems.tip = $('<div />', { 'class': 'ui-tooltip-tip' }).css({ width: width, height: height }).prependTo(tooltip);
// Create tip drawing element(s)
if(hasCanvas) {
// save() as soon as we create the canvas element so FF2 doesn't bork on our first restore()!
$('<canvas />').appendTo(elems.tip)[0].getContext('2d').save();
}
else {
vml = '<vml:shape coordorigin="0,0" style="display:inline-block; position:absolute; behavior:url(#default#VML);"></vml:shape>';
elems.tip.html( border ? vml += vml : vml );
}
},
update: function(corner)
{
var tip = elems.tip,
inner = tip.children(),
width = size.width,
height = size.height,
regular = 'px solid ',
transparent = 'px dashed transparent', // Dashed IE6 border-transparency hack. Awesome!
mimic = opts.mimic,
round = Math.round,
precedance, context, coords, translate, newSize;
// Re-determine tip if not already set
if(!corner) { corner = self.corner; }
// Use corner property if we detect an invalid mimic value
if(mimic === FALSE) { mimic = corner; }
// Otherwise inherit mimic properties from the corner object as necessary
else {
mimic = new PLUGINS.Corner(mimic);
mimic.precedance = corner.precedance;
if(mimic.x === 'inherit') { mimic.x = corner.x; }
else if(mimic.y === 'inherit') { mimic.y = corner.y; }
else if(mimic.x === mimic.y) {
mimic[ corner.precedance ] = corner[ corner.precedance ];
}
}
precedance = mimic.precedance;
// Update our colours
self.detectColours();
// Detect border width, taking into account colours
border = color.border === 'transparent' || color.border === '#123456' ? 0 :
opts.border === TRUE ? borderWidth(corner, NULL, TRUE) : opts.border;
// Calculate coordinates
coords = calculateTip(mimic, width , height);
// Determine tip size
newSize = calculateSize(corner);
tip.css(newSize);
// Calculate tip translation
if(corner.precedance === 'y') {
translate = [
round(mimic.x === 'left' ? border : mimic.x === 'right' ? newSize.width - width - border : (newSize.width - width) / 2),
round(mimic.y === 'top' ? newSize.height - height : 0)
];
}
else {
translate = [
round(mimic.x === 'left' ? newSize.width - width : 0),
round(mimic.y === 'top' ? border : mimic.y === 'bottom' ? newSize.height - height - border : (newSize.height - height) / 2)
];
}
// Canvas drawing implementation
if(hasCanvas) {
// Set the canvas size using calculated size
inner.attr(newSize);
// Grab canvas context and clear/save it
context = inner[0].getContext('2d');
context.restore(); context.save();
context.clearRect(0,0,3000,3000);
// Translate origin
context.translate(translate[0], translate[1]);
// Draw the tip
context.beginPath();
context.moveTo(coords[0][0], coords[0][1]);
context.lineTo(coords[1][0], coords[1][1]);
context.lineTo(coords[2][0], coords[2][1]);
context.closePath();
context.fillStyle = color.fill;
context.strokeStyle = color.border;
context.lineWidth = border * 2;
context.lineJoin = 'miter';
context.miterLimit = 100;
context.stroke();
context.fill();
}
// VML (IE Proprietary implementation)
else {
// Setup coordinates string
coords = 'm' + coords[0][0] + ',' + coords[0][1] + ' l' + coords[1][0] +
',' + coords[1][1] + ' ' + coords[2][0] + ',' + coords[2][1] + ' xe';
// Setup VML-specific offset for pixel-perfection
translate[2] = border && /^(r|b)/i.test(corner.string()) ?
parseFloat($.browser.version, 10) === 8 ? 2 : 1 : 0;
// Set initial CSS
inner.css({
antialias: ''+(mimic.string().indexOf('center') > -1),
left: translate[0] - (translate[2] * Number(precedance === 'x')),
top: translate[1] - (translate[2] * Number(precedance === 'y')),
width: width + border,
height: height + border
})
.each(function(i) {
var $this = $(this);
// Set shape specific attributes
$this.attr({
coordsize: (width+border) + ' ' + (height+border),
path: coords,
fillcolor: color.fill,
filled: !!i,
stroked: !!!i
})
.css({ display: border || i ? 'block' : 'none' });
// Check if border is enabled and add stroke element
if(!i && border > 0 && $this.html() === '') {
$this.html(
'<vml:stroke weight="'+(border*2)+'px" color="red" miterlimit="1000" joinstyle="miter" ' +
' style="behavior:url(#default#VML); display:inline-block;" />'
);
}
});
}
return self.position(corner, 1);
},
// Tip positioning method
position: function(corner, set)
{
var tip = elems.tip,
position = {},
offset = Math.max(0, opts.offset),
precedance, dimensions,
adjust;
// Return if tips are disabled or tip is not yet rendered
if(opts.corner === FALSE || !tip) { return FALSE; }
// Inherit corner if not provided
corner = corner || self.corner;
precedance = corner.precedance;
// Determine which tip dimension to use for adjustment
dimensions = calculateSize(corner);
// Setup IE specific dimension adjustments
adjust = $.browser.msie && parseFloat($.browser.version, 10) == 8 &&
border && /^(b|r)/i.test(corner.string()) ? 1 : 0;
// Calculate tip position
$.each(
precedance === 'y' ? [ corner.x, corner.y ] : [ corner.y, corner.x ],
function(i, side)
{
var b, br;
if(side === 'center') {
b = precedance === 'y' ? 'left' : 'top';
position[ b ] = '50%';
position['margin-' + b] = -Math.round(dimensions[ precedance === 'y' ? 'width' : 'height' ] / 2) + offset;
}
else {
b = borderWidth(corner, side, TRUE);
br = borderRadius(corner);
position[ side ] = i || !border ?
borderWidth(corner, side) + (!i ? br : 0) :
offset + (br > b ? br : 0);
}
}
);
position[ corner[precedance] ] -= dimensions[ precedance === 'x' ? 'width' : 'height' ] + adjust;
// Set and return new position
if(set) { tip.css({ top: '', bottom: '', left: '', right: '', margin: '' }).css(position); }
return position;
},
destroy: function()
{
// Remov tip and bound events
if(elems.tip) { elems.tip.remove(); }
tooltip.unbind(namespace);
}
});
self.init();
}
PLUGINS.tip = function(api)
{
var self = api.plugins.tip;
return 'object' === typeof self ? self : (api.plugins.tip = new Tip(api));
};
// Initialize tip on render
PLUGINS.tip.initialize = 'render';
// Setup plugin sanitization options
PLUGINS.tip.sanitize = function(options)
{
var style = options.style, opts;
if(style && 'tip' in style) {
opts = options.style.tip;
if(typeof opts !== 'object'){ options.style.tip = { corner: opts }; }
if(!(/string|boolean/i).test(typeof opts.corner)) { opts.corner = TRUE; }
if(typeof opts.width !== 'number'){ delete opts.width; }
if(typeof opts.height !== 'number'){ delete opts.height; }
if(typeof opts.border !== 'number' && opts.border !== TRUE){ delete opts.border; }
if(typeof opts.offset !== 'number'){ delete opts.offset; }
}
};
// Extend original qTip defaults
$.extend(TRUE, QTIP.defaults, {
style: {
tip: {
corner: TRUE,
mimic: FALSE,
width: 6,
height: 6,
border: TRUE,
offset: 0
}
}
});