Skip to content

Commit f94ea9f

Browse files
author
Starzu
committed
EventName - event names enum
1 parent 0b3054b commit f94ea9f

File tree

6 files changed

+97
-71
lines changed

6 files changed

+97
-71
lines changed

example/src/main/scala/io/udash/demos/jquery/views/functions/AnimateView.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AnimateView extends FunctionView {
3939
).render
4040

4141
override protected val script = () => {
42-
jQ("#go1").on("click", (_: Element, _: JQueryEvent) => {
42+
jQ("#go1").on(EventName.click, (_: Element, _: JQueryEvent) => {
4343
jQ( "#block1" )
4444
.animate(Map(
4545
"width" -> "90%"
@@ -51,18 +51,18 @@ class AnimateView extends FunctionView {
5151
.animate(Map("borderRightWidth" -> "15px"), 1500)
5252
})
5353

54-
jQ("#go2").on("click", (_: Element, _: JQueryEvent) => {
54+
jQ("#go2").on(EventName.click, (_: Element, _: JQueryEvent) => {
5555
jQ("#block2")
5656
.animate(Map("width" -> "90%"), 1000)
5757
.animate(Map("fontSize" -> "24px"), 1000)
5858
.animate(Map("borderLeftWidth" -> "15px"), 1000)
5959
})
6060

61-
jQ("#go3").on("click", (_: Element, _: JQueryEvent) => {
61+
jQ("#go3").on(EventName.click, (_: Element, _: JQueryEvent) => {
6262
jQ("#go1").add("#go2").trigger("click")
6363
})
6464

65-
jQ("#go4").on("click", (_: Element, _: JQueryEvent) => {
65+
jQ("#go4").on(EventName.click, (_: Element, _: JQueryEvent) => {
6666
// TODO: It does not work without explicit Map elements type
6767
import scala.scalajs.js.`|`
6868
jQ("div").css(Map[String, String | Int | Double | Boolean](

example/src/main/scala/io/udash/demos/jquery/views/functions/AttrView.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AttrView extends FunctionView {
4040
).render
4141

4242
override protected val script = () => {
43-
jQ(".demo input").on("change", (input: Element, _: JQueryEvent) => {
43+
jQ(".demo input").on(EventName.change, (input: Element, _: JQueryEvent) => {
4444
jQ(".demo p").html(
4545
s""".attr('data-checked'): ${jQ(input).attr("data-checked")}<br/>
4646
|.prop('checked'): ${jQ(input).prop("checked")}<br/>

example/src/main/scala/io/udash/demos/jquery/views/functions/OnOneOffView.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class OnOneOffView extends FunctionView {
2626
disabled := "disabled",
2727
onclick :+= ((_: Event) => {
2828
jQ(".demo #click")
29-
.off("click", onCallback)
30-
.off("click", oneCallback)
29+
.off(EventName.click, onCallback)
30+
.off(EventName.click, oneCallback)
3131
false
3232
})
3333
)("Off")
3434
).render
3535

3636
override protected val script = () => {
3737
jQ(".demo #click")
38-
.on("click", onCallback)
39-
.one("click", oneCallback)
38+
.on(EventName.click, onCallback)
39+
.one(EventName.click, oneCallback)
4040

4141
jQ(".demo button")
4242
.prop("disabled", "")

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ trait JQuery extends js.Object {
9696

9797
/** For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. <br/>
9898
* See: <a href="http://api.jquery.com/closest/">jQuery Docs</a> */
99-
def closest(selector: String | Element | JQuery): JQuery = js.native
99+
def closest(selector: Selector | Element | JQuery): JQuery = js.native
100100

101101
/** For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. <br/>
102102
* See: <a href="http://api.jquery.com/closest/">jQuery Docs</a> */
103-
def closest(selector: String, context: Element): JQuery = js.native
103+
def closest(selector: Selector, context: Element): JQuery = js.native
104104

105105
/** Get the children of each element in the set of matched elements, including text and comment nodes. <br/>
106106
* See: <a href="http://api.jquery.com/contents/">jQuery Docs</a> */
@@ -194,7 +194,7 @@ trait JQuery extends js.Object {
194194

195195
/** Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. <br/>
196196
* See: <a href="http://api.jquery.com/find/">jQuery Docs</a> */
197-
def find(selector: String | Element | JQuery): JQuery = js.native
197+
def find(selector: Selector | Element | JQuery): JQuery = js.native
198198

199199
/** Stop the currently-running animation, remove all queued animations, and complete all animations for the matched elements. <br/>
200200
* See: <a href="http://api.jquery.com/finish/">jQuery Docs</a> */
@@ -223,7 +223,7 @@ trait JQuery extends js.Object {
223223

224224
/** Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element. <br/>
225225
* See: <a href="http://api.jquery.com/has/">jQuery Docs</a> */
226-
def has(selector: String | Element): JQuery = js.native
226+
def has(selector: Selector | Element): JQuery = js.native
227227

228228
/** Determine whether any of the matched elements are assigned the given class. <br/>
229229
* See: <a href="http://api.jquery.com/hasClass/">jQuery Docs</a> */
@@ -275,15 +275,15 @@ trait JQuery extends js.Object {
275275

276276
/** Insert every element in the set of matched elements after the target. <br/>
277277
* See: <a href="http://api.jquery.com/insertAfter/">jQuery Docs</a> */
278-
def insertAfter(selector: String | Element | JQuery): JQuery = js.native
278+
def insertAfter(selector: Selector | Element | JQuery): JQuery = js.native
279279

280280
/** Insert every element in the set of matched elements before the target. <br/>
281281
* See: <a href="http://api.jquery.com/insertBefore/">jQuery Docs</a> */
282-
def insertBefore(selector: String | Element | JQuery): JQuery = js.native
282+
def insertBefore(selector: Selector | Element | JQuery): JQuery = js.native
283283

284284
/** Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments. <br/>
285285
* See: <a href="http://api.jquery.com/is/">jQuery Docs</a> */
286-
def is(selector: String | Element | JQuery): Boolean = js.native
286+
def is(selector: Selector | Element | JQuery): Boolean = js.native
287287

288288
/** A string containing the jQuery version number. <br/>
289289
* See: <a href="http://api.jquery.com/jquery/">jQuery Docs</a> */
@@ -375,7 +375,7 @@ trait JQuery extends js.Object {
375375

376376
/** Remove elements from the set of matched elements. <br/>
377377
* See: <a href="http://api.jquery.com/not/">jQuery Docs</a> */
378-
def not(selector: String | JQuery): JQuery = js.native
378+
def not(selector: Selector | JQuery): JQuery = js.native
379379

380380
/** Remove elements from the set of matched elements. <br/>
381381
* See: <a href="http://api.jquery.com/not/">jQuery Docs</a> */
@@ -391,7 +391,7 @@ trait JQuery extends js.Object {
391391

392392
/** Remove an event handler. <br/>
393393
* See: <a href="http://api.jquery.com/off/">jQuery Docs</a> */
394-
def off(jEvent: JQueryEvent, selector: String = js.native): JQuery = js.native
394+
def off(jEvent: JQueryEvent, selector: Selector = js.native): JQuery = js.native
395395

396396
/** Get the closest ancestor element that is positioned. <br/>
397397
* See: <a href="http://api.jquery.com/offsetParent/">jQuery Docs</a> */
@@ -530,7 +530,7 @@ trait JQuery extends js.Object {
530530

531531
/** Get the siblings of each element in the set of matched elements, optionally filtered by a selector. <br/>
532532
* See: <a href="http://api.jquery.com/siblings/">jQuery Docs</a> */
533-
def siblings(selector: String = js.native): JQuery = js.native
533+
def siblings(selector: Selector = js.native): JQuery = js.native
534534

535535
/** Reduce the set of matched elements to a subset specified by a range of indices. Including `start`, without `end`. <br/>
536536
* See: <a href="http://api.jquery.com/slice/">jQuery Docs</a> */
@@ -583,11 +583,11 @@ trait JQuery extends js.Object {
583583

584584
/** Execute all handlers and behaviors attached to the matched elements for the given event type. <br/>
585585
* See: <a href="http://api.jquery.com/trigger/">jQuery Docs</a> */
586-
def trigger(event: String | JQueryEvent): JQuery = js.native
586+
def trigger(event: EventName | JQueryEvent): JQuery = js.native
587587

588588
/** Execute all handlers attached to an element for an event. <br/>
589589
* See: <a href="http://api.jquery.com/triggerHandler/">jQuery Docs</a> */
590-
def triggerHandler(event: String | JQueryEvent): JQuery = js.native
590+
def triggerHandler(event: EventName | JQueryEvent): JQuery = js.native
591591

592592
/** Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. <br/>
593593
* See: <a href="http://api.jquery.com/unwrap/">jQuery Docs</a> */
@@ -629,15 +629,15 @@ object JQuery {
629629
def unregister(): Unit
630630
}
631631

632-
case class CallbackParameters(once: Boolean, selector: String, data: js.Any)
633-
case class CallbackRegistrationRef(event: String, callback: JQueryCallback, registration: CallbackRegistration)
632+
case class CallbackParameters(once: Boolean, selector: Selector, data: js.Any)
633+
case class CallbackRegistrationRef(event: EventName, callback: JQueryCallback, registration: CallbackRegistration)
634634

635635
private val registrations: mutable.Map[Element, mutable.Buffer[CallbackRegistrationRef]] = mutable.Map[Element, mutable.Buffer[CallbackRegistrationRef]]()
636636

637637
implicit class JQueryWrapper(private val jquery: JQuery) {
638638
import js.JSConverters._
639639

640-
class OnCallbackRegistration(event: String, callback: JQueryCallback,
640+
class OnCallbackRegistration(event: EventName, callback: JQueryCallback,
641641
reg: (ThisFunction1[Element, JQueryEvent, Any]) => Any,
642642
unreg: (String, ThisFunction1[Element, JQueryEvent, Any]) => js.Any = (event, c) => jquery.asInstanceOf[js.Dynamic].off(event, c))
643643
extends CallbackRegistration {
@@ -1038,63 +1038,63 @@ object JQuery {
10381038

10391039
/** Attach an event handler function for one or more events to the selected elements. <br/>
10401040
* See: <a href="http://api.jquery.com/on/">jQuery Docs</a> */
1041-
def on(event: String, callback: JQueryCallback): JQuery = {
1041+
def on(event: EventName, callback: JQueryCallback): JQuery = {
10421042
separateCallbacks(event, callback, new CallbackParameters(false, null, null))
10431043
jquery
10441044
}
10451045

10461046
/** Attach an event handler function for one or more events to the selected elements. <br/>
10471047
* See: <a href="http://api.jquery.com/on/">jQuery Docs</a> */
1048-
def on(event: String, data: js.Any, callback: JQueryCallback): JQuery = {
1048+
def on(event: EventName, data: js.Any, callback: JQueryCallback): JQuery = {
10491049
separateCallbacks(event, callback, new CallbackParameters(false, null, data))
10501050
jquery
10511051
}
10521052

10531053
/** Attach an event handler function for one or more events to the selected elements. <br/>
10541054
* See: <a href="http://api.jquery.com/on/">jQuery Docs</a> */
1055-
def on(event: String, selector: String, callback: JQueryCallback): JQuery = {
1055+
def on(event: EventName, selector: Selector, callback: JQueryCallback): JQuery = {
10561056
separateCallbacks(event, callback, new CallbackParameters(false, selector, null))
10571057
jquery
10581058
}
10591059

10601060
/** Attach an event handler function for one or more events to the selected elements. <br/>
10611061
* See: <a href="http://api.jquery.com/on/">jQuery Docs</a> */
1062-
def on(event: String, selector: String, data: js.Any, callback: JQueryCallback): JQuery = {
1062+
def on(event: EventName, selector: Selector, data: js.Any, callback: JQueryCallback): JQuery = {
10631063
separateCallbacks(event, callback, new CallbackParameters(false, selector, data))
10641064
jquery
10651065
}
10661066

10671067
/** Attach a handler to an event for the elements. The handler is executed at most once per element per event type. <br/>
10681068
* See: <a href="http://api.jquery.com/one/">jQuery Docs</a> */
1069-
def one(event: String, callback: JQueryCallback): JQuery = {
1069+
def one(event: EventName, callback: JQueryCallback): JQuery = {
10701070
separateCallbacks(event, callback, new CallbackParameters(true, null, null))
10711071
jquery
10721072
}
10731073

10741074
/** Attach a handler to an event for the elements. The handler is executed at most once per element per event type. <br/>
10751075
* See: <a href="http://api.jquery.com/one/">jQuery Docs</a> */
1076-
def one(event: String, data: js.Any, callback: JQueryCallback): JQuery = {
1076+
def one(event: EventName, data: js.Any, callback: JQueryCallback): JQuery = {
10771077
separateCallbacks(event, callback, new CallbackParameters(true, null, data))
10781078
jquery
10791079
}
10801080

10811081
/** Attach a handler to an event for the elements. The handler is executed at most once per element per event type. <br/>
10821082
* See: <a href="http://api.jquery.com/one/">jQuery Docs</a> */
1083-
def one(event: String, selector: String, callback: JQueryCallback): JQuery = {
1083+
def one(event: EventName, selector: Selector, callback: JQueryCallback): JQuery = {
10841084
separateCallbacks(event, callback, new CallbackParameters(true, selector, null))
10851085
jquery
10861086
}
10871087

10881088
/** Attach a handler to an event for the elements. The handler is executed at most once per element per event type. <br/>
10891089
* See: <a href="http://api.jquery.com/one/">jQuery Docs</a> */
1090-
def one(event: String, selector: String, data: js.Any, callback: JQueryCallback): JQuery = {
1090+
def one(event: EventName, selector: Selector, data: js.Any, callback: JQueryCallback): JQuery = {
10911091
separateCallbacks(event, callback, new CallbackParameters(true, selector, data))
10921092
jquery
10931093
}
10941094

10951095
/** Remove an event handler. <br/>
10961096
* See: <a href="http://api.jquery.com/off/">jQuery Docs</a> */
1097-
def off(event: String, callback: JQueryCallback): JQuery = {
1097+
def off(event: EventName, callback: JQueryCallback): JQuery = {
10981098
jquery.asInstanceOf[js.Dynamic].toArray().asInstanceOf[js.Array[Element]]
10991099
.foreach( el => {
11001100
if (registrations.contains(el)) {
@@ -1114,7 +1114,7 @@ object JQuery {
11141114
jquery
11151115
}
11161116

1117-
private def separateCallbacks(event: String, callback: JQueryCallback, params: CallbackParameters): Unit = {
1117+
private def separateCallbacks(event: EventName, callback: JQueryCallback, params: CallbackParameters): Unit = {
11181118

11191119
type ThisFunctionCallback = ThisFunction1[Element, JQueryEvent, Any]
11201120

@@ -1136,7 +1136,7 @@ object JQuery {
11361136
}))
11371137
}
11381138

1139-
private def collectRegistration(event: String, callback: JQueryCallback, el: Element, index: Int, params: CallbackParameters, reg: CallbackRegistration) = {
1139+
private def collectRegistration(event: EventName, callback: JQueryCallback, el: Element, index: Int, params: CallbackParameters, reg: CallbackRegistration) = {
11401140
val jqueryRegs: mutable.Buffer[CallbackRegistrationRef] = registrations.getOrElse(el, mutable.Buffer[CallbackRegistrationRef]())
11411141
jqueryRegs += CallbackRegistrationRef(event, callback, reg)
11421142
registrations.update(el, jqueryRegs)
@@ -1322,22 +1322,22 @@ object JQuery {
13221322

13231323
/** Execute all handlers and behaviors attached to the matched elements for the given event type. <br/>
13241324
* See: <a href="http://api.jquery.com/trigger/">jQuery Docs</a> */
1325-
def trigger(event: String | JQueryEvent, extraParams: Map[String, Any]): JQuery =
1325+
def trigger(event: EventName | JQueryEvent, extraParams: Map[String, Any]): JQuery =
13261326
jquery.asInstanceOf[js.Dynamic].trigger(event.asInstanceOf[js.Dynamic], extraParams.toJSDictionary).asInstanceOf[JQuery]
13271327

13281328
/** Execute all handlers and behaviors attached to the matched elements for the given event type. <br/>
13291329
* See: <a href="http://api.jquery.com/trigger/">jQuery Docs</a> */
1330-
def trigger(event: String | JQueryEvent, extraParams: Seq[Any]): JQuery =
1330+
def trigger(event: EventName | JQueryEvent, extraParams: Seq[Any]): JQuery =
13311331
jquery.asInstanceOf[js.Dynamic].trigger(event.asInstanceOf[js.Dynamic], extraParams.toJSArray).asInstanceOf[JQuery]
13321332

13331333
/** Execute all handlers attached to an element for an event. <br/>
13341334
* See: <a href="http://api.jquery.com/triggerHandler/">jQuery Docs</a> */
1335-
def triggerHandler(event: String | JQueryEvent, extraParams: Map[String, Any]): JQuery =
1335+
def triggerHandler(event: EventName | JQueryEvent, extraParams: Map[String, Any]): JQuery =
13361336
jquery.asInstanceOf[js.Dynamic].triggerHandler(event.asInstanceOf[js.Dynamic], extraParams.toJSDictionary).asInstanceOf[JQuery]
13371337

13381338
/** Execute all handlers attached to an element for an event. <br/>
13391339
* See: <a href="http://api.jquery.com/triggerHandler/">jQuery Docs</a> */
1340-
def triggerHandler(event: String | JQueryEvent, extraParams: Seq[Any]): JQuery =
1340+
def triggerHandler(event: EventName | JQueryEvent, extraParams: Seq[Any]): JQuery =
13411341
jquery.asInstanceOf[js.Dynamic].triggerHandler(event.asInstanceOf[js.Dynamic], extraParams.toJSArray).asInstanceOf[JQuery]
13421342

13431343
/** Set the value of each element in the set of matched elements. <br/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package object jquery {
99
/** See: <a href="http://api.jquery.com/category/selectors/">jQuery Docs</a> */
1010
type Selector = String
1111
type EasingFunction = String
12+
type EventName = String
1213
type JQueryCallback = (Element, JQueryEvent) => Any
1314

1415
def jQ: JQueryStatic = js.Dynamic.global.jQuery.asInstanceOf[JQueryStatic]

0 commit comments

Comments
 (0)