Refactor AddDialog and MoveDialog into a single Dialog class

This commit is contained in:
Nathan Sobo 2012-05-08 13:16:52 -06:00
parent 9215d7f083
commit b528196d1b
7 changed files with 78 additions and 96 deletions

View File

@ -421,7 +421,7 @@ describe "TreeView", ->
beforeEach ->
fileView.click()
treeView.trigger "tree-view:add"
addDialog = rootView.find(".add-dialog").view()
addDialog = rootView.find(".tree-view-dialog").view()
describe "when a file is selected", ->
it "opens an add dialog with the file's current directory path populated", ->
@ -486,7 +486,7 @@ describe "TreeView", ->
addDialog.cancel()
dirView.click()
treeView.trigger "tree-view:add"
addDialog = rootView.find(".add-dialog").view()
addDialog = rootView.find(".tree-view-dialog").view()
expect(addDialog).toExist()
expect(addDialog.prompt.text()).toBeTruthy()
@ -500,7 +500,7 @@ describe "TreeView", ->
addDialog.cancel()
treeView.root.click()
treeView.trigger "tree-view:add"
addDialog = rootView.find(".add-dialog").view()
addDialog = rootView.find(".tree-view-dialog").view()
expect(addDialog.miniEditor.getText().length).toBe 0
@ -511,23 +511,23 @@ describe "TreeView", ->
beforeEach ->
fileView.click()
treeView.trigger "tree-view:move"
moveDialog = rootView.find(".move-dialog").view()
moveDialog = rootView.find(".tree-view-dialog").view()
it "opens a move dialog with the file's current path (excluding extension) populated", ->
extension = fs.extension(filePath)
fileNameWithoutExtension = fs.base(filePath, extension)
expect(moveDialog).toExist()
expect(moveDialog.prompt.text()).toBe "Enter the new path for the file:"
expect(moveDialog.editor.getText()).toBe(project.relativize(filePath))
expect(moveDialog.editor.getSelectedText()).toBe fs.base(fileNameWithoutExtension)
expect(moveDialog.editor.isFocused).toBeTruthy()
expect(moveDialog.miniEditor.getText()).toBe(project.relativize(filePath))
expect(moveDialog.miniEditor.getSelectedText()).toBe fs.base(fileNameWithoutExtension)
expect(moveDialog.miniEditor.isFocused).toBeTruthy()
describe "when the path is changed and confirmed", ->
describe "when all the directories along the new path exist", ->
it "moves the file, updates the tree view, and closes the dialog", ->
runs ->
newPath = fs.join(rootDirPath, 'renamed-test-file.txt')
moveDialog.editor.setText(newPath)
moveDialog.miniEditor.setText(newPath)
moveDialog.trigger 'tree-view:confirm'
@ -547,7 +547,7 @@ describe "TreeView", ->
it "creates the target directory before moving the file", ->
runs ->
newPath = fs.join(rootDirPath, 'new-directory', 'renamed-test-file.txt')
moveDialog.editor.setText(newPath)
moveDialog.miniEditor.setText(newPath)
moveDialog.trigger 'tree-view:confirm'

View File

@ -11,6 +11,6 @@ window.keymap.bindKeys '.tree-view'
'backspace': 'tree-view:remove'
'alt-tab': 'tree-view:unfocus'
window.keymap.bindKeys '.move-dialog .mini.editor, .add-dialog .mini.editor'
window.keymap.bindKeys '.tree-view-dialog .mini.editor'
'enter': 'tree-view:confirm'
'escape': 'tree-view:cancel'

View File

@ -1,44 +0,0 @@
{View, $$} = require 'space-pen'
Editor = require 'editor'
fs = require 'fs'
$ = require 'jquery'
module.exports =
class AddDialog extends View
miniEditor: null
rootView: null
path: null
@content: ->
@div class: 'add-dialog', =>
@div "Enter the path for the new file/dir. Dirs end with '/':", outlet: 'prompt'
@subview 'miniEditor', new Editor(mini: true)
initialize: (@rootView, @path) ->
@miniEditor.focus()
@on 'tree-view:confirm', => @confirm()
@on 'tree-view:cancel', => @cancel()
@miniEditor.on 'focusout', => @remove()
directoryPath = if fs.isFile(@path) then fs.directory(@path) else @path
relativePath = @rootView.project.relativize(directoryPath)
relativePath += '/' if relativePath.length > 0
@miniEditor.setText(relativePath)
confirm: ->
relativePath = @miniEditor.getText()
endsWithDirectorySeperator = /\/$/.test(relativePath)
path = @rootView.project.resolve(relativePath)
if endsWithDirectorySeperator
fs.makeDirectory(path)
else
fs.write(path, "")
@rootView.open(path)
@remove()
$('#root-view').focus()
cancel: ->
@remove()
$('.tree-view').focus()

View File

@ -0,0 +1,34 @@
{View, $$} = require 'space-pen'
Editor = require 'editor'
fs = require 'fs'
$ = require 'jquery'
module.exports =
class Dialog extends View
@content: ({prompt}) ->
@div class: 'tree-view-dialog', =>
@div prompt, outlet: 'prompt'
@subview 'miniEditor', new Editor(mini: true)
initialize: ({path, @onConfirm, select}) ->
@miniEditor.focus()
@on 'tree-view:confirm', => @confirm()
@on 'tree-view:cancel', => @cancel()
@miniEditor.on 'focusout', => @remove()
@miniEditor.setText(path)
if select
extension = fs.extension(path)
baseName = fs.base(path)
range = [[0, path.length - baseName.length], [0, path.length - extension.length]]
@miniEditor.setSelectionBufferRange(range)
confirm: ->
@onConfirm(@miniEditor.getText())
@remove()
$('#root-view').focus()
cancel: ->
@remove()
$('.tree-view').focus()

View File

@ -1,37 +0,0 @@
{View, $$} = require 'space-pen'
Editor = require 'editor'
fs = require 'fs'
$ = require 'jquery'
module.exports =
class MoveDialog extends View
@content: ->
@div class: 'move-dialog', =>
@div "Enter the new path for the file:", outlet: 'prompt'
@subview 'editor', new Editor(mini: true)
initialize: (@project, @path) ->
@editor.focus()
@on 'tree-view:confirm', => @confirm()
@on 'tree-view:cancel', => @cancel()
@editor.on 'focusout', => @remove()
relativePath = @project.relativize(@path)
@editor.setText(relativePath)
extension = fs.extension(path)
baseName = fs.base(path)
range = [[0, relativePath.length - baseName.length], [0, relativePath.length - extension.length]]
@editor.setSelectionBufferRange(range)
confirm: ->
path = @project.resolve(@editor.getText())
directoryPath = fs.directory(path)
fs.makeDirectory(directoryPath) unless fs.exists(directoryPath)
fs.move(@path, path)
@remove()
$('#root-view').focus()
cancel: ->
@remove()
$('.tree-view').focus()

View File

@ -2,8 +2,7 @@
Directory = require 'directory'
DirectoryView = require 'tree-view/directory-view'
FileView = require 'tree-view/file-view'
MoveDialog = require 'tree-view/move-dialog'
AddDialog = require 'tree-view/add-dialog'
Dialog = require 'tree-view/dialog'
Native = require 'native'
fs = require 'fs'
$ = require 'jquery'
@ -143,7 +142,19 @@ class TreeView extends View
moveSelectedEntry: ->
entry = @selectedEntry()
return unless entry
@rootView.append(new MoveDialog(@rootView.project, entry.getPath()))
oldPath = @selectedEntry().getPath()
dialog = new Dialog
prompt: "Enter the new path for the file:"
path: @rootView.project.relativize(oldPath)
select: true
onConfirm: (newPath) =>
newPath = @rootView.project.resolve(newPath)
directoryPath = fs.directory(newPath)
fs.makeDirectory(directoryPath) unless fs.exists(directoryPath)
fs.move(oldPath, newPath)
@rootView.append(dialog)
removeSelectedEntry: ->
entry = @selectedEntry()
@ -161,7 +172,25 @@ class TreeView extends View
Native.alert message, detailedMessage, buttons
add: ->
@rootView.append(new AddDialog(@rootView, @selectedEntry().getPath()))
selectedPath = @selectedEntry().getPath()
directoryPath = if fs.isFile(selectedPath) then fs.directory(selectedPath) else selectedPath
relativeDirectoryPath = @rootView.project.relativize(directoryPath)
relativeDirectoryPath += '/' if relativeDirectoryPath.length > 0
dialog = new Dialog
prompt: "Enter the path for the new file/directory. Directories end with '/':"
path: relativeDirectoryPath
select: false
onConfirm: (relativePath) =>
endsWithDirectorySeperator = /\/$/.test(relativePath)
path = @rootView.project.resolve(relativePath)
if endsWithDirectorySeperator
fs.makeDirectory(path)
else
fs.write(path, "")
@rootView.open(path)
@rootView.append(dialog)
selectedEntry: ->
@find('.selected')?.view()

View File

@ -36,7 +36,7 @@
color: black;
}
.add-dialog, .move-dialog {
.tree-view-dialog {
position: absolute;
bottom: 0;
width: 100%;