|
|
@ -4,31 +4,40 @@ var $ = require('jquery') |
|
|
|
var remixLib = require('remix-lib') |
|
|
|
var remixLib = require('remix-lib') |
|
|
|
var yo = require('yo-yo') |
|
|
|
var yo = require('yo-yo') |
|
|
|
var EventManager = remixLib.EventManager |
|
|
|
var EventManager = remixLib.EventManager |
|
|
|
|
|
|
|
var globalRegistry = require('../../global/registry') |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
attach to files event (removed renamed) |
|
|
|
attach to files event (removed renamed) |
|
|
|
opt needs: |
|
|
|
|
|
|
|
- filesProviders |
|
|
|
|
|
|
|
- config |
|
|
|
|
|
|
|
- editor |
|
|
|
|
|
|
|
trigger: currentFileChanged |
|
|
|
trigger: currentFileChanged |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
class FileManager { |
|
|
|
class FileManager { |
|
|
|
constructor (opt = {}) { |
|
|
|
constructor (localRegistry) { |
|
|
|
this.tabbedFiles = {} |
|
|
|
this.tabbedFiles = {} |
|
|
|
this.event = new EventManager() |
|
|
|
this.event = new EventManager() |
|
|
|
|
|
|
|
|
|
|
|
var self = this |
|
|
|
var self = this |
|
|
|
this.opt = opt |
|
|
|
self._components = {} |
|
|
|
this.opt.filesProviders['browser'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
self._components.registry = localRegistry || globalRegistry |
|
|
|
this.opt.filesProviders['localhost'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
self._deps = { |
|
|
|
this.opt.filesProviders['config'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
compilerImport: self._components.registry.get('compilerimport').api, |
|
|
|
this.opt.filesProviders['gist'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
editor: self._components.registry.get('editor').api, |
|
|
|
this.opt.filesProviders['browser'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
config: self._components.registry.get('config').api, |
|
|
|
this.opt.filesProviders['localhost'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
browserExplorer: self._components.registry.get('fileproviders/browser').api, |
|
|
|
this.opt.filesProviders['config'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
localhostExplorer: self._components.registry.get('fileproviders/localhost').api, |
|
|
|
this.opt.filesProviders['gist'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
configExplorer: self._components.registry.get('fileproviders/config').api, |
|
|
|
|
|
|
|
gistExplorer: self._components.registry.get('fileproviders/gist').api, |
|
|
|
|
|
|
|
filesProviders: self._components.registry.get('fileproviders').api |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
|
|
|
|
self._deps.localhostExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
|
|
|
|
self._deps.configExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
|
|
|
|
self._deps.gistExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) |
|
|
|
|
|
|
|
self._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
|
|
|
|
self._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
|
|
|
|
self._deps.configExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
|
|
|
|
self._deps.gistExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) |
|
|
|
|
|
|
|
|
|
|
|
// tabs
|
|
|
|
// tabs
|
|
|
|
var $filesEl = $('#files') |
|
|
|
var $filesEl = $('#files') |
|
|
@ -49,17 +58,18 @@ class FileManager { |
|
|
|
if (Object.keys(self.tabbedFiles).length) { |
|
|
|
if (Object.keys(self.tabbedFiles).length) { |
|
|
|
self.switchFile(Object.keys(self.tabbedFiles)[0]) |
|
|
|
self.switchFile(Object.keys(self.tabbedFiles)[0]) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
opt.editor.displayEmptyReadOnlySession() |
|
|
|
self._deps.editor.displayEmptyReadOnlySession() |
|
|
|
self.opt.config.set('currentFile', '') |
|
|
|
self._deps.config.set('currentFile', '') |
|
|
|
} |
|
|
|
} |
|
|
|
return false |
|
|
|
return false |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileRenamedEvent (oldName, newName, isFolder) { |
|
|
|
fileRenamedEvent (oldName, newName, isFolder) { |
|
|
|
|
|
|
|
var self = this |
|
|
|
if (!isFolder) { |
|
|
|
if (!isFolder) { |
|
|
|
this.opt.config.set('currentFile', '') |
|
|
|
self._deps.config.set('currentFile', '') |
|
|
|
this.opt.editor.discard(oldName) |
|
|
|
self._deps.editor.discard(oldName) |
|
|
|
if (this.tabbedFiles[oldName]) { |
|
|
|
if (this.tabbedFiles[oldName]) { |
|
|
|
delete this.tabbedFiles[oldName] |
|
|
|
delete this.tabbedFiles[oldName] |
|
|
|
this.tabbedFiles[newName] = newName |
|
|
|
this.tabbedFiles[newName] = newName |
|
|
@ -67,12 +77,12 @@ class FileManager { |
|
|
|
this.switchFile(newName) |
|
|
|
this.switchFile(newName) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
var newFocus |
|
|
|
var newFocus |
|
|
|
for (var k in this.opt.tabbedFiles) { |
|
|
|
for (var k in this.tabbedFiles) { |
|
|
|
if (k.indexOf(oldName + '/') === 0) { |
|
|
|
if (k.indexOf(oldName + '/') === 0) { |
|
|
|
var newAbsolutePath = k.replace(oldName, newName) |
|
|
|
var newAbsolutePath = k.replace(oldName, newName) |
|
|
|
this.tabbedFiles[newAbsolutePath] = newAbsolutePath |
|
|
|
this.tabbedFiles[newAbsolutePath] = newAbsolutePath |
|
|
|
delete this.tabbedFiles[k] |
|
|
|
delete this.tabbedFiles[k] |
|
|
|
if (this.opt.config.get('currentFile') === k) { |
|
|
|
if (self._deps.config.get('currentFile') === k) { |
|
|
|
newFocus = newAbsolutePath |
|
|
|
newFocus = newAbsolutePath |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -85,17 +95,19 @@ class FileManager { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentPath () { |
|
|
|
currentPath () { |
|
|
|
var currentFile = this.opt.config.get('currentFile') |
|
|
|
var self = this |
|
|
|
|
|
|
|
var currentFile = self._deps.config.get('currentFile') |
|
|
|
var reg = /(.*\/).*/ |
|
|
|
var reg = /(.*\/).*/ |
|
|
|
var path = reg.exec(currentFile) |
|
|
|
var path = reg.exec(currentFile) |
|
|
|
return path ? path[1] : null |
|
|
|
return path ? path[1] : null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fileRemovedEvent (path) { |
|
|
|
fileRemovedEvent (path) { |
|
|
|
if (path === this.opt.config.get('currentFile')) { |
|
|
|
var self = this |
|
|
|
this.opt.config.set('currentFile', '') |
|
|
|
if (path === self._deps.config.get('currentFile')) { |
|
|
|
|
|
|
|
self._deps.config.set('currentFile', '') |
|
|
|
} |
|
|
|
} |
|
|
|
this.opt.editor.discardCurrentSession() |
|
|
|
self._deps.editor.discardCurrentSession() |
|
|
|
delete this.tabbedFiles[path] |
|
|
|
delete this.tabbedFiles[path] |
|
|
|
this.refreshTabs() |
|
|
|
this.refreshTabs() |
|
|
|
} |
|
|
|
} |
|
|
@ -124,7 +136,7 @@ class FileManager { |
|
|
|
var self = this |
|
|
|
var self = this |
|
|
|
if (file) return _switchFile(file) |
|
|
|
if (file) return _switchFile(file) |
|
|
|
else { |
|
|
|
else { |
|
|
|
var browserProvider = self.opt.filesProviders['browser'] |
|
|
|
var browserProvider = self._.filesProviders['browser'] |
|
|
|
browserProvider.resolveDirectory('browser', (error, filesTree) => { |
|
|
|
browserProvider.resolveDirectory('browser', (error, filesTree) => { |
|
|
|
if (error) console.error(error) |
|
|
|
if (error) console.error(error) |
|
|
|
var fileList = Object.keys(filesTree) |
|
|
|
var fileList = Object.keys(filesTree) |
|
|
@ -132,22 +144,22 @@ class FileManager { |
|
|
|
_switchFile(browserProvider.type + '/' + fileList[0]) |
|
|
|
_switchFile(browserProvider.type + '/' + fileList[0]) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self.event.trigger('currentFileChanged', []) |
|
|
|
self.event.trigger('currentFileChanged', []) |
|
|
|
self.opt.editor.displayEmptyReadOnlySession() |
|
|
|
self._deps.editor.displayEmptyReadOnlySession() |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
function _switchFile (file) { |
|
|
|
function _switchFile (file) { |
|
|
|
self.saveCurrentFile() |
|
|
|
self.saveCurrentFile() |
|
|
|
self.opt.config.set('currentFile', file) |
|
|
|
self._deps.config.set('currentFile', file) |
|
|
|
self.refreshTabs(file) |
|
|
|
self.refreshTabs(file) |
|
|
|
self.fileProviderOf(file).get(file, (error, content) => { |
|
|
|
self.fileProviderOf(file).get(file, (error, content) => { |
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
|
console.log(error) |
|
|
|
console.log(error) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (self.fileProviderOf(file).isReadOnly(file)) { |
|
|
|
if (self.fileProviderOf(file).isReadOnly(file)) { |
|
|
|
self.opt.editor.openReadOnly(file, content) |
|
|
|
self._deps.editor.openReadOnly(file, content) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self.opt.editor.open(file, content) |
|
|
|
self._deps.editor.open(file, content) |
|
|
|
} |
|
|
|
} |
|
|
|
self.event.trigger('currentFileChanged', [file, self.fileProviderOf(file)]) |
|
|
|
self.event.trigger('currentFileChanged', [file, self.fileProviderOf(file)]) |
|
|
|
} |
|
|
|
} |
|
|
@ -165,12 +177,12 @@ class FileManager { |
|
|
|
|
|
|
|
|
|
|
|
fileProviderOf (file) { |
|
|
|
fileProviderOf (file) { |
|
|
|
var provider = file.match(/[^/]*/) |
|
|
|
var provider = file.match(/[^/]*/) |
|
|
|
if (provider !== null && this.opt.filesProviders[provider[0]]) { |
|
|
|
if (provider !== null && this._deps.filesProviders[provider[0]]) { |
|
|
|
return this.opt.filesProviders[provider[0]] |
|
|
|
return this._deps.filesProviders[provider[0]] |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
for (var handler of this.opt.compilerImport.handlers()) { |
|
|
|
for (var handler of this._deps.compilerImport.handlers()) { |
|
|
|
if (handler.match.exec(file)) { |
|
|
|
if (handler.match.exec(file)) { |
|
|
|
return this.opt.filesProviders[handler.type] |
|
|
|
return this._deps.filesProviders[handler.type] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -178,9 +190,9 @@ class FileManager { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
saveCurrentFile () { |
|
|
|
saveCurrentFile () { |
|
|
|
var currentFile = this.opt.config.get('currentFile') |
|
|
|
var currentFile = this._deps.config.get('currentFile') |
|
|
|
if (currentFile && this.opt.editor.current()) { |
|
|
|
if (currentFile && this._deps.editor.current()) { |
|
|
|
var input = this.opt.editor.get(currentFile) |
|
|
|
var input = this._deps.editor.get(currentFile) |
|
|
|
if (input) { |
|
|
|
if (input) { |
|
|
|
var provider = this.fileProviderOf(currentFile) |
|
|
|
var provider = this.fileProviderOf(currentFile) |
|
|
|
if (provider) { |
|
|
|
if (provider) { |
|
|
|