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://"
|
|
|
|
@socket = new WebSocket proto + @_api.HOST + "/lua-api/os/apigateway?ws=1"
|
|
|
|
@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
|
|
|
|
|
|
|
|
this.OS.dependencies = ["ace/ace"]
|
|
|
|
this.OS.register "LuaPlayground", LuaPlayground
|