|
| 1 | +package ghpages.examples |
| 2 | + |
| 3 | +import ghpages.examples.util.SingleSide |
| 4 | +import japgolly.scalajs.react._ |
| 5 | +import japgolly.scalajs.react.vdom.prefix_<^._ |
| 6 | +import org.scalajs.dom |
| 7 | + |
| 8 | +/** |
| 9 | + * Example of using Touch events. |
| 10 | + * |
| 11 | + * TouchList is JavaScript collection, so it is converted to Scala IndexedSeq. |
| 12 | + * Showing only top 10 events, so mobile phone will not crash. |
| 13 | + * Preventing default events, so move and zoom events could also be tested |
| 14 | + */ |
| 15 | +object TouchExample { |
| 16 | + |
| 17 | + def content = SingleSide.Content(source, TouchExampleApp()) |
| 18 | + |
| 19 | + val source = |
| 20 | + """ |
| 21 | + |// Recommended to test with real Touch screens or with Chrome "Emulate touch screen" |
| 22 | + | |
| 23 | + |/** Keeping history of events **/ |
| 24 | + |case class State(log: List[String] = List()) { |
| 25 | + | def withEntry(name: String) = copy(log = name :: log) |
| 26 | + | |
| 27 | + | def limit(max: Int) = if (log.size > max) copy(log = log.init) else this |
| 28 | + |} |
| 29 | + | |
| 30 | + |/** Saving touch event details to state */ |
| 31 | + |class Backend(val $: BackendScope[Unit, State]) { |
| 32 | + | def debugEvent(e: ReactTouchEvent): Unit = preventDefault(e) { state => |
| 33 | + | state withEntry s"${e.nativeEvent.`type`}: ${formatTouches(e.changedTouches)}" limit 10 |
| 34 | + | } |
| 35 | + | |
| 36 | + | private def preventDefault(e: ReactTouchEvent)(transformer: State => State): Unit = { |
| 37 | + | e.preventDefault() |
| 38 | + | $.modState(transformer) |
| 39 | + | } |
| 40 | + | |
| 41 | + | private def formatTouches(touches: dom.TouchList) = |
| 42 | + | toSeq(touches).map(formatCoordinates).mkString(" | ") |
| 43 | + | |
| 44 | + | private def toSeq[A](list: dom.DOMList[A]) = |
| 45 | + | for(i <- 0 to list.length - 1) yield list.item(i) |
| 46 | + | |
| 47 | + | private def formatCoordinates(touch: dom.Touch) = |
| 48 | + | s"${touch.screenX}x${touch.screenY}: ${touch.radiusX}x${touch.radiusY}" |
| 49 | + |} |
| 50 | + | |
| 51 | + |/** Rendering touch area and history of events */ |
| 52 | + |val TouchExampleApp = ReactComponentB[Unit]("TouchExample") |
| 53 | + | .initialState(new State) |
| 54 | + | .backend(new Backend(_)) |
| 55 | + | .render { (P, S, B) => |
| 56 | + | |
| 57 | + | <.div( |
| 58 | + | <.div( |
| 59 | + | "Area to test touch events", |
| 60 | + | ^.width := 200, // Basic style |
| 61 | + | ^.height := 200, |
| 62 | + | ^.border := "1px solid blue", |
| 63 | + | ^.onTouchStart ==> B.debugEvent, // Forwarding touch events |
| 64 | + | ^.onTouchMove ==> B.debugEvent, |
| 65 | + | ^.onTouchEnd ==> B.debugEvent, |
| 66 | + | ^.onTouchCancel ==> B.debugEvent |
| 67 | + | ), |
| 68 | + | <.ul ( // Rendering history of events |
| 69 | + | S.log.map( |
| 70 | + | <.li(_) |
| 71 | + | ) |
| 72 | + | ) |
| 73 | + | ) |
| 74 | + | |
| 75 | + |}.buildU |
| 76 | + |""".stripMargin |
| 77 | + |
| 78 | + /** Keeping history of events **/ |
| 79 | + case class State(log: List[String] = List()) { |
| 80 | + def withEntry(name: String) = copy(log = name :: log) |
| 81 | + |
| 82 | + def limit(max: Int) = if (log.size > max) copy(log = log.init) else this |
| 83 | + } |
| 84 | + |
| 85 | + /** Saving touch event details to state */ |
| 86 | + class Backend(val $: BackendScope[Unit, State]) { |
| 87 | + def debugEvent(e: ReactTouchEvent): Unit = preventDefault(e) { state => |
| 88 | + state withEntry s"${e.nativeEvent.`type`}: ${formatTouches(e.changedTouches)}" limit 10 |
| 89 | + } |
| 90 | + |
| 91 | + private def preventDefault(e: ReactTouchEvent)(transformer: State => State): Unit = { |
| 92 | + e.preventDefault() |
| 93 | + $.modState(transformer) |
| 94 | + } |
| 95 | + |
| 96 | + private def formatTouches(touches: dom.TouchList) = |
| 97 | + toSeq(touches).map(formatCoordinates).mkString(" | ") |
| 98 | + |
| 99 | + private def toSeq[A](list: dom.DOMList[A]) = |
| 100 | + for(i <- 0 to list.length - 1) yield list.item(i) |
| 101 | + |
| 102 | + private def formatCoordinates(touch: dom.Touch) = |
| 103 | + s"${touch.screenX}x${touch.screenY}: ${touch.radiusX}x${touch.radiusY}" |
| 104 | + } |
| 105 | + |
| 106 | + /** Rendering touch area and history of events */ |
| 107 | + val TouchExampleApp = ReactComponentB[Unit]("TouchExample") |
| 108 | + .initialState(new State) |
| 109 | + .backend(new Backend(_)) |
| 110 | + .render { (P, S, B) => |
| 111 | + |
| 112 | + <.div( |
| 113 | + <.div( |
| 114 | + "Area to test touch events", |
| 115 | + ^.width := 200, // Basic style |
| 116 | + ^.height := 200, |
| 117 | + ^.border := "1px solid blue", |
| 118 | + ^.onTouchStart ==> B.debugEvent, // Forwarding touch events |
| 119 | + ^.onTouchMove ==> B.debugEvent, |
| 120 | + ^.onTouchEnd ==> B.debugEvent, |
| 121 | + ^.onTouchCancel ==> B.debugEvent |
| 122 | + ), |
| 123 | + <.ul ( // Rendering history of events |
| 124 | + S.log.map( |
| 125 | + <.li(_) |
| 126 | + ) |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + }.buildU |
| 131 | +} |
0 commit comments