antos-frontend/src/core/api.coffee

122 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2017-08-26 16:50:13 +02:00
self.OS.API =
2017-08-07 00:49:24 +02:00
# the handler object could be a any remote or local handle to
# fetch user data, used by the API to make requests
# handlers are defined in /src/handlers
2018-01-23 18:16:41 +01:00
handler: { }
2017-08-07 00:49:24 +02:00
#request a user data
2017-08-26 16:50:13 +02:00
post: (p, d, c, f) ->
q = _courrier.getMID()
_API.loading q, p
2017-08-27 23:40:02 +02:00
2017-08-26 16:50:13 +02:00
$.ajax {
2017-08-27 23:40:02 +02:00
type: 'POST',
2017-08-26 16:50:13 +02:00
url: p,
2017-08-27 23:40:02 +02:00
contentType: 'application/json',
data: JSON.stringify d,
dataType: 'json',
success: null
2017-08-26 16:50:13 +02:00
}
2017-08-27 23:40:02 +02:00
#$.getJSON p, d
2017-08-26 16:50:13 +02:00
.done (data) ->
_API.loaded q, p, "OK"
c(data)
.fail (e, s) ->
_API.loaded q, p, "FAIL"
f(e, s)
2018-01-25 19:15:41 +01:00
blob: (p, c, f) ->
q = _courrier.getMID()
r = new XMLHttpRequest()
r.open "GET", p, true
r.responseType = "arraybuffer"
r.onload = (e) ->
if @status is 200 and @readyState is 4
c @response
_API.loaded q, p, "OK"
else
f e, @
_API.loaded q, p, "FAIL"
_API.loading q, p
r.send()
upload: (p, d, c, f) ->
q = _courrier.getMID()
#insert a temporal file selector
o = ($ '<input>').attr('type', 'file').css("display", "none")
o.change () ->
_API.loading q, p
formd = new FormData()
formd.append 'path', d
# TODO: only one file is selected at this time
formd.append 'upload', o[0].files[0]
$.ajax {
url: p,
data: formd,
type: 'POST',
contentType: false,
processData: false,
}
.done (data) ->
_API.loaded q, p, "OK"
c(data)
.fail (e, s) ->
_API.loaded q, p, "FAIL"
f(e, s)
o.click()
saveblob: (name, b) ->
url = window.URL.createObjectURL b
o = ($ '<a>')
.attr("href", url)
.attr("download", name)
.css("display", "none")
.appendTo("body")
o[0].click()
window.URL.revokeObjectURL(url)
o.remove()
2017-08-07 00:49:24 +02:00
systemConfig: ->
_API.request 'config', (result) ->
console.log result
2017-08-26 16:50:13 +02:00
loading: (q, p) ->
_courrier.trigger "loading", { id: q, data: { m: "#{p}", s: true }, name: "OS" }
loaded: (q, p, m ) ->
_courrier.trigger "loaded", { id: q, data: { m: "#{m}: #{p}", s: false }, name: "OS" }
get: (p, c, f) ->
2017-08-26 16:50:13 +02:00
q = _courrier.getMID()
_API.loading q, p
$.get p
2017-08-26 16:50:13 +02:00
.done (data) ->
_API.loaded q, p, "OK"
c(data)
.fail (e, s) ->
_API.loaded q, p, "FAIL"
f(e, s)
script: (p, c, f) ->
q = _courrier.getMID()
_API.loading q, p
$.getScript p
.done (data) ->
_API.loaded q, p, "OK"
c(data)
.fail (e, s) ->
_API.loaded q, p, "FAIL"
f(e, s)
2017-08-27 23:40:02 +02:00
resource: (r, c, f) ->
path = "resources/#{r}"
_API.get path, c, f
2018-01-25 19:15:41 +01:00
2018-01-24 01:03:14 +01:00
throwe: (n) ->
err = undefined
try
throw new Error(n)
catch e
err = e
return "" if not err
return err.stack
2017-08-11 01:58:46 +02:00