r3571 committed - Position: Pulled in offset setter from jQuery 1.4....
Revision: 3571
Author: scott.gonzalez
Date: Wed Dec 30 20:44:55 2009
Log: Position: Pulled in offset setter from jQuery 1.4.
Fixes #5030 - Position: change by option to using to match jQuery's offset
setter.
http://code.google.com/p/jquery-ui/source/detail?r=3571
Modified:
/branches/dev/demos/position/cycler.html
/branches/dev/demos/position/default.html
/branches/dev/tests/unit/position/position_core.js
/branches/dev/ui/jquery.ui.position.js
=======================================
--- /branches/dev/demos/position/cycler.html Wed Dec 16 17:36:20 2009
+++ /branches/dev/demos/position/cycler.html Wed Dec 30 20:44:55 2009
@@ -6,7 +6,6 @@
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
<script type="text/javascript"
src="../../ui/jquery.ui.widget.js"></script>
- <script type="text/javascript"
src="../../ui/jquery.ui.draggable.js"></script>
<script type="text/javascript"
src="../../ui/jquery.ui.position.js"></script>
<link type="text/css" href="../demos.css" rel="stylesheet" />
@@ -23,7 +22,7 @@
$.fn.position2 = function(options) {
return this.position($.extend({
of: window,
- by: function(to) {
+ using: function(to) {
$(this).css({
top: to.top,
left: to.left
@@ -33,27 +32,27 @@
}, options));
}
- $.fn.left = function(by) {
+ $.fn.left = function(using) {
return this.position2({
my: "right middle",
at: "left middle",
offset: "25 0",
- by: by
+ using: using
});
}
- $.fn.right = function(by) {
+ $.fn.right = function(using) {
return this.position2({
my: "left middle",
at: "right middle",
offset: "-25 0",
- by: by
+ using: using
});
}
- $.fn.center = function(by) {
+ $.fn.center = function(using) {
return this.position2({
my: "center middle",
at: "center middle",
- by: by
+ using: using
});
};
=======================================
--- /branches/dev/demos/position/default.html Wed Dec 16 17:36:20 2009
+++ /branches/dev/demos/position/default.html Wed Dec 30 20:44:55 2009
@@ -6,6 +6,7 @@
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
<script type="text/javascript"
src="../../ui/jquery.ui.widget.js"></script>
+ <script type="text/javascript" src="../../ui/jquery.ui.mouse.js"></script>
<script type="text/javascript"
src="../../ui/jquery.ui.draggable.js"></script>
<script type="text/javascript"
src="../../ui/jquery.ui.position.js"></script>
<link type="text/css" href="../demos.css" rel="stylesheet" />
@@ -44,23 +45,23 @@
<script type="text/javascript">
$(function() {
- function position(by) {
+ function position(using) {
$('.positionable').position({
of: $('#parent'),
my: $('#my_horizontal').val() + ' ' +
$('#my_vertical').val(),
at: $('#at_horizontal').val() + ' '+
$('#at_vertical').val(),
offset: $('#offset').val(),
- by: by,
+ using: using,
collision: $("#collision_horizontal").val() + ' ' +
$("#collision_vertical").val()
});
}
$('.positionable').css("opacity", 0.5);
- $(':input').bind('click keyup change', position);
+ $(':input').bind('click keyup change', function() { position(); });
$("#parent").draggable({
- drag: position
+ drag: function() { position(); }
});
$('.positionable').draggable({
=======================================
--- /branches/dev/tests/unit/position/position_core.js Sat Sep 26 04:12:14
2009
+++ /branches/dev/tests/unit/position/position_core.js Wed Dec 30 20:44:55
2009
@@ -204,7 +204,7 @@
same($('#elx').offset(), { top: 57, left: 45 }, 'with units');
});
-test('by', function() {
+test('using', function() {
expect(6);
var count = 0,
@@ -221,7 +221,7 @@
my: 'left top',
at: 'left top',
of: '#parentx',
- by: function(position) {
+ using: function(position) {
same(this, elems[count], 'correct context for call #' + count);
same(position, expectedPosition, 'correct position for call #' + count);
count++;
=======================================
--- /branches/dev/ui/jquery.ui.position.js Mon Dec 28 19:02:57 2009
+++ /branches/dev/ui/jquery.ui.position.js Wed Dec 30 20:44:55 2009
@@ -141,10 +141,7 @@
});
(options.stackfix !== false && $.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));
+ elem.offset($.extend(position, { using: options.using }));
});
};
@@ -181,50 +178,40 @@
}
};
-
-// the following functionality is planned for jQuery 1.4
-// based on http://plugins.jquery.com/files/offset.js.txt
-$.fn.extend({
- _offset: $.fn.offset,
- offset: function(newOffset) {
- return !newOffset ? this._offset() : this.each(function() {
- var elem = $(this),
- // we need to convert static positioning to relative positioning
- isRelative = /relative|static/.test(elem.css('position')),
- hide = elem.css('display') == 'none';
-
- (isRelative && elem.css('position', 'relative'));
- (hide && elem.show());
-
- var offset = elem.offset(),
- delta = {
- left : parseInt(elem.css('left'), 10),
- top: parseInt(elem.css('top'), 10)
- };
-
- // in case of 'auto'
- delta.left = !isNaN(delta.left)
- ? delta.left
- : isRelative
- ? 0
- : this.offsetLeft;
- delta.top = !isNaN(delta.top)
- ? delta.top
- : isRelative
- ? 0
- : this.offsetTop;
-
- // allow setting only left or only top
- if (newOffset.left || newOffset.left === 0) {
- elem.css('left', newOffset.left - offset.left + delta.left);
- }
- if (newOffset.top || newOffset.top === 0) {
- elem.css('top', newOffset.top - offset.top + delta.top);
- }
-
- (hide && elem.hide());
- });
- }
-});
+// offset setter from jQuery 1.4
+if (!$.offset.setOffset) {
+ $.offset.setOffset = function( elem, options ) {
+ // set position first, in-case top/left are set even on static elem
+ if ( /static/.test( jQuery.curCSS( elem, 'position' ) ) ) {
+ elem.style.position = 'relative';
+ }
+ var curElem = jQuery( elem ),
+ curOffset = curElem.offset(),
+ curTop = parseInt( jQuery.curCSS( elem, 'top', true ), 10 ) || 0,
+ curLeft = parseInt( jQuery.curCSS( elem, 'left', true ), 10) || 0,
+ props = {
+ top: (options.top - curOffset.top) + curTop,
+ left: (options.left - curOffset.left) + curLeft
+ };
+
+ if ( 'using' in options ) {
+ options.using.call( elem, props );
+ } else {
+ curElem.css( props );
+ }
+ };
+
+ var _offset = $.fn.offset;
+ $.fn.offset = function( options ) {
+ var elem = this[0];
+ if ( !elem || !elem.ownerDocument ) { return null; }
+ if ( options ) {
+ return this.each(function() {
+ $.offset.setOffset( this, options );
+ });
+ }
+ return _offset.call(this);
+ }
+}
})(jQuery);
--