Skip to content

Commit

Permalink
move blob conversion to downloadhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
cornerman committed Aug 20, 2019
1 parent e14ab50 commit 5a39d33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 14 additions & 0 deletions webApp/src/main/scala/wust/webApp/views/DownloadHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ package wust.webApp.views

import org.scalajs.dom
import scala.scalajs.js
import scala.scalajs.js.JSConverters._

object DownloadHelper {

// type: is type of data, e.g. text/csv, application/json
// endings: 'transparent' (keep lineendings as is) or 'native' (convert to system lineendings)
def promptDownload(fileName: String, data: String, `type`: Option[String] = None, endings: Option[String] = None): Unit = {
promptDownload(
fileName,
new dom.Blob(
js.Array(data),
js.Dynamic.literal(`type` = `type`.orUndefined, endings = endings.orUndefined).asInstanceOf[dom.BlobPropertyBag]
)
)
}

def promptDownload(fileName: String, blob: dom.Blob): Unit = {
val elem = dom.document.createElement("a").asInstanceOf[dom.html.Anchor]
val url = dom.URL.createObjectURL(blob)
Expand Down
7 changes: 3 additions & 4 deletions webApp/src/main/scala/wust/webApp/views/TableView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,9 @@ object TableView {
val data = CsvHelper.tableToCsv(node, propertyGroup)
DownloadHelper.promptDownload(
fileName = StringOps.trimToMaxLength(node.str, 50) + ".csv",
blob = new dom.Blob(js.Array(data), js.Dynamic.literal(
`type` = "text/csv",// type of content
endings = "native" // convert lineendings for us to support native os preference
).asInstanceOf[dom.BlobPropertyBag])
data = data,
`type` = Some("text/csv"),
endings = Some("native")
)
}
)
Expand Down

0 comments on commit 5a39d33

Please sign in to comment.