Start on directory change events

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-04-25 11:33:44 -06:00
parent ddc29f294c
commit 3f97d98ef9
6 changed files with 58 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
_ = require 'underscore'
fs = require 'fs'
File = require 'file'
EventEmitter = require 'event-emitter'
module.exports =
class Directory
@@ -18,3 +20,18 @@ class Directory
files.push(new File(path))
directories.concat(files)
afterSubscribe: ->
@subscribeToNativeChangeEvents() if @subscriptionCount() == 1
afterUnsubscribe: ->
@unsubscribeFromNativeChangeEvents() if @subscriptionCount() == 0
subscribeToNativeChangeEvents: ->
$native.watchPath @path, (eventTypes) =>
@trigger 'contents-change' if eventTypes.modified?
unsubscribeFromNativeChangeEvents: ->
$native.unwatchPath(@path)
_.extend Directory.prototype, EventEmitter

View File

@@ -14,6 +14,8 @@ module.exports =
@eventHandlersByNamespace[namespace][eventName] ?= []
@eventHandlersByNamespace[namespace][eventName].push(handler)
@afterSubscribe?()
trigger: (eventName, event) ->
[eventName, namespace] = eventName.split('.')
@@ -43,6 +45,8 @@ module.exports =
else
delete @eventHandlersByEventName?[eventName]
@afterUnsubscribe?()
subscriptionCount: ->
count = 0
for name, handlers of @eventHandlersByEventName

View File

@@ -10,9 +10,10 @@ class Project
constructor: (@path) ->
@buffers = []
@rootDirectory = new Directory(@path)
getRootDirectory: ->
new Directory(@path)
@rootDirectory
getFilePaths: ->
projectPath = @path
@@ -36,6 +37,9 @@ class Project
filePath = fs.join(@path, filePath) unless filePath[0] == '/'
fs.absolute filePath
relativize: (fullPath) ->
fullPath.replace(@path, "")
bufferWithId: (id) ->
return buffer for buffer in @buffers when buffer.id == id

View File

@@ -117,7 +117,7 @@ class RootView extends View
@fileFinder = null
else
@project.getFilePaths().done (paths) =>
relativePaths = (path.replace(@project.path, "") for path in paths)
relativePaths = (@project.relativize(path) for path in paths)
@fileFinder = new FileFinder
paths: relativePaths
selected: (relativePath) => @open(relativePath)

View File

@@ -87,6 +87,7 @@ class DirectoryView extends View
@disclosureArrow.on 'click', => @toggleExpansion()
buildEntries: ->
@entries?.remove()
@entries = $$ -> @ol class: 'entries'
for entry in @directory.getEntries()
if entry instanceof Directory
@@ -105,6 +106,7 @@ class DirectoryView extends View
@buildEntries()
@deserializeEntryExpansionsStates(@entryStates) if @entryStates?
@isExpanded = true
@watchEntries()
false
collapse: ->
@@ -113,8 +115,15 @@ class DirectoryView extends View
@disclosureArrow.text('')
@entries.remove()
@entries = null
@unwatchEntries()
@isExpanded = false
watchEntries: ->
@directory.on "contents-change.#{@directory.path}", => @buildEntries()
unwatchEntries: ->
@directory.off ".#{@directory.path}"
serializeEntryExpansionStates: ->
entryStates = {}
@entries.find('> .directory.expanded').each ->