Skip to content

Commit 0021446

Browse files
committed
Merge pull request DefinitelyTyped#4456 from EnableSoftware/farbtastic
Added new definition for Farbtastic jQuery plugin
2 parents 7137205 + 010acb1 commit 0021446

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

farbtastic/farbtastic-tests.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/// <reference path="farbtastic.d.ts" />
2+
3+
var callback = () => {};
4+
var domNode = document.createElement("div");
5+
6+
// Basic usage
7+
8+
// Can add a ready() handler to the document which initializes the color picker and links it to the text field
9+
$(document).ready(function() {
10+
$("#colorpicker").farbtastic("#color");
11+
});
12+
13+
// Advanced Usage: jQuery Method
14+
15+
// Create color pickers in the selected objects
16+
$("#colorpicker").farbtastic();
17+
18+
// Optional callback using a callback function
19+
$("#colorpicker").farbtastic(callback);
20+
$("#colourpicker").farbtastic(function (color) {
21+
console.log(typeof color === "string");
22+
});
23+
24+
// Optional callback using a DOM node
25+
$("#colorpicker").farbtastic(domNode);
26+
27+
// Optional callback using a jQuery object
28+
$("#colorpicker").farbtastic($("#color"));
29+
30+
// Optional callback using a jQuery selector
31+
$("#colorpicker").farbtastic("#color");
32+
33+
// Advanced Usage: Object
34+
35+
// Can invoke method for returning Farbtastic object instead of a jQuery object
36+
$.farbtastic(domNode);
37+
$.farbtastic($("#color"));
38+
$.farbtastic("#color");
39+
40+
// Optional callback using a callback function
41+
$.farbtastic(domNode, callback);
42+
$.farbtastic($("#color"), callback);
43+
$.farbtastic("#color", callback);
44+
45+
// Optional callback using a DOM node
46+
$.farbtastic(domNode, domNode);
47+
$.farbtastic($("#color"), domNode);
48+
$.farbtastic("#color", domNode);
49+
50+
// Optional callback using a jQuery object
51+
$.farbtastic(domNode, $("#color"));
52+
$.farbtastic($("#color"), $("#color"));
53+
$.farbtastic("#color", $("#color"));
54+
55+
// Optional callback using a jQuery selector
56+
$.farbtastic(domNode, "#color");
57+
$.farbtastic($("#color"), "#color");
58+
$.farbtastic("#color", "#color");
59+
60+
// Advanced Usage: Options
61+
$("#colorpicker").farbtastic({
62+
callback: (color) => {
63+
console.log(color);
64+
}
65+
});
66+
$.farbtastic(domNode, {
67+
width: 500
68+
});
69+
$.farbtastic($("#color"), {
70+
wheelWidth: 300
71+
});
72+
$.farbtastic("#color", {});
73+
74+
// Advanced Usage: Methods
75+
$.farbtastic("#colorpicker").linkTo(callback);
76+
$.farbtastic("#colorpicker").linkTo(domNode);
77+
$.farbtastic("#colorpicker").linkTo("#color");
78+
$.farbtastic("#colorpicker").linkTo($("#color"));
79+
80+
$.farbtastic("#colorpicker").setColor("#aabbcc");
81+
$.farbtastic("#colorpicker").setColor([0.1, 0.2, 0.3]);
82+
$.farbtastic("#colorpicker").setHSL([0.1, 0.2, 0.3]);
83+
84+
// Advanced Usage: Properties
85+
$.farbtastic("#colorpicker").color === "#aabbcc";
86+
$.farbtastic("#colorpicker").hsl === [0.1, 0.2, 0.3];
87+
$.farbtastic("#colorpicker").linked === $("#colorpicker");
88+
$.farbtastic("#colorpicker").linked === callback;
89+
90+
// Can chain jQuery methods
91+
$("#colorpicker")
92+
.farbtastic()
93+
.addClass("color-picker");
94+
95+
// Can chain Farbtastic methods
96+
$.farbtastic("#colorpicker")
97+
.linkTo(domNode)
98+
.setColor("#000000")
99+
.setHSL([0, 0, 0]);
100+

farbtastic/farbtastic.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Type definitions for Farbtastic: jQuery Color Wheel v2.0.0-alpha.1
2+
// Project: http://mattfarina.github.io/farbtastic/
3+
// Definitions by: Matt Brooks <https://github.com/EnableSoftware>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/// <reference path="../jquery/jquery.d.ts" />
7+
8+
declare module JQueryFarbtastic {
9+
type Placeholder = string | Element | JQuery;
10+
type CallbackFunction = (color: string) => any;
11+
type Callback = CallbackFunction | Placeholder;
12+
13+
interface Options {
14+
callback?: Callback;
15+
width?: number;
16+
wheelWidth?: number;
17+
}
18+
19+
interface Farbtastic {
20+
linked: CallbackFunction | JQuery;
21+
color: string;
22+
hsl: number[];
23+
24+
linkTo(callback: Callback): Farbtastic;
25+
setColor(color: string | number[]): Farbtastic;
26+
setHSL(hsl: number[]): Farbtastic;
27+
}
28+
}
29+
30+
interface JQueryStatic {
31+
farbtastic(placeholder: JQueryFarbtastic.Placeholder, callback: JQueryFarbtastic.Callback): JQueryFarbtastic.Farbtastic;
32+
farbtastic(placeholder: JQueryFarbtastic.Placeholder, options: JQueryFarbtastic.Options): JQueryFarbtastic.Farbtastic;
33+
farbtastic(placeholder: JQueryFarbtastic.Placeholder): JQueryFarbtastic.Farbtastic;
34+
}
35+
36+
interface JQuery {
37+
farbtastic(callback: JQueryFarbtastic.Callback): JQuery;
38+
farbtastic(options: JQueryFarbtastic.Options): JQuery;
39+
farbtastic(): JQuery;
40+
}

0 commit comments

Comments
 (0)