Merge pull request #400 from ethereum/files-api

Implement the files API
pull/1/head
chriseth 8 years ago committed by GitHub
commit 159aeffff7
  1. 111
      src/app/files.js
  2. 11
      src/app/storage.js

@ -0,0 +1,111 @@
'use strict'
var EventManager = require('../lib/eventManager')
function Files (storage) {
var event = new EventManager()
this.event = event
var readonly = {}
this.exists = function (path) {
// NOTE: ignore the config file
if (path === '.browser-solidity.json') {
return false
}
return this.isReadOnly(path) || storage.exists(path)
}
this.get = function (path) {
// NOTE: ignore the config file
if (path === '.browser-solidity.json') {
return null
}
return readonly[path] || storage.get(path)
}
this.set = function (path, content) {
// NOTE: ignore the config file
if (path === '.browser-solidity.json') {
return false
}
if (!this.isReadOnly(path)) {
var exists = storage.exists(path)
if (!storage.set(path, content)) {
return false
}
if (!exists) {
event.trigger('fileAdded', [path, false])
} else {
event.trigger('fileChanged', [path])
}
return true
}
return false
}
this.addReadOnly = function (path, content) {
if (!storage.exists(path)) {
readonly[path] = content
event.trigger('fileAdded', [path, true])
return true
}
return false
}
this.isReadOnly = function (path) {
return !!readonly[path]
}
this.remove = function (path) {
if (!this.exists(path)) {
return false
}
if (this.isReadOnly(path)) {
readonly[path] = undefined
} else {
if (!storage.remove(path)) {
return false
}
}
event.trigger('fileRemoved', [path])
return true
}
this.rename = function (oldPath, newPath) {
if (!this.isReadOnly(oldPath) && storage.exists(oldPath)) {
if (!storage.rename(oldPath, newPath)) {
return false
}
event.trigger('fileRenamed', [oldPath, newPath])
return true
}
return false
}
this.list = function () {
var files = {}
// add r/w files to the list
storage.keys().forEach(function (path) {
// NOTE: as a temporary measure do not show the config file
if (path !== '.browser-solidity.json') {
files[path] = false
}
})
// add r/o files to the list
Object.keys(readonly).forEach(function (path) {
files[path] = true
})
return files
}
}
module.exports = Files

@ -10,7 +10,12 @@ function Storage () {
} }
this.set = function (name, content) { this.set = function (name, content) {
try {
window.localStorage.setItem('sol:' + name, content) window.localStorage.setItem('sol:' + name, content)
} catch (exception) {
return false
}
return true
} }
function safeKeys () { function safeKeys () {
@ -28,12 +33,16 @@ function Storage () {
this.remove = function (name) { this.remove = function (name) {
window.localStorage.removeItem('sol:' + name) window.localStorage.removeItem('sol:' + name)
return true
} }
this.rename = function (originalName, newName) { this.rename = function (originalName, newName) {
var content = this.get(originalName) var content = this.get(originalName)
this.set(newName, content) if (!this.set(newName, content)) {
return false
}
this.remove(originalName) this.remove(originalName)
return true
} }
this.loadFile = function (filename, content) { this.loadFile = function (filename, content) {

Loading…
Cancel
Save