Skip to content

Commit 1ece649

Browse files
author
starzu
committed
jQuery wrapper
1 parent eb56529 commit 1ece649

File tree

12 files changed

+2286
-1
lines changed

12 files changed

+2286
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# scala-js-jquery
2-
Static types for the jQuery API for Scala.js programs.
2+
Static types for the jQuery API for [Scala.js](http://www.scala-js.org/) programs.
3+
4+
Sponsored by [AVSystem](http://www.avsystem.com/)

build.sbt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name := "udash-jquery"
2+
3+
version := "0.1.0"
4+
organization := "io.udash"
5+
scalaVersion := "2.11.7"
6+
scalacOptions in ThisBuild ++= Seq(
7+
"-feature",
8+
"-deprecation",
9+
"-unchecked",
10+
"-language:implicitConversions",
11+
"-language:existentials",
12+
"-language:dynamics",
13+
"-Xfuture",
14+
"-Xfatal-warnings",
15+
"-Xlint:_,-missing-interpolator,-adapted-args"
16+
)
17+
18+
libraryDependencies +=
19+
"org.scala-js" %%% "scalajs-dom" % "0.8.2"
20+
21+
jsDependencies +=
22+
"org.webjars" % "jquery" % "2.2.0" / "2.2.0/jquery.js" minified "2.2.0/jquery.min.js"
23+
24+
lazy val root = project.in(file("."))
25+
.enablePlugins(ScalaJSPlugin)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 0.13.9

project/plugins.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logLevel := Level.Warn
2+
3+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.6")

src/main/scala/io/udash/wrappers/jquery/JQuery.scala

Lines changed: 1562 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package io.udash.wrappers.jquery
2+
3+
import scala.scalajs.js
4+
5+
/** See: <a href="http://api.jquery.com/jQuery.ajax/#jqXHR">jQuery Docs</a> */
6+
@js.native
7+
trait JQueryXHR extends js.Object {
8+
import scala.scalajs.js.`|`
9+
10+
def overrideMimeType(tpe: String): Unit = js.native
11+
12+
/** An alternative construct to the success callback option. (data, textStatus, jqXHR) => anything */
13+
def done(callback: ((js.Any, String, JQueryXHR) => js.Any)*): Unit = js.native
14+
15+
/** An alternative construct to the error callback option. (jqXHR, textStatus, errorThrown) => anything */
16+
def fail(callback: ((JQueryXHR, String, js.Any) => js.Any)*): Unit = js.native
17+
18+
/** An alternative construct to the complete callback option. (data|jqXHR, textStatus, jqXHR|errorThrown) => anything */
19+
def always(callback: ((js.Any | JQueryXHR, String, JQueryXHR | js.Any) => js.Any)*): Unit = js.native
20+
21+
/** Incorporates the functionality of the .done() and .fail() methods. (data, textStatus, jqXHR) => anything, (jqXHR, textStatus, errorThrown) => anything */
22+
def `then`(done: ((js.Any, String, JQueryXHR) => js.Any), fail: ((JQueryXHR, String, js.Any) => js.Any)): Unit = js.native
23+
}
24+
25+
/** jQuery AJAX settings object<br/>
26+
* See: <a href="http://api.jquery.com/jQuery.ajax/">jQuery Docs</a> */
27+
@js.native
28+
trait JQueryAjaxSettings extends js.Object {
29+
/** The content type sent in the request header that tells the server what kind of response it will accept in return. */
30+
def accepts: js.Any = js.native
31+
32+
/** By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. */
33+
def async: Boolean = js.native
34+
35+
/** A pre-request callback function that can be used to modify the [[JQueryXHR]] object before it is sent.
36+
* Returning false in the beforeSend function will cancel the request. */
37+
def beforeSend(jqXHR: JQueryXHR, settings: JQueryAjaxSettings): Boolean = js.native
38+
39+
/** If set to false, it will force requested pages not to be cached by the browser. */
40+
def cache: Boolean = js.native
41+
42+
/** A function to be called when the request finishes (after success and error callbacks are executed). */
43+
def complete(jqXHR: JQueryXHR, textStatus: String): js.Dynamic = js.native
44+
45+
/** An object of string/regular-expression pairs that determine how jQuery will parse the response, given its content type. */
46+
def contents: js.Any = js.native
47+
48+
/** When sending data to the server, use this content type. */
49+
def contentType: js.Any = js.native
50+
51+
/** This object will be the context of all Ajax-related callbacks. */
52+
def context: js.Any = js.native
53+
54+
/** An object containing dataType-to-dataType converters. */
55+
def converters: js.Any = js.native
56+
57+
/** If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. */
58+
def crossDomain: Boolean = js.native
59+
60+
/** Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. */
61+
def data: js.Any = js.native
62+
63+
/** A function to be used to handle the raw response data of XMLHttpRequest. */
64+
def dataFilter(data: js.Any, tpe: js.Any): js.Any = js.native
65+
66+
/** The type of data that you're expecting back from the server. */
67+
def dataType: String = js.native
68+
69+
/** A function to be called if the request fails. */
70+
def error(jqXHR: JQueryXHR, textStatus: String, errorThrow: String): js.Any = js.native
71+
72+
/** Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to prevent the global handlers like `ajaxStart` or `ajaxStop` from being triggered. */
73+
def global: Boolean = js.native
74+
75+
/** An object of additional header key/value pairs to send along with requests using the XMLHttpRequest transport. */
76+
def headers: js.Any = js.native
77+
78+
/** Allow the request to be successful only if the response has changed since the last request. */
79+
def ifModified: Boolean = js.native
80+
81+
/** Allow the current environment to be recognized as "local," (e.g. the filesystem), even if jQuery does not recognize it as such by default. */
82+
def isLocal: Boolean = js.native
83+
84+
/** Override the callback function name in a JSONP request. */
85+
def jsonp: String = js.native
86+
87+
/** Specify the callback function name for a JSONP request. */
88+
def jsonpCallback: js.Any = js.native
89+
90+
/** The HTTP method to use for the request (e.g. "POST", "GET", "PUT"). */
91+
def method: String = js.native
92+
93+
/** A mime type to override the XHR mime type. */
94+
def mimeType: String = js.native
95+
96+
/** A password to be used with XMLHttpRequest in response to an HTTP access authentication request. */
97+
def password: String = js.native
98+
99+
/** By default, data passed in to the data option as an object (technically, anything other than a string) will be
100+
* processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". */
101+
def processData: Boolean = js.native
102+
103+
/** Only applies when the "script" transport is used. */
104+
def scriptCharset: String = js.native
105+
106+
/** An object of numeric HTTP codes and functions to be called when the response has the corresponding code. */
107+
def statusCode: js.Any = js.native
108+
109+
/** A function to be called if the request succeeds. */
110+
def success(data: js.Any, textStatus: String, jqXHR: JQueryXHR): js.Any = js.native
111+
112+
/** Set a timeout (in milliseconds) for the request. */
113+
def timeout: Double = js.native
114+
115+
/** Set this to true if you wish to use the traditional style of param serialization. */
116+
def traditional: Boolean = js.native
117+
118+
/** An alias for method. */
119+
@deprecated(message = "Use `method` instead.", since = "0.1.0")
120+
def `type`: String = js.native
121+
122+
/** A string containing the URL to which the request is sent. */
123+
def url: String = js.native
124+
125+
/** A username to be used with XMLHttpRequest in response to an HTTP access authentication request. */
126+
def username: String = js.native
127+
128+
/** Callback for creating the XMLHttpRequest object. */
129+
def xhr: js.Any = js.native
130+
131+
/** An object of fieldName-fieldValue pairs to set on the native XHR object. */
132+
def xhrFields: js.Object = js.native
133+
}
134+
135+
object JQueryAjaxSettings {
136+
def apply(accepts: js.Any = null, async: Option[Boolean] = None, beforeSend: (JQueryXHR, JQueryAjaxSettings) => js.Any = null,
137+
cache: Option[Boolean] = None, complete: (JQueryXHR, String) => js.Any = null, contents: js.Any = null, contentType: js.Any = null,
138+
context: js.Any = null, converters: js.Any = null, crossDomain: Option[Boolean] = None, data: js.Any = null, dataFilter: (js.Any, js.Any) => js.Any = null,
139+
dataType: String = null, error: (JQueryXHR, String) => js.Any = null, global: Option[Boolean] = None, headers: js.Any = null,
140+
ifModified: Option[Boolean] = None, isLocal: Option[Boolean] = None, jsonp: String = null, jsonpCallback: js.Any = null, method: String = null,
141+
mimeType: String = null, password: String = null, processData: Option[Boolean] = None, scriptCharset: String = null, statusCode: js.Any = null,
142+
success: (js.Any, String, JQueryXHR) => js.Any = null, timeout: Option[Double] = None, traditional: Option[Boolean] = None, `type`: String = null,
143+
url: String = null, username: String = null, xhr: js.Any = null, xhrFields: js.Object = null)
144+
: JQueryAjaxSettings = {
145+
val o = js.Dynamic.literal()
146+
147+
if (accepts != null) o.accepts = accepts
148+
if (async.isDefined) o.async = async.get
149+
if (beforeSend != null) o.beforeSend = beforeSend
150+
if (cache.isDefined) o.cache = cache.get
151+
if (complete != null) o.complete = complete
152+
if (contents != null) o.contents = contents
153+
if (contentType != null) o.contentType = contentType
154+
if (context != null) o.context = context
155+
if (converters != null) o.converters = converters
156+
if (crossDomain.isDefined) o.crossDomain = crossDomain.get
157+
if (data != null) o.data = data
158+
if (dataFilter != null) o.dataFilter = dataFilter
159+
if (dataType != null) o.dataType = dataType
160+
if (error != null) o.error = error
161+
if (global.isDefined) o.global = global.get
162+
if (headers != null) o.headers = headers
163+
if (ifModified.isDefined) o.ifModified = ifModified.get
164+
if (isLocal.isDefined) o.isLocal = isLocal.get
165+
if (jsonp != null) o.jsonp = jsonp
166+
if (jsonpCallback != null) o.jsonpCallback = jsonpCallback
167+
if (method != null) o.method = method
168+
if (mimeType != null) o.mimeType = mimeType
169+
if (password != null) o.password = password
170+
if (processData.isDefined) o.processData = processData.get
171+
if (scriptCharset != null) o.scriptCharset = scriptCharset
172+
if (statusCode != null) o.statusCode = statusCode
173+
if (success != null) o.success = success
174+
if (timeout.isDefined) o.timeout = timeout.get
175+
if (traditional.isDefined) o.traditional = traditional.get
176+
if (`type` != null) o.`type` = `type`
177+
if (url != null) o.url = url
178+
if (username != null) o.username = username
179+
if (xhr != null) o.xhr = xhr
180+
if (xhrFields != null) o.xhrFields = xhrFields
181+
182+
o.asInstanceOf[JQueryAjaxSettings]
183+
}
184+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.udash.wrappers.jquery
2+
3+
import scala.scalajs.js
4+
5+
@js.native
6+
trait JQueryCallbacks[FunType <: js.Function1[ArgType, js.Any], ArgType] extends js.Any {
7+
/** Add a callback or a collection of callbacks to a callback list. <br/>
8+
* See: <a href="http://api.jquery.com/callbacks.add/">jQuery Docs</a> */
9+
def add(callback: FunType): JQueryCallbacks[FunType, ArgType] = js.native
10+
11+
/** Disable a callback list from doing anything more. <br/>
12+
* See: <a href="http://api.jquery.com/callbacks.disable/">jQuery Docs</a> */
13+
def disable(): JQueryCallbacks[FunType, ArgType] = js.native
14+
15+
/** Determine if the callbacks list has been disabled. <br/>
16+
* See: <a href="http://api.jquery.com/callbacks.disabled/">jQuery Docs</a> */
17+
def disabled: Boolean = js.native
18+
19+
/** Call all of the callbacks with the given arguments. <br/>
20+
* See: <a href="http://api.jquery.com/callbacks.fire/">jQuery Docs</a> */
21+
def fire(args: ArgType): JQueryCallbacks[FunType, ArgType] = js.native
22+
23+
/** Determine if the callbacks have already been called at least once. <br/>
24+
* See: <a href="http://api.jquery.com/callbacks.fired/">jQuery Docs</a> */
25+
def fired: Boolean = js.native
26+
27+
/** Call all callbacks in a list with the given context and arguments. <br/>
28+
* See: <a href="http://api.jquery.com/callbacks.fireWith/">jQuery Docs</a> */
29+
def fireWith(context: js.Any, args: js.Any*): JQueryCallbacks[FunType, ArgType] = js.native
30+
31+
/** Determine whether or not the list has any callbacks attached. <br/>
32+
* See: <a href="http://api.jquery.com/callbacks.has/">jQuery Docs</a> */
33+
def has: Boolean = js.native
34+
35+
/** Determine whether `callback` is in a list. <br/>
36+
* See: <a href="http://api.jquery.com/callbacks.has/">jQuery Docs</a> */
37+
def has(callback: FunType): Boolean = js.native
38+
39+
/** Lock a callback list in its current state. <br/>
40+
* See: <a href="http://api.jquery.com/callbacks.lock/">jQuery Docs</a> */
41+
def lock(): JQueryCallbacks[FunType, ArgType] = js.native
42+
43+
/** Determine if the callbacks list has been locked. <br/>
44+
* See: <a href="http://api.jquery.com/callbacks.locked/">jQuery Docs</a> */
45+
def locked: Boolean = js.native
46+
47+
/** Remove a callback or a collection of callbacks from a callback list. <br/>
48+
* See: <a href="http://api.jquery.com/callbacks.remove/">jQuery Docs</a> */
49+
def remove(callback: FunType): JQueryCallbacks[FunType, ArgType] = js.native
50+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.udash.wrappers.jquery
2+
3+
import org.scalajs.dom._
4+
5+
import scala.scalajs.js
6+
7+
@js.native
8+
trait JQueryPromise[FunType <: js.Function1[ArgType, js.Any], ArgType] extends js.Any {
9+
/** Add handlers to be called when the Deferred object is either resolved or rejected. <br/>
10+
* See: <a href="http://api.jquery.com/deferred.always/">jQuery Docs</a> */
11+
def always(callbacks: FunType*): JQueryDeferred[FunType, ArgType] = js.native
12+
13+
/** Add handlers to be called when the Deferred object is resolved. <br/>
14+
* See: <a href="http://api.jquery.com/deferred.done/">jQuery Docs</a> */
15+
def done(callbacks: FunType*): JQueryDeferred[FunType, ArgType] = js.native
16+
17+
/** Add handlers to be called when the Deferred object is rejected. <br/>
18+
* See: <a href="http://api.jquery.com/deferred.fail/">jQuery Docs</a> */
19+
def fail(callbacks: FunType*): JQueryDeferred[FunType, ArgType] = js.native
20+
21+
/** Call the progressCallbacks on a Deferred object with the given `context` and `args`. <br/>
22+
* See: <a href="http://api.jquery.com/deferred.progress/">jQuery Docs</a> */
23+
def progress(callbacks: FunType*): JQueryDeferred[FunType, ArgType] = js.native
24+
25+
/** Return a Deferred's Promise object. <br/>
26+
* See: <a href="http://api.jquery.com/deferred.promise/">jQuery Docs</a> */
27+
def promise(target: js.Object = js.native): JQueryPromise[FunType, ArgType] = js.native
28+
29+
/** Determine the current state of a Deferred object. <br/>
30+
* See: <a href="http://api.jquery.com/deferred.resolveWith/">jQuery Docs</a> */
31+
private[jquery] def state: String = js.native
32+
}
33+
34+
35+
@js.native
36+
trait JQueryDeferred[FunType <: js.Function1[ArgType, js.Any], ArgType] extends JQueryPromise[FunType, ArgType] {
37+
/** Call the progressCallbacks on a Deferred object with the given `args`. <br/>
38+
* See: <a href="http://api.jquery.com/deferred.notify/">jQuery Docs</a> */
39+
def notify(args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
40+
41+
/** Call the progressCallbacks on a Deferred object with the given `context` and `args`. <br/>
42+
* See: <a href="http://api.jquery.com/deferred.notifyWith/">jQuery Docs</a> */
43+
def notifyWith(context: js.Any, args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
44+
45+
/** Reject a Deferred object and call any failCallbacks with the given `args`. <br/>
46+
* See: <a href="http://api.jquery.com/deferred.reject/">jQuery Docs</a> */
47+
def reject(args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
48+
49+
/** Reject a Deferred object and call any failCallbacks with the given `context` and `args`. <br/>
50+
* See: <a href="http://api.jquery.com/deferred.rejectWith/">jQuery Docs</a> */
51+
def rejectWith(context: js.Any, args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
52+
53+
/** Resolve a Deferred object and call any doneCallbacks with the given `args`. <br/>
54+
* See: <a href="http://api.jquery.com/deferred.resolve/">jQuery Docs</a> */
55+
def resolve(args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
56+
57+
/** Resolve a Deferred object and call any doneCallbacks with the given `context` and `args`. <br/>
58+
* See: <a href="http://api.jquery.com/deferred.resolveWith/">jQuery Docs</a> */
59+
def resolveWith(context: js.Any, args: ArgType): JQueryDeferred[FunType, ArgType] = js.native
60+
}
61+
62+
object JQueryPromise {
63+
sealed trait JQueryDeferredState
64+
case object JQueryDeferredPending extends JQueryDeferredState
65+
case object JQueryDeferredResolved extends JQueryDeferredState
66+
case object JQueryDeferredRejected extends JQueryDeferredState
67+
case class JQueryDeferredUnknown(describe: String) extends JQueryDeferredState
68+
69+
implicit class JQueryPromiseExt(jQueryPromise: JQueryPromise[_, _]) {
70+
/** Determine the current state of a Deferred object. <br/>
71+
* See: <a href="http://api.jquery.com/deferred.resolveWith/">jQuery Docs</a> */
72+
def state: JQueryDeferredState = jQueryPromise.state match {
73+
case "pending" => JQueryDeferredPending
74+
case "resolved" => JQueryDeferredResolved
75+
case "rejected" => JQueryDeferredRejected
76+
case unknown: String => JQueryDeferredUnknown(unknown)
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)