adapt files listAsTree(path, level)

pull/3094/head
serapath 7 years ago
parent 688af97554
commit d87fcb6b7f
  1. 67
      src/app/files/browser-files.js
  2. 31
      src/app/files/shared-folder.js

@ -7,6 +7,7 @@ function Files (storage) {
this.event = event this.event = event
var readonly = {} var readonly = {}
this.type = 'browser' this.type = 'browser'
this.filesTree = null
this.exists = function (path) { this.exists = function (path) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
@ -19,7 +20,10 @@ function Files (storage) {
} }
this.init = function (cb) { this.init = function (cb) {
cb() listAsTree(this, this.list(), (error, tree) => {
this.filesTree = tree
if (cb) cb(error)
})
} }
this.get = function (path, cb) { this.get = function (path, cb) {
@ -49,6 +53,7 @@ function Files (storage) {
return false return false
} }
if (!exists) { if (!exists) {
this.init()
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false])
} else { } else {
event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) event.trigger('fileChanged', [this.type + '/' + unprefixedpath])
@ -63,6 +68,7 @@ function Files (storage) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
if (!storage.exists(unprefixedpath)) { if (!storage.exists(unprefixedpath)) {
readonly[unprefixedpath] = content readonly[unprefixedpath] = content
this.init()
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, true]) event.trigger('fileAdded', [this.type + '/' + unprefixedpath, true])
return true return true
} }
@ -88,6 +94,7 @@ function Files (storage) {
return false return false
} }
} }
this.init()
event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) event.trigger('fileRemoved', [this.type + '/' + unprefixedpath])
return true return true
} }
@ -99,6 +106,7 @@ function Files (storage) {
if (!storage.rename(unprefixedoldPath, unprefixednewPath)) { if (!storage.rename(unprefixedoldPath, unprefixednewPath)) {
return false return false
} }
this.init()
event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder])
return true return true
} }
@ -140,7 +148,49 @@ function Files (storage) {
// } // }
// } // }
// //
this.listAsTree = function () { this.listAsTree = function (path, level) {
var nodes = path ? path.split('/') : []
var tree = this.filesTree
try {
while (nodes.length) {
var key = nodes.shift()
if (key) tree = tree[key]
}
} catch (e) {
tree = {}
}
if (level) {
var leveltree = {}
build(tree, level, leveltree)
tree = leveltree
}
return tree
}
// rename .browser-solidity.json to .remix.config
if (this.exists('.browser-solidity.json')) {
this.rename('.browser-solidity.json', '.remix.config')
}
this.init()
}
module.exports = Files
function build (tree, level, leveltree) {
if (!level) return
Object.keys(tree).forEach(key => {
var value = tree[key]
var more = value === Object(value)
if (more) {
leveltree[key] = {}
build(value, level - 1, leveltree[key])
} else leveltree[key] = value
})
}
function listAsTree (self, filesList, callback) {
function hashmapize (obj, path, val) { function hashmapize (obj, path, val) {
var nodes = path.split('/') var nodes = path.split('/')
var i = 0 var i = 0
@ -158,22 +208,13 @@ function Files (storage) {
var tree = {} var tree = {}
var self = this
// This does not include '.remix.config', because it is filtered // This does not include '.remix.config', because it is filtered
// inside list(). // inside list().
Object.keys(this.list()).forEach(function (path) { Object.keys(filesList).forEach(function (path) {
hashmapize(tree, path, { hashmapize(tree, path, {
'/readonly': self.isReadOnly(path), '/readonly': self.isReadOnly(path),
'/content': self.get(path) '/content': self.get(path)
}) })
}) })
return tree callback(null, tree)
} }
// rename .browser-solidity.json to .remix.config
if (this.exists('.browser-solidity.json')) {
this.rename('.browser-solidity.json', '.remix.config')
}
}
module.exports = Files

@ -138,8 +138,23 @@ class SharedFolder {
return this.files return this.files
} }
listAsTree () { listAsTree (path, level) {
return this.filesTree var nodes = path ? path.split('/') : []
var tree = this.filesTree
try {
while (nodes.length) {
var key = nodes.shift()
if (key) tree = tree[key]
}
} catch (e) {
tree = {}
}
if (level) {
var leveltree = {}
build(tree, level, leveltree)
tree = leveltree
}
return tree
} }
removePrefix (path) { removePrefix (path) {
@ -159,6 +174,18 @@ class SharedFolder {
// } // }
// } // }
// //
function build (tree, level, leveltree) {
if (!level) return
Object.keys(tree).forEach(key => {
var value = tree[key]
var more = value === Object(value)
if (more) {
leveltree[key] = {}
build(value, level - 1, leveltree[key])
} else leveltree[key] = value
})
}
function listAsTree (self, filesList, callback) { function listAsTree (self, filesList, callback) {
function hashmapize (obj, path, val) { function hashmapize (obj, path, val) {
var nodes = path.split('/') var nodes = path.split('/')

Loading…
Cancel
Save