Add save alert when closing a modified buffer

This commit is contained in:
Corey Johnson 2012-06-12 09:20:04 -07:00
parent 0fec1fc5ba
commit e56ef733ad
2 changed files with 22 additions and 6 deletions

View File

@ -1690,8 +1690,9 @@ describe "Editor", ->
describe "when buffer is modified", ->
it "triggers alert and does not close session", ->
originalBuffer = editor.buffer
spyOn(editor, 'remove')
spyOn($native, 'alert')
editor.insertText("I AM CHANGED!")
editor.trigger "close"
expect(editor.buffer).toBe originalBuffer
expect(editor.remove).not.toHaveBeenCalled()
expect($native.alert).toHaveBeenCalled()

View File

@ -7,6 +7,8 @@ Range = require 'range'
EditSession = require 'edit-session'
CursorView = require 'cursor-view'
SelectionView = require 'selection-view'
Native = require 'native'
fs = require 'fs'
$ = require 'jquery'
_ = require 'underscore'
@ -605,11 +607,13 @@ class Editor extends View
save: ->
if not @buffer.getPath()
path = $native.saveDialog()
return if not path
return false if not path
@buffer.saveAs(path)
else
@buffer.save()
true
clipScreenPosition: (screenPosition, options={}) ->
@renderer.clipScreenPosition(screenPosition, options)
@ -784,8 +788,19 @@ class Editor extends View
close: ->
return if @mini
return if @buffer.isModified()
@removeActiveEditSession()
if @buffer.isModified()
filename = if @buffer.getPath() then fs.base(@buffer.getPath()) else "untitled buffer"
message = "'#{filename}' has changes, do you want to save them?"
detailedMessage = "Your changes will be lost if you don't save them"
buttons = [
["Save", => @save() and @removeActiveEditSession()]
["Cancel", =>]
["Don't save", => @removeActiveEditSession()]
]
Native.alert message, detailedMessage, buttons
else
@removeActiveEditSession()
unsubscribeFromBuffer: ->
@buffer.off ".editor#{@id}"