|  | 
| 1 | 1 | package org.scalajs.dom.ext | 
| 2 | 2 | 
 | 
|  | 3 | +import java.nio.ByteBuffer | 
|  | 4 | + | 
| 3 | 5 | import scala.language.implicitConversions | 
| 4 | 6 | import scala.concurrent.{Promise, Future} | 
| 5 | 7 | 
 | 
| 6 | 8 | import scala.scalajs.js | 
|  | 9 | +import scala.scalajs.js.typedarray._ | 
|  | 10 | +import scala.scalajs.js.typedarray.TypedArrayBufferOps._ | 
| 7 | 11 | 
 | 
| 8 | 12 | import org.scalajs.dom | 
| 9 | 13 | import org.scalajs.dom.{html, raw} | 
|  | 14 | +import org.scalajs.dom.raw.Blob | 
|  | 15 | + | 
| 10 | 16 | 
 | 
| 11 | 17 | /** | 
| 12 | 18 |  * Used to extend out javascript *Collections to make them usable as normal | 
| @@ -236,42 +242,73 @@ case class AjaxException(xhr: dom.XMLHttpRequest) extends Exception { | 
| 236 | 242 |  * Wraps an XMLHttpRequest to provide an easy one-line way of making  | 
| 237 | 243 |  * an Ajax call, returning a Future. | 
| 238 | 244 |  */ | 
| 239 |  | -object Ajax{ | 
|  | 245 | +object Ajax { | 
|  | 246 | + /** | 
|  | 247 | + * Supported data formats for Ajax are implicitly converted to InputData | 
|  | 248 | + */ | 
|  | 249 | + sealed trait InputData extends js.Any | 
|  | 250 | + | 
|  | 251 | + object InputData { | 
|  | 252 | + implicit def str2ajax(s: String): InputData = s.asInstanceOf[InputData] | 
|  | 253 | + | 
|  | 254 | + implicit def arrayBufferView2ajax(b: ArrayBufferView): InputData = b.asInstanceOf[InputData] | 
|  | 255 | + | 
|  | 256 | + implicit def blob2ajax(b: Blob): InputData = b.asInstanceOf[InputData] | 
|  | 257 | + | 
|  | 258 | + implicit def byteBuffer2ajax(data: ByteBuffer): InputData = { | 
|  | 259 | + if (data.hasTypedArray()) { | 
|  | 260 | + // get relevant part of the underlying typed array | 
|  | 261 | + data.typedArray().subarray(data.position, data.limit) | 
|  | 262 | + } else { | 
|  | 263 | + // fall back to copying the data | 
|  | 264 | + val tempBuffer = ByteBuffer.allocateDirect(data.remaining) | 
|  | 265 | + val origPosition = data.position | 
|  | 266 | + tempBuffer.put(data) | 
|  | 267 | + data.position(origPosition) | 
|  | 268 | + tempBuffer.typedArray() | 
|  | 269 | + } | 
|  | 270 | + } | 
|  | 271 | + } | 
|  | 272 | + | 
| 240 | 273 |  def get(url: String, | 
| 241 |  | - data: String = "", | 
|  | 274 | + data: InputData = "", | 
| 242 | 275 |  timeout: Int = 0, | 
| 243 | 276 |  headers: Map[String, String] = Map.empty, | 
| 244 |  | - withCredentials: Boolean = false,  | 
|  | 277 | + withCredentials: Boolean = false, | 
| 245 | 278 |  responseType: String = "") = { | 
| 246 | 279 |  apply("GET", url, data, timeout, headers, withCredentials, responseType) | 
| 247 | 280 |  } | 
|  | 281 | + | 
| 248 | 282 |  def post(url: String, | 
| 249 |  | - data: String = "", | 
|  | 283 | + data: InputData = "", | 
| 250 | 284 |  timeout: Int = 0, | 
| 251 | 285 |  headers: Map[String, String] = Map.empty, | 
| 252 |  | - withCredentials: Boolean = false,  | 
|  | 286 | + withCredentials: Boolean = false, | 
| 253 | 287 |  responseType: String = "") = { | 
| 254 | 288 |  apply("POST", url, data, timeout, headers, withCredentials, responseType) | 
| 255 | 289 |  } | 
|  | 290 | + | 
| 256 | 291 |  def put(url: String, | 
| 257 |  | -  data: String = "", | 
| 258 |  | -  timeout: Int = 0, | 
| 259 |  | -  headers: Map[String, String] = Map.empty, | 
| 260 |  | -  withCredentials: Boolean = false, | 
| 261 |  | -  responseType: String = "") = { | 
|  | 292 | + data: InputData = "", | 
|  | 293 | + timeout: Int = 0, | 
|  | 294 | + headers: Map[String, String] = Map.empty, | 
|  | 295 | + withCredentials: Boolean = false, | 
|  | 296 | + responseType: String = "") = { | 
| 262 | 297 |  apply("PUT", url, data, timeout, headers, withCredentials, responseType) | 
| 263 | 298 |  } | 
|  | 299 | + | 
| 264 | 300 |  def delete(url: String, | 
| 265 |  | - data: String = "", | 
|  | 301 | + data: InputData = "", | 
| 266 | 302 |  timeout: Int = 0, | 
| 267 | 303 |  headers: Map[String, String] = Map.empty, | 
| 268 | 304 |  withCredentials: Boolean = false, | 
| 269 | 305 |  responseType: String = "") = { | 
| 270 | 306 |  apply("DELETE", url, data, timeout, headers, withCredentials, responseType) | 
| 271 | 307 |  } | 
|  | 308 | + | 
| 272 | 309 |  def apply(method: String, | 
| 273 | 310 |  url: String, | 
| 274 |  | - data: String, | 
|  | 311 | + data: InputData, | 
| 275 | 312 |  timeout: Int, | 
| 276 | 313 |  headers: Map[String, String], | 
| 277 | 314 |  withCredentials: Boolean,  | 
|  | 
0 commit comments