support coffeescript too

Andrew Cantino 10 years ago
parent
commit
5ea7e240f0

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

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

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

@@ -134,17 +134,24 @@ class @AgentEditPage
134 134
       unless $(this).data('initialized')
135 135
         $(this).data('initialized', true)
136 136
         $source = $($(this).data('source')).hide()
137
-        syntax = $(this).data('syntax')
138 137
         editor = ace.edit(this)
139 138
         $(this).data('ace-editor', editor)
140 139
         editor.getSession().setTabSize(2)
141 140
         editor.getSession().setUseSoftTabs(true)
142 141
         editor.getSession().setUseWrapMode(false)
143 142
         editor.setTheme("ace/theme/chrome")
144
-        if syntax == 'javascript'
145
-          editor.getSession().setMode("ace/mode/javascript")
146
-        else
147
-          editor.getSession().setMode("ace/mode/text")
143
+
144
+        setSyntax = ->
145
+          syntax = $("[name='agent[options][language]']").val()
146
+          if syntax == 'JavaScript'
147
+            editor.getSession().setMode("ace/mode/javascript")
148
+          else if syntax == 'CoffeeScript'
149
+            editor.getSession().setMode("ace/mode/coffee")
150
+          else
151
+            editor.getSession().setMode("ace/mode/text")
152
+
153
+        $("[name='agent[options][language]']").on 'change', setSyntax
154
+        setSyntax()
148 155
 
149 156
         editor.getSession().setValue($source.val())
150 157
 

+ 1 - 1
app/concerns/form_configurable.rb

@@ -32,7 +32,7 @@ module FormConfigurable
32 32
       options = args.extract_options!.reverse_merge(roles: [], type: :string)
33 33
 
34 34
       if args.all? { |arg| arg.is_a?(Symbol) }
35
-        options.assert_valid_keys([:type, :roles, :values, :syntax])
35
+        options.assert_valid_keys([:type, :roles, :values, :ace])
36 36
       end
37 37
 
38 38
       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
27 27
       * `this.error(message)`
28 28
     MD
29 29
 
30
-    form_configurable :code, type: :text, syntax: :javascript
30
+    form_configurable :language, type: :array, values: %w[JavaScript CoffeeScript]
31
+    form_configurable :code, type: :text, ace: true
31 32
     form_configurable :expected_receive_period_in_days
32 33
     form_configurable :expected_update_period_in_days
33 34
 
@@ -85,9 +86,10 @@ module Agents
85 86
       JS
86 87
 
87 88
       {
88
-        "code" => Utils.unindent(js_code),
89
-        'expected_receive_period_in_days' => "2",
90
-        'expected_update_period_in_days' => "2"
89
+        'code' => Utils.unindent(js_code),
90
+        'language' => 'JavaScript',
91
+        'expected_receive_period_in_days' => '2',
92
+        'expected_update_period_in_days' => '2'
91 93
       }
92 94
     end
93 95
 
@@ -111,7 +113,11 @@ module Agents
111 113
         end
112 114
       end
113 115
 
114
-      context.eval(code)
116
+      if (options['language'] || '').downcase == 'coffeescript'
117
+        context.eval(CoffeeScript.compile code)
118
+      else
119
+        context.eval(code)
120
+      end
115 121
       context.eval("Agent.#{js_function}();")
116 122
     end
117 123
 

+ 2 - 2
app/presenters/form_configurable_agent_presenter.rb

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