app/assets/javascripts/editor.js.coffee in smalruby-editor-0.0.5 vs app/assets/javascripts/editor.js.coffee in smalruby-editor-0.0.6
- old
+ new
@@ -1,14 +1,25 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
+changed = false
+
$ ->
+ saving = false
+
textEditor = ace.edit('text-editor')
- textEditor.setTheme('ace/theme/clouds')
+ textEditor.setTheme('ace/theme/github')
textEditor.setShowInvisibles(true)
+ textEditor.focus()
+ textEditor.gotoLine(0, 0)
+
+ textEditor.on('change', (e) ->
+ changed = true
+ )
+
session = textEditor.getSession()
session.setMode('ace/mode/ruby')
session.setTabSize(2)
session.setUseSoftTabs(true)
@@ -18,43 +29,77 @@
# をクリックしてもリンク先に遷移しないようにしている。
$('ul.nav li a').click (e) ->
e.preventDefault()
false
- $('#save-button').click ->
+ $('#check-button').click (e) ->
+ e.preventDefault()
+ data =
+ source_code:
+ data: session.getDocument().getValue()
+ success = (data, textStatus, jqXHR) ->
+ for errorInfo in data
+ do (errorInfo) ->
+ msg = $('<div class="alert alert-error" style="display: none">')
+ .append('<button type="button" class="close" data-dismiss="alert">×</button>')
+ .append('<h4><i class="icon-exclamation-sign"></i>エラー</h4>')
+ .append("#{errorInfo.row}行")
+ if errorInfo.column > 0
+ msg.append("、#{errorInfo.column}文字")
+ msg.append(": #{errorInfo.message}")
+ $('#messages').append(msg)
+ msg.fadeIn('slow')
+ $.post('/source_codes/check', data, success, 'json')
+
+ $('#save-button').click (e) ->
+ e.preventDefault()
filename = $.trim($('#filename').val())
if filename.length <= 0
$('#filename').focus()
else
data =
- filename: filename
- source: session.getDocument().getValue()
- location.href = '/editor/save_file?' + $.param(data)
+ source_code:
+ filename: filename
+ data: session.getDocument().getValue()
+ success = (data, textStatus, jqXHR) ->
+ saving = true
+ changed = false
+ $('#download-link').click()
+ $.post('/source_codes/', data, success, 'json')
- $('#load-button').click ->
- $(@).parent().find('input[name="load_file"]').click()
-
$('#filename').keypress (e) ->
e = window.event if !e
if e.keyCode == 13
$('#save-button').click()
false
else
true
+ $('#load-button').click (e) ->
+ e.preventDefault()
+ if changed
+ return unless confirm('まだセーブしていないのでロードするとプログラムが消えてしまうよ!それでもロードしますか?')
+ $(@).parent().find('input[name="source_code[file]"]').click()
+
$('#file-form').fileupload(
dataType: 'json'
- add: (e, data) ->
- data.submit()
done: (e, data) ->
- file = data.result
- if file.error
+ info = data.result.source_code
+ if info.error
msg = $('<div class="alert alert-error" style="display: none">')
.append('<button type="button" class="close" data-dismiss="alert">×</button>')
.append('<h4><i class="icon-exclamation-sign"></i>エラー</h4>')
- .append(file.name + 'は' + file.error)
+ .append(info.filename + 'は' + info.error)
$('#messages').append(msg)
msg.fadeIn('slow').delay(5000).fadeOut('slow')
else
- $('#filename').val(file.name)
- session.getDocument().setValue(file.source)
+ $('#filename').val(info.filename)
+ session.getDocument().setValue(info.data)
+ changed = false
)
+
+ window.onbeforeunload = (event) ->
+ if !saving && session.getDocument().getValue().length > 0
+ return '作成中のプログラムが消えてしまいます。'
+ else
+ saving = false
+ return