Skip to content

Commit 925a128

Browse files
committed
jQuery: JSDoc'd coordinates and added test suite
Added interface to cover coordinates definition.
1 parent 6baada5 commit 925a128

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

jquery/jquery-tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,21 @@ function test_width() {
16691669
});
16701670
}
16711671

1672+
function test_coordinates() {
1673+
var p = $("p:last");
1674+
var offset = p.offset();
1675+
p.html("left: " + offset.left + ", top: " + offset.top);
1676+
1677+
$("*", document.body).click(function (event) {
1678+
var offset = $(this).offset();
1679+
event.stopPropagation();
1680+
$("#result").text(this.tagName +
1681+
" coords ( " + offset.left + ", " + offset.top + " )");
1682+
});
1683+
1684+
$("p:last").offset({ top: 10, left: 30 });
1685+
}
1686+
16721687
function test_hide() {
16731688
$('.target').hide();
16741689
$('#clickme').click(function () {

jquery/jquery.d.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ interface JQueryEventConstructor {
348348
new (name: string, eventProperties?: any): JQueryEventObject;
349349
}
350350

351+
/**
352+
* The interface used to specify coordinates.
353+
*/
354+
interface JQueryCoordinates {
355+
left: number;
356+
top: number;
357+
}
358+
351359
/**
352360
* The interface used to specify easing functions.
353361
*/
@@ -1023,9 +1031,22 @@ interface JQuery {
10231031
*/
10241032
innerWidth(): number;
10251033

1026-
offset(): { left: number; top: number; };
1027-
offset(coordinates: any): JQuery;
1028-
offset(func: (index: any, coords: any) => any): JQuery;
1034+
/**
1035+
* Get the current coordinates of the first element in the set of matched elements, relative to the document.
1036+
*/
1037+
offset(): JQueryCoordinates;
1038+
/**
1039+
* An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1040+
*
1041+
* @param coordinates An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1042+
*/
1043+
offset(coordinates: JQueryCoordinates): JQuery;
1044+
/**
1045+
* An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1046+
*
1047+
* @param func A function to return the coordinates to set. Receives the index of the element in the collection as the first argument and the current coordinates as the second argument. The function should return an object with the new top and left properties.
1048+
*/
1049+
offset(func: (index: number, coords: JQueryCoordinates) => JQueryCoordinates): JQuery;
10291050

10301051
outerHeight(includeMargin?: boolean): number;
10311052
outerHeight(value: number, includeMargin?: boolean): JQuery;

0 commit comments

Comments
 (0)