From 3f692cc3b6bfb62468d169e7810e77be8847e946 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 16 Jan 2014 13:23:16 +0000 Subject: [PATCH 1/3] jQuery: added param JSDoc and test suite --- jquery/jquery-tests.ts | 52 ++++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 15 ++++++++++++ 2 files changed, 67 insertions(+) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 518e8a45d..0ad13a4bf 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -1876,6 +1876,58 @@ function test_scrollTop() { $("div.demo").scrollTop(300); } +function test_param() { + + function test1() { + var myObject = { + a: { + one: 1, + two: 2, + three: 3 + }, + b: [1, 2, 3] + }; + var recursiveEncoded = $.param(myObject); + var recursiveDecoded = decodeURIComponent($.param(myObject)); + + alert(recursiveEncoded); + alert(recursiveDecoded); + } + + function test2() { + var myObject = { + a: { + one: 1, + two: 2, + three: 3 + }, + b: [1, 2, 3] + }; + var shallowEncoded = $.param(myObject, true); + var shallowDecoded = decodeURIComponent(shallowEncoded); + + alert(shallowEncoded); + alert(shallowDecoded); + } + + var params = { width: 1680, height: 1050 }; + var str = jQuery.param(params); + $("#results").text(str); + + // <=1.3.2: + $.param({ a: [2, 3, 4] }); // "a=2&a=3&a=4" + // >=1.4: + $.param({ a: [2, 3, 4] }); // "a[]=2&a[]=3&a[]=4" + + // <=1.3.2: + $.param({ a: { b: 1, c: 2 }, d: [3, 4, { e: 5 }] }); + // "a=[object+Object]&d=3&d=4&d=[object+Object]" + + // >=1.4: + $.param({ a: { b: 1, c: 2 }, d: [3, 4, { e: 5 }] }); + // "a[b]=1&a[c]=2&d[]=3&d[]=4&d[2][e]=5" +} + function test_position() { var p = $("p:first"); var position = p.position(); diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 1dab9e790..d690a649e 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -333,7 +333,19 @@ interface JQuerySupport { } interface JQueryParam { + /** + * Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request. + * + * @param obj An array or object to serialize. + */ (obj: any): string; + + /** + * Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request. + * + * @param obj An array or object to serialize. + * @param traditional A Boolean indicating whether to perform a traditional "shallow" serialization. + */ (obj: any, traditional: boolean): string; } @@ -510,6 +522,9 @@ interface JQueryStatic { */ getScript(url: string, success?: (script: string, textStatus: string, jqXHR: JQueryXHR) => any): JQueryXHR; + /** + * Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request. + */ param: JQueryParam; /** From 07041a3088e7284058df81a35961b61d5faff106 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 16 Jan 2014 13:38:16 +0000 Subject: [PATCH 2/3] jQuery: queue, dequeue, hasdata added queue test suite --- jquery/jquery-tests.ts | 62 ++++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 45 ++++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 0ad13a4bf..e024f1dc6 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -1095,6 +1095,68 @@ function test_dequeue() { }); } +function test_queue() { + + $("#show").click(function () { + var n = jQuery.queue($("div")[0], "fx"); + $("span").text("Queue length is: " + n.length); + }); + + function runIt() { + $("div") + .show("slow") + .animate({ + left: "+=200" + }, 2000) + .slideToggle(1000) + .slideToggle("fast") + .animate({ + left: "-=200" + }, 1500) + .hide("slow") + .show(1200) + .slideUp("normal", runIt); + } + + runIt(); + + $(document.body).click(function () { + var divs = $("div") + .show("slow") + .animate({ left: "+=200" }, 2000); + jQuery.queue(divs[0], "fx", function () { + $(this).addClass("newcolor"); + jQuery.dequeue(this); + }); + divs.animate({ left: "-=200" }, 500); + jQuery.queue(divs[0], "fx", function () { + $(this).removeClass("newcolor"); + jQuery.dequeue(this); + }); + divs.slideUp(); + }); + + $("#start").click(function () { + var divs = $("div") + .show("slow") + .animate({ left: "+=200" }, 5000); + jQuery.queue(divs[0], "fx", function () { + $(this).addClass("newcolor"); + jQuery.dequeue(this); + }); + divs.animate({ left: "-=200" }, 1500); + jQuery.queue(divs[0], "fx", function () { + $(this).removeClass("newcolor"); + jQuery.dequeue(this); + }); + divs.slideUp(); + }); + $("#stop").click(function () { + jQuery.queue($("div")[0], "fx", []); + $("div").stop(); + }); +} + function test_detach() { $("p").click(function () { $(this).toggleClass("off"); diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index d690a649e..df6256293 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -423,9 +423,9 @@ interface JQueryEasing { swing(p: number): number; } -/* - Static members of jQuery (those on $ and jQuery themselves) -*/ +/** + * Static members of jQuery (those on $ and jQuery themselves) + */ interface JQueryStatic { /** @@ -659,6 +659,9 @@ interface JQueryStatic { */ when(...deferreds: any[]): JQueryPromise; + /** + * Hook directly into jQuery to override how particular CSS properties are retrieved or set, normalize CSS property naming, or create custom properties. + */ cssHooks: { [key: string]: any; }; cssNumber: any; @@ -684,12 +687,44 @@ interface JQueryStatic { */ data(element: Element): any; - dequeue(element: Element, queueName?: string): any; + /** + * Execute the next function on the queue for the matched element. + * + * @param element A DOM element from which to remove and execute a queued function. + * @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue. + */ + dequeue(element: Element, queueName?: string): void; + /** + * Determine whether an element has any jQuery data associated with it. + * + * @param element A DOM element to be checked for data. + */ hasData(element: Element): boolean; + /** + * Show the queue of functions to be executed on the matched element. + * + * @param element A DOM element to inspect for an attached queue. + * @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue. + */ queue(element: Element, queueName?: string): any[]; - queue(element: Element, queueName: string, newQueueOrCallback: any): JQuery; + /** + * Manipulate the queue of functions to be executed on the matched element. + * + * @param element A DOM element where the array of queued functions is attached. + * @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue. + * @param newQueue An array of functions to replace the current queue contents. + */ + queue(element: Element, queueName: string, newQueue: Function[]): JQuery; + /** + * Manipulate the queue of functions to be executed on the matched element. + * + * @param element A DOM element on which to add a queued function. + * @param queueName A string containing the name of the queue. Defaults to fx, the standard effects queue. + * @param callback The new function to add to the queue. + */ + queue(element: Element, queueName: string, callback: Function): JQuery; removeData(element: Element, name?: string): JQuery; From 02348d3eb0d88f81b526e4a20ca358ce49692e0c Mon Sep 17 00:00:00 2001 From: John Reilly Date: Thu, 16 Jan 2014 14:00:56 +0000 Subject: [PATCH 3/3] jQuery: added proxy / removeData JSDoc + tests --- jquery/jquery-tests.ts | 109 +++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 50 ++++++++++++++++--- 2 files changed, 153 insertions(+), 6 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index e024f1dc6..95fed2635 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -946,6 +946,17 @@ function test_removeData() { $("span:eq(3)").text("" + $("div").data("test2")); } +function test_jQuery_removeData() { + var div = $("div")[0]; + $("span:eq(0)").text("" + $("div").data("test1")); + jQuery.data(div, "test1", "VALUE-1"); + jQuery.data(div, "test2", "VALUE-2"); + $("span:eq(1)").text("" + jQuery.data(div, "test1")); + jQuery.removeData(div, "test1"); + $("span:eq(2)").text("" + jQuery.data(div, "test1")); + $("span:eq(3)").text("" + jQuery.data(div, "test2")); +} + function test_dblclick() { $('#target').dblclick(function () { alert('Handler for .dblclick() called.'); @@ -1731,6 +1742,104 @@ function test_hasData() { $p.append(jQuery.hasData(p) + " "); } +function test_jQuery_proxy() { + + function test1() { + var me = { + type: "zombie", + test: function (event?) { + // Without proxy, `this` would refer to the event target + // use event.target to reference that element. + var element = event.target; + $(element).css("background-color", "red"); + + // With proxy, `this` refers to the me object encapsulating + // this function. + $("#log").append("Hello " + this.type + "
"); + $("#test").off("click", this.test); + } + }; + + var you = { + type: "person", + test: function (event?) { + $("#log").append(this.type + " "); + } + }; + + // Execute you.test() in the context of the `you` object + // no matter where it is called + // i.e. the `this` keyword will refer to `you` + var youClick = $.proxy(you.test, you); + + // attach click handlers to #test + $("#test") + // this === "zombie"; handler unbound after first click + .on("click", $.proxy(me.test, me)) + + // this === "person" + .on("click", youClick) + + // this === "zombie" + .on("click", $.proxy(you.test, me)) + + // this === "