PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/lib/js/modules/z.tooltip.js

https://code.google.com/p/zotonic/
JavaScript | 100 lines | 65 code | 14 blank | 21 comment | 3 complexity | 15d8b4581d8007589aed65c40b7851e0 MD5 | raw file
  1/* tooltip js
  2----------------------------------------------------------
  3
  4@package:	Zotonic 2009	
  5@Author: 	Tim Benniks <tim@timbenniks.nl>
  6
  7Copyright 2009 Tim Benniks
  8
  9Licensed under the Apache License, Version 2.0 (the "License");
 10you may not use this file except in compliance with the License.
 11You may obtain a copy of the License at
 12 
 13http://www.apache.org/licenses/LICENSE-2.0
 14 
 15Unless required by applicable law or agreed to in writing, software
 16distributed under the License is distributed on an "AS IS" BASIS,
 17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18See the License for the specific language governing permissions and
 19limitations under the License.
 20
 21---------------------------------------------------------- */
 22
 23$.widget("ui.tooltip", 
 24{
 25	_init: function() 
 26	{
 27		var self = this;
 28		var obj  = this.element;
 29		var tip  = undefined;
 30		
 31		if(this.title == '')
 32		{
 33			obj.unbind(self.options.inevent, self.options.outevent);
 34			return false;
 35		}
 36
 37		obj.bind(self.options.inevent, function(e) 
 38		{
 39			this.tip 		= this.title;
 40			var tip_content = this.tip;
 41			this.title 		= "";
 42			
 43			tip = $('<div></div>')
 44					.addClass('tooltip')
 45					.html(tip_content)
 46					.css({top: e.pageY + self.options.offsetY, left: e.pageX + self.options.offsetX, width: self.options.width, maxWidth: self.options.maxwidth });
 47			
 48			$(document.body).append(tip);
 49
 50			var left = $(this).position().left;
 51			var top  = $(this).position().top;
 52							
 53			tip.css({top: top - 30});
 54			
 55			if(left + tip.width() > $(window).width())
 56			{
 57				tip.css({left: Math.ceil(left) - Math.ceil((obj.width() / 2)) - Math.ceil((tip.width() / 2 ))});
 58			}
 59			else
 60			{
 61				tip.css({left: Math.ceil(left) + Math.ceil((obj.width() / 2)) - Math.ceil((tip.width() / 2 ))});
 62			}
 63			
 64			tip.stop().animate({opacity: 'show'}, 200);
 65		});
 66		
 67		obj.bind(self.options.outevent, function(e) 
 68		{
 69			tip.stop().animate({opacity: 'show'}, 200, function()
 70			{
 71				$(this).remove();
 72			});
 73			
 74			self.destroy();
 75			this.title = this.tip;
 76		});
 77	},
 78	destroy: function()
 79	{
 80	    var self = this;
 81		var obj  = this.element;
 82
 83		obj.unbind(self.options.inevent, self.options.outevent);
 84		$(document).unbind('mousemove');
 85	}
 86});
 87
 88$.fn.tooltip.destroy = function()
 89{
 90	$('.tooltip').tooltip('destroy');
 91}
 92
 93$.ui.tooltip.defaults = {
 94	offsetY: 	0,
 95	offsetX: 	0,
 96	inevent: 	'mouseover',
 97	outevent: 	'mouseout',
 98	width: 		'auto',
 99	maxwidth: 	'330px'
100}