diff --git a/src/app/files/browser-files-tree.js b/src/app/files/browser-files-tree.js index 8051b742c6..3c8eecf991 100644 --- a/src/app/files/browser-files-tree.js +++ b/src/app/files/browser-files-tree.js @@ -2,21 +2,35 @@ var EventManager = require('../../lib/events') -function FilesTree (name, storage) { - var self = this - var event = new EventManager() - this.event = event - this.type = name - this.structFile = '.' + name + '.tree' - this.tree = {} - - this.exists = function (path, cb) { +import { ApiFactory } from 'remix-plugin' + +class FilesTree extends ApiFactory { + + constructor (name, storage) { + super() + this.event = new EventManager() + this.storage = storage + this.type = name + this.structFile = '.' + name + '.tree' + this.tree = {} + } + + get profile () { + // TODO should make them promisable + return { + name: this.type, + methods: ['get', 'set', 'remove'], + description: 'service - read/write file to the `config` explorer without need of additionnal permission.' + } + } + + exists (path, cb) { cb(null, this._exists(path)) } - function updateRefs (path, type) { + updateRefs (path, type) { var split = path.split('/') // this should be unprefixed path - var crawlpath = self.tree + var crawlpath = this.tree var intermediatePath = '' split.forEach((pathPart, index) => { intermediatePath += pathPart @@ -30,91 +44,90 @@ function FilesTree (name, storage) { delete crawlpath[intermediatePath] } }) - storage.set(self.structFile, JSON.stringify(self.tree)) + this.storage.set(this.structFile, JSON.stringify(this.tree)) } - this._exists = function (path) { + _exists (path) { var unprefixedpath = this.removePrefix(path) - return storage.exists(unprefixedpath) + return this.storage.exists(unprefixedpath) } - this.init = function (cb) { - var tree = storage.get(this.structFile) + init (cb) { + var tree = this.storage.get(this.structFile) this.tree = tree ? JSON.parse(tree) : {} if (cb) cb() } - this.get = function (path, cb) { + get (path, cb) { var unprefixedpath = this.removePrefix(path) - var content = storage.get(unprefixedpath) + var content = this.storage.get(unprefixedpath) if (cb) { cb(null, content) } return content } - this.set = function (path, content, cb) { + set (path, content, cb) { var unprefixedpath = this.removePrefix(path) - updateRefs(unprefixedpath, 'add') - var exists = storage.exists(unprefixedpath) - if (!storage.set(unprefixedpath, content)) { + this.updateRefs(unprefixedpath, 'add') + var exists = this.storage.exists(unprefixedpath) + if (!this.storage.set(unprefixedpath, content)) { if (cb) cb('error updating ' + path) return false } if (!exists) { - event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) + this.event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) } else { - event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) + this.event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) } if (cb) cb() return true } - this.addReadOnly = function (path, content) { + addReadOnly (path, content) { return this.set(path, content) } - this.isReadOnly = function (path) { + isReadOnly (path) { return false } - this.remove = function (path) { + remove (path) { var unprefixedpath = this.removePrefix(path) - updateRefs(unprefixedpath, 'remove') + this.updateRefs(unprefixedpath, 'remove') if (!this._exists(unprefixedpath)) { return false } - if (!storage.remove(unprefixedpath)) { + if (!this.storage.remove(unprefixedpath)) { return false } - event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) + this.event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) return true } - this.rename = function (oldPath, newPath, isFolder) { + rename (oldPath, newPath, isFolder) { var unprefixedoldPath = this.removePrefix(oldPath) var unprefixednewPath = this.removePrefix(newPath) - updateRefs(unprefixedoldPath, 'remove') - updateRefs(unprefixednewPath, 'add') - if (storage.exists(unprefixedoldPath)) { - if (!storage.rename(unprefixedoldPath, unprefixednewPath)) { + this.updateRefs(unprefixedoldPath, 'remove') + this.updateRefs(unprefixednewPath, 'add') + if (this.storage.exists(unprefixedoldPath)) { + if (!this.storage.rename(unprefixedoldPath, unprefixednewPath)) { return false } - event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) + this.event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) return true } return false } - this.resolveDirectory = function (path, callback) { - var self = this + resolveDirectory (path, callback) { if (path[0] === '/') path = path.substring(1) - if (!path) return callback(null, { [self.type]: { } }) + if (!path) return callback(null, { [this.type]: { } }) var tree = {} - path = self.removePrefix(path) + path = this.removePrefix(path) var split = path.split('/') // this should be unprefixed path - var crawlpath = self.tree + var crawlpath = this.tree split.forEach((pathPart, index) => { if (crawlpath[pathPart]) crawlpath = crawlpath[pathPart] }) @@ -125,20 +138,12 @@ function FilesTree (name, storage) { callback(null, tree) } - this.removePrefix = function (path) { + removePrefix (path) { path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path if (path[0] === '/') return path.substring(1) return path } - this.profile = function () { - // TODO should make them promisable - return { - name: this.type, - methods: ['get', 'set', 'remove'], - description: 'service - read/write file to the `config` explorer without need of additionnal permission.' - } - } } module.exports = FilesTree diff --git a/src/app/files/fileManager.js b/src/app/files/fileManager.js index e54a1a286d..c6c755514d 100644 --- a/src/app/files/fileManager.js +++ b/src/app/files/fileManager.js @@ -3,14 +3,16 @@ const EventEmitter = require('events') var globalRegistry = require('../../global/registry') var CompilerImport = require('../compiler/compiler-imports') +import { ApiFactory } from 'remix-plugin' /* attach to files event (removed renamed) trigger: currentFileChanged */ -class FileManager { +class FileManager extends ApiFactory { constructor (localRegistry) { + super() this.openedFiles = {} // list all opened files this.events = new EventEmitter() this._components = {} @@ -19,30 +21,29 @@ class FileManager { } init () { - var self = this - self._deps = { - 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 + this._deps = { + editor: this._components.registry.get('editor').api, + config: this._components.registry.get('config').api, + browserExplorer: this._components.registry.get('fileproviders/browser').api, + localhostExplorer: this._components.registry.get('fileproviders/localhost').api, + configExplorer: this._components.registry.get('fileproviders/config').api, + gistExplorer: this._components.registry.get('fileproviders/gist').api, + filesProviders: this._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) }) - self._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(self._deps.localhostExplorer) }) - self._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(self._deps.localhostExplorer) }) + this._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) + this._deps.localhostExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) + this._deps.configExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) + this._deps.gistExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) + this._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) + this._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) + this._deps.configExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) + this._deps.gistExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) + this._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(this._deps.localhostExplorer) }) + this._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(this._deps.localhostExplorer) }) } - profile () { + get profile () { return { displayName: 'file manager', name: 'fileManager', @@ -53,10 +54,9 @@ class FileManager { } fileRenamedEvent (oldName, newName, isFolder) { - var self = this if (!isFolder) { - self._deps.config.set('currentFile', '') - self._deps.editor.discard(oldName) + this._deps.config.set('currentFile', '') + this._deps.editor.discard(oldName) if (this.openedFiles[oldName]) { delete this.openedFiles[oldName] this.openedFiles[newName] = newName @@ -69,7 +69,7 @@ class FileManager { var newAbsolutePath = k.replace(oldName, newName) this.openedFiles[newAbsolutePath] = newAbsolutePath delete this.openedFiles[k] - if (self._deps.config.get('currentFile') === k) { + if (this._deps.config.get('currentFile') === k) { newFocus = newAbsolutePath } } @@ -105,8 +105,7 @@ class FileManager { } currentPath () { - var self = this - var currentFile = self._deps.config.get('currentFile') + var currentFile = this._deps.config.get('currentFile') var reg = /(.*)(\/).*/ var path = reg.exec(currentFile) return path ? path[1] : null @@ -154,47 +153,45 @@ class FileManager { } fileRemovedEvent (path) { - var self = this if (!this.openedFiles[path]) return - if (path === self._deps.config.get('currentFile')) { - self._deps.config.set('currentFile', '') + if (path === this._deps.config.get('currentFile')) { + this._deps.config.set('currentFile', '') } - self._deps.editor.discard(path) + this._deps.editor.discard(path) delete this.openedFiles[path] this.events.emit('fileRemoved', path) this.switchFile() } switchFile (file) { - var self = this if (file) return _switchFile(file) else { - var browserProvider = self._deps.filesProviders['browser'] + var browserProvider = this._deps.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.events.emit('currentFileChanged') - self._deps.editor.displayEmptyReadOnlySession() + this.events.emit('currentFileChanged') + this._deps.editor.displayEmptyReadOnlySession() } }) } - function _switchFile (file) { - self.saveCurrentFile() - self._deps.config.set('currentFile', file) - self.openedFiles[file] = file - self.fileProviderOf(file).get(file, (error, content) => { + const _switchFile = (file) => { + this.saveCurrentFile() + this._deps.config.set('currentFile', file) + this.openedFiles[file] = file + this.fileProviderOf(file).get(file, (error, content) => { if (error) { console.log(error) } else { - if (self.fileProviderOf(file).isReadOnly(file)) { - self._deps.editor.openReadOnly(file, content) + if (this.fileProviderOf(file).isReadOnly(file)) { + this._deps.editor.openReadOnly(file, content) } else { - self._deps.editor.open(file, content) + this._deps.editor.open(file, content) } - self.events.emit('currentFileChanged', file) + this.events.emit('currentFileChanged', file) } }) } @@ -243,7 +240,6 @@ class FileManager { } syncEditor (path) { - var self = this var currentFile = this._deps.config.get('currentFile') if (path !== currentFile) return @@ -251,7 +247,7 @@ class FileManager { if (provider) { provider.get(currentFile, (error, content) => { if (error) console.log(error) - self._deps.editor.setText(content) + this._deps.editor.setText(content) }) } else { console.log('cannot save ' + currentFile + '. Does not belong to any explorer') diff --git a/src/app/panels/tab-proxy.js b/src/app/panels/tab-proxy.js index d1729b4bf0..b72bf4b21b 100644 --- a/src/app/panels/tab-proxy.js +++ b/src/app/panels/tab-proxy.js @@ -52,13 +52,15 @@ export class TabProxy { appStore.event.on('activate', (name) => { const { profile } = appStore.getOne(name) - if (profile.prefferedLocation === 'mainPanel') { - this.addTab(name, () => { - this.event.emit('switchApp', name) - }, () => { - this.event.emit('closeApp', name) - this.appManager.deactivateOne(name) - }) + if (profile.location === 'mainPanel') { + this.addTab( + name, + () => this.event.emit('switchApp', name), + () => { + this.event.emit('closeApp', name) + this.appManager.deactivateOne(name) + } + ) } })