r2914 - first tooltip prototype with visual test

r2914 - first tooltip prototype with visual test


Author: joern.zaefferer
Date: Sat Jul 11 04:23:16 2009
New Revision: 2914
Added:
branches/dev/tooltip/tests/visual/tooltip/
branches/dev/tooltip/tests/visual/tooltip/default.html (contents,
props changed)
branches/dev/tooltip/ui/ui.positionTo.js
branches/dev/tooltip/ui/ui.tooltip.js
Modified:
branches/dev/tooltip/themes/base/ui.base.css
Log:
first tooltip prototype with visual test
Added: branches/dev/tooltip/tests/visual/tooltip/default.html
==============================================================================
--- (empty file)
+++ branches/dev/tooltip/tests/visual/tooltip/default.html    Sat Jul 11
04:23:16 2009
@@ -0,0 +1,29 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <title>Tabs Visual Test : Default</title>
+    <link rel="stylesheet" href="../visual.css" type="text/css" />
+    <link rel="stylesheet" href="../../../themes/base/ui.all.css"
type="text/css">
+    <script type="text/javascript" src="../../../jquery-1.3.2.js"></script>
+    <script type="text/javascript" src="../../../ui/ui.core.js"></script>
+    <script type="text/javascript" src="../../../ui/ui.tooltip.js"></script>
+    <script type="text/javascript"
src="../../../ui/ui.positionTo.js"></script>
+    <script type="text/javascript">
+    $(function() {
+        $("a").tooltip();
+    });
+    </script>
+</head>
+<body>
+
+<ul>
+    <li><a href="#" title="Tooltip text 1">Anchor 1</a></li>
+    <li><a href="#" title="Tooltip text 2">Anchor 2</a></li>
+    <li><a href="#" title="Tooltip text 3">Anchor 3</a></li>
+    <li><a href="#" title="Tooltip text 4 more Tooltip text Tooltip
text ">Anchor 4</a></li>
+    <li><a href="#" title="Tooltip text 5 more Tooltip text Tooltip
text ">Anchor 5</a></li>
+    <li><a href="#" title="Tooltip text 6 more Tooltip text Tooltip
text ">Anchor 6</a></li>
+</ul>
+
+</body>
+</html>
Modified: branches/dev/tooltip/themes/base/ui.base.css
==============================================================================
--- branches/dev/tooltip/themes/base/ui.base.css    (original)
+++ branches/dev/tooltip/themes/base/ui.base.css    Sat Jul 11 04:23:16 2009
@@ -7,3 +7,4 @@
@import url("ui.resizable.css");
@import url("ui.slider.css");
@import url("ui.tabs.css");
+@import url("ui.tooltip.css");
Added: branches/dev/tooltip/ui/ui.positionTo.js
==============================================================================
--- (empty file)
+++ branches/dev/tooltip/ui/ui.positionTo.js    Sat Jul 11 04:23:16 2009
@@ -0,0 +1,232 @@
+/*
+ * jQuery UI positionTo @VERSION
+ *
+ * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * TODO: create document page for positionTo and add link here
+ */
+(function($) {
+
+var horizontalPositions = /left|center|right/,
+    horizontalDefault = 'center',
+    verticalPositions = /top|middle|bottom/,
+    verticalDefault = 'middle';
+
+$.fn.positionTo = function(options) {
+    options = $.extend({
+        stackFix: true
+    }, options);
+
+    var target = $(options.of),
+        collision = (options.collision || 'flip').split(' '),
+        offset = options.offset ? options.offset.split(' ') : [0, 0],
+        targetWidth,
+        targetHeight,
+        basePosition;
+
+    if (options.of == document) {
+        targetWidth = target.width();
+        targetHeight = target.height();
+        basePosition = { top: 0, left: 0 };
+    } else if (options.of == window) {
+        targetWidth = target.width();
+        targetHeight = target.height();
+        basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
+    } else if (options.of.preventDefault) {
+        targetWidth = targetHeight = 0;
+        basePosition = { top: options.of.pageY, left: options.of.pageX };
+    } else {
+        targetWidth = target.outerWidth();
+        targetHeight = target.outerHeight();
+        basePosition = target.offset();
+    }
+
+    // force my and at to have valid horizontal and veritcal positions
+    // if a value is missing or invalid, it will be converted to center or
middle
+    $.each(['my', 'at'], function() {
+        var pos = options[this].split(' ');
+        pos = pos.length == 1
+            ? horizontalPositions.test(pos[0])
+                ? pos.concat([verticalDefault])
+                : verticalPositions.test(pos[0])
+                    ? [horizontalDefault].concat(pos)
+                    : [horizontalDefault, verticalDefault]
+            : pos;
+        pos[0] = horizontalPositions.test(pos[0]) ? pos[0] : horizontalDefault;
+        pos[1] = verticalPositions.test(pos[1]) ? pos[1] : verticalDefault;
+        options[this] = pos;
+    });
+
+    // normalize collision option
+    if (collision.length == 1) {
+        collision[1] = collision[0];
+    }
+
+    // normalize offset option
+    offset[0] = parseInt(offset[0], 10) || 0;
+    if (offset.length == 1) {
+        offset[1] = offset[0];
+    }
+    offset[1] = parseInt(offset[1], 10) || 0;
+
+    switch (options.at[0]) {
+        case 'right':
+            basePosition.left += targetWidth;
+            break;
+        case horizontalDefault:
+            basePosition.left += targetWidth / 2;
+            break;
+    }
+
+    switch (options.at[1]) {
+        case 'bottom':
+            basePosition.top += targetHeight;
+            break;
+        case verticalDefault:
+            basePosition.top += targetHeight / 2;
+            break;
+    }
+
+    basePosition.left += offset[0];
+    basePosition.top += offset[1];
+
+    return this.each(function() {
+        var elem = $(this),
+            elemWidth = elem.outerWidth(),
+            elemHeight = elem.outerHeight(),
+            position = $.extend({}, basePosition),
+            over,
+            myOffset,
+            atOffset;
+
+        switch (options.my[0]) {
+            case 'right':
+                position.left -= elemWidth;
+                break;
+            case horizontalDefault:
+                position.left -= elemWidth / 2;
+                break;
+        }
+
+        switch (options.my[1]) {
+            case 'bottom':
+                position.top -= elemHeight;
+                break;
+            case verticalDefault:
+                position.top -= elemHeight / 2;
+                break;
+        }
+
+        $.each(['left', 'top'], function(i, dir) {
+            ($.ui.position[collision[i]] &&
+                $.ui.position[collision[i]][dir](position, {
+                    targetWidth: targetWidth,
+                    targetHeight: targetHeight,
+                    elemWidth: elemWidth,
+                    elemHeight: elemHeight,
+                    offset: offset,
+                    my: options.my,
+                    at: options.at
+                }));
+        });
+
+        (options.stackfix && $.fn.stackfix && elem.stackfix());
+        // the by function is passed the offset values, not the position values
+        // we'll need the logic from the .offset() setter to be accessible for
+        // us to calculate the position values to make the by option more useful
+        ($.isFunction(options.by) ? options.by.call(this, position) :
elem.offset(position));
+    });
+};
+
+$.ui.position = {
+    fit: {
+        left: function(position, data) {
+            var over = position.left + data.elemWidth - $(window).width() -
$(window).scrollLeft();
+            position.left = over > 0 ? position.left - over : Math.max(0,
position.left);
+        },
+        top: function(position, data) {
+            var over = position.top + data.elemHeight - $(window).height() -
$(window).scrollTop();
+            position.top = over > 0 ? position.top - over : Math.max(0,
position.top);
+        }
+    },
+
+    flip: {
+        left: function(position, data) {
+            var over = position.left + data.elemWidth - $(window).width() -
$(window).scrollLeft(),
+                myOffset = data.my[0] == 'left' ? -data.elemWidth : data.elemWidth,
+                offset = -2 * data.offset[0];
+            position.left += position.left < 0 ? myOffset + data.targetWidth +
offset : over > 0 ? myOffset - data.targetWidth + offset : 0;
+        },
+        top: function(position, data) {
+            var over = position.top + data.elemHeight - $(window).height() -
$(window).scrollTop(),
+                myOffset = data.my[1] == 'top' ? -data.elemHeight : data.elemHeight,
+                atOffset = data.at[1] == 'top' ? data.targetHeight :
-data.targetHeight,
+                offset = -2 * data.offset[1];
+            position.top += position.top < 0 ? myOffset + data.targetHeight +
offset : over > 0 ? myOffset + atOffset + offset : 0;
+        }
+    }
+};
+
+
+// the following functionality is planned for jQuery 1.4
+// copied from http://plugins.jquery.com/files/offset.js.txt
+$.fn.extend({
+
+    /**
+     * Stores the original version of offset(), so that we don't lose it
+     */
+    _offset : $.fn.offset,
+    
+    /**
+     * Set or get the specific left and top position of the matched
+     * elements, relative the the browser window by calling setXY
+     * @param {Object} newOffset
+     */
+    offset : function(newOffset){
+     return !newOffset ? this._offset() : this.each(function(){
+            var el = this;
+            
+            var hide = false;
+            
+            if($(el).css('display')=='none'){
+                hide = true;
+                $(el).show();
+            };
+            
+            var style_pos = $(el).css('position');
+            
+            // default to relative
+            if (style_pos == 'static') {
+                $(el).css('position','relative');
+                style_pos = 'relative';
+            };
+            
+            var offset = $(el).offset();
+            
+            if (offset){
+                var delta = {
+                    left : parseInt($(el).css('left'), 10),
+                    top: parseInt($(el).css('top'), 10)
+                };
+                
+                // in case of 'auto'
+                if (isNaN(delta.left))
+                    delta.left = (style_pos == 'relative') ? 0 : el.offsetLeft;
+                if (isNaN(delta.top))
+                    delta.top = (style_pos == 'relative') ? 0 : el.offsetTop;
+                
+                if (newOffset.left || newOffset.left===0)
+                    $(el).css('left',newOffset.left - offset.left + delta.left + 'px');
+            
+                if (newOffset.top || newOffset.top===0)
+                    $(el).css('top',newOffset.top - offset.top + delta.top + 'px');
+            };
+            if(hide) $(el).hide();
+        });
+    }
+
+});
+
+})(jQuery);
Added: branches/dev/tooltip/ui/ui.tooltip.js
==============================================================================
--- (empty file)
+++ branches/dev/tooltip/ui/ui.tooltip.js    Sat Jul 11 04:23:16 2009
@@ -0,0 +1,69 @@
+/*
+ * jQuery UI Tooltip @VERSION
+ *
+ * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * http://docs.jquery.com/UI/Tooltip
+ *
+ * Depends:
+ *    ui.core.js
+ */
+(function($) {
+
+$.widget("ui.tooltip", {
+
+    _init: function() {
+        var self = this;
+        this.tooltip =
$("<div/>").addClass(this._classes()).text(this._content()).appendTo(document.body).hide();
+        this.element
+            .bind("mouseenter", function() { self._show(); })
+            .bind("mouseleave", function() { self._hide(); });
+            
+        this._arrow();
+    },
+    
+    _arrow: function() {
+        var arrow = $("<div/>").addClass("ui-tooltip-pointer
ui-widget-content").appendTo(this.tooltip);
+        
$("<div/>").addClass("ui-tooltip-pointer-inner").css("border-right-color", "rgb(255,
255, 255)").appendTo(arrow);
+    },
+    
+    _classes: function() {
+        return "ui-tooltip ui-widget ui-widget-content ui-corner-all
ui-tooltip-arrow-" + this._direction();
+    },
+    
+    _content: function() {
+        return this.options.content.call(this.element[0]);
+    },
+    
+    _direction: function() {
+        return "lt";
+    },
+    
+    _show: function() {
+        this.tooltip.positionTo({
+            my: "left top",
+            at: "right center",
+            of: this.element,
+            offset: "15 -12"
+        }).fadeIn();
+    },
+    
+    _hide: function() {
+        this.tooltip.fadeOut();
+    }
+
+});
+
+
+$.extend($.ui.tooltip, {
+    version: "@VERSION",
+    defaults: {
+        content: function() {
+            return this.title;
+        }
+    }
+});
+
+})(jQuery);