src/tools/coffee_script_editor.coffee in luca-0.9.2 vs src/tools/coffee_script_editor.coffee in luca-0.9.4
- old
+ new
@@ -1,19 +1,21 @@
_.def("Luca.tools.CoffeeEditor").extends("Luca.tools.CodeMirrorField").with
name : "coffeescript_editor"
+ autoCompile: true
+
compileOptions:
bare: true
hooks:[
"editor:change"
]
initialize: (@options)->
Luca.tools.CodeMirrorField::initialize.apply(@, arguments)
- _.bindAll(@, "editorChange")
+ _.bindAll(@, "editorChange", "toggleSource")
editor = @
@state = new Luca.Model
currentMode: "coffeescript"
@@ -28,10 +30,16 @@
model.set('javascript',compiled)
@state.bind "change:javascript", (model)->
editor.onJavascriptChange?.call(editor, model.get('javascript') )
+ @state.bind "change:currentMode", (model)->
+ if model.get('currentMode') is "javascript"
+ editor.setValue model.get('javascript')
+ else
+ editor.setValue model.get('coffeescript')
+
compile: (code, callback)->
response = {}
code ||= @getValue()
try
@@ -46,15 +54,29 @@
response =
success: false
compiled: ''
message: error.message
+ toggleMode: ()->
+ if @currentMode() is "coffeescript"
+ @state.set('currentMode', 'javascript')
+ else if @currentMode() is "javascript"
+ @state.set('currentMode', 'coffeescript')
+
currentMode: ()->
@state.get("currentMode")
- fixTabs: ()->
- value = @getValue(false)
- debugger
+ getCoffeescript: ()->
+ @state.get("coffeescript")
+
+ getJavascript: (recompile=false)->
+ js = @state.get("javascript")
+ if recompile is true or js?.length is 0
+ results = @compile( @getCoffeescript() )
+ js = results?.compiled
+
+ js
+
editorChange: ()->
- @fixTabs()
- @state.set( @currentMode(), @getValue() )
+ if @autoCompile is true
+ @state.set( @currentMode(), @getValue() )