$ = require 'jquery' _ = require 'underscore' {activeWindow} = require 'app' File = require 'fs' Pane = require 'pane' module.exports = class Project extends Pane showing: false position: 'left' html: $ require "project/project.html" keymap: -> 'Command-Ctrl-N': 'toggle' initialize: -> @reload(File.workingDirectory()) @editor = activeWindow.document activeWindow.project = this @editor.ace.on 'open', ({filename}) => if File.isDirectory filename @reload filename else openedPaths = @get 'openedPaths', [] if not _.include openedPaths, filename openedPaths.push filename @set 'openedPaths', openedPaths @editor.ace.on 'close', ({filename}) => if File.isFile filename openedPaths = @get 'openedPaths', [] openedPaths = _.without openedPaths, filename @set 'openedPaths', openedPaths @editor.ace.on 'loaded', => # Reopen files (remove ones that no longer exist) openedPaths = @get 'openedPaths', [] for path in openedPaths if File.isFile path @editor.open path else if not File.exists path openedPaths = _.without openedPaths, path @set 'openedPaths', openedPaths $('#project li').live 'click', (event) => $('#project .active').removeClass 'active' el = $(event.currentTarget) path = decodeURIComponent el.attr 'path' if File.isDirectory path openedPaths = @get 'openedPaths', [] if el.hasClass 'open' openedPaths = _.without openedPaths, path el.removeClass 'open' el.children("ul").remove() else openedPaths.push path unless _.include openedPaths, path el.addClass 'open' list = @createList path el.append list @set 'openedPaths', openedPaths else el.addClass 'active' activeWindow.open path false # Don't bubble! storageNamespace: -> @.constructor.name + @dir reload: (dir) -> @dir = dir @html.children('#project .cwd').text _.last @dir.split '/' fileList = @createList @dir fileList.addClass('files') @html.children('#project .files').replaceWith(fileList) createList: (dir) -> paths = File.list dir list = $('