-
Notifications
You must be signed in to change notification settings - Fork 8
/
extensions.coffee
73 lines (58 loc) · 1.77 KB
/
extensions.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Pretend Jadelet is a real package
PACKAGE.dependencies["!jadelet"] =
entryPoint: "main"
distribution:
main: PACKAGE.distribution["lib/jadelet.min"]
# Extend JSON with toBlob method
JSON.toBlob ?= (object, mime="application/json") ->
new Blob [JSON.stringify(object)], type: "#{mime}; charset=utf-8"
# Add some utility readers to the Blob API
Blob::readAsText = ->
file = this
new Promise (resolve, reject) ->
reader = new FileReader
reader.onload = ->
resolve reader.result
reader.onerror = reject
reader.readAsText(file)
Blob::getURL = ->
Promise.resolve URL.createObjectURL(this)
Blob::readAsJSON = ->
@readAsText()
.then JSON.parse
Blob::readAsDataURL = ->
file = this
new Promise (resolve, reject) ->
reader = new FileReader
reader.onload = ->
resolve reader.result
reader.onerror = reject
reader.readAsDataURL(file)
# BlobSham interface must implement getURL and readAs* methods
# Load an image from a blob returning a promise that is fulfilled with the
# loaded image or rejected with an error
Image.fromBlob = (blob) ->
blob.getURL()
.then (url) ->
new Promise (resolve, reject) ->
img = new Image
img.onload = ->
resolve img
img.onerror = reject
img.src = url
FileList::forEach ?= (args...) ->
Array::forEach.apply(this, args)
# Event#path polyfill for Firefox
unless "path" in Event.prototype
Object.defineProperty Event.prototype, "path",
get: ->
path = []
currentElem = this.target
while currentElem
path.push currentElem
currentElem = currentElem.parentElement
if path.indexOf(window) is -1 && path.indexOf(document) is -1
path.push(document)
if path.indexOf(window) is -1
path.push(window)
path