Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean app.js (4) #1392

Merged
merged 7 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
file manager
  • Loading branch information
yann300 committed Jul 4, 2018
commit 24e55f721b195e80e959b28ad57bcbc0b1cd10d5
7 changes: 1 addition & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org

// ----------------- file manager ----------------------------

self._components.fileManager = new FileManager({
config: config,
editor: editor,
filesProviders: filesProviders,
compilerImport: self._components.compilerImport
})
self._components.fileManager = new FileManager()
var fileManager = self._components.fileManager
registry.put({api: fileManager, name: 'filemanager'})

Expand Down
84 changes: 48 additions & 36 deletions src/app/files/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,40 @@ var $ = require('jquery')
var remixLib = require('remix-lib')
var yo = require('yo-yo')
var EventManager = remixLib.EventManager
var globalRegistry = require('../../global/registry')

/*
attach to files event (removed renamed)
opt needs:
- filesProviders
- config
- editor
trigger: currentFileChanged
*/

class FileManager {
constructor (opt = {}) {
constructor (localRegistry) {
this.tabbedFiles = {}
this.event = new EventManager()

var self = this
this.opt = opt
this.opt.filesProviders['browser'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this.opt.filesProviders['localhost'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this.opt.filesProviders['config'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this.opt.filesProviders['gist'].event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this.opt.filesProviders['browser'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this.opt.filesProviders['localhost'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this.opt.filesProviders['config'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this.opt.filesProviders['gist'].event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
self._components = {}
self._components.registry = localRegistry || globalRegistry
self._deps = {
compilerImport: self._components.registry.get('compilerimport').api,
editor: self._components.registry.get('editor').api,
config: self._components.registry.get('config').api,
browserExplorer: self._components.registry.get('fileproviders/browser').api,
localhostExplorer: self._components.registry.get('fileproviders/localhost').api,
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
var $filesEl = $('#files')
Expand All @@ -49,30 +58,31 @@ class FileManager {
if (Object.keys(self.tabbedFiles).length) {
self.switchFile(Object.keys(self.tabbedFiles)[0])
} else {
opt.editor.displayEmptyReadOnlySession()
self.opt.config.set('currentFile', '')
self._deps.editor.displayEmptyReadOnlySession()
self._deps.config.set('currentFile', '')
}
return false
})
}

fileRenamedEvent (oldName, newName, isFolder) {
var self = this
if (!isFolder) {
this.opt.config.set('currentFile', '')
this.opt.editor.discard(oldName)
self._deps.config.set('currentFile', '')
self._deps.editor.discard(oldName)
if (this.tabbedFiles[oldName]) {
delete this.tabbedFiles[oldName]
this.tabbedFiles[newName] = newName
}
this.switchFile(newName)
} else {
var newFocus
for (var k in this.opt.tabbedFiles) {
for (var k in this.tabbedFiles) {
if (k.indexOf(oldName + '/') === 0) {
var newAbsolutePath = k.replace(oldName, newName)
this.tabbedFiles[newAbsolutePath] = newAbsolutePath
delete this.tabbedFiles[k]
if (this.opt.config.get('currentFile') === k) {
if (self._deps.config.get('currentFile') === k) {
newFocus = newAbsolutePath
}
}
Expand All @@ -85,17 +95,19 @@ class FileManager {
}

currentPath () {
var currentFile = this.opt.config.get('currentFile')
var self = this
var currentFile = self._deps.config.get('currentFile')
var reg = /(.*\/).*/
var path = reg.exec(currentFile)
return path ? path[1] : null
}

fileRemovedEvent (path) {
if (path === this.opt.config.get('currentFile')) {
this.opt.config.set('currentFile', '')
var self = this
if (path === self._deps.config.get('currentFile')) {
self._deps.config.set('currentFile', '')
}
this.opt.editor.discardCurrentSession()
self._deps.editor.discardCurrentSession()
delete this.tabbedFiles[path]
this.refreshTabs()
}
Expand Down Expand Up @@ -124,30 +136,30 @@ class FileManager {
var self = this
if (file) return _switchFile(file)
else {
var browserProvider = self.opt.filesProviders['browser']
var browserProvider = self._.filesProviders['browser']
browserProvider.resolveDirectory('browser', (error, filesTree) => {
if (error) console.error(error)
var fileList = Object.keys(filesTree)
if (fileList.length) {
_switchFile(browserProvider.type + '/' + fileList[0])
} else {
self.event.trigger('currentFileChanged', [])
self.opt.editor.displayEmptyReadOnlySession()
self._deps.editor.displayEmptyReadOnlySession()
}
})
}
function _switchFile (file) {
self.saveCurrentFile()
self.opt.config.set('currentFile', file)
self._deps.config.set('currentFile', file)
self.refreshTabs(file)
self.fileProviderOf(file).get(file, (error, content) => {
if (error) {
console.log(error)
} else {
if (self.fileProviderOf(file).isReadOnly(file)) {
self.opt.editor.openReadOnly(file, content)
self._deps.editor.openReadOnly(file, content)
} else {
self.opt.editor.open(file, content)
self._deps.editor.open(file, content)
}
self.event.trigger('currentFileChanged', [file, self.fileProviderOf(file)])
}
Expand All @@ -165,22 +177,22 @@ class FileManager {

fileProviderOf (file) {
var provider = file.match(/[^/]*/)
if (provider !== null && this.opt.filesProviders[provider[0]]) {
return this.opt.filesProviders[provider[0]]
if (provider !== null && this._deps.filesProviders[provider[0]]) {
return this._deps.filesProviders[provider[0]]
} else {
for (var handler of this.opt.compilerImport.handlers()) {
for (var handler of this._deps.compilerImport.handlers()) {
if (handler.match.exec(file)) {
return this.opt.filesProviders[handler.type]
return this._deps.filesProviders[handler.type]
}
}
}
return null
}

saveCurrentFile () {
var currentFile = this.opt.config.get('currentFile')
if (currentFile && this.opt.editor.current()) {
var input = this.opt.editor.get(currentFile)
var currentFile = this._deps.config.get('currentFile')
if (currentFile && this._deps.editor.current()) {
var input = this._deps.editor.get(currentFile)
if (input) {
var provider = this.fileProviderOf(currentFile)
if (provider) {
Expand Down