mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Start on directory change events
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 ->
|
||||
|
||||
Reference in New Issue
Block a user