Browse Source

support coffeescript too

Andrew Cantino 10 years ago
parent
commit
5ea7e240f0

+ 1 - 0
app/assets/javascripts/ace.js.coffee

@@ -1,3 +1,4 @@
 #= require ace/ace
 #= require ace/mode-javascript.js
 #= require ace/mode-markdown.js
+#= require ace/mode-coffee.js

+ 12 - 5
app/assets/javascripts/pages/agent-edit-page.js.coffee

@@ -134,17 +134,24 @@ class @AgentEditPage
       unless $(this).data('initialized')
         $(this).data('initialized', true)
         $source = $($(this).data('source')).hide()
-        syntax = $(this).data('syntax')
         editor = ace.edit(this)
         $(this).data('ace-editor', editor)
         editor.getSession().setTabSize(2)
         editor.getSession().setUseSoftTabs(true)
         editor.getSession().setUseWrapMode(false)
         editor.setTheme("ace/theme/chrome")
-        if syntax == 'javascript'
-          editor.getSession().setMode("ace/mode/javascript")
-        else
-          editor.getSession().setMode("ace/mode/text")
+
+        setSyntax = ->
+          syntax = $("[name='agent[options][language]']").val()
+          if syntax == 'JavaScript'
+            editor.getSession().setMode("ace/mode/javascript")
+          else if syntax == 'CoffeeScript'
+            editor.getSession().setMode("ace/mode/coffee")
+          else
+            editor.getSession().setMode("ace/mode/text")
+
+        $("[name='agent[options][language]']").on 'change', setSyntax
+        setSyntax()
 
         editor.getSession().setValue($source.val())
 

+ 1 - 1
app/concerns/form_configurable.rb

@@ -32,7 +32,7 @@ module FormConfigurable
       options = args.extract_options!.reverse_merge(roles: [], type: :string)
 
       if args.all? { |arg| arg.is_a?(Symbol) }
-        options.assert_valid_keys([:type, :roles, :values, :syntax])
+        options.assert_valid_keys([:type, :roles, :values, :ace])
       end
 
       if options[:type] == :array && (options[:values].blank? || !options[:values].is_a?(Array))

+ 11 - 5
app/models/agents/java_script_agent.rb

@@ -27,7 +27,8 @@ module Agents
       * `this.error(message)`
     MD
 
-    form_configurable :code, type: :text, syntax: :javascript
+    form_configurable :language, type: :array, values: %w[JavaScript CoffeeScript]
+    form_configurable :code, type: :text, ace: true
     form_configurable :expected_receive_period_in_days
     form_configurable :expected_update_period_in_days
 
@@ -85,9 +86,10 @@ module Agents
       JS
 
       {
-        "code" => Utils.unindent(js_code),
-        'expected_receive_period_in_days' => "2",
-        'expected_update_period_in_days' => "2"
+        'code' => Utils.unindent(js_code),
+        'language' => 'JavaScript',
+        'expected_receive_period_in_days' => '2',
+        'expected_update_period_in_days' => '2'
       }
     end
 
@@ -111,7 +113,11 @@ module Agents
         end
       end
 
-      context.eval(code)
+      if (options['language'] || '').downcase == 'coffeescript'
+        context.eval(CoffeeScript.compile code)
+      else
+        context.eval(code)
+      end
       context.eval("Agent.#{js_function}();")
     end
 

+ 2 - 2
app/presenters/form_configurable_agent_presenter.rb

@@ -22,8 +22,8 @@ class FormConfigurableAgentPresenter < Decorator
     when :text
       @view.content_tag 'div' do
         @view.concat @view.text_area_tag("agent[options][#{attribute}]", value, html_options.merge(class: 'form-control', rows: 3))
-        if data[:syntax].present?
-          @view.concat @view.content_tag('div', '', class: 'ace-editor', data: { source: "[name='agent[options][#{attribute}]']", syntax: data[:syntax] })
+        if data[:ace].present?
+          @view.concat @view.content_tag('div', '', class: 'ace-editor', data: { source: "[name='agent[options][#{attribute}]']" })
         end
       end
     when :boolean