-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.qtip-1.0.0-rc3.js
2155 lines (1812 loc) · 83.2 KB
/
jquery.qtip-1.0.0-rc3.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* jquery.qtip. The jQuery tooltip plugin
*
* Copyright (c) 2009 Craig Thompson
* http://craigsworks.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
* Launch : February 2009
* Version : 1.0.0-rc3
* Released: Tuesday 12th May, 2009 - 00:00
* Debug: jquery.qtip.debug.js
*/
(function($)
{
// Implementation
$.fn.qtip = function(options, blanket)
{
var i, id, interfaces, opts, obj, command, config, api;
// Return API / Interfaces if requested
if(typeof options == 'string')
{
// Make sure API data exists if requested
if(typeof $(this).data('qtip') !== 'object')
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.NO_TOOLTIP_PRESENT, false);
// Return requested object
if(options == 'api')
return $(this).data('qtip').interfaces[ $(this).data('qtip').current ];
else if(options == 'interfaces')
return $(this).data('qtip').interfaces;
}
// Validate provided options
else
{
// Set null options object if no options are provided
if(!options) options = {};
// Sanitize option data
if(typeof options.content !== 'object' || (options.content.jquery && options.content.length > 0)) options.content = { text: options.content };
if(typeof options.content.title !== 'object') options.content.title = { text: options.content.title };
if(typeof options.position !== 'object') options.position = { corner: options.position };
if(typeof options.position.corner !== 'object') options.position.corner = { target: options.position.corner, tooltip: options.position.corner };
if(typeof options.show !== 'object') options.show = { when: options.show };
if(typeof options.show.when !== 'object') options.show.when = { event: options.show.when };
if(typeof options.show.effect !== 'object') options.show.effect = { type: options.show.effect };
if(typeof options.hide !== 'object') options.hide = { when: options.hide };
if(typeof options.hide.when !== 'object') options.hide.when = { event: options.hide.when };
if(typeof options.hide.effect !== 'object') options.hide.effect = { type: options.hide.effect };
if(typeof options.style !== 'object') options.style = { name: options.style };
options.style = sanitizeStyle(options.style);
// Build main options object
opts = $.extend(true, {}, $.fn.qtip.defaults, options);
// Inherit all style properties into one syle object and include original options
opts.style = buildStyle.call({ options: opts }, opts.style);
opts.user = $.extend(true, {}, options);
};
// Iterate each matched element
return $(this).each(function() // Return original elements as per jQuery guidelines
{
// Check for API commands
if(typeof options == 'string')
{
command = options.toLowerCase();
interfaces = $(this).qtip('interfaces');
// Make sure API data exists$('.qtip').qtip('destroy')
if(typeof interfaces == 'object')
{
// Check if API call is a BLANKET DESTROY command
if(blanket === true && command == 'destroy')
while(interfaces.length > 0) interfaces[interfaces.length-1].destroy();
// API call is not a BLANKET DESTROY command
else
{
// Check if supplied command effects this tooltip only (NOT BLANKET)
if(blanket !== true) interfaces = [ $(this).qtip('api') ];
// Execute command on chosen qTips
for(i = 0; i < interfaces.length; i++)
{
// Destroy command doesn't require tooltip to be rendered
if(command == 'destroy') interfaces[i].destroy();
// Only call API if tooltip is rendered and it wasn't a destroy call
else if(interfaces[i].status.rendered === true)
{
if(command == 'show') interfaces[i].show();
else if(command == 'hide') interfaces[i].hide();
else if(command == 'focus') interfaces[i].focus();
else if(command == 'disable') interfaces[i].disable(true);
else if(command == 'enable') interfaces[i].disable(false);
};
};
};
};
}
// No API commands, continue with qTip creation
else
{
// Create unique configuration object
config = $.extend(true, {}, opts);
config.hide.effect.length = opts.hide.effect.length;
config.show.effect.length = opts.show.effect.length;
// Sanitize target options
if(config.position.container === false) config.position.container = $(document.body);
if(config.position.target === false) config.position.target = $(this);
if(config.show.when.target === false) config.show.when.target = $(this);
if(config.hide.when.target === false) config.hide.when.target = $(this);
// Determine tooltip ID (Reuse array slots if possible)
id = $.fn.qtip.interfaces.length;
for(i = 0; i < id; i++)
{
if(typeof $.fn.qtip.interfaces[i] == 'undefined'){ id = i; break; };
};
// Instantiate the tooltip
obj = new qTip($(this), config, id);
// Add API references
$.fn.qtip.interfaces[id] = obj;
// Check if element already has qTip data assigned
if(typeof $(this).data('qtip') == 'object')
{
// Set new current interface id
if(typeof $(this).attr('qtip') === 'undefined')
$(this).data('qtip').current = $(this).data('qtip').interfaces.length;
// Push new API interface onto interfaces array
$(this).data('qtip').interfaces.push(obj);
}
// No qTip data is present, create now
else $(this).data('qtip', { current: 0, interfaces: [obj] });
// If prerendering is disabled, create tooltip on showEvent
if(config.content.prerender === false && config.show.when.event !== false && config.show.ready !== true)
{
config.show.when.target.bind(config.show.when.event+'.qtip-'+id+'-create', { qtip: id }, function(event)
{
// Retrieve API interface via passed qTip Id
api = $.fn.qtip.interfaces[ event.data.qtip ];
// Unbind show event and cache mouse coords
api.options.show.when.target.unbind(api.options.show.when.event+'.qtip-'+event.data.qtip+'-create');
api.cache.mouse = { x: event.pageX, y: event.pageY };
// Render tooltip and start the event sequence
construct.call( api );
api.options.show.when.target.trigger(api.options.show.when.event);
});
}
// Prerendering is enabled, create tooltip now
else
{
// Set mouse position cache to top left of the element
obj.cache.mouse = {
x: config.show.when.target.offset().left,
y: config.show.when.target.offset().top
};
// Construct the tooltip
construct.call(obj);
}
};
});
};
// Instantiator
function qTip(target, options, id)
{
// Declare this reference
var self = this;
// Setup class attributes
self.id = id;
self.options = options;
self.status = {
animated: false,
rendered: false,
disabled: false,
focused: false
};
self.elements = {
target: target.addClass(self.options.style.classes.target),
tooltip: null,
wrapper: null,
content: null,
contentWrapper: null,
title: null,
button: null,
tip: null,
bgiframe: null
};
self.cache = {
mouse: {},
position: {},
toggle: 0
};
self.timers = {};
// Define exposed API methods
$.extend(self, self.options.api,
{
show: function(event)
{
var returned, solo;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'show');
// Only continue if element is visible
if(self.elements.tooltip.css('display') !== 'none') return self;
// Clear animation queue
self.elements.tooltip.stop(true, false);
// Call API method and if return value is false, halt
returned = self.beforeShow.call(self, event);
if(returned === false) return self;
// Define afterShow callback method
function afterShow()
{
// Call API method and focus if it isn't static
if(self.options.position.type !== 'static') self.focus();
self.onShow.call(self, event);
// Prevent antialias from disappearing in IE7 by removing filter attribute
if($.browser.msie) self.elements.tooltip.get(0).style.removeAttribute('filter');
};
// Maintain toggle functionality if enabled
self.cache.toggle = 1;
// Update tooltip position if it isn't static
if(self.options.position.type !== 'static')
self.updatePosition(event, (self.options.show.effect.length > 0));
// Hide other tooltips if tooltip is solo
if(typeof self.options.show.solo == 'object') solo = $(self.options.show.solo);
else if(self.options.show.solo === true) solo = $('div.qtip').not(self.elements.tooltip);
if(solo) solo.each(function(){ if($(this).qtip('api').status.rendered === true) $(this).qtip('api').hide(); });
// Show tooltip
if(typeof self.options.show.effect.type == 'function')
{
self.options.show.effect.type.call(self.elements.tooltip, self.options.show.effect.length);
self.elements.tooltip.queue(function(){ afterShow(); $(this).dequeue(); });
}
else
{
switch(self.options.show.effect.type.toLowerCase())
{
case 'fade':
self.elements.tooltip.fadeIn(self.options.show.effect.length, afterShow);
break;
case 'slide':
self.elements.tooltip.slideDown(self.options.show.effect.length, function()
{
afterShow();
if(self.options.position.type !== 'static') self.updatePosition(event, true);
});
break;
case 'grow':
self.elements.tooltip.show(self.options.show.effect.length, afterShow);
break;
default:
self.elements.tooltip.show(null, afterShow);
break;
};
// Add active class to tooltip
self.elements.tooltip.addClass(self.options.style.classes.active);
};
// Log event and return
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_SHOWN, 'show');
},
hide: function(event)
{
var returned;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'hide');
// Only continue if element is visible
else if(self.elements.tooltip.css('display') === 'none') return self;
// Stop show timer and animation queue
clearTimeout(self.timers.show);
self.elements.tooltip.stop(true, false);
// Call API method and if return value is false, halt
returned = self.beforeHide.call(self, event);
if(returned === false) return self;
// Define afterHide callback method
function afterHide(){ self.onHide.call(self, event); };
// Maintain toggle functionality if enabled
self.cache.toggle = 0;
// Hide tooltip
if(typeof self.options.hide.effect.type == 'function')
{
self.options.hide.effect.type.call(self.elements.tooltip, self.options.hide.effect.length);
self.elements.tooltip.queue(function(){ afterHide(); $(this).dequeue(); });
}
else
{
switch(self.options.hide.effect.type.toLowerCase())
{
case 'fade':
self.elements.tooltip.fadeOut(self.options.hide.effect.length, afterHide);
break;
case 'slide':
self.elements.tooltip.slideUp(self.options.hide.effect.length, afterHide);
break;
case 'grow':
self.elements.tooltip.hide(self.options.hide.effect.length, afterHide);
break;
default:
self.elements.tooltip.hide(null, afterHide);
break;
};
// Remove active class to tooltip
self.elements.tooltip.removeClass(self.options.style.classes.active);
};
// Log event and return
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_HIDDEN, 'hide');
},
updatePosition: function(event, animate)
{
var i, target, tooltip, coords, mapName, imagePos, newPosition, ieAdjust, ie6Adjust, borderAdjust, mouseAdjust, offset, curPosition, returned
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updatePosition');
// If tooltip is static, return
else if(self.options.position.type == 'static')
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.CANNOT_POSITION_STATIC, 'updatePosition');
// Define property objects
target = {
position: { left: 0, top: 0 },
dimensions: { height: 0, width: 0 },
corner: self.options.position.corner.target
};
tooltip = {
position: self.getPosition(),
dimensions: self.getDimensions(),
corner: self.options.position.corner.tooltip
};
// Target is an HTML element
if(self.options.position.target !== 'mouse')
{
// If the HTML element is AREA, calculate position manually
if(self.options.position.target.get(0).nodeName.toLowerCase() == 'area')
{
// Retrieve coordinates from coords attribute and parse into integers
coords = self.options.position.target.attr('coords').split(',');
for(i = 0; i < coords.length; i++) coords[i] = parseInt(coords[i]);
// Setup target position object
mapName = self.options.position.target.parent('map').attr('name');
imagePos = $('img[usemap="#'+mapName+'"]:first').offset();
target.position = {
left: Math.floor(imagePos.left + coords[0]),
top: Math.floor(imagePos.top + coords[1])
};
// Determine width and height of the area
switch(self.options.position.target.attr('shape').toLowerCase())
{
case 'rect':
target.dimensions = {
width: Math.ceil(Math.abs(coords[2] - coords[0])),
height: Math.ceil(Math.abs(coords[3] - coords[1]))
};
break;
case 'circle':
target.dimensions = {
width: coords[2] + 1,
height: coords[2] + 1
};
break;
case 'poly':
target.dimensions = {
width: coords[0],
height: coords[1]
};
for(i = 0; i < coords.length; i++)
{
if(i % 2 == 0)
{
if(coords[i] > target.dimensions.width)
target.dimensions.width = coords[i];
if(coords[i] < coords[0])
target.position.left = Math.floor(imagePos.left + coords[i]);
}
else
{
if(coords[i] > target.dimensions.height)
target.dimensions.height = coords[i];
if(coords[i] < coords[1])
target.position.top = Math.floor(imagePos.top + coords[i]);
};
};
target.dimensions.width = target.dimensions.width - (target.position.left - imagePos.left);
target.dimensions.height = target.dimensions.height - (target.position.top - imagePos.top);
break;
default:
return $.fn.qtip.log.error.call(self, 4, $.fn.qtip.constants.INVALID_AREA_SHAPE, 'updatePosition');
break;
};
// Adjust position by 2 pixels (Positioning bug?)
target.dimensions.width -= 2; target.dimensions.height -= 2;
}
// Target is the document
else if(self.options.position.target.add(document.body).length === 1)
{
target.position = { left: $(document).scrollLeft(), top: $(document).scrollTop() };
target.dimensions = { height: $(window).height(), width: $(window).width() };
}
// Target is a regular HTML element, find position normally
else
{
// Check if the target is another tooltip. If its animated, retrieve position from newPosition data
if(typeof self.options.position.target.attr('qtip') !== 'undefined')
target.position = self.options.position.target.qtip('api').cache.position;
else
target.position = self.options.position.target.offset();
// Setup dimensions objects
target.dimensions = {
height: self.options.position.target.outerHeight(),
width: self.options.position.target.outerWidth()
};
};
// Calculate correct target corner position
newPosition = $.extend({}, target.position);
if(target.corner.search(/right/i) !== -1)
newPosition.left += target.dimensions.width;
if(target.corner.search(/bottom/i) !== -1)
newPosition.top += target.dimensions.height;
if(target.corner.search(/((top|bottom)Middle)|center/) !== -1)
newPosition.left += (target.dimensions.width / 2);
if(target.corner.search(/((left|right)Middle)|center/) !== -1)
newPosition.top += (target.dimensions.height / 2);
}
// Mouse is the target, set position to current mouse coordinates
else
{
// Setup target position and dimensions objects
target.position = newPosition = { left: self.cache.mouse.x, top: self.cache.mouse.y };
target.dimensions = { height: 1, width: 1 };
};
// Calculate correct target corner position
if(tooltip.corner.search(/right/i) !== -1)
newPosition.left -= tooltip.dimensions.width;
if(tooltip.corner.search(/bottom/i) !== -1)
newPosition.top -= tooltip.dimensions.height;
if(tooltip.corner.search(/((top|bottom)Middle)|center/) !== -1)
newPosition.left -= (tooltip.dimensions.width / 2);
if(tooltip.corner.search(/((left|right)Middle)|center/) !== -1)
newPosition.top -= (tooltip.dimensions.height / 2);
// Setup IE adjustment variables (Pixel gap bugs)
ieAdjust = ($.browser.msie) ? 1 : 0; // And this is why I hate IE...
ie6Adjust = ($.browser.msie && parseInt($.browser.version.charAt(0)) === 6) ? 1 : 0; // ...and even more so IE6!
// Adjust for border radius
if(self.options.style.border.radius > 0)
{
if(tooltip.corner.search(/Left/) !== -1)
newPosition.left -= self.options.style.border.radius;
else if(tooltip.corner.search(/Right/) !== -1)
newPosition.left += self.options.style.border.radius;
if(tooltip.corner.search(/Top/) !== -1)
newPosition.top -= self.options.style.border.radius;
else if(tooltip.corner.search(/Bottom/) !== -1)
newPosition.top += self.options.style.border.radius;
};
// IE only adjustments (Pixel perfect!)
if(ieAdjust)
{
if(tooltip.corner.search(/top/) !== -1)
newPosition.top -= ieAdjust
else if(tooltip.corner.search(/bottom/) !== -1)
newPosition.top += ieAdjust
if(tooltip.corner.search(/left/) !== -1)
newPosition.left -= ieAdjust
else if(tooltip.corner.search(/right/) !== -1)
newPosition.left += ieAdjust
if(tooltip.corner.search(/leftMiddle|rightMiddle/) !== -1)
newPosition.top -= 1
};
// If screen adjustment is enabled, apply adjustments
if(self.options.position.adjust.screen === true)
newPosition = screenAdjust.call(self, newPosition, target, tooltip);
// If mouse is the target, prevent tooltip appearing directly under the mouse
if(self.options.position.target === 'mouse' && self.options.position.adjust.mouse === true)
{
if(self.options.position.adjust.screen === true && self.elements.tip)
mouseAdjust = self.elements.tip.attr('rel');
else
mouseAdjust = self.options.position.corner.tooltip;
newPosition.left += (mouseAdjust.search(/right/i) !== -1) ? -6 : 6;
newPosition.top += (mouseAdjust.search(/bottom/i) !== -1) ? -6 : 6;
}
// Initiate bgiframe plugin in IE6 if tooltip overlaps a select box or object element
if(!self.elements.bgiframe && $.browser.msie && parseInt($.browser.version.charAt(0)) == 6)
{
$('select, object').each(function()
{
offset = $(this).offset();
offset.bottom = offset.top + $(this).height();
offset.right = offset.left + $(this).width();
if(newPosition.top + tooltip.dimensions.height >= offset.top
&& newPosition.left + tooltip.dimensions.width >= offset.left)
bgiframe.call(self);
});
};
// Add user xy adjustments
newPosition.left += self.options.position.adjust.x;
newPosition.top += self.options.position.adjust.y;
// Set new tooltip position if its moved, animate if enabled
curPosition = self.getPosition();
if(newPosition.left != curPosition.left || newPosition.top != curPosition.top)
{
// Call API method and if return value is false, halt
returned = self.beforePositionUpdate.call(self, event);
if(returned === false) return self;
// Cache new position
self.cache.position = newPosition;
// Check if animation is enabled
if(animate === true)
{
// Set animated status
self.status.animated = true;
// Animate and reset animated status on animation end
self.elements.tooltip.animate(newPosition, 200, 'swing', function(){ self.status.animated = false });
}
// Set new position via CSS
else self.elements.tooltip.css(newPosition);
// Call API method and log event if its not a mouse move
self.onPositionUpdate.call(self, event);
if(typeof event !== 'undefined' && event.type && event.type !== 'mousemove')
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_POSITION_UPDATED, 'updatePosition');
};
return self;
},
updateWidth: function(newWidth)
{
var hidden;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateWidth');
// Make sure supplied width is a number and if not, return
else if(newWidth && typeof newWidth !== 'number')
return $.fn.qtip.log.error.call(self, 2, 'newWidth must be of type number', 'updateWidth');
// Setup elements which must be hidden during width update
hidden = self.elements.contentWrapper.siblings().add(self.elements.tip).add(self.elements.button);
// Calculate the new width if one is not supplied
if(!newWidth)
{
// Explicit width is set
if(typeof self.options.style.width.value == 'number')
newWidth = self.options.style.width.value;
// No width is set, proceed with auto detection
else
{
// Set width to auto initally to determine new width and hide other elements
self.elements.tooltip.css({ width: 'auto' });
hidden.hide();
// Set position and zoom to defaults to prevent IE hasLayout bug
if($.browser.msie)
self.elements.wrapper.add(self.elements.contentWrapper.children()).css({ zoom: 'normal' });
// Set the new width
newWidth = self.getDimensions().width + 1;
// Make sure its within the maximum and minimum width boundries
if(!self.options.style.width.value)
{
if(newWidth > self.options.style.width.max) newWidth = self.options.style.width.max
if(newWidth < self.options.style.width.min) newWidth = self.options.style.width.min
};
};
};
// Adjust newWidth by 1px if width is odd (IE6 rounding bug fix)
if(newWidth % 2 !== 0) newWidth -= 1;
// Set the new calculated width and unhide other elements
self.elements.tooltip.width(newWidth);
hidden.show();
// Set the border width, if enabled
if(self.options.style.border.radius)
{
self.elements.tooltip.find('.qtip-betweenCorners').each(function(i)
{
$(this).width(newWidth - (self.options.style.border.radius * 2));
})
};
// IE only adjustments
if($.browser.msie)
{
// Reset position and zoom to give the wrapper layout (IE hasLayout bug)
self.elements.wrapper.add(self.elements.contentWrapper.children()).css({ zoom: '1' });
// Set the new width
self.elements.wrapper.width(newWidth);
// Adjust BGIframe height and width if enabled
if(self.elements.bgiframe) self.elements.bgiframe.width(newWidth).height(self.getDimensions.height);
};
// Log event and return
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_WIDTH_UPDATED, 'updateWidth');
},
updateStyle: function(name)
{
var tip, borders, context, corner, coordinates;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateStyle');
// Return if style is not defined or name is not a string
else if(typeof name !== 'string' || !$.fn.qtip.styles[name])
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.STYLE_NOT_DEFINED, 'updateStyle');
// Set the new style object
self.options.style = buildStyle.call(self, $.fn.qtip.styles[name], self.options.user.style);
// Update initial styles of content and title elements
self.elements.content.css( jQueryStyle(self.options.style) );
if(self.options.content.title.text !== false)
self.elements.title.css( jQueryStyle(self.options.style.title, true) );
// Update CSS border colour
self.elements.contentWrapper.css({ borderColor: self.options.style.border.color });
// Update tip color if enabled
if(self.options.style.tip.corner !== false)
{
if($('<canvas>').get(0).getContext)
{
// Retrieve canvas context and clear
tip = self.elements.tooltip.find('.qtip-tip canvas:first');
context = tip.get(0).getContext('2d');
context.clearRect(0,0,300,300);
// Draw new tip
corner = tip.parent('div[rel]:first').attr('rel');
coordinates = calculateTip(corner, self.options.style.tip.size.width, self.options.style.tip.size.height);
drawTip.call(self, tip, coordinates, self.options.style.tip.color || self.options.style.border.color);
}
else if($.browser.msie)
{
// Set new fillcolor attribute
tip = self.elements.tooltip.find('.qtip-tip [nodeName="shape"]');
tip.attr('fillcolor', self.options.style.tip.color || self.options.style.border.color);
};
};
// Update border colors if enabled
if(self.options.style.border.radius > 0)
{
self.elements.tooltip.find('.qtip-betweenCorners').css({ backgroundColor: self.options.style.border.color });
if($('<canvas>').get(0).getContext)
{
borders = calculateBorders(self.options.style.border.radius)
self.elements.tooltip.find('.qtip-wrapper canvas').each(function()
{
// Retrieve canvas context and clear
context = $(this).get(0).getContext('2d');
context.clearRect(0,0,300,300);
// Draw new border
corner = $(this).parent('div[rel]:first').attr('rel')
drawBorder.call(self, $(this), borders[corner],
self.options.style.border.radius, self.options.style.border.color);
});
}
else if($.browser.msie)
{
// Set new fillcolor attribute on each border corner
self.elements.tooltip.find('.qtip-wrapper [nodeName="arc"]').each(function()
{
$(this).attr('fillcolor', self.options.style.border.color)
});
};
};
// Log event and return
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_STYLE_UPDATED, 'updateStyle');
},
updateContent: function(content, reposition)
{
var parsedContent, images, loadedImages;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateContent');
// Make sure content is defined before update
else if(!content)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.NO_CONTENT_PROVIDED, 'updateContent');
// Call API method and set new content if a string is returned
parsedContent = self.beforeContentUpdate.call(self, content);
if(typeof parsedContent == 'string') content = parsedContent;
else if(parsedContent === false) return;
// Set position and zoom to defaults to prevent IE hasLayout bug
if($.browser.msie) self.elements.contentWrapper.children().css({ zoom: 'normal' });
// Append new content if its a DOM array and show it if hidden
if(content.jquery && content.length > 0)
content.clone(true).appendTo(self.elements.content).show();
// Content is a regular string, insert the new content
else self.elements.content.html(content);
// Check if images need to be loaded before position is updated to prevent mis-positioning
images = self.elements.content.find('img[complete=false]');
if(images.length > 0)
{
loadedImages = 0;
images.each(function(i)
{
$('<img src="'+ $(this).attr('src') +'" />')
.load(function(){ if(++loadedImages == images.length) afterLoad(); });
});
}
else afterLoad();
function afterLoad()
{
// Update the tooltip width
self.updateWidth();
// If repositioning is enabled, update positions
if(reposition !== false)
{
// Update position if tooltip isn't static
if(self.options.position.type !== 'static')
self.updatePosition(self.elements.tooltip.is(':visible'), true);
// Reposition the tip if enabled
if(self.options.style.tip.corner !== false)
positionTip.call(self);
};
};
// Call API method and log event
self.onContentUpdate.call(self);
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_CONTENT_UPDATED, 'loadContent');
},
loadContent: function(url, data, method)
{
var returned;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'loadContent');
// Call API method and if return value is false, halt
returned = self.beforeContentLoad.call(self);
if(returned === false) return self;
// Load content using specified request type
if(method == 'post')
$.post(url, data, setupContent);
else
$.get(url, data, setupContent);
function setupContent(content)
{
// Call API method and log event
self.onContentLoad.call(self);
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_CONTENT_LOADED, 'loadContent');
// Update the content
self.updateContent(content);
};
return self;
},
updateTitle: function(content)
{
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'updateTitle');
// Make sure content is defined before update
else if(!content)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.NO_CONTENT_PROVIDED, 'updateTitle');
// Call API method and if return value is false, halt
returned = self.beforeTitleUpdate.call(self);
if(returned === false) return self;
// Set the new content and reappend the button if enabled
if(self.elements.button) self.elements.button = self.elements.button.clone(true);
self.elements.title.html(content)
if(self.elements.button) self.elements.title.prepend(self.elements.button);
// Call API method and log event
self.onTitleUpdate.call(self);
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_TITLE_UPDATED, 'updateTitle');
},
focus: function(event)
{
var curIndex, newIndex, elemIndex, returned;
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'focus');
else if(self.options.position.type == 'static')
return $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.CANNOT_FOCUS_STATIC, 'focus');
// Set z-index variables
curIndex = parseInt( self.elements.tooltip.css('z-index') );
newIndex = 6000 + $('div.qtip[qtip]').length - 1;
// Only update the z-index if it has changed and tooltip is not already focused
if(!self.status.focused && curIndex !== newIndex)
{
// Call API method and if return value is false, halt
returned = self.beforeFocus.call(self, event);
if(returned === false) return self;
// Loop through all other tooltips
$('div.qtip[qtip]').not(self.elements.tooltip).each(function()
{
if($(this).qtip('api').status.rendered === true)
{
elemIndex = parseInt($(this).css('z-index'));
// Reduce all other tooltip z-index by 1
if(typeof elemIndex == 'number' && elemIndex > -1)
$(this).css({ zIndex: parseInt( $(this).css('z-index') ) - 1 });
// Set focused status to false
$(this).qtip('api').status.focused = false;
}
})
// Set the new z-index and set focus status to true
self.elements.tooltip.css({ zIndex: newIndex });
self.status.focused = true;
// Call API method and log event
self.onFocus.call(self, event);
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_FOCUSED, 'focus');
};
return self;
},
disable: function(state)
{
// Make sure tooltip is rendered and if not, return
if(!self.status.rendered)
return $.fn.qtip.log.error.call(self, 2, $.fn.qtip.constants.TOOLTIP_NOT_RENDERED, 'disable');
if(state)
{
// Tooltip is not already disabled, proceed
if(!self.status.disabled)
{
// Set the disabled flag and log event
self.status.disabled = true;
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_DISABLED, 'disable');
}
// Tooltip is already disabled, inform user via log
else $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.TOOLTIP_ALREADY_DISABLED, 'disable');
}
else
{
// Tooltip is not already enabled, proceed
if(self.status.disabled)
{
// Reassign events, set disable status and log
self.status.disabled = false;
$.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.EVENT_ENABLED, 'disable');
}
// Tooltip is already enabled, inform the user via log
else $.fn.qtip.log.error.call(self, 1, $.fn.qtip.constants.TOOLTIP_ALREADY_ENABLED, 'disable');
};
return self;
},
destroy: function()
{
var i, returned, interfaces;
// Call API method and if return value is false, halt
returned = self.beforeDestroy.call(self);
if(returned === false) return self;
// Check if tooltip is rendered
if(self.status.rendered)
{
// Remove event handlers and remove element
self.options.show.when.target.unbind('mousemove.qtip', self.updatePosition);
self.options.show.when.target.unbind('mouseout.qtip', self.hide);
self.options.show.when.target.unbind(self.options.show.when.event + '.qtip');
self.options.hide.when.target.unbind(self.options.hide.when.event + '.qtip');
self.elements.tooltip.unbind(self.options.hide.when.event + '.qtip');
self.elements.tooltip.unbind('mouseover.qtip', self.focus);
self.elements.tooltip.remove();
}
// Tooltip isn't yet rendered, remove render event
else self.options.show.when.target.unbind(self.options.show.when.event+'.qtip-create');
// Check to make sure qTip data is present on target element
if(typeof self.elements.target.data('qtip') == 'object')
{
// Remove API references from interfaces object
interfaces = self.elements.target.data('qtip').interfaces;
if(typeof interfaces == 'object' && interfaces.length > 0)