2018-02-18 21:11:49 +01:00
|
|
|
String.prototype.hash = () ->
|
|
|
|
hash = 5381
|
|
|
|
i = this.length
|
|
|
|
hash = (hash * 33) ^ this.charCodeAt(--i) while i
|
|
|
|
hash >>> 0
|
|
|
|
|
|
|
|
String.prototype.asBase64 = () ->
|
|
|
|
tmp = encodeURIComponent this
|
|
|
|
return btoa ( tmp.replace /%([0-9A-F]{2})/g, (match, p1) ->
|
|
|
|
return String.fromCharCode (parseInt p1, 16)
|
|
|
|
)
|
|
|
|
String.prototype.unescape = () ->
|
|
|
|
d = @
|
|
|
|
d = d.replace /\\\\/g, "\\"
|
|
|
|
d = d.replace /\\"/g, '"'
|
|
|
|
d = d.replace /\\n/g, "\n"
|
|
|
|
d = d.replace /\\t/g, "\t"
|
|
|
|
d = d.replace /\\b/g, "\b"
|
|
|
|
d = d.replace /\\f/g, "\f"
|
|
|
|
d = d.replace /\\r/g, "\r"
|
|
|
|
d
|
|
|
|
|
|
|
|
Date.prototype.toString = () ->
|
|
|
|
dd = @getDate()
|
|
|
|
mm = @getMonth() + 1
|
|
|
|
yyyy = @getFullYear()
|
|
|
|
hh = @getHours()
|
|
|
|
mi = @getMinutes()
|
|
|
|
se = @getSeconds()
|
|
|
|
|
|
|
|
dd = "0#{dd}" if dd < 10
|
|
|
|
mm = "0#{mm}" if mm < 10
|
|
|
|
hh = "0#{hh}" if hh < 10
|
|
|
|
mi = "0#{mi}" if mi < 10
|
|
|
|
se = "0#{se}" if se < 10
|
|
|
|
return "#{dd}/#{mm}/#{yyyy} #{hh}:#{mi}:#{se}"
|
|
|
|
|
|
|
|
Date.prototype.timestamp = () ->
|
|
|
|
return @getTime() / 1000 | 0
|
|
|
|
|
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: { }
|
2018-02-16 18:38:14 +01:00
|
|
|
shared: {} # shared libraries
|
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,
|
2018-01-29 19:16:29 +01:00
|
|
|
dataType: 'json',
|
2017-08-27 23:40:02 +02:00
|
|
|
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" }
|
2018-01-29 19:16:29 +01:00
|
|
|
get: (p, c, f, t) ->
|
|
|
|
conf =
|
|
|
|
type: 'GET',
|
|
|
|
url: p,
|
|
|
|
conf.dataType = t if t
|
|
|
|
|
2017-08-26 16:50:13 +02:00
|
|
|
q = _courrier.getMID()
|
|
|
|
_API.loading q, p
|
2018-01-29 19:16:29 +01:00
|
|
|
$.ajax conf
|
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-29 19:16:29 +01:00
|
|
|
|
2018-02-27 16:40:36 +01:00
|
|
|
libready: (l) ->
|
|
|
|
return _API.shared[l] || false
|
|
|
|
|
2018-02-16 18:38:14 +01:00
|
|
|
require: (l,f) ->
|
|
|
|
if not _API.shared[l]
|
2018-02-27 16:40:36 +01:00
|
|
|
if l.match /^(https?:\/\/[^\s]+)/g
|
|
|
|
_API.script l, () ->
|
|
|
|
_API.shared[l] = true
|
|
|
|
_courrier.trigger "sharedlibraryloaded", l
|
|
|
|
f() if f
|
|
|
|
, (e, s) ->
|
|
|
|
_courrier.oserror "Cannot load 3rd library at: #{l}", e, r
|
|
|
|
else
|
|
|
|
path = "os:///scripts/"
|
|
|
|
js = "#{path}#{l}.js"
|
|
|
|
js.asFileHandler().onready (d) ->
|
|
|
|
_API.shared[l] = true
|
|
|
|
el = $ '<script>', { src: "#{_API.handler.get}/#{js}" }
|
|
|
|
.appendTo 'head'
|
|
|
|
#load css file
|
|
|
|
css = "#{path}#{l}.css"
|
|
|
|
css.asFileHandler().onready (d) ->
|
|
|
|
el = $ '<link>', { rel: 'stylesheet', type: 'text/css', 'href': "#{_API.handler.get}/#{css}" }
|
|
|
|
.appendTo 'head'
|
|
|
|
, () ->
|
|
|
|
console.log "loaded", l
|
|
|
|
_courrier.trigger "sharedlibraryloaded", l
|
|
|
|
f() if f
|
2018-02-16 18:38:14 +01:00
|
|
|
else
|
|
|
|
console.log l, "Library exist, no need to load"
|
|
|
|
_courrier.trigger "sharedlibraryloaded", l
|
|
|
|
|
|
|
|
requires:(libs, f) ->
|
|
|
|
return f() unless libs.length > 0
|
|
|
|
_courrier.observable.one "sharedlibraryloaded", (l) ->
|
|
|
|
libs.splice 0, 1
|
|
|
|
_API.requires libs, f
|
|
|
|
_API.require libs[0], null
|
2018-01-29 19:16:29 +01:00
|
|
|
packages:
|
|
|
|
fetch: (f) ->
|
|
|
|
_API.handler.packages {
|
|
|
|
command: "list", args: { paths: _OS.setting.system.pkgpaths }
|
|
|
|
}, f
|
2018-02-01 19:36:09 +01:00
|
|
|
cache: (f) ->
|
|
|
|
_API.handler.packages {
|
|
|
|
command: "cache", args: { paths: _OS.setting.system.pkgpaths }
|
|
|
|
}, 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
|
2018-01-26 18:57:28 +01:00
|
|
|
return err
|
2017-08-11 01:58:46 +02:00
|
|
|
|