antosdk-apps/LuaPlayground/coffees/main.coffee

62 lines
2.0 KiB
CoffeeScript
Raw Normal View History

2018-04-14 17:41:57 +02:00
class LuaPlayground extends this.OS.GUI.BaseApplication
constructor: ( args ) ->
super "LuaPlayground", args
main: () ->
me = @
@datarea = @find "editorea"
@output = @find "output"
@.editor = ace.edit @datarea
@.editor.setOptions {
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
fontSize: "10pt"
}
@editor.getSession().setUseWrapMode true
@editor.session.setMode "ace/mode/lua"
@editor.setTheme "ace/theme/monokai"
@on "vboxchange", () ->
me.editor.resize()
2018-04-14 19:10:58 +02:00
(@find "log-clear").set "onbtclick", (e) ->
me.log "clean"
2018-04-14 17:41:57 +02:00
@socket = null
2018-04-14 19:10:58 +02:00
@bindKey "CTRL-R", () -> me.run()
2018-04-14 17:41:57 +02:00
menu: () ->
me = @
menu = [{
text: "__(Code)",
child: [
{ text: "__(Run)", dataid: "#{@name}-Run", shortcut: "C-R" }
],
onmenuselect: (e) -> me.run()
}]
menu
log: (t, m) ->
return $(@output).empty() if t is "clean"
p = ($ "<p>").attr("class", t.toLowerCase())[0]
$(p).html "#{t}: #{m.__()}"
($ @output).append p
($ @output).scrollTop @output.scrollHeight
run: () ->
me = @
value = @editor.getValue().trim()
return unless value and value isnt ""
proto = if window.location.protocol is "https:" then "wss://" else "ws://"
2018-09-16 20:29:49 +02:00
@socket = new WebSocket proto + @_api.HOST + "/system/apigateway?ws=1"
2018-04-14 17:41:57 +02:00
@socket.onopen = () ->
#send data to server
me.socket.send( JSON.stringify { code: value } )
@socket.onmessage = (e) -> me.log "INFO", e.data if e.data
@socket.onclose = () ->
me.socket = null
console.log "socket closed"
cleanup: (e)->
@socket.close() if @socket
2018-07-24 19:18:07 +02:00
LuaPlayground.dependencies = ["ace/ace"]
2018-04-14 17:41:57 +02:00
this.OS.register "LuaPlayground", LuaPlayground